home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_07 / barbu / engine.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-17  |  3.2 KB  |  125 lines

  1. article\Listing4
  2.  
  3. /////////////////////////////////////////////////////
  4. //    ENGINE implementation, an abstract generator
  5. /////////////////////////////////////////////////////
  6. #include "ENGINE.HPP"
  7. #include "BSTACK.HPP"
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11.  
  12. const char ENGINE::_Class[] = "[CLASS]";
  13. const char ENGINE::_FlagOn[] = "#[";
  14. const char ENGINE::_FlagOff[] = "#]";
  15.  
  16. ENGINE::ENGINE(const char szHpp[], const char szCpp[])
  17.     : _inHpp(szHpp), _inCpp(szCpp), _stamp(50)
  18. {
  19. _stamp += "//////// AB CLASSGEN ";
  20. time_t t = time(&t);
  21. _stamp += ctime(&t);
  22. _stamp[_stamp.len() - 1] = ' ';
  23. _stamp += "////////\n";
  24. }
  25.  
  26. const char* ENGINE::_substitute(const char newClass[])
  27. {
  28. int i, last;
  29. _outLine = "";
  30. i = last = 0;
  31. while(-1 != (i=_inLine.hasin(_Class, last))){
  32.     for(int j = last; j < i; j++)
  33.         _outLine += _inLine[j];
  34.     _outLine += newClass;
  35.     last = i+strlen(_Class);
  36.     }
  37. _outLine += _inLine+last;
  38. return _outLine;
  39. }
  40.  
  41. void ENGINE::_fileJob(const char szInFile[],
  42.                 const char szOutFile[],
  43.                 const char newClass[], SYM *pSym)
  44. {
  45. FILE *in, *out;
  46. if(0 == (in = fopen(szInFile, "rt"))){
  47.     cannotOpen(szInFile);
  48.     return;
  49.     }
  50. if(0 != (out = fopen(szOutFile, "rt"))){
  51.     fclose(out);
  52.     if(0 == overwriteQuest(szOutFile)){
  53.         fclose(in);
  54.         return;
  55.         }
  56.     }
  57. if(0 == (out = fopen(szOutFile, "wt"))){
  58.     fclose(in);
  59.     cannotOpen(szOutFile);
  60.     return;
  61.     }
  62. fputs(_stamp, out);
  63. BSTACK Markers;
  64. STR InBuf(MAXLEN);
  65. for(int l = 0;
  66.         fgets((char*)(const char*)InBuf, MAXLEN, in);
  67.         l++){
  68.     _inLine = InBuf;
  69.     STR sym(40);
  70.     STR val(40);
  71.     int i, k;
  72.     if(-1 == (i = _inLine.hasin(_FlagOn))){
  73.         if(-1 == _inLine.hasin(_FlagOff)){
  74.             if(Markers.top())
  75.                 fputs(_substitute(newClass), out);
  76.             continue;
  77.             }
  78.         else{    // it's the end of a block
  79.             if(Markers.empty()){
  80.                 unexpectedEOB(szInFile, l+1);
  81.                 break;
  82.                 }
  83.             Markers.pop();
  84.             continue;
  85.             }
  86.         }
  87.     // Ok, it's the beginning of a block:
  88.     if(!Markers.top()){ // do not generate anyway
  89.         Markers.push(BSTACK::NOT);
  90.         continue;
  91.         }
  92.     i += strlen(_FlagOn);
  93.     for(k = 0; _inLine[i] && _inLine[i] != '=';
  94.             i++, k++)
  95.         sym += _inLine[i];
  96.     if(!_inLine[i]){
  97.         Markers.push(BSTACK::NOT);
  98.         continue;
  99.         }
  100.     for(k = 0, i++; _inLine[i] ; i++, k++)
  101.         val += _inLine[i];
  102.     sym.noTrailSpace(); sym.noFrontSpace();
  103.     val.noTrailSpace(); val.noFrontSpace();
  104.     Markers.push(atoi(val) == pSym->get(sym) ?
  105.                         BSTACK::YES : BSTACK::NOT);
  106.     continue;
  107.     }
  108. if(!Markers.empty())
  109.     missingEOB(szInFile, l);
  110. fclose(in);
  111. fclose(out);
  112. runEditor(szOutFile);
  113. }
  114.  
  115. void ENGINE::go(const char szDir[],
  116.             const char szClass[], SYM* pSym)
  117. {
  118. STR Hpp(szClass); Hpp += ".HPP";
  119. STR Cpp(szClass); Cpp += ".CPP";
  120. STR OutHpp(szDir); OutHpp += '\\'; OutHpp += Hpp;
  121. STR OutCpp(szDir); OutCpp += '\\'; OutCpp += Cpp;
  122. _fileJob(_inHpp, OutHpp, szClass, pSym);
  123. _fileJob(_inCpp, OutCpp, szClass, pSym);
  124. }
  125.