72 file.exceptions(ios::badbit | ios::failbit);
74 file.open(ename.c_str());
81 }
catch (
const std::exception& e) {
83 "Cannot open output file: ",
85 "\nMaybe you don't have write access to the directory or the file?");
97 streampos fpos = file.tellp();
99 if (!fpos) std::filesystem::remove(
expand_path(name).c_str());
121 find_file(matching_files, ename, allpaths);
123 if (matching_files.
nelem()) ename = matching_files[0];
129 file.exceptions(ios::badbit);
132 file.open(ename.c_str());
138 "Cannot open input file: ",
140 "\nMaybe the file does not exist?");
160 while (is && is.good() && !is.eof()) {
162 getline(is, linebuffer);
165 text.push_back(linebuffer);
200 }
catch (
const std::runtime_error& x) {
216 Index j = s.find(what);
217 while (j != s.
npos) {
218 s.replace(j, 1, with);
219 j = s.find(what, j + with.size());
242 const char* cp =
d.c_str();
243 while (*cp ==
'\n') cp++;
245 if (!(*cp)) result = 1;
247 if (!result &&
d[
d.length() - 1] !=
'\n')
249 else if (!result &&
d.length() > 2 &&
d[
d.length() - 1] ==
'\n' &&
250 d[
d.length() - 2] ==
'\n')
266 return std::filesystem::exists(filename) &&
267 !std::filesystem::is_directory(filename);
285 const std::string_view filename,
292 if (!paths.
nelem() || std::filesystem::path(efilename).is_absolute()) {
293 for (
const auto& ext : extensions) {
294 const String fullpath{efilename + ext};
297 if (std::find(matches.begin(), matches.end(), fullpath) ==
299 matches.push_back(fullpath);
306 for (
const auto& path : paths) {
307 for (
const auto& ext : extensions) {
311 if (std::find(matches.begin(), matches.end(), fullpath) ==
313 matches.push_back(fullpath);
346 find_file(matching_files, filename, allpaths, {
"",
".xml",
".gz",
".xml.gz"});
349 "Cannot find input file: ",
354 if (matching_files.
nelem() > 1) {
357 <<
" WARNING: More than one file matching this name exists in the data path.\n"
358 <<
" Using the first file (1) found:\n";
359 for (Index i = 0; i < matching_files.
nelem(); i++)
360 out1 <<
" (" << i + 1 <<
") " << matching_files[i] <<
"\n";
363 filename = matching_files[0];
385 find_file(matching_files, filename, allpaths, {
"",
".xml",
".gz",
".xml.gz"});
387 if (matching_files.
nelem()) {
388 filename = matching_files[0];
406 if ((path.length() == 1 && path[0] ==
'~') ||
407 (path.length() > 1 && path[0] ==
'~' && path[1] ==
'/')) {
432 return expanded_path;
446 if (!path.length())
return dirname;
450 if (path[0] ==
'/') dirname =
"/";
451 if (fileparts.
nelem() > 1) {
452 for (Index i = 0; i < fileparts.
nelem() - 1; i++) {
453 dirname += fileparts[i];
454 if (i < fileparts.
nelem() - 2) dirname +=
"/";
472 "Error accessing directory: ",
476 for (
const auto& filename :
477 std::filesystem::directory_iterator{dirname}) {
478 files.push_back(filename.path().string());
497 String basename = filename;
501 if (extension.length()) {
502 size_t pos = filename.rfind(extension);
503 if (pos == filename.length() - extension.length()) {
504 basename = filename.substr(0, filename.length() - extension.length());
505 extensionname = extension;
509 Index filenumber = 0;
510 ostringstream newfilename;
511 newfilename << basename << extensionname;
517 newfilename << basename <<
"." << filenumber << extensionname;
520 return newfilename.str();
This file contains the definition of Array.
The global header file for ARTS.
Index nelem() const ARTS_NOEXCEPT
Structure to hold all command line Parameters.
ArrayOfString includepath
List of paths to search for include files.
String outdir
If this is specified (with the -o –outdir option), it is used as the base directory for the report fi...
ArrayOfString datapath
List of paths to search for data files.
void split(Array< my_basic_string< charT > > &aos, const my_basic_string< charT > &delim) const
static const Index npos
Define npos:
#define ARTS_USER_ERROR(...)
#define ARTS_USER_ERROR_IF(condition,...)
ArrayOfString read_text_from_stream(istream &is)
Read an ASCII stream and append the contents to the String array text.
ArrayOfString list_directory(const std::string_view dirname)
Return list of files in directory.
ArrayOfString read_text_from_file(const std::string_view name)
Reads an ASCII file and appends the contents to the String vector text.
String add_basedir(const std::string_view path)
void find_xml_file(String &filename, const Verbosity &verbosity)
Find an xml file.
bool find_xml_file_existence(String &filename)
As find_xml_file but does not throw in the main body.
String expand_path(String path)
void replace_all(String &s, const std::string_view what, const std::string_view with)
Replace all occurances of ‘what’ in ‘s’ with ‘with’.
void filename_ascii(String &filename, const String &varname)
Gives the default file name for the ASCII formats.
void open_input_file(ifstream &file, const std::string_view name)
Open a file for reading.
int check_newline(const std::string_view s)
Checks if there is exactly one newline character at the end of the string.
String make_filename_unique(const std::string_view filename, const String &extension)
Make filename unique.
bool file_exists(const std::string_view filename)
Checks if the given file exists.
void open_output_file(ofstream &file, const std::string_view name)
Open a file for writing.
bool find_file(ArrayOfString &matches, const std::string_view filename, const ArrayOfString &paths, const ArrayOfString &extensions)
Searches through paths for a file with a matching name.
String get_dirname(const std::string_view path)
Return the parent directory of a path.
void cleanup_output_file(ofstream &file, const std::string_view name)
Closes the file.
This file contains basic functions to handle ASCII files.
String out_basename
The basename for the report file and for all other output files.
my_basic_string< char > String
The String type for ARTS.
Parameters parameters
Holds the command line parameters.
This file contains header information for the dealing with command line parameters.