home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C18 / Showerr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.5 KB  |  90 lines

  1. //: C18:Showerr.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Un-comment error generators
  7. #include "../require.h"
  8. #include <iostream>
  9. #include <fstream>
  10. #include <strstream>
  11. #include <cctype>
  12. #include <cstring>
  13. using namespace std;
  14. char* marker = "//!";
  15.  
  16. char* usage =
  17. "usage: showerr filename chapnum\n"
  18. "where filename is a C++ source file\n"
  19. "and chapnum is the chapter name it's in.\n"
  20. "Finds lines commented with //! and removes\n"
  21. "comment, appending //(#) where # is unique\n"
  22. "across all files, so you can determine\n"
  23. "if your compiler finds the error.\n"
  24. "showerr /r\n"
  25. "resets the unique counter.";
  26.  
  27. // File containing error number counter:
  28. char* errnum = "../errnum.txt";
  29. // File containing error lines:
  30. char* errfile = "../errlines.txt";
  31. ofstream errlines(errfile,ios::app);
  32.  
  33. int main(int argc, char* argv[]) {
  34.   requireArgs(argc, 2, usage);
  35.   if(argv[1][0] == '/' || argv[1][0] == '-') {
  36.     // Allow for other switches:
  37.     switch(argv[1][1]) {
  38.       case 'r': case 'R':
  39.         cout << "reset counter" << endl;
  40.         remove(errnum); // Delete files
  41.         remove(errfile);
  42.         return 0;
  43.       default:
  44.         cerr << usage << endl;
  45.         return 1;
  46.     }
  47.   }
  48.   char* chapter = argv[2];
  49.   strstream edited; // Edited file
  50.   int counter = 0;
  51.   {
  52.     ifstream infile(argv[1]);
  53.     assure(infile, argv[1]);
  54.     ifstream count(errnum);
  55.     assure(count, errnum);
  56.     if(count) count >> counter;
  57.     int linecount = 0;
  58.     const int sz = 255;
  59.     char buf[sz];
  60.     while(infile.getline(buf, sz)) {
  61.       linecount++;
  62.       // Eat white space:
  63.       int i = 0;
  64.       while(isspace(buf[i]))
  65.         i++;
  66.       // Find marker at start of line:
  67.       if(strstr(&buf[i], marker) == &buf[i]) {
  68.         // Erase marker:
  69.         memset(&buf[i], ' ', strlen(marker));
  70.         // Append counter & error info:
  71.         ostrstream out(buf, sz, ios::ate);
  72.         out << "//(" << ++counter << ") "
  73.             << "Chapter " << chapter
  74.             << " File: " << argv[1]
  75.             << " Line " << linecount << endl
  76.             << ends;
  77.           edited << buf;
  78.         errlines << buf; // Append error file
  79.       } else
  80.         edited << buf << "\n"; // Just copy
  81.     }
  82.   } // Closes files
  83.   ofstream outfile(argv[1]); // Overwrites
  84.   assure(outfile, argv[1]);
  85.   outfile << edited.rdbuf();
  86.   ofstream count(errnum); // Overwrites
  87.   assure(count, errnum);
  88.   count << counter; // Save new counter
  89. } ///:~
  90.