00001 00002 // 00003 // Copyright (C) 2000 00004 // Ralf Westram 00005 // (Coded@ReallySoft.de) 00006 // 00007 // Permission to use, copy, modify, distribute and sell this software 00008 // and its documentation for any purpose is hereby granted without fee, 00009 // provided that the above copyright notice appear in all copies and 00010 // that both that copyright notice and this permission notice appear 00011 // in supporting documentation. Ralf Westram makes no 00012 // representations about the suitability of this software for any 00013 // purpose. It is provided "as is" without express or implied warranty. 00014 // 00015 // This code is part of my library. 00016 // You may find a more recent version at http://www.reallysoft.de/ 00017 // 00019 00020 #ifndef INPUTFILE_H 00021 #define INPUTFILE_H 00022 00023 #ifndef __CSTDIO__ 00024 #include <cstdio> 00025 #endif 00026 00027 #ifndef ERR_H 00028 #include "Err.h" 00029 #endif 00030 #ifndef TOOLS_H 00031 #include "Tools.h" 00032 #endif 00033 00034 namespace rs { 00035 namespace file { 00036 00037 #define RS_INPUTFILE_BUFSIZE 2048 // max size of a line 00038 00039 // -------------------------------------------------------------------------------- 00040 // class InputFile 00041 // -------------------------------------------------------------------------------- 00043 class InputFile { 00044 private: 00045 std::string filename; 00046 unsigned long current_line; 00047 FILE *in; 00048 size_t bufsize; 00049 size_t offset; // read offset into buffer 00050 size_t filled; // no of bytes in buffer 00051 char *buffer; 00052 bool end_of_file_seen; 00053 bool ignore_empty; 00054 int do_auto_break; // 0 -> no autobreak 00055 std::string breakAt; // inside this string autobreak is allowed 00056 int breakAtPos; // at this position of 'breakAt' 00057 00058 void fillBuffer() { 00059 assert(!end_of_file_seen); 00060 filled = fread(buffer, 1, bufsize, in); 00061 if (filled<bufsize) end_of_file_seen = true; 00062 offset = 0; 00063 } 00064 00065 public: 00071 InputFile(const std::string& filename_, int bufsize_=RS_INPUTFILE_BUFSIZE); 00072 00076 InputFile(int bufsize_=RS_INPUTFILE_BUFSIZE); 00077 00079 virtual ~InputFile() { 00080 delete buffer; 00081 if (in) fclose(in); 00082 } 00083 00100 void autoLinebreak(int max_line_length, const std::string& toBreak, int toBreakAtPos); 00101 00106 bool getLine(std::string& result); 00107 00111 unsigned long getLineNumber() const { return current_line; } 00112 00116 void ignoreEmptyLines(bool ignore) { ignore_empty = ignore; } 00117 00126 void throw_Error(const std::string& message) const { 00127 throw err::Error(str::strf("Error in %s (#%i): %s", filename.c_str(), current_line, message.c_str())); 00128 } 00129 }; 00130 00131 }; 00132 }; 00133 00134 #else 00135 #error InputFile.h included twice 00136 #endif // INPUTFILE_H
Contact me in case of errors or questions. This documentation is powered by . |
(C) 2000-2002 |