TextFile.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2001 Jani Kajala
  3. *
  4. * Permission to use, copy, modify, distribute and sell this
  5. * software and its documentation for any purpose is hereby
  6. * granted without fee, provided that the above copyright notice
  7. * appear in all copies and that both that copyright notice and
  8. * this permission notice appear in supporting documentation.
  9. * Jani Kajala makes no representations about the suitability
  10. * of this software for any purpose. It is provided "as is"
  11. * without express or implied warranty.
  12. */
  13. #ifndef _DEV_TEXTFILE_H
  14. #define _DEV_TEXTFILE_H
  15. namespace dev
  16. {
  17. /**
  18. * ASCII-7 text file parser. Doesnt throw exceptions.
  19. */
  20. class TextFile
  21. {
  22. public:
  23. /** Error code. */
  24. enum ErrorType
  25. {
  26. /** No error. */
  27. ERROR_NONE,
  28. /** File open failed. */
  29. ERROR_OPEN,
  30. /** File reading failed. */
  31. ERROR_READ,
  32. /** Syntax error. */
  33. ERROR_PARSE
  34. };
  35. /** Opens a file. */
  36. explicit TextFile( const char* filename );
  37. /***/
  38. ~TextFile();
  39. /**
  40. * Reads a single character.
  41. * @return true if read ok.
  42. */
  43. bool readChar( char* ch );
  44. /**
  45. * Peeks a single character.
  46. * @return true if peek ok.
  47. */
  48. bool peekChar( char* ch );
  49. /**
  50. * Reads whitespace delimited string.
  51. * If the string doesnt fit to the buffer then
  52. * the rest of the string is skipped. Buffer
  53. * is always 0-terminated.
  54. * @param buf [out] Pointer to string buffer.
  55. * @param size String buffer size. Must be larger than 0.
  56. * @return false if end-of-file reached before any characters was read.
  57. */
  58. bool readString( char* buf, int size );
  59. /** Skips the rest of the line. */
  60. void skipLine();
  61. /** Reads hex integer. Skips preceding whitespace. */
  62. long readHex();
  63. /**
  64. * Skips whitespace characters.
  65. * @return false if end-of-file reached.
  66. */
  67. bool skipWhitespace();
  68. /** Returns true if end-of-file have been reached. */
  69. bool eof() const;
  70. /** Returns error code or 0 (ERROR_NONE) if no error. */
  71. ErrorType error() const;
  72. /** Returns line number of last successful read character. */
  73. int line() const;
  74. private:
  75. class TextFileImpl;
  76. TextFileImpl* m_this;
  77. TextFile( const TextFile& );
  78. TextFile& operator=( const TextFile& );
  79. };
  80. } /* dev */
  81. #endif /* _DEV_TEXTFILE_H */