00001
00002
00003
00004
00005
00006
00007
00008 #ifndef REQUIRE_H
00009 #define REQUIRE_H
00010 #include <cstdio>
00011 #include <cstdlib>
00012 #include <fstream>
00013 #include <string>
00014
00015 inline void require(bool requirement,
00016 const std::string& msg = "Requirement failed"){
00017 using namespace std;
00018 if (!requirement) {
00019 fputs(msg.c_str(), stderr);
00020 fputs("\n", stderr);
00021 exit(1);
00022 }
00023 }
00024
00025 inline void requireArgs(int argc, int args,
00026 const std::string& msg =
00027 "Must use %d arguments") {
00028 using namespace std;
00029 if (argc != args + 1) {
00030 fprintf(stderr, msg.c_str(), args);
00031 fputs("\n", stderr);
00032 exit(1);
00033 }
00034 }
00035
00036 inline void requireMinArgs(int argc, int minArgs,
00037 const std::string& msg =
00038 "Must use at least %d arguments") {
00039 using namespace std;
00040 if(argc < minArgs + 1) {
00041 fprintf(stderr, msg.c_str(), minArgs);
00042 fputs("\n", stderr);
00043 exit(1);
00044 }
00045 }
00046
00047 inline void assure(std::ifstream& in,
00048 const std::string& filename = "") {
00049 using namespace std;
00050 if(!in) {
00051 fprintf(stderr, "Could not open file %s\n",
00052 filename.c_str());
00053 exit(1);
00054 }
00055 }
00056
00057 inline void assure(std::ofstream& out,
00058 const std::string& filename = "") {
00059 using namespace std;
00060 if(!out) {
00061 fprintf(stderr, "Could not open file %s\n",
00062 filename.c_str());
00063 exit(1);
00064 }
00065 }
00066 #endif // REQUIRE_H