home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbutil.zip / BUFOP.ZIP / FILES.CPP < prev    next >
Text File  |  1993-09-05  |  5KB  |  132 lines

  1. #include "hostvars.hpp"
  2. #include "options.hpp"
  3. #include "compsql.hpp"
  4. #include "files.hpp"
  5. #include <itime.hpp>
  6. #include <idate.hpp>
  7. #include <fstream.h>
  8. #include <Istring.hpp>
  9.  
  10.  
  11. const char strtcomment[] ="/* * * * * S Q L   R E M O V E D * * * * \n";
  12. const char endcomment[] = " \n* * * * * E N D  O F   S Q L   R E M O V E */ \n";
  13.  
  14. void removeComments(IString & theStr,Boolean inSQLStmt);
  15. void processFile(IString fileName)
  16. /****************************************************************/
  17. /*  this routine will process the input file and generate calls */
  18. /*  for the IBM DB2 OS/2 database                                         */
  19. /*    The header files can include other includes */
  20. /*  but they can not be recursive (i.e.  header a include header b */
  21. /*  which includes header a)                                    */
  22. /****************************************************************/
  23. {        
  24.         ifstream fin;
  25.         ofstream fout;
  26.         int count;
  27.         int total;
  28.         char instr[256];        // largest input line
  29.         IString line="" ,source;
  30.         fin.open(fileName,ios::in);
  31.         if (!fin) {
  32.            cout << "could not process file: " << fileName << "\n";
  33.            return;                  // big deal
  34.         } /* endif */
  35.  
  36. //  make the output file name
  37. //  if the suffix was sqh make into hpp
  38. //  if the suffix was sqc make into cpp
  39.         fileName = fileName.upperCase();
  40.         fileName = fileName.change(".SQH",".HPP");  // if header file
  41.         fileName = fileName.change(".SQC",".CPP");  // if source file
  42.         fout.open(fileName);
  43.         if (!fout) {
  44.            cout << " could not open output file " << fileName << "\n";
  45.         } /* endif */
  46.         count = 0;
  47.         IString msg;
  48.         msg = "Processing ";
  49.         msg += fileName;
  50.         monitorMessage( msg);
  51.         ITime t;
  52.         IDate d;
  53.         fout << "// \n";
  54.         fout << "// processed by precompiler  " << d.today() << "  " << t.now() << "\n";
  55.         fout << "// \n";
  56.         while (fin.eof()== 0) {
  57.               count++;
  58.               fin.getline(instr,255);
  59.               line = instr;
  60.               source = line;
  61.               removeComments(line,false);
  62.               if (IsSQL(line)) {        // if this is an SQL command
  63.                     //    fout << strtcomment;  // start a begin comment block
  64.                     source.insert("//### ");  // comment each line
  65.                     while (! line.includes(";")) {     // look for end of SQL Stm
  66.                         fin.getline(instr,255);
  67.                         source = source + "\n//### " +instr;   // comment &append the source
  68.                         IString tmp = instr;
  69.                         removeComments(tmp,true);
  70.                         line = line + tmp;
  71.                         count++;
  72.                     } /* endwhile */
  73.                     source = source + "\n ";
  74.                     char * cp = &source[0];
  75.                     fout.write(cp,source.length());
  76.                     //  fout << endcomment;  // end of comment block
  77.                     prepareSQL(line,fout,count);
  78.                     line = "";
  79.                     source = "";
  80.               } /* endif */
  81.               else{
  82.                  fout  << source << "\n";
  83.                  if (InDeclare()) {
  84.                      processDeclare(line);
  85.                   } /*endif */
  86.               }
  87.            } /* endwhile */
  88.            fout.close();
  89. }  // end process file
  90.  
  91.  
  92.  
  93. static int inComment = 0;
  94. void removeComments(IString & theStr, Boolean InSQLStmt)
  95. {
  96.     int copypos=0;
  97. /***************************************************** */
  98. /* if we were in the middle of a multi line comment */
  99. /* check to see of the end of comment is on this line */
  100. /* if it is, then delete up to the end of the comment */
  101. /***************************************************** */
  102.  
  103.     if (inComment > 0) {          // check if comming out of comment
  104.        if (theStr.includes("*/")){   // end of comment in this line
  105.           theStr.remove(0,theStr.indexOf("*/")+2);
  106.           inComment--;              // dec comment counter
  107.        }
  108.          else
  109.           { theStr= "";             // in a comment return empty str
  110.             return;
  111.           }/* endif end of comment */
  112.  
  113.     } /* endif was in comment line */
  114.        if (InSQLStmt) {             // allow ANSI standard sql comments
  115.           theStr.change("--","//"); // by making them look like C++ comments
  116.        } /* endif */
  117.        if (theStr.includes("//")) {        // check for one line comments
  118.           copypos = theStr.indexOf("//");
  119.        theStr = theStr.subString(0,copypos-1);
  120.        } /* endif // type comment */
  121.     if (theStr.includes("/*")) {
  122.        copypos = theStr.indexOf("/*");   // check for normal comment
  123.        inComment++;
  124.        IString tmp = theStr.subString(0,copypos-1);
  125.        removeComments(theStr,false);  // recursive call to check for end of comment
  126.        theStr.insert(tmp);
  127.     } /* endif */
  128.     return;
  129. }  // end of remove comments
  130.  
  131.  
  132.