home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / ECKELT / 5 / CPPCHECK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  5.7 KB  |  160 lines

  1. // File from page 252 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: CPPCHECK.CPP -- Configures .H & .CPP files
  34. // To conform to style standard.
  35. // Tests existing files for conformance
  36. #include <fstream.h>
  37. #include <strstrea.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include <assert.h>
  41. #define SZ 40  // Buffer sizes
  42. #define BSZ 100
  43.  
  44. main(int argc, char* argv[]) {
  45.   assert(argc == 2); // File set name
  46.   enum bufs { base, header, implement,
  47.     Hline1, guard1, guard2, guard3,
  48.     CPPline1, include, bufnum };
  49.   char b[bufnum][SZ];
  50.   ostrstream osarray[] = {
  51.     ostrstream(b[base], SZ),
  52.     ostrstream(b[header], SZ),
  53.     ostrstream(b[implement], SZ),
  54.     ostrstream(b[Hline1], SZ),
  55.     ostrstream(b[guard1], SZ),
  56.     ostrstream(b[guard2], SZ),
  57.     ostrstream(b[guard3], SZ),
  58.     ostrstream(b[CPPline1], SZ),
  59.     ostrstream(b[include], SZ),
  60.   };
  61.   osarray[base] << argv[1] << ends;
  62.   // Find any '.' in the string using the
  63.   // Standard C library function strchr():
  64.   char* period = strchr(b[base], '.');
  65.   if(period) *period = 0; // Strip extension
  66.   // Force to upper case:
  67.   for(int i = 0; b[base][i]; i++)
  68.     b[base][i] = toupper(b[base][i]);
  69.   // Create file names and internal lines:
  70.   osarray[header] << b[base] << ".H" << ends;
  71.   osarray[implement] << b[base] << ".CPP" << ends;
  72.   osarray[Hline1] << "//:" << ' ' << b[header]
  73.     << " -- " << ends;
  74.   osarray[guard1] << "#ifndef " << b[base]
  75.                   << "_H_" << ends;
  76.   osarray[guard2] << "#define " << b[base]
  77.                   << "_H_" << ends;
  78.   osarray[guard3] << "#endif // " << b[base]
  79.                   << "_H_" << ends;
  80.   osarray[CPPline1] << "//:" << ' ' 
  81.                     << b[implement]
  82.                     << " -- " << ends;
  83.   osarray[include] << "#include \""
  84.                    << b[header] << "\"" <<ends;
  85.   // First, try to open existing files:
  86.   ifstream existh(b[header]),
  87.            existcpp(b[implement]);
  88.   if(!existh) { // Doesn't exist; create it
  89.     ofstream newheader(b[header]);
  90.     newheader << b[Hline1] << endl
  91.       << b[guard1] << endl
  92.       << b[guard2] << endl << endl
  93.       << b[guard3] << endl;
  94.   }
  95.   if(!existcpp) { // Create cpp file
  96.     ofstream newcpp(implement);
  97.     newcpp << b[CPPline1] << endl
  98.       << b[include] << endl;
  99.   }
  100.   if(existh) { // Already exists; verify it
  101.     strstream hfile; // Write & read
  102.     ostrstream newheader; // Write
  103.     hfile << existh.rdbuf() << ends;
  104.     // Check that first line conforms:
  105.     char buf[BSZ];
  106.     if(hfile.getline(buf, BSZ)) {
  107.       if(!strstr(buf, "//:") ||
  108.          !strstr(buf, b[header]))
  109.         newheader << b[Hline1] << endl;
  110.     }
  111.     // Ensure guard lines are in header:
  112.     if(!strstr(hfile.str(), b[guard1]) ||
  113.        !strstr(hfile.str(), b[guard2]) ||
  114.        !strstr(hfile.str(), b[guard3])) {
  115.        newheader << b[guard1] << endl
  116.          << b[guard2] << endl
  117.          << buf
  118.          << hfile.rdbuf() << endl
  119.          << b[guard3] << endl << ends;
  120.     } else
  121.       newheader << buf
  122.         << hfile.rdbuf() << ends;
  123.     // If there were changes, overwrite file:
  124.     if(strcmp(hfile.str(),newheader.str())!=0){
  125.       existh.close();
  126.       ofstream newH(b[header]);
  127.       newH << "//@//" << endl // change marker
  128.         << newheader.rdbuf();
  129.     }
  130.     delete hfile.str();
  131.     delete newheader.str();
  132.   }
  133.   if(existcpp) { // Already exists; verify it
  134.     strstream cppfile;
  135.     ostrstream newcpp;
  136.     cppfile << existcpp.rdbuf() << ends;
  137.     char buf[BSZ];
  138.     // Check that first line conforms:
  139.     if(cppfile.getline(buf, BSZ))
  140.       if(!strstr(buf, "//:") ||
  141.          !strstr(buf, b[implement]))
  142.         newcpp << b[CPPline1] << endl;
  143.     // Ensure header is included:
  144.     if(!strstr(cppfile.str(), b[include]))
  145.       newcpp << b[include] << endl;
  146.     // Put in the rest of the file:
  147.     newcpp << buf << endl; // First line read
  148.     newcpp << cppfile.rdbuf() << ends;
  149.     // If there were changes, overwrite file:
  150.     if(strcmp(cppfile.str(),newcpp.str())!=0){
  151.       existcpp.close();
  152.       ofstream newCPP(b[implement]);
  153.       newCPP << "//@//" << endl // change marker
  154.         << newcpp.rdbuf();
  155.     }
  156.     delete cppfile.str();
  157.     delete newcpp.str();
  158.   }
  159. }
  160.