preprocessor.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <fstream>
  5. #include "subprojects/tedi2html/tedi2html.h"
  6. #include "preprocessor.h"
  7. #include "exception.h"
  8. using namespace std;
  9. /*
  10. * Check if entered file has:
  11. * - Every "__" (list tag) has a ",," (end list tag)
  12. * Returns true if it's a valid tedi file.
  13. */
  14. bool test_file(string file){
  15. stringstream file_stringstream(file);
  16. string line;
  17. int start_list = 0;
  18. int end_list = 0;
  19. while(getline(file_stringstream, line)){
  20. if(line[0] != '<'){
  21. size_t ul = line.find("__");
  22. if(ul != std::string::npos)
  23. ++start_list;
  24. size_t ul_end = line.find(",,");
  25. if(ul_end != std::string::npos)
  26. ++end_list;
  27. }
  28. }
  29. bool result = true;
  30. if(start_list != end_list)
  31. result = false;
  32. return result;
  33. }
  34. /*
  35. * Load every file from '+' tag.
  36. */
  37. string one_file(string file){
  38. string line;
  39. string text;
  40. ifstream reader(file);
  41. size_t slash = file.rfind("/");
  42. if(!reader)
  43. throw Invalid("File not loaded. Maybe file doesn't exist ", file);
  44. while(getline(reader, line)){
  45. if(line[0] != '+')
  46. text += line + "\n";
  47. else{
  48. line = line.erase(0, 1);
  49. if(slash != string::npos)
  50. line = file.substr(0, slash + 1) + line;
  51. ifstream include(line);
  52. string include_line;
  53. if(!include)
  54. throw Invalid("File not loaded. Maybe file doesn't exist ", line);
  55. while(getline(include, include_line))
  56. text += include_line + "\n";
  57. include.close();
  58. }
  59. }
  60. reader.close();
  61. return text;
  62. }