home *** CD-ROM | disk | FTP | other *** search
- // File from page 252 in "Thinking in C++" by Bruce Eckel
- //////////////////////////////////////////////////
- // From the compressed package ECKELT01.ZIP 2/21/95
- // Copyright (c) Bruce Eckel, 1995
- // Source code file from the book "Thinking in C++",
- // Prentice Hall, 1995, ISBN: 0-13-917709-4
- // All rights reserved EXCEPT as allowed by the following
- // statements: You may freely use this file for your own
- // work, including modifications and distribution in
- // executable form only. You may copy and distribute this
- // file, as long as it is only distributed in the complete
- // (compressed) package with the other files from this
- // book and you do not remove this copyright and notice.
- // You may not distribute modified versions of the source
- // code in this package. This package may be freely placed
- // on bulletin boards, internet nodes, shareware disks and
- // product vendor disks. You may not use this file in
- // printed media without the express permission of the
- // author. Bruce Eckel makes no
- // representation about the suitability of this software
- // for any purpose. It is provided "as is" without express
- // or implied warranty of any kind. The entire risk as to
- // the quality and performance of the software is with
- // you. Should the software prove defective, you assume
- // the cost of all necessary servicing, repair, or
- // correction.
- // If you think you've found an error, please
- // email all modified files with loudly commented changes
- // to: eckel@aol.com (please use the same
- // address for non-code errors found in the book).
- //////////////////////////////////////////////////
-
- //: CPPCHECK.CPP -- Configures .H & .CPP files
- // To conform to style standard.
- // Tests existing files for conformance
- #include <fstream.h>
- #include <strstrea.h>
- #include <string.h>
- #include <ctype.h>
- #include <assert.h>
- #define SZ 40 // Buffer sizes
- #define BSZ 100
-
- main(int argc, char* argv[]) {
- assert(argc == 2); // File set name
- enum bufs { base, header, implement,
- Hline1, guard1, guard2, guard3,
- CPPline1, include, bufnum };
- char b[bufnum][SZ];
- ostrstream osarray[] = {
- ostrstream(b[base], SZ),
- ostrstream(b[header], SZ),
- ostrstream(b[implement], SZ),
- ostrstream(b[Hline1], SZ),
- ostrstream(b[guard1], SZ),
- ostrstream(b[guard2], SZ),
- ostrstream(b[guard3], SZ),
- ostrstream(b[CPPline1], SZ),
- ostrstream(b[include], SZ),
- };
- osarray[base] << argv[1] << ends;
- // Find any '.' in the string using the
- // Standard C library function strchr():
- char* period = strchr(b[base], '.');
- if(period) *period = 0; // Strip extension
- // Force to upper case:
- for(int i = 0; b[base][i]; i++)
- b[base][i] = toupper(b[base][i]);
- // Create file names and internal lines:
- osarray[header] << b[base] << ".H" << ends;
- osarray[implement] << b[base] << ".CPP" << ends;
- osarray[Hline1] << "//:" << ' ' << b[header]
- << " -- " << ends;
- osarray[guard1] << "#ifndef " << b[base]
- << "_H_" << ends;
- osarray[guard2] << "#define " << b[base]
- << "_H_" << ends;
- osarray[guard3] << "#endif // " << b[base]
- << "_H_" << ends;
- osarray[CPPline1] << "//:" << ' '
- << b[implement]
- << " -- " << ends;
- osarray[include] << "#include \""
- << b[header] << "\"" <<ends;
- // First, try to open existing files:
- ifstream existh(b[header]),
- existcpp(b[implement]);
- if(!existh) { // Doesn't exist; create it
- ofstream newheader(b[header]);
- newheader << b[Hline1] << endl
- << b[guard1] << endl
- << b[guard2] << endl << endl
- << b[guard3] << endl;
- }
- if(!existcpp) { // Create cpp file
- ofstream newcpp(implement);
- newcpp << b[CPPline1] << endl
- << b[include] << endl;
- }
- if(existh) { // Already exists; verify it
- strstream hfile; // Write & read
- ostrstream newheader; // Write
- hfile << existh.rdbuf() << ends;
- // Check that first line conforms:
- char buf[BSZ];
- if(hfile.getline(buf, BSZ)) {
- if(!strstr(buf, "//:") ||
- !strstr(buf, b[header]))
- newheader << b[Hline1] << endl;
- }
- // Ensure guard lines are in header:
- if(!strstr(hfile.str(), b[guard1]) ||
- !strstr(hfile.str(), b[guard2]) ||
- !strstr(hfile.str(), b[guard3])) {
- newheader << b[guard1] << endl
- << b[guard2] << endl
- << buf
- << hfile.rdbuf() << endl
- << b[guard3] << endl << ends;
- } else
- newheader << buf
- << hfile.rdbuf() << ends;
- // If there were changes, overwrite file:
- if(strcmp(hfile.str(),newheader.str())!=0){
- existh.close();
- ofstream newH(b[header]);
- newH << "//@//" << endl // change marker
- << newheader.rdbuf();
- }
- delete hfile.str();
- delete newheader.str();
- }
- if(existcpp) { // Already exists; verify it
- strstream cppfile;
- ostrstream newcpp;
- cppfile << existcpp.rdbuf() << ends;
- char buf[BSZ];
- // Check that first line conforms:
- if(cppfile.getline(buf, BSZ))
- if(!strstr(buf, "//:") ||
- !strstr(buf, b[implement]))
- newcpp << b[CPPline1] << endl;
- // Ensure header is included:
- if(!strstr(cppfile.str(), b[include]))
- newcpp << b[include] << endl;
- // Put in the rest of the file:
- newcpp << buf << endl; // First line read
- newcpp << cppfile.rdbuf() << ends;
- // If there were changes, overwrite file:
- if(strcmp(cppfile.str(),newcpp.str())!=0){
- existcpp.close();
- ofstream newCPP(b[implement]);
- newCPP << "//@//" << endl // change marker
- << newcpp.rdbuf();
- }
- delete cppfile.str();
- delete newcpp.str();
- }
- }
-