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

  1. //: :require.h
  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. // Test for error conditions in programs
  7. // Local "using namespace std" for old compilers
  8. #ifndef REQUIRE_H
  9. #define REQUIRE_H
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <fstream>
  13.  
  14. inline void require(bool requirement, 
  15.   const char* msg = "Requirement failed") {
  16.   using namespace std;
  17.   if (!requirement) {
  18.     fprintf(stderr, "%s", msg);
  19.     exit(1);
  20.   }
  21. }
  22.  
  23. inline void requireArgs(int argc, int args, 
  24.   const char* msg = "Must use %d arguments") {
  25.   using namespace std;
  26.    if (argc != args + 1) {
  27.      fprintf(stderr, msg, args);
  28.      exit(1);
  29.    }
  30. }
  31.  
  32. inline void requireMinArgs(int argc, int minArgs,
  33.   const char* msg = 
  34.     "Must use at least %d arguments") {
  35.   using namespace std;
  36.   if(argc < minArgs + 1) {
  37.     fprintf(stderr, msg, minArgs);
  38.     exit(1);
  39.   }
  40. }
  41.   
  42. inline void assure(std::ifstream& in, 
  43.   const char* filename = "") {
  44.   using namespace std;
  45.   if(!in) {
  46.     fprintf(stderr,
  47.       "Could not open file %s", filename);
  48.     exit(1);
  49.   }
  50. }
  51.  
  52. inline void assure(std::ofstream& in, 
  53.   const char* filename = "") {
  54.   using namespace std;
  55.   if(!in) {
  56.     fprintf(stderr,
  57.       "Could not open file %s", filename);
  58.     exit(1);
  59.   }
  60. }
  61. #endif // REQUIRE_H ///:~
  62.