Advertisement
Guest User

require.h

a guest
Apr 15th, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #ifndef REQUIRE_H
  2. #define REQUIRE_H
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. inline void require(bool requirement,
  9.   const std::string& msg = "Requirement failed"){
  10.   using namespace std;
  11.   if (!requirement) {
  12.     fputs(msg.c_str(), stderr);
  13.     fputs("\n", stderr);
  14.     exit(1);
  15.   }
  16. }
  17.  
  18. inline void requireArgs(int argc, int args,
  19.   const std::string& msg =
  20.     "Must use %d arguments") {
  21.   using namespace std;
  22.    if (argc != args + 1) {
  23.      fprintf(stderr, msg.c_str(), args);
  24.      fputs("\n", stderr);
  25.      exit(1);
  26.    }
  27. }
  28.  
  29. inline void requireMinArgs(int argc, int minArgs,
  30.   const std::string& msg =
  31.     "Must use at least %d arguments") {
  32.   using namespace std;
  33.   if(argc < minArgs + 1) {
  34.     fprintf(stderr, msg.c_str(), minArgs);
  35.     fputs("\n", stderr);
  36.     exit(1);
  37.   }
  38. }
  39.  
  40. inline void assure(std::ifstream& in,
  41.   const std::string& filename = "") {
  42.   using namespace std;
  43.   if(!in) {
  44.     fprintf(stderr, "Could not open file %s\n",
  45.       filename.c_str());
  46.     exit(1);
  47.   }
  48. }
  49.  
  50. inline void assure(std::ofstream& out,
  51.   const std::string& filename = "") {
  52.   using namespace std;
  53.   if(!out) {
  54.     fprintf(stderr, "Could not open file %s\n",
  55.       filename.c_str());
  56.     exit(1);
  57.   }
  58. }
  59. #endif // REQUIRE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement