recursivefilereader.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file recursivefilereader.h
  3. /// \brief The RecursiveFileReader class
  4. ///
  5. /// $Date: 2014-09-09 10:49:13 +0200 (mar, 09 sep 2014) $
  6. ///////////////////////////////////////////////////////////////////////////////////////
  7. #ifndef RECURSIVEFILEREADER_H
  8. #define RECURSIVEFILEREADER_H
  9. // Disable warning in old VC 6.0 due to the limited support for STL
  10. #if defined(_MSC_VER) && (_MSC_VER<1300)
  11. #pragma warning (disable : 4786)
  12. #endif
  13. #include <gutil.h>
  14. #include <stack>
  15. /// Class for reading from files which include other files
  16. /** This class gives plib the illusion that its reading from one simple long
  17. * flat file, even if in reality there are several files involved due to the
  18. * fact that plib scripts can include other plib scripts.
  19. *
  20. * This class behaves like a regular C FILE* stream. At the moment only
  21. * feof and fgetc are implemented since that is all plib needs, however it
  22. * should be easy to add other standard functions if needed.
  23. *
  24. * To use the class, create an object and call addfile with the path to the
  25. * initial file. Then start reading from the file with the member functions of
  26. * RecursiveFileReader. When a new file should be included, call addfile again
  27. * and the RecursiveFileReader object will start reading from that file until
  28. * it is empty (or it includes another file). When reaching the end of a file
  29. * RecursiveFileReader will automatically go back and start reading from the
  30. * previous file.
  31. */
  32. class RecursiveFileReader {
  33. public:
  34. /// Creates a RecursiveFileReader object without any file opened
  35. RecursiveFileReader();
  36. /// Closes all remaining open files
  37. ~RecursiveFileReader();
  38. /// Checks if we have reached the end of the file(s)
  39. /** Works like the standard feof function but takes into account
  40. * that we might be inside of an included file and thus may have
  41. * more to read by going back to the including file.
  42. *
  43. * Odd capitalization due to the fact that some compilers may
  44. * have feof defined as a macro.
  45. */
  46. int Feof();
  47. /// Gets the next character from the stream
  48. /** Works like the standard fgetc function but maintains the illusion
  49. * that we're reading from a single flat file.
  50. *
  51. * Odd capitalization due to the fact that some compilers may
  52. * have fgetc defined as a macro.
  53. */
  54. int Fgetc();
  55. /// Start reading from a new file
  56. /** The file will be opened and will be the new current file from
  57. * which reading is done. When we reach the end of the new file
  58. * we will go back to the file which was the current file when
  59. * addfile was called.
  60. *
  61. * @returns whether the file could be opened or not.
  62. */
  63. bool addfile(const char* path);
  64. /// The file we're currently reading from
  65. /** Even though we want plib to have the illusion of one flat file,
  66. * it needs to be able to get the current file name in order to print
  67. * out good error messages.
  68. */
  69. xtring currentfilename() const;
  70. /// The line number we're at in the current file.
  71. int currentlineno() const;
  72. private:
  73. /// Closes the current file and pops it from the stack
  74. void closeandpop();
  75. /// The FILE* we're currently reading from
  76. FILE* currentstream() const;
  77. /// All we need to know about each opened file
  78. struct File {
  79. File(FILE* in, const xtring& file)
  80. : stream(in), filename(file), lineno(1) {}
  81. FILE* stream;
  82. xtring filename;
  83. int lineno;
  84. };
  85. /// The currently opened files
  86. std::stack<File> files;
  87. };
  88. #endif // RECURSIVEFILEREADER_H