ARTS 2.5.9 (git: 825fa5f2)
file.h File Reference

This file contains basic functions to handle ASCII files. More...

#include <fstream>
#include "double_imanip.h"
#include "messages.h"
#include "mystring.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. More...
 
void filename_bin (String &filename, const std::string_view varname)
 
void open_output_file (std::ofstream &file, const std::string_view name)
 
void cleanup_output_file (std::ofstream &file, const std::string_view name)
 
void open_input_file (std::ifstream &file, const std::string_view name)
 
ArrayOfString read_text_from_stream (std::istream &is)
 
ArrayOfString read_text_from_file (const std::string_view name)
 Reads an ASCII file and appends the contents to the String vector text. More...
 
void replace_all (String &s, const std::string_view what, const std::string_view with)
 Replace all occurances of ‘what’ in ‘s’ with ‘with’. More...
 
int check_newline (const std::string_view s)
 Checks if there is exactly one newline character at the end of the string. More...
 
bool file_exists (const std::string_view filename)
 Checks if the given file exists. More...
 
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. More...
 
void find_xml_file (String &filename, const Verbosity &verbosity)
 Find an xml file. More...
 
bool find_xml_file_existence (String &filename)
 As find_xml_file but does not throw in the main body. More...
 
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. More...
 
ArrayOfString list_directory (const std::string_view dirname)
 Return list of files in directory. More...
 
String make_filename_unique (const std::string_view filename, const String &extension="")
 Make filename unique. More...
 

Detailed Description

This file contains basic functions to handle ASCII files.

Author
Patrick Eriksson
Date
2000-10-28

Definition in file file.h.

Function Documentation

◆ add_basedir()

String add_basedir ( const std::string_view  path)

Adds base directory to the given path if it is a relative path.

Parameters
[in]pathString with path
Returns
Expanded path.
Author
Oliver Lemke
Date
2012-05-01

Definition at line 441 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().

◆ check_newline()

int check_newline ( const std::string_view  s)

Checks if there is exactly one newline character at the end of the string.

Parameters
sThe String to check.
Returns
Error code (0=ok, 1=empty, 2=missing, 3=extra newline
Author
Oliver Lemke

Definition at line 250 of file file.cc.

References d, and replace_all().

Referenced by md_sanity_checks().

◆ cleanup_output_file()

void cleanup_output_file ( std::ofstream &  file,
const std::string_view  name 
)

◆ expand_path()

String expand_path ( String  path)

Expands the ~ to home directory location in given path.

Parameters
[in]pathString with path
Returns
Expanded path.
Author
Oliver Lemke
Date
2010-04-30

Definition at line 422 of file file.cc.

Referenced by add_basedir(), cleanup_output_file(), find_file(), nca_read_from_file(), and open_input_file().

◆ file_exists()

bool file_exists ( const std::string_view  filename)

Checks if the given file exists.

Parameters
filenameFile to check.
Returns
Error code (true = file exists, false = file doesn't exist)
Author
Oliver Lemke

Definition at line 282 of file file.cc.

Referenced by find_file(), make_filename_unique(), and ScatSpeciesScatAndMetaRead().

◆ filename_ascii()

void filename_ascii ( String filename,
const String varname 
)

Gives the default file name for the ASCII formats.

The default name is only used if the file name is empty.

Parameters
filenameOutput: file name
varnamevariable name
Author
Patrick Eriksson
Date
2000-11-01

Definition at line 60 of file file.cc.

References out_basename.

◆ filename_bin()

void filename_bin ( String filename,
const std::string_view  varname 
)

◆ find_file()

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.

Parameters
[in,out]matchesMatching files are appended to this list.
[in]filenameFile to find.
[in]pathsList of paths to look in for the file.
[in]extensionsList of extensions to add to base filename.
Returns
True if matches were found, else false
Author
Oliver Lemke

Definition at line 301 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_xml_file()

void find_xml_file ( String filename,
const Verbosity verbosity 
)

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.

Parameters
[in,out]filenameFile to check.
[in]verbosityVerbosity
Exceptions
runtime_errorif file is not found.
Author
Oliver Lemke

Definition at line 355 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().

◆ find_xml_file_existence()

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

Parameters
[in,out]filenameFile to check.
Returns
If file is found
Author
Oliver Lemke

Definition at line 394 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().

◆ get_dirname()

String get_dirname ( const std::string_view  path)

Return the parent directory of a path.

Extracts the parent directory part of the given path.

Parameters
[out]dirnameParent directory of path
[in]pathPath
Author
Oliver Lemke

Definition at line 461 of file file.cc.

References Array< base >::nelem(), and my_basic_string< charT >::split().

Referenced by get_parameters(), and ArtsParser::parse_agenda().

◆ list_directory()

ArrayOfString list_directory ( const std::string_view  dirname)

Return list of files in directory.

Returns list of all files in the given path.

Parameters
[out]filesList of files
[in]dirnameParent directory of path
Author
Oliver Lemke

Definition at line 487 of file file.cc.

References ARTS_USER_ERROR_IF.

Referenced by abs_cia_dataReadFromCIA().

◆ make_filename_unique()

String make_filename_unique ( const std::string_view  filename,
const String extension 
)

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

Parameters
[in]filenameFilename
[in]extensionOptional, number is inserted before the extension
Returns
Unique filename
Author
Oliver Lemke

Definition at line 513 of file file.cc.

References file_exists().

Referenced by xml_write_to_file().

◆ open_input_file()

void open_input_file ( std::ifstream &  file,
const std::string_view  name 
)

◆ open_output_file()

void open_output_file ( std::ofstream &  file,
const std::string_view  name 
)

◆ read_text_from_file()

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

See also
read_text_from_stream. TEXT IS NOT OVERWRITTEN, BUT APPENDED!
Parameters
textOutput. The contents fo the file
nameName of file to read
Exceptions
IOError
Version
1
Author
Stefan Buehler

Definition at line 203 of file file.cc.

References ARTS_USER_ERROR, open_input_file(), and read_text_from_stream().

Referenced by SourceText::AppendFile().

◆ read_text_from_stream()

ArrayOfString read_text_from_stream ( std::istream &  is)

◆ replace_all()

void replace_all ( String s,
const std::string_view  what,
const std::string_view  with 
)

Replace all occurances of ‘what’ in ‘s’ with ‘with’.

Parameters
sOutput. The String to act on.
whatThe String to replace.
withThe replacement.
Author
Stefan Buehler

Definition at line 232 of file file.cc.

References my_basic_string< charT >::npos.

Referenced by check_newline().