home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / texted / vedcnv.cpp < prev    next >
C/C++ Source or Header  |  1998-11-09  |  10KB  |  344 lines

  1. //=======================================================================
  2. //    vedcnv.cpp:    Source for vedTextEditor class
  3. //=======================================================================
  4.  
  5. #include <v/vnotice.h>
  6. #ifdef VIDE
  7. #define VCmdWindow videCmdWindow
  8. #include "videapp.h"
  9. #include "vedcnv.h"
  10. #include "videcmdw.h"
  11. #else
  12. #define VCmdWindow vedCmdWindow
  13. #include "vedapp.h"
  14. #include "vedcnv.h"
  15. #include "vedcmdw.h"
  16. #endif
  17.  
  18.     const int maxBuff = 300;
  19.  
  20. //===================>>> vedTextEditor::vedTextEditor <<<====================
  21.   vedTextEditor::vedTextEditor(VCmdWindow* parent) : 
  22.       vTextEditor((vCmdWindow*)parent,1)
  23.   {
  24.   }
  25.  
  26. //===================>>> vedTextEditor::~vedTextEditor <<<====================
  27.   vedTextEditor::~vedTextEditor()
  28.   {
  29.   }
  30.  
  31. #define isSpace(x) (x == ' ' || x == '\t')
  32.  
  33. #define isOp(x) (x=='+' || x=='-' || x=='*' || x=='/' || x=='<' || x=='>' \
  34.     || x=='?' || x==':' || x==';' || x=='!' || x=='%' || x=='^' || x=='&' \
  35.         || x=='\\' || x=='|' || x == '=' || x==',' || x=='\'' || x=='\"' \
  36.         || x==')' || x=='(' || x=='[' || x==']' || x=='{' || x=='}')
  37.  
  38.  
  39. // ======================>>> vTextEditor::paintLine <<<=====================
  40.   void vedTextEditor::paintLine(char* linout, int lineStart,
  41.         int hiStart, int hiLast, long lineNum)
  42.   {
  43.     // paint a line.
  44.     // linout: the line to output with tabs converted to spaces, etc.
  45.     // lineStart: where to begin printing the line (for hoiz. scrolling)
  46.     // hiStart, hiLast: reverse text attribute
  47.     // lineNum: the real line number in the buffer this is.
  48.     // This version overrides the original to handle syntax highlighting
  49.  
  50.     ChrAttr attrs[MAX_LINE+1];    // for attributes
  51.     int wasComment = 0;
  52.  
  53.     int linlen = strlen(linout);
  54.     if (linlen <= 0)             // only draw if there!
  55.         return;
  56.  
  57.     for (int ix = 0 ; ix <= MAX_LINE ; ++ix)    // assume normal
  58.         attrs[ix] = ChNormal;
  59.  
  60.     // Parse the line for syntax
  61.  
  62.     if (GetFileType() == CPP)            // if a C file, parse
  63.         wasComment = parseC(linout,attrs,lineNum);
  64.     else if (GetFileType() == gccError)
  65.       {
  66.         int ig = 0;
  67.         while (isSpace(linout[ig]))
  68.           ++ig;
  69.         if (linout[ig] == '>')
  70.           {
  71.             while (linout[ig])
  72.                 attrs[ig++] = ChBlue;
  73.           }
  74.         else if (linout[ig] == '!')
  75.           {
  76.             while (linout[ig])
  77.                 attrs[ig++] = ChRed;
  78.           }
  79.       }
  80.  
  81.     // Now fill in highlight attributes
  82.     for (int ih = 0 ; linout[ih] != 0 ; ++ih)
  83.       {
  84.         if (ih >= hiStart && ih < hiLast)
  85.             attrs[ih] = ChHighlight;        // override syntax colors
  86.       }
  87.  
  88.  
  89.     for (int ixx = lineStart ; linout[ixx] != 0 ; ++ixx)
  90.     DrawChar(linout[ixx],attrs[ixx]);
  91.  
  92.   }
  93.  
  94. #ifdef VIDE
  95. //===================>>> vedTextEditor::TextMouseDown <<<====================
  96.   void vedTextEditor::TextMouseDown(int row, int col, int button)
  97.   {
  98.     int btn = (GetFileType() == gccError) ? 1 : button;
  99.  
  100.     vTextEditor::TextMouseDown(row, col, btn);    // translate to left
  101.   }
  102.  
  103. //===================>>> vedTextEditor::TextMouseUP <<<====================
  104.   void vedTextEditor::TextMouseUp(int row, int col, int button)
  105.   {
  106.     int btn = (GetFileType() == gccError) ? 1 : button;
  107.     vTextEditor::TextMouseUp(row, col, btn);
  108.     if (button == 3 && GetFileType() == gccError)  // open error file
  109.       {
  110.         ((VCmdWindow*)_parent)->GotoErrorLine();
  111.       }
  112.   }
  113. #endif
  114.  
  115.  
  116. //===================>>> vedTextEditor::parseC <<<====================
  117.   int vedTextEditor::parseC(char* linout, ChrAttr* attrs, long lineNum)
  118.   {
  119.  
  120.     int ix = 0;
  121.     int wasComment = 0;
  122.     char token[MAX_LINE+1];
  123.  
  124.     for (ix = 0 ; linout[ix] != 0 ; ++ix)
  125.         if (!isSpace(linout[ix]))
  126.             break;            // skip leading spaces
  127.  
  128.     while (linout[ix] != 0)
  129.       {
  130.         // gather a token
  131.         int iy;
  132.         for (iy = ix ; linout[iy] != 0 ; ++iy)
  133.           {
  134.             if (isOp(linout[iy]) || isSpace(linout[iy]) )
  135.                 break;
  136.           }
  137.         // token goes from ix to iy
  138.         int from = ix;
  139.         if (ix == iy)
  140.             ++iy;        // single char tokens
  141.  
  142.         char *tp;
  143.         for (tp = token ; from < iy ; ++from)
  144.           *tp++ = linout[from];
  145.         *tp = 0;
  146.         // OK, now highlight the token appropriately
  147.         if (isOp(*token))
  148.           {
  149.             if (*token == '"')
  150.               {
  151.                 // A String...
  152.                 int ij;
  153.                 for (ij = ix ; linout[ij] ; ++ij)
  154.                   {
  155.                     attrs[ij] = ChRed;
  156.                     if (linout[ij] == '\\')
  157.                       {
  158.                         attrs[ij+1] = ChRed;
  159.                         ++ij;
  160.                       }
  161.                     else if (linout[ij] == '"' && ij != ix)
  162.                         break;
  163.                   }
  164.                 iy = ij+1;
  165.               }
  166.             else if (*token == '/' && linout[ix+1] == '/')
  167.               {
  168.                 // A comment...
  169.                 int ij;
  170.                 for (ij = ix ; linout[ij] ; ++ij)
  171.                     attrs[ij] = ChGreen | ChDimColor;
  172.                 iy = ij;
  173.                 wasComment = 1;
  174.               }
  175.             else if (*token == '/' && linout[ix+1] == '*')
  176.               {
  177.                 /* A comment... */
  178.                 int ij;
  179.                 for (ij = ix ; linout[ij] ; ++ij)
  180.                   {
  181.                     attrs[ij] = ChYellow | ChDimColor;
  182.                     if (linout[ij] =='/' && linout[ij-1] == '*') // "*/"
  183.                         break;
  184.                   }
  185.                 wasComment = 1;
  186.                 if (linout[ij] == 0)
  187.                     break;
  188.                 else
  189.                     iy = ij+1;
  190.               }
  191.             else if (*token == '*' && linout[ix+1] == '/')
  192.               {
  193.                 /* A comment... */
  194.                 int ij;
  195.                 for (ij = 0 ; linout[ij] && ij <= ix+1 ; ++ij)
  196.                   {
  197.                     attrs[ij] = ChYellow | ChDimColor;
  198.                     if (linout[ij] =='/' && linout[ij-1] == '*')
  199.                         break;
  200.                   }
  201.                 wasComment = 1;
  202.                 if (linout[ij] == 0)
  203.                     break;
  204.                 else
  205.                     iy = ij+1;
  206.               }
  207.             else
  208.                 attrs[ix] |= ChBlue | ChDimColor;
  209.           }
  210.         else if (*token == '#')
  211.           {
  212.             for (int ij = ix ; ij < iy ; ++ij)
  213.                 attrs[ij] |= ChCyan | ChDimColor;
  214.           }
  215.         else if (*token >= '0' && *token <= '9')
  216.           {
  217.             for (int ij = ix ; ij < iy ; ++ij)
  218.                 attrs[ij] |= ChRed;
  219.           }
  220.         else if (isKeyWord(token))
  221.           {
  222.             for (int ij = ix ; ij < iy ; ++ij)
  223.                 attrs[ij] |= ChBlue;
  224.           }
  225.  
  226.         ix = iy;
  227.       }
  228.     return wasComment;
  229.   }
  230. //===================>>> vedTextEditor::isKeyWord <<<====================
  231.   int vedTextEditor::isKeyWord(char* token)
  232.   {
  233.     char* keywords[] =
  234.      {
  235.     "FALSE", "NULL", "TRUE",
  236.     "asm", "auto", "bool", "break", 
  237.     "case", "catch", "char", "class", "const", "const_cast", 
  238.     "continue", "default", "delete", "do", "double", 
  239.     "dynamic_cast", "else", "enum", "explicit", "extern", 
  240.     "false", "float", "for", "friend", "goto", "if", 
  241.     "inline", "int", "long", "mutable", "namespace", "new",
  242.     "operator", "private", "protected", "public", "register", 
  243.     "reinterpret_cast", "return", "short", "signed", "sizeof", 
  244.     "static", "static_cast", "struct", "switch", "template", 
  245.     "this", "throw", "true", "try", "typedef", "typeid", 
  246.     "typename", "union", "unsigned", "using", "virtual", "void", 
  247.     "volatile", "wchar_t", "while", ""
  248.      };
  249.  
  250.      for (int ix = 0 ; *keywords[ix] ; ++ix)
  251.        {
  252.          if (strcmp(token,keywords[ix]) == 0)
  253.              return 1;
  254.        }
  255.      return 0;
  256.   }
  257.  
  258. //===================>>> vedTextEditor::ChangeLoc <<<====================
  259.   void vedTextEditor::ChangeLoc(long line, int col)
  260.   {
  261.     ((VCmdWindow*)_parent)->ChangeLoc(line,col);
  262.   }
  263.  
  264. //===================>>> vedTextEditor::ChangeInsMode <<<====================
  265.   void vedTextEditor::ChangeInsMode(int IsInsMode, char* msg)
  266.   {
  267.     ((VCmdWindow*)_parent)->ChangeInsMode(IsInsMode, msg);
  268.   }
  269.  
  270. //===================>>> vedTextEditor::StatusMessage <<<====================
  271.   void vedTextEditor::StatusMessage(char *msg)
  272.   {
  273.     ((VCmdWindow*)_parent)->StatusMessage(msg);
  274.   }
  275.  
  276. //===================>>> vedTextEditor::ErrorMsg <<<====================
  277.   void vedTextEditor::ErrorMsg(char *str)
  278.   {
  279.   }
  280.  
  281. //===================>>> vedTextEditor::ReadFile <<<====================
  282.   int vedTextEditor::ReadFile(char* name, int paintIt)
  283.   {
  284.  
  285.     char buff[maxBuff+2];
  286.  
  287.     if (!name || !*name)
  288.     return 0;
  289.  
  290.     ifstream inFile(name);
  291.  
  292.     if (!inFile)
  293.     return 0;        // file not there
  294.  
  295.     resetBuff();        // this buffer is empty
  296.  
  297.     while (inFile.getline(buff,maxBuff))
  298.       {
  299.     if (!addLine(buff))
  300.       {
  301.         vNoticeDialog note(theApp);
  302.         note.Notice("File too big -- only paritally read.");
  303.         break;
  304.       }
  305.       }
  306.     inFile.close();
  307.     displayBuff(1,paintIt);        // Now, display the buffer , don't paint
  308.     return 1;
  309.   }
  310.  
  311. //===================>>> vedTextEditor::SaveFile <<<====================
  312.   int vedTextEditor::SaveFile(char* name)
  313.   {
  314.     char buff[maxBuff+2];
  315.  
  316.     char* prefix = "Saved ";
  317.  
  318.     ofstream ofs(name);
  319.     if (!ofs)
  320.       {
  321.     StatusMessage("Can't save file");
  322.     return 0;
  323.       }
  324.  
  325.     for (long lx = 1 ; lx <= GetLines() ; ++lx)
  326.       {
  327.     getLine(buff,maxBuff,lx);
  328.     ofs << buff << "\n";
  329.       }
  330.  
  331.     ofs.close();
  332.  
  333.     strcpy(buff,prefix);
  334.  
  335.     int ix;
  336.     for (ix = strlen(prefix) ; ix < 40 && *name != 0 ; ++ix)
  337.     buff[ix] = *name++;
  338.     buff[ix] = 0;
  339.     StatusMessage(buff);
  340.  
  341.     state.changes = 0;
  342.     return 1;
  343.   }
  344.