ixxra

Parser para los problemas de fisica

Oct 12th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. /*
  2.  * DISCLAIMER: Este codigo utiliza archivos... cualquier perdida de informacion es responsabilidad solamente de quien usa este
  3.  * programa.
  4.  * DISCLAIMER: This programs works with files. By using this programm, you accept that the author of the code is not responsible
  5.  *of any damage to your files.
  6.  * Para compilar: g++ main.cpp
  7.  * Para utilizar: En una terminal, escribe ./parse_physics_problems <Archivo.tex
  8.  * Para simplificar el codigo, lo programe para que lea de la "entrada estandard" que es un termino muy de unix
  9.  * */
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <sstream>
  14.  
  15. using namespace std;
  16.  
  17. const char* PROBLEM_BEGIN = "\\begin{prob}";
  18. const char* PROBLEM_END = "\\end{prob}";
  19.  
  20.  
  21.  
  22.  
  23. int main(int argc, char **argv) {
  24.   if (argc > 1){
  25.     cout << "usage: parse_physics_problems" << endl;
  26.     cout << "Reads physics problems from stdin" << endl;
  27.     return 1;
  28.   }
  29.  
  30.   int file_count = 0;
  31.   string line;
  32.   ofstream stream;  
  33.   stringstream ss;
  34.  
  35.  
  36.   while (!cin.eof()){
  37.    
  38.     getline(cin, line);
  39.     if (line == PROBLEM_BEGIN){
  40.       file_count++;
  41.       ss << file_count << "-test.prob" << ends;
  42.       stream.open(ss.str().c_str());
  43.     }
  44.     else if (line == PROBLEM_END){
  45.       if (!stream.is_open()){
  46.     cout << "Error parseando el archivo!" << endl;
  47.       }
  48.       stream.close();
  49.       //ss.str("");
  50.       ss.seekp(0);//This is tricky, but avoids some memory allocations.
  51.     }
  52.     else {
  53.       if (stream.is_open())
  54.     stream << line << endl;
  55.       else
  56.     cout << line << endl;
  57.     }
  58.   }
  59. }
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment