00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020
00022
00033
00034
00036
00037 #include <stdexcept>
00038 #include <cmath>
00039 #include <cfloat>
00040 #include "arts.h"
00041 #include "matpackI.h"
00042 #include "array.h"
00043 #include "messages.h"
00044 #include "parameters.h"
00045 #include "file.h"
00046
00047
00049
00051
00053
00064 void filename_ascii(
00065 String& filename,
00066 const String& varname )
00067 {
00068 if ( "" == filename )
00069 {
00070 extern const String out_basename;
00071 filename = out_basename+"."+varname+".aa";
00072 }
00073 }
00074
00075
00076
00078
00080
00082
00091 void open_output_file(ofstream& file, const String& name)
00092 {
00093
00094
00095
00096
00097
00098 file.exceptions(ios::badbit |
00099 ios::failbit);
00100
00101
00102 file.open(name.c_str() );
00103
00104
00105
00106
00107
00108
00109 if (!file)
00110 {
00111 ostringstream os;
00112 os << "Cannot open output file: " << name << '\n'
00113 << "Maybe you don't have write access "
00114 << "to the directory or the file?";
00115 throw runtime_error(os.str());
00116 }
00117 }
00118
00119
00120
00122
00130 void open_input_file(ifstream& file, const String& name)
00131 {
00132
00133
00134
00135
00136 file.exceptions(ios::badbit);
00137
00138
00139 file.open(name.c_str() );
00140
00141
00142
00143
00144 if (!file)
00145 {
00146 ostringstream os;
00147 os << "Cannot open input file: " << name << '\n'
00148 << "Maybe the file does not exist?";
00149 throw runtime_error(os.str());
00150 }
00151 }
00152
00153
00154
00156
00165 void read_text_from_stream(ArrayOfString& text, istream& is)
00166 {
00167 String linebuffer;
00168
00169
00170
00171
00172
00173 while (is && is.good() && !is.eof())
00174 {
00175
00176 getline(is,linebuffer);
00177
00178
00179 text.push_back(linebuffer);
00180 }
00181
00182
00183
00184
00185 if ( !is.eof() ) {
00186 ostringstream os;
00187 os << "Read Error. Last line read:\n" << linebuffer;
00188 throw runtime_error(os.str());
00189 }
00190
00191 }
00192
00193
00194
00196
00206 void read_text_from_file(ArrayOfString& text, const String& name)
00207 {
00208 ifstream ifs;
00209
00210
00211 open_input_file(ifs, name);
00212
00213
00214
00215
00216
00217
00218 try
00219 {
00220 read_text_from_stream(text,ifs);
00221 }
00222 catch (runtime_error x)
00223 {
00224 ostringstream os;
00225 os << "Error reading file: " << name << '\n'
00226 << x.what();
00227 throw runtime_error(os.str());
00228 }
00229 }
00230
00231
00233
00241 void replace_all(String& s, const String& what, const String& with)
00242 {
00243 Index j = s.find(what);
00244 while ( j != s.npos )
00245 {
00246 s.replace(j,1,with);
00247 j = s.find(what,j+with.size());
00248 }
00249 }
00250
00252
00262 int check_newline(const String& s)
00263 {
00264 String d = s;
00265 int result = 0;
00266
00267
00268 replace_all (d, " ", "");
00269 replace_all (d, "\t", "");
00270 replace_all (d, "\r", "");
00271
00272 const char *cp = d.c_str ();
00273 while ((*cp == '\n') && *cp) cp++;
00274
00275 if (!(*cp))
00276 result = 1;
00277
00278 if (!result && d[d.length () - 1] != '\n')
00279 result = 2;
00280 else if (!result && d.length () > 2
00281 && d[d.length () - 1] == '\n' && d[d.length () - 2] == '\n')
00282 result = 3;
00283
00284 return result;
00285 }
00286
00287
00289
00298 bool file_exists(const String& filename)
00299 {
00300 bool exists = false;
00301 fstream fin;
00302 fin.open(filename.c_str(), ios::in);
00303 if (fin.is_open())
00304 {
00305 exists=true;
00306 }
00307 fin.close();
00308 return exists;
00309 }
00310
00311
00313
00324 bool find_file(String& filename, const char* extension)
00325 {
00326 bool exists = true;
00327 extern const Parameters parameters;
00328
00329 if (!file_exists (filename))
00330 {
00331 if (file_exists (filename + extension))
00332 {
00333 filename += extension;
00334 }
00335 else
00336 {
00337 Index i;
00338 for (i=0; i < parameters.includepath.nelem(); i++)
00339 {
00340 String fullpath;
00341 fullpath = parameters.includepath[i] + "/" + filename;
00342 if (file_exists (fullpath))
00343 {
00344 filename = fullpath;
00345 break;
00346 }
00347 else if (file_exists (fullpath + extension))
00348 {
00349 filename = fullpath + extension;
00350 break;
00351 }
00352 }
00353 if (i == parameters.includepath.nelem())
00354 {
00355 exists = false;
00356 }
00357 }
00358 }
00359
00360 return exists;
00361 }
00362