ARTS  2.0.49
file.h File Reference

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

#include <fstream>
#include "matpackI.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 String &varname)
 
void open_output_file (ofstream &file, const String &name)
 Open a file for writing. More...
 
void cleanup_output_file (ofstream &file, const String &name)
 Closes the file. More...
 
void open_input_file (ifstream &file, const String &name)
 Open a file for reading. More...
 
void read_text_from_stream (ArrayOfString &text, istream &is)
 Read an ASCII stream and append the contents to the String array text. More...
 
void read_text_from_file (ArrayOfString &text, const String &name)
 Reads an ASCII file and appends the contents to the String vector text. More...
 
String expand_path (const String &path)
 
void replace_all (String &s, const String &what, const String &with)
 Replace all occurances of ‘what’ in ‘s’ with ‘with’. More...
 
int check_newline (const String &s)
 Checks if there is exactly one newline character at the end of the string. More...
 
bool file_exists (const String &filename)
 Checks if the given file exists. More...
 
bool find_file (String &filename, const char *extension)
 Find the given file. 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

◆ check_newline()

int check_newline ( const String 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 317 of file file.cc.

References replace_all().

Referenced by md_sanity_checks().

◆ cleanup_output_file()

void cleanup_output_file ( ofstream &  ,
const String  
)

Closes the file.

If it is empty, the file is deleted.

Parameters
fileFile pointer
Author
Oliver Lemke
Exceptions
ios_base::failureCould for example mean that the directory is read only.

Definition at line 146 of file file.cc.

Referenced by arts_exit().

◆ expand_path()

String expand_path ( const 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 274 of file file.cc.

References my_basic_string< charT >::nelem().

Referenced by open_input_file(), open_output_file(), xml_read_arts_catalogue_from_file(), xml_read_from_file(), and xml_write_to_file().

◆ file_exists()

bool file_exists ( const String 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 353 of file file.cc.

Referenced by find_file().

◆ 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 71 of file file.cc.

References out_basename.

◆ filename_bin()

void filename_bin ( String filename,
const String varname 
)

◆ find_file()

bool find_file ( String filename,
const char *  extension 
)

Find the given file.

If it doesn't exist in the current directory, also search the include path.

Parameters
filenameFile to check.
extensionExtension tried to be appended to the filename.
Returns
Error code (true = file found, false = file not found)
Author
Oliver Lemke

Definition at line 379 of file file.cc.

References file_exists(), Parameters::includepath, Array< base >::nelem(), and parameters.

Referenced by ArtsParser::parse_agenda(), xml_read_arts_catalogue_from_file(), and xml_read_from_file().

◆ open_input_file()

void open_input_file ( ifstream &  file,
const String name 
)

Open a file for reading.

If the file cannot be opened, the exception IOError is thrown.

Parameters
fileFile pointer
nameName of the file to open
Author
Stefan Buehler
Version
1
Exceptions
ios_base::failureSomehow the file cannot be opened.

Definition at line 160 of file file.cc.

References expand_path().

Referenced by abs_linesReadFromArtsObsolete(), abs_linesReadFromHitran(), abs_linesReadFromHitran2004(), abs_linesReadFromJpl(), abs_linesReadFromMytran2(), abs_speciesDefineAllInScenario(), and read_text_from_file().

◆ open_output_file()

void open_output_file ( ofstream &  file,
const String name 
)

Open a file for writing.

If the file cannot be opened, the exception IOError is thrown.

Parameters
fileFile pointer
nameName of the file to open
Author
Stefan Buehler
Version
1
Exceptions
ios_base::failureCould for example mean that the directory is read only.

Definition at line 97 of file file.cc.

References expand_path().

Referenced by main().

◆ read_text_from_file()

void read_text_from_file ( ArrayOfString text,
const String 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 238 of file file.cc.

References open_input_file(), and read_text_from_stream().

Referenced by SourceText::AppendFile().

◆ read_text_from_stream()

void read_text_from_stream ( ArrayOfString text,
istream &  is 
)

Read an ASCII stream and append the contents to the String array text.

TEXT IS NOT OVERWRITTEN, BUT APPENDED!

Parameters
textOutput. The contents fo the file
isStream from which to read
Exceptions
IOErrorSome error occured during the read
Version
1
Author
Stefan Buehler

Definition at line 197 of file file.cc.

Referenced by read_text_from_file().

◆ replace_all()

void replace_all ( String s,
const String what,
const String 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 296 of file file.cc.

References my_basic_string< charT >::npos.

Referenced by check_newline().