fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <array>
  5. #include <complex>
  6. #include <map>
  7. #include <fstream>
  8. #include <climits>
  9.  
  10. //read in string literals
  11. template<class e, class t, int N>
  12. std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e(&sliteral)[N]) {
  13. std::array<e, N-1> buffer; //get buffer
  14. in >> buffer[0]; //skips whitespace
  15. if (N>2)
  16. in.read(&buffer[1], N-2); //read the rest
  17. if (strncmp(&buffer[0], sliteral, N-1)) //if it failed
  18. in.setstate(in.rdstate() | std::ios::failbit); //set the state
  19. return in;
  20. }
  21. template<class e, class t>
  22. std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e& cliteral) {
  23. e buffer; //get buffer
  24. in >> buffer; //read data
  25. if (buffer != cliteral) //if it failed
  26. in.setstate(in.rdstate() | std::ios::failbit); //set the state
  27. return in;
  28. }
  29. template<class e, class t, int N>
  30. std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, e(&carray)[N]) {
  31. return std::operator>>(in, carray);
  32. }
  33.  
  34. //default handlers
  35. typedef void (*handler)(std::string, int);
  36. static void errorHandlerDefault(std::string message, int severity) //Supplies a default error handler
  37. {std::cerr << "ERROR: " << severity << ' ' << message << '\n';}
  38. static void warningHandlerDefault(std::string message, int severity) //Supplies a default warning handler
  39. {std::cerr << "WARNING: " << severity << ' ' << message << '\n';}
  40.  
  41. //structs
  42. struct libitem {
  43. std::string name;
  44. std::complex<double> somecomplex;
  45. std::complex<double> someothercomplex;
  46. };
  47.  
  48. struct lib {
  49. std::string filelibfilename;
  50. std::map<std::string, libitem> items;
  51. };
  52.  
  53. struct myothertag
  54. {
  55. std::string type;
  56. int above, below, id;
  57. };
  58.  
  59. struct fileset
  60. {
  61. std::string filelibfilename;
  62. std::map<int, std::string> mytagdata;
  63. std::vector<myothertag> myothertagdata;
  64. };
  65.  
  66. template<class T>
  67. std::istream& loadObjectFromFile(std::istream& file, T& obj,
  68. handler errorH = errorHandlerDefault,
  69. handler warningH = warningHandlerDefault)
  70. {
  71. return file>>obj;
  72. }
  73.  
  74. //custom complex parser
  75. template<class T>
  76. std::istream& loadObjectFromFile(std::istream& file, std::complex<T>& obj,
  77. handler errorH = errorHandlerDefault,
  78. handler warningH = warningHandlerDefault)
  79. {
  80. if (!file)
  81. return file;
  82. std::string fullline;
  83. std::getline(file, fullline);
  84. T r, i;
  85. std::stringstream ss(fullline);
  86. if (ss >> '(' >> r >> ',' >> 'i' >> ')') {
  87. obj = std::complex<T>(r, i);
  88. return file;
  89. }
  90. ss.clear();
  91. ss.seekg(0);
  92. if (ss >> '(' >> r >> 'i' >> ')') {
  93. obj = std::complex<T>(r, i);
  94. return file;
  95. }
  96. ss.clear();
  97. ss.seekg(0);
  98. if (ss >> r >> ',' >> i) {
  99. obj = std::complex<T>(r, i);
  100. return file;
  101. }
  102. ss.clear();
  103. ss.seekg(0);
  104. if (ss >> r >> i) {
  105. obj = std::complex<T>(r, i);
  106. return file;
  107. }
  108. file.setstate(file.rdstate() | std::ios::failbit);
  109. return file;
  110. }
  111.  
  112. std::istream& loadObjectFromFile(std::istream& file, std::string& obj,
  113. handler errorH = errorHandlerDefault,
  114. handler warningH = warningHandlerDefault)
  115. {
  116. obj.clear();
  117. //ignore leading whitespace
  118. while(isspace(file.peek()))
  119. file.get();
  120. //read everything but EOF, # and =
  121. int c = file.peek();
  122. for(;
  123. c!=EOF && c!='#' && c!='=' && c<CHAR_MAX && isspace(c)==false;
  124. c = file.peek())
  125. {
  126. obj.append(1, char(c));
  127. file.get();
  128. }
  129. //if there was nothing, return EOF, # or =
  130. if (obj.empty())
  131. obj.append(1, char(c));
  132. return file;
  133. }
  134.  
  135. std::istream& loadObjectFromFile(std::istream& file, libitem& obj,
  136. handler errorH = errorHandlerDefault,
  137. handler warningH = warningHandlerDefault)
  138. {
  139. std::string symbol;
  140. while(loadObjectFromFile(file, symbol)) {
  141. if (symbol[0] == '#') {
  142. std::getline(file, symbol);
  143. } else if (symbol == "<LIBITEM_END>") {
  144. break;
  145. } else if (symbol == "NAME") {
  146. if (! (file>>'=' && loadObjectFromFile(file, obj.name, errorH, warningH)) )
  147. errorH(symbol + " incorrectly formatted", 1);
  148. } else if (symbol == "SOMECOMPLEX") {
  149. if (!(file>>'=' && loadObjectFromFile(file,obj.somecomplex, errorH, warningH)))
  150. errorH(symbol + " incorrectly formatted", 1);
  151. } else if (symbol == "SOMEOTHERCOMPLEX") {
  152. if (!(file>>'=' && loadObjectFromFile(file,obj.someothercomplex, errorH, warningH)))
  153. errorH(symbol + " incorrectly formatted", 1);
  154. } else { //not a member: failure
  155. errorH(symbol + " is not a member of libitem", 1);
  156. file.setstate(file.rdstate() | std::ios::failbit);
  157. break;
  158. }
  159. }
  160. return file;
  161. }
  162.  
  163. std::istream& loadObjectFromFile(std::istream& file, lib& obj,
  164. handler errorH = errorHandlerDefault,
  165. handler warningH = warningHandlerDefault)
  166. {
  167. std::string symbol;
  168. while(loadObjectFromFile(file, symbol)) {
  169. if (symbol[0] == '#') {
  170. std::getline(file, symbol);
  171. } else if (symbol == "<FILE_LIBRARY_END>") {
  172. break;
  173. } else if (symbol == "FILE") {
  174. if (! (file>>'=' && loadObjectFromFile(file, obj.filelibfilename)))
  175. errorH(symbol + " incorrectly formatted", 1);
  176. else {
  177. std::ifstream recur(obj.filelibfilename.c_str());
  178. if (!loadObjectFromFile(recur, obj, errorH, warningH))
  179. file.setstate(file.rdstate() | std::ios::failbit);
  180. }
  181. } else if (symbol == "<LIBITEM_BEG>") {
  182. libitem t;
  183. if (loadObjectFromFile(file, t, errorH, warningH))
  184. obj.items[t.name] = t;
  185. } else {
  186. errorH(symbol + " is not a member of library", 1);
  187. file.setstate(file.rdstate() | std::ios::failbit);
  188. }
  189. }
  190. if (file.eof()) //eof is valid here
  191. file.clear();
  192. return file;
  193. }
  194.  
  195. std::istream& loadObjectFromFile(std::istream& file, myothertag& obj,
  196. const std::map<int, std::string>& mytagdata,
  197. handler errorH = errorHandlerDefault,
  198. handler warningH = warningHandlerDefault)
  199. {
  200. std::string symbol;
  201. while(loadObjectFromFile(file, symbol)) {
  202. if (symbol[0] == '#') {
  203. std::getline(file, symbol);
  204. } else if (symbol == "<MYOTHERTAG_END>") {
  205. break;
  206. } else if (symbol == "ID") {
  207. if (! (file>>'='>>obj.id))
  208. errorH(symbol + " incorrectly formatted", 1);
  209. } else if (symbol == "TYPE") {
  210. if (! (file>>'='>>obj.type))
  211. errorH(symbol + " incorrectly formatted", 1);
  212. } else if (symbol == "MYTAG_BELOW_ID") {
  213. if (! (file>>'='>>obj.below))
  214. errorH(symbol + " incorrectly formatted", 1);
  215. else {
  216. if (obj.below!=-1 && obj.below!=0 && mytagdata.find(obj.below)==mytagdata.cend())
  217. errorH(symbol + " has invalid value", 1);
  218. }
  219. } else if (symbol == "MYTAG_ABOVE_ID") {
  220. if (! (file>>'='>>obj.above))
  221. errorH(symbol + " incorrectly formatted", 1);
  222. else {
  223. if (obj.above!=-1 && obj.above!=0 && mytagdata.find(obj.above)==mytagdata.cend())
  224. errorH(symbol + " has invalid value", 1);
  225. }
  226. } else { //not a member: failure
  227. errorH(symbol + " is not a member of myothertag", 1);
  228. file.setstate(file.rdstate() | std::ios::failbit);
  229. break;
  230. }
  231. }
  232. return file;
  233. }
  234.  
  235. std::istream& loadObjectFromFile(std::istream& file, std::map<int, std::string>& obj,
  236. const lib& mylib,
  237. handler errorH = errorHandlerDefault,
  238. handler warningH = warningHandlerDefault)
  239. {
  240. std::string symbol;
  241. while(loadObjectFromFile(file, symbol)) {
  242. if (symbol[0] == '#') {
  243. std::getline(file, symbol);
  244. } else if (symbol == "<MYTAG_END>") {
  245. break;
  246. } else if (symbol.compare(0,6,"MYTAG<")==0) {
  247. std::string& value = obj[atoi(symbol.data()+6)];
  248. if (! (file>>'=' && loadObjectFromFile(file, value, errorH, warningH)) )
  249. errorH(symbol + " incorrectly formatted", 1);
  250. else if (mylib.items.find(value)==mylib.items.end())
  251. errorH(value + " was not found in the library", 1);
  252. } else { //not a member: failure
  253. errorH(symbol + " is not a member of tags", 1);
  254. file.setstate(file.rdstate() | std::ios::failbit);
  255. break;
  256. }
  257. }
  258. return file;
  259. }
  260.  
  261. std::istream& loadObjectFromFile(std::istream& file, fileset& obj,
  262. handler errorH = errorHandlerDefault,
  263. handler warningH = warningHandlerDefault)
  264. {
  265. std::string symbol;
  266. lib mylib;
  267. while(loadObjectFromFile(file, symbol)) {
  268. if (symbol[0] == '#') {
  269. std::getline(file, symbol);
  270. } else if (symbol == "<FILE_END>") {
  271. break;
  272. } else if (symbol == "<FILE_BEG>") {
  273. loadObjectFromFile(file, obj, errorH, warningH);
  274. } else if (symbol == "FILE") {
  275. std::string filename;
  276. if (! (file>>'='>>filename) )
  277. errorH(symbol + " incorrectly formatted", 1);
  278. else {
  279. std::ifstream recur(filename.c_str());
  280. if (!loadObjectFromFile(recur, obj, errorH, warningH))
  281. file.setstate(file.rdstate() | std::ios::failbit);
  282. }
  283. } else if (symbol == "<FILE_LIBRARY_BEG>") {
  284. loadObjectFromFile(file, mylib, errorH, warningH);
  285. obj.filelibfilename = mylib.filelibfilename;
  286. } else if (symbol == "<MYTAG_BEG>") {
  287. loadObjectFromFile(file, obj.mytagdata, mylib, errorH, warningH);
  288. } else if (symbol == "<MYOTHERTAG_BEG>") {
  289. myothertag t;
  290. if (loadObjectFromFile(file, t, obj.mytagdata, errorH, warningH))
  291. obj.myothertagdata.push_back(t);
  292. } else { //not a member: failure
  293. errorH(symbol + " is not a member of fileset", 1);
  294. file.setstate(file.rdstate() | std::ios::failbit);
  295. break;
  296. }
  297. }
  298. if (file.eof()) //eof is valid here
  299. file.clear();
  300. return file;
  301. }
  302.  
  303.  
  304.  
  305.  
  306. int main() {
  307. fileset obj;
  308. std::ifstream file("C:/input.in");
  309. if (!file)
  310. std::cout << "error opening file";
  311. else if (!loadObjectFromFile(file, obj))
  312. std::cout << "error loading file";
  313. else if (obj.myothertagdata.size() == 2)
  314. std::cout << "success";
  315. else
  316. std::cout << "unknown failure";
  317. return 0;
  318. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/array:40,
                 from prog.cpp:4:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/c++0x_warning.h:36:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
prog.cpp: In function ‘std::istream& loadObjectFromFile(std::istream&, myothertag&, const std::map<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<int>, std::allocator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, void (*)(std::string, int), void (*)(std::string, int))’:
prog.cpp:216: error: ‘const class std::map<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<int>, std::allocator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’ has no member named ‘cend’
prog.cpp:223: error: ‘const class std::map<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<int>, std::allocator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’ has no member named ‘cend’
prog.cpp: In function ‘std::istream& loadObjectFromFile(std::istream&, std::map<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<int>, std::allocator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, const lib&, void (*)(std::string, int), void (*)(std::string, int))’:
prog.cpp:247: error: ‘atoi’ was not declared in this scope
stdout
Standard output is empty