VERB_code_2.3
error.h
Go to the documentation of this file.
1 
13 #ifndef ERROR_MESSAGES_H
14 #define ERROR_MESSAGES_H
15 
16 #include <string>
17 #include <vector>
18 #include <stdarg.h>
19 #include <stdio.h>
20 
21 using namespace std;
22 
26 class single_error {
27 public:
29  string code;
30 
32  string msg;
33 
35  single_error(string code) {
36  // store error code and empty message
37  this->code = code;
38  this->msg = "";
39  }
40 
42  single_error(string code, string msg) {
43  // store error code and message
44  this->code = code;
45  this->msg = msg;
46  }
47 };
48 
49 //typedef vector<error_msg> error_stack;
50 
54 class error_msg {
55 public:
57  vector<single_error> errors_stack;
58 
60  error_msg();
64  error_msg(char * code) {
65  errors_stack.push_back(single_error(code));
66  }
67  //error_msg(string code, string msg) {
68  // errors_stack.push_back(single_error(code, msg));
69  //}
70 
74 // !!! error_msg(char * code, char * msg, ...) {
75  error_msg(const char * code, const char * msg, ...) {
76 
77  // converting msg and args into one message and add error in stack with this message
78 
79  int len;
80  va_list args;
81  va_start( args, msg );
82  char *buffer = new char[1024];
83 
85  len = vsnprintf(buffer, 1024, msg, args ) + 1;
86  if (len == 0) throw error_msg("OUTPUT", "Output does not work :-(");
87 
88  errors_stack.push_back(single_error(code, buffer));
89 
90  delete buffer;
91  }
92 
96  void add(single_error err) {
97  errors_stack.push_back(err);
98  }
99 
103  void add(string code) {
104  errors_stack.push_back(single_error(code));
105  }
106 
110  void add(string code, string msg) {
111  errors_stack.push_back(single_error(code, msg));
112  }
113 
117  string what() {
118  unsigned i;
119  string result = "";
120  for (i = 0; i < errors_stack.size(); i++) {
121  // return all stack of errors as a string
122  result = result + errors_stack[i].code + ": " + errors_stack[i].msg + "\n";
123  }
124  return result;
125  }
126 
127 };
128 
129 #endif
void add(single_error err)
Definition: error.h:96
string code
Error code.
Definition: error.h:29
string what()
Definition: error.h:117
error_msg(char *code)
Definition: error.h:64
error_msg(const char *code, const char *msg,...)
Definition: error.h:75
single_error(string code)
Constructor.
Definition: error.h:35
string msg
Error message.
Definition: error.h:32
single_error(string code, string msg)
Constructor.
Definition: error.h:42
vector< single_error > errors_stack
Stack of errors.
Definition: error.h:57
Hold some information about an error in the code.
Definition: error.h:26
Error message - stack of single_errors.
Definition: error.h:54
void add(string code)
Definition: error.h:103
void add(string code, string msg)
Definition: error.h:110