00001 00002 // 00003 // Copyright (C) 2000 00004 // Ralf Westram 00005 // Time-stamp: <Fri May/03/2002 18:56 MET Coder@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 #include "InputFile.h" 00021 00022 using namespace std; 00023 using namespace rs; 00024 using namespace rs::err; 00025 using namespace rs::file; 00026 00027 // start of implementation of class InputFile: 00028 00029 // ------------------------------------------------------------------- 00030 // InputFile::InputFile(const string& filename_, int bufsize_) 00031 // ------------------------------------------------------------------- 00032 InputFile::InputFile(const string& filename_, int bufsize_) 00033 : filename(filename_), 00034 current_line(0), 00035 in(0), 00036 bufsize(bufsize_), 00037 offset(0), 00038 filled(0), 00039 buffer(0), 00040 end_of_file_seen(false), 00041 ignore_empty(false), 00042 do_auto_break(0) 00043 { 00044 in = fopen(filename.c_str(), "rt"); 00045 // cerr << "filename='" << filename << "'" << endl; 00046 if (!in) throw IOError(filename); 00047 00048 buffer = new char[bufsize]; 00049 fillBuffer(); 00050 } 00051 00052 // ------------------------------------------- 00053 // InputFile::InputFile(int bufsize_) 00054 // ------------------------------------------- 00055 InputFile::InputFile(int bufsize_) 00056 : filename("STDIN"), 00057 current_line(0), 00058 in(0), 00059 bufsize(bufsize_), 00060 offset(0), 00061 filled(0), 00062 buffer(0), 00063 end_of_file_seen(false), 00064 ignore_empty(false), 00065 do_auto_break(0) 00066 { 00067 in = stdin; 00068 buffer = new char[bufsize]; 00069 fillBuffer(); 00070 } 00071 00072 // --------------------------------------------------------------------------------------------------- 00073 // void InputFile::autoLinebreak(int max_line_length, const string& toBreak, int toBreakAtPos) 00074 // --------------------------------------------------------------------------------------------------- 00075 void InputFile::autoLinebreak(int max_line_length, const string& toBreak, int toBreakAtPos) 00076 { 00077 do_auto_break = max_line_length; 00078 breakAt = toBreak; 00079 breakAtPos = toBreakAtPos; 00080 } 00081 00082 // ------------------------------------------------ 00083 // bool InputFile::getLine(string& result) 00084 // ------------------------------------------------ 00085 bool InputFile::getLine(string& result) { 00086 bool got_line = false; 00087 result = ""; 00088 do { 00089 // cout << "result[1]='" << result << "'\n"; 00090 00091 while (offset>=filled) { // alles ausgelesen 00092 if (end_of_file_seen) { 00093 if (result.length()) return true; 00094 return false; 00095 } 00096 fillBuffer(); 00097 } 00098 00099 assert(offset<filled); 00100 size_t rest = filled-offset; 00101 char *line_end = (char*)memchr(buffer+offset, '\n', rest); 00102 00103 if (line_end) { 00104 int line_length = (line_end-buffer)-offset; // length of line excl. \n 00105 assert(line_length>=0); 00106 assert(size_t(line_length) <= rest); 00107 result = result+string(buffer+offset, line_length); 00108 offset += line_length+1; 00109 got_line = true; 00110 current_line++; 00111 } 00112 else { 00113 // line is only partially in buffer 00114 00115 result = result+string(buffer+offset, rest); 00116 // cout << "result[2]='" << result << "'\n"; 00117 if (do_auto_break && result.length() > size_t(do_auto_break)) { // maybe line should be broken 00118 size_t best_offset = string::npos; 00119 size_t curr_offset = 0; 00120 00121 while (curr_offset < size_t(do_auto_break)) { 00122 size_t found = result.find(breakAt, curr_offset); 00123 if (found == string::npos) break; 00124 00125 if ((found+breakAtPos) <= size_t(do_auto_break)) best_offset = found+breakAtPos; 00126 curr_offset = found+1; 00127 } 00128 00129 if (best_offset != string::npos) { // found a position where line will be broken 00130 rest -= best_offset; 00131 assert(filled >= rest); 00132 offset = filled-rest; 00133 result.erase(best_offset); 00134 // cout << "result[3]='" << result << "'\n"; 00135 got_line = true; 00136 } 00137 } 00138 if (!got_line) { // did not break -> append next line 00139 rest = 0; 00140 offset = filled; 00141 } 00142 } 00143 00144 if (got_line && ignore_empty && result.empty()) got_line = false; 00145 } 00146 while (!got_line); 00147 // cout << "result[4]='" << result << "'\n"; 00148 return true; 00149 } 00150 00151 // -end- of implementation of class InputFile. 00152 00153 00154 // // ------------------------------------------------ 00155 // // bool InputFile::getLine(string& result) 00156 // // ------------------------------------------------ 00157 // bool InputFile::getLine(string& result) { 00158 // if (end_of_file_seen) return false; 00159 // if (in.eof()) { 00160 // end_of_file_seen = true; 00161 // return false; 00162 // } 00163 // if (!in.good()) { 00164 // throw IOError(filename); 00165 // } 00166 // char buf[bufsize]; 00167 00168 // if (!in.getline(buf, bufsize)) { 00169 // end_of_file_seen = true; 00170 // return false; 00171 // } 00172 00173 // ++current_line; 00174 // result = string(buf); 00175 // return true; 00176 // } 00177 00178 00179 00180
Contact me in case of errors or questions. This documentation is powered by ![]() |
(C) 2000-2002 ![]() |