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