home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / tex / lametex_.z / lametex_ / lametex / src / Label.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-07  |  3.2 KB  |  131 lines

  1. /* Label.C
  2.  *
  3.  * Stores user-defined labels for backwards and forwards referencing with
  4.  * the \ref and \pageref commands.
  5.  *
  6.  * Copyright 1992 Jonathan Monsarrat. Permission given to freely distribute,
  7.  * edit and use as long as this copyright statement remains intact.
  8.  *
  9.  */
  10.  
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "Global.h"
  16. #include "Counter.h"
  17. #include "Operator.h"
  18.  
  19. Label::Label(char *name, int chapter, int section)
  20. {
  21.      _name = strdup(name);
  22.      _chapter = chapter;
  23.      _section = section;
  24.      _referenced = 0;
  25. }
  26.  
  27. Label::~Label()
  28. {
  29.      delete _name;
  30. }
  31.  
  32. Label::match(char *name)
  33. {
  34.      return !strcmp(name, _name);
  35. }
  36.  
  37. Labels::Labels()
  38. {
  39.      numlabels = 0;
  40.      unknown_references = 0;
  41. }
  42.  
  43. Labels::~Labels()
  44. {
  45.      for(int x=0; x < numlabels; x++)
  46.       delete label[x];
  47. }
  48.  
  49. void Labels::add_label(char *labelname)
  50. {
  51.      if(numlabels > MAXLABELS - 2)
  52.       Global::files->fatal_error("Too many labels defined");
  53.      for(int x=0; x < numlabels; x++)
  54.       if(label[x]->match(labelname)) {
  55.            char message[50];
  56.            sprintf(message, "Same label name %s defined twice", labelname);
  57.            Global::files->fatal_error(message);
  58.       }
  59.  
  60.      label[numlabels++] =
  61.       new Label(labelname,
  62.             (int)Stack::get(Environment::PCounter,
  63.                     Counter::Chapter, ""),
  64.             (int)Stack::get(Environment::PCounter,
  65.                     Counter::Section, ""));
  66. }
  67.  
  68.  
  69. /* Print a reference to a given labelname done from the \ref command.
  70.  * Must handle forward referencing as a special case.
  71.  */
  72. void Labels::print_ref(char *labelname)
  73. {
  74.      for(int x=0; x < numlabels; x++)
  75.       if(label[x]->match(labelname)) {
  76.            char printout[MAXSTRING];
  77.            sprintf(printout,"%d.%d",label[x]->_chapter,label[x]->_section);
  78.            Operator::plaintext(printout);
  79.            label[x]->_referenced = 1;
  80.            return;
  81.       }
  82.  
  83.      /* Label is undefined. We assume it is a forward referencing problem */
  84.      unknown_references = 1;
  85.      Global::files->outfile << endl << "LAMETEXLABELREF" << labelname << endl;
  86. }
  87.  
  88. /* Shutdown the program by going through the file and changing all the
  89.  * undone label references to the correct information.
  90.  */
  91. void Labels::shutdown()
  92. {
  93.      ifstream outfile;
  94.      ofstream tempfile;
  95.      char buffer[MAXSTRING];
  96.      char *p;
  97.      char *labelname;
  98.  
  99.      if(!unknown_references)
  100.       return;
  101.      cerr << "Handling forward references..." << endl;
  102.      outfile.open(Global::files->outfilename);
  103.      tempfile.open("lametex.temp");
  104.      outfile.getline(buffer, MAXSTRING, '\n');
  105.      while(!outfile.eof() && !outfile.fail()) {
  106.       if((p = strstr(buffer, "LAMETEXLABELREF"))!=NULL) {
  107.            labelname = (char *)(p + 15);
  108.            for(int x=0; x < numlabels; x++)
  109.             if(label[x]->match(labelname)) {
  110.              tempfile << "(" << label[x]->_chapter
  111.                  << "." << label[x]->_section
  112.                  << ") NW" << endl;
  113.              label[x]->_referenced = 1;
  114.              break;
  115.             }
  116.            if(x >= numlabels) {
  117.             cerr << "No match found for label " << labelname << endl;
  118.             exit(-1);
  119.            }
  120.           } else {
  121.                tempfile << buffer << endl;
  122.           }
  123.       outfile.getline(buffer, MAXSTRING, '\n');
  124.      }
  125.  
  126.      outfile.close();
  127.      tempfile.close();
  128.      sprintf(buffer, "mv lametex.temp %s", Global::files->outfilename);
  129.      system(buffer);
  130. }
  131.