ARTS 2.5.11 (git: 6827797f)
|
This file contains basic functions to handle ASCII data files. More...
#include "arts.h"
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <stdexcept>
#include "array.h"
#include "file.h"
#include "parameters.h"
Go to the source code of this file.
Functions | |
void | filename_ascii (String &filename, const String &varname) |
Gives the default file name for the ASCII formats. | |
void | open_output_file (ofstream &file, const std::string_view name) |
Open a file for writing. | |
void | cleanup_output_file (ofstream &file, const std::string_view name) |
Closes the file. | |
void | open_input_file (ifstream &file, const std::string_view name) |
Open a file for reading. | |
ArrayOfString | read_text_from_stream (istream &is) |
Read an ASCII stream and append the contents to the String array text. | |
ArrayOfString | read_text_from_file (const std::string_view name) |
Reads an ASCII file and appends the contents to the String vector text. | |
void | replace_all (String &s, const std::string_view what, const std::string_view with) |
Replace all occurances of ‘what’ in ‘s’ with ‘with’. | |
int | check_newline (const std::string_view s) |
Checks if there is exactly one newline character at the end of the string. | |
bool | file_exists (const std::string_view filename) |
Checks if the given file exists. | |
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. | |
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) |
String | add_basedir (const std::string_view path) |
String | get_dirname (const std::string_view path) |
Return the parent directory of a path. | |
ArrayOfString | list_directory (const std::string_view dirname) |
Return list of files in directory. | |
String | make_filename_unique (const std::string_view filename, const String &extension) |
Make filename unique. | |
This file contains basic functions to handle ASCII data files.
Definition in file file.cc.
String add_basedir | ( | const std::string_view | path | ) |
Adds base directory to the given path if it is a relative path.
[in] | path | String with path |
Definition at line 424 of file file.cc.
References expand_path(), my_basic_string< charT >::nelem(), Parameters::outdir, and parameters.
Referenced by arts_exit(), arts_exit_with_error_message(), nca_write_to_file(), open_output_file(), and xml_write_to_file().
int check_newline | ( | const std::string_view | s | ) |
Checks if there is exactly one newline character at the end of the string.
s | The String to check. |
Definition at line 233 of file file.cc.
References d, and replace_all().
Referenced by md_sanity_checks().
void cleanup_output_file | ( | ofstream & | file, |
const std::string_view | name | ||
) |
Closes the file.
If it is empty, the file is deleted.
file | File pointer |
ios_base::failure | Could for example mean that the directory is read only. |
Definition at line 95 of file file.cc.
References expand_path().
Referenced by arts_exit().
Expands the ~ to home directory location in given path.
[in] | path | String with path |
Definition at line 405 of file file.cc.
Referenced by add_basedir(), cleanup_output_file(), find_file(), nca_read_from_file(), and open_input_file().
bool file_exists | ( | const std::string_view | filename | ) |
Checks if the given file exists.
filename | File to check. |
Definition at line 265 of file file.cc.
Referenced by find_file(), make_filename_unique(), and ScatSpeciesScatAndMetaRead().
Gives the default file name for the ASCII formats.
The default name is only used if the file name is empty.
filename | Output: file name |
varname | variable name |
Definition at line 43 of file file.cc.
References out_basename.
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.
If the filename starts with '/', the search path is ignored.
[in,out] | matches | Matching files are appended to this list. |
[in] | filename | File to find. |
[in] | paths | List of paths to look in for the file. |
[in] | extensions | List of extensions to add to base filename. |
Definition at line 284 of file file.cc.
References expand_path(), file_exists(), and Array< base >::nelem().
Referenced by find_xml_file(), find_xml_file_existence(), open_input_file(), and ArtsParser::parse_agenda().
Find an xml file.
If it doesn't exist in the current directory, also search the include and data path. Also tests if a compressed version exists.
The filename will be modified to contain the full path to the found match.
[in,out] | filename | File to check. |
[in] | verbosity | Verbosity |
runtime_error | if file is not found. |
Definition at line 338 of file file.cc.
References ARTS_USER_ERROR_IF, CREATE_OUT1, Parameters::datapath, find_file(), Parameters::includepath, Array< base >::nelem(), and parameters.
Referenced by abs_speciesDefineAllInScenario(), ScatSpeciesScatAndMetaRead(), xml_find_and_open_input_file(), and xml_read_from_file().
bool find_xml_file_existence | ( | String & | filename | ) |
As find_xml_file but does not throw in the main body.
The filename will be modified to contain the full path to the found match if there is a match
[in,out] | filename | File to check. |
Definition at line 377 of file file.cc.
References Parameters::datapath, find_file(), Parameters::includepath, Array< base >::nelem(), and parameters.
Referenced by abs_lines_per_speciesReadSpeciesSplitCatalog(), and abs_linesReadSpeciesSplitCatalog().
String get_dirname | ( | const std::string_view | path | ) |
Return the parent directory of a path.
Extracts the parent directory part of the given path.
[out] | dirname | Parent directory of path |
[in] | path | Path |
Definition at line 444 of file file.cc.
References Array< base >::nelem(), and my_basic_string< charT >::split().
Referenced by get_parameters(), and ArtsParser::parse_agenda().
ArrayOfString list_directory | ( | const std::string_view | dirname | ) |
Return list of files in directory.
Returns list of all files in the given path.
[out] | files | List of files |
[in] | dirname | Parent directory of path |
Definition at line 470 of file file.cc.
References ARTS_USER_ERROR_IF.
Referenced by abs_cia_dataReadFromCIA().
Make filename unique.
Checks if a file (or a gzipped version of it) with the given name already exists und appends a unique number to the filename if necessary
[in] | filename | Filename |
[in] | extension | Optional, number is inserted before the extension |
Definition at line 496 of file file.cc.
References file_exists().
Referenced by xml_write_to_file().
void open_input_file | ( | ifstream & | file, |
const std::string_view | name | ||
) |
Open a file for reading.
If the file cannot be opened, the exception IOError is thrown.
file | File pointer |
name | Name of the file to open |
ios_base::failure | Somehow the file cannot be opened. |
Definition at line 111 of file file.cc.
References ARTS_USER_ERROR_IF, Parameters::datapath, expand_path(), find_file(), Parameters::includepath, Array< base >::nelem(), and parameters.
Referenced by read_text_from_file(), CIARecord::ReadFromCIA(), ReadHITRAN(), ReadJPL(), ReadLBLRTM(), telsem_atlasesReadAscii(), telsem_atlasReadAscii(), and TessemNNReadAscii().
void open_output_file | ( | ofstream & | file, |
const std::string_view | name | ||
) |
Open a file for writing.
If the file cannot be opened, the exception IOError is thrown.
file | File pointer |
name | Name of the file to open |
ios_base::failure | Could for example mean that the directory is read only. |
Definition at line 63 of file file.cc.
References add_basedir(), and ARTS_USER_ERROR.
Referenced by main().
ArrayOfString read_text_from_file | ( | const std::string_view | name | ) |
Reads an ASCII file and appends the contents to the String vector text.
This uses the function
text | Output. The contents fo the file |
name | Name of file to read |
IOError |
Definition at line 186 of file file.cc.
References ARTS_USER_ERROR, open_input_file(), and read_text_from_stream().
Referenced by SourceText::AppendFile().
ArrayOfString read_text_from_stream | ( | istream & | is | ) |
Read an ASCII stream and append the contents to the String array text.
TEXT IS NOT OVERWRITTEN, BUT APPENDED!
text | Output. The contents fo the file |
is | Stream from which to read |
IOError | Some error occured during the read |
Definition at line 152 of file file.cc.
References ARTS_USER_ERROR_IF.
Referenced by read_text_from_file().
void replace_all | ( | String & | s, |
const std::string_view | what, | ||
const std::string_view | with | ||
) |
Replace all occurances of ‘what’ in ‘s’ with ‘with’.
s | Output. The String to act on. |
what | The String to replace. |
with | The replacement. |
Definition at line 215 of file file.cc.
References my_basic_string< charT >::npos.
Referenced by check_newline().