ARTS  2.4.0(git:4fb77825)
file.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2012 Stefan Buehler <sbuehler@ltu.se>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
19 // File description
21 
30 #ifndef file_h
31 #define file_h
32 
33 #include <fstream>
34 #include "messages.h"
35 #include "mystring.h"
36 
38 // Default file names
40 
41 void filename_ascii(String& filename, const String& varname);
42 
43 void filename_bin(String& filename, const String& varname);
44 
46 // Functions to open and read ASCII files
48 
49 void open_output_file(std::ofstream& file, const String& name);
50 
51 void cleanup_output_file(std::ofstream& file, const String& name);
52 
53 void open_input_file(std::ifstream& file, const String& name);
54 
55 void read_text_from_stream(ArrayOfString& text, std::istream& is);
56 
57 void read_text_from_file(ArrayOfString& text, const String& name);
58 
59 void replace_all(String& s, const String& what, const String& with);
60 
61 int check_newline(const String& s);
62 
63 bool file_exists(const String& filename);
64 
65 bool find_file(ArrayOfString& matches,
66  const String& filename,
67  const ArrayOfString& paths,
68  const ArrayOfString& extensions = {""});
69 
70 void find_xml_file(String& filename, const Verbosity& verbosity);
71 
72 bool find_xml_file_existence(String& filename);
73 
74 String expand_path(const String& path);
75 
76 String add_basedir(const String& path);
77 
78 void get_dirname(String& dirname, const String& path);
79 
80 void list_directory(ArrayOfString& files, String dirname);
81 
82 void make_filename_unique(String& filename, const String& extension = "");
83 
85 // IO manipulation classes for parsing nan and inf
87 
88 // Not all compilers do support parsing of nan and inf.
89 // The code below is taken (and slightly modified) from the discussion at:
90 // http://stackoverflow.com/questions/11420263/is-it-possible-to-read-infinity-or-nan-values-using-input-streams
91 
94  public:
95  double_istream(std::istream& i) : in(i) {}
96 
97  double_istream& parse_on_fail(double& x, bool neg);
98 
100  bool neg = false;
101  char c;
102  if (!in.good()) return *this;
103  while (isspace(c = (char)in.peek())) in.get();
104  if (c == '-') {
105  neg = true;
106  }
107  in >> x;
108  if (!in.fail()) return *this;
109  return parse_on_fail(x, neg);
110  }
111 
112  private:
113  std::istream& in;
114 };
115 
118  public:
119  const double_imanip& operator>>(double& x) const {
120  double_istream(*in) >> x;
121  return *this;
122  }
123  std::istream& operator>>(const double_imanip&) const { return *in; }
124 
125  friend const double_imanip& operator>>(std::istream& in,
126  const double_imanip& dm);
127 
128  private:
129  mutable std::istream* in;
130 };
131 
132 const double_imanip& operator>>(std::istream& in, const double_imanip& dm);
133 
134 #endif
replace_all
void replace_all(String &s, const String &what, const String &with)
Replace all occurances of ‘what’ in ‘s’ with ‘with’.
Definition: file.cc:253
cleanup_output_file
void cleanup_output_file(std::ofstream &file, const String &name)
open_output_file
void open_output_file(std::ofstream &file, const String &name)
ARTS::Var::verbosity
Verbosity verbosity(Workspace &ws) noexcept
Definition: autoarts.h:7112
double_imanip::operator>>
const double_imanip & operator>>(double &x) const
Definition: file.h:119
find_xml_file_existence
bool find_xml_file_existence(String &filename)
As find_xml_file but does not throw in the main body.
Definition: file.cc:452
make_filename_unique
void make_filename_unique(String &filename, const String &extension="")
Make filename unique.
Definition: file.cc:571
Array< String >
list_directory
void list_directory(ArrayOfString &files, String dirname)
Return list of files in directory.
Definition: file.cc:543
double_istream::operator>>
double_istream & operator>>(double &x)
Definition: file.h:99
filename_bin
void filename_bin(String &filename, const String &varname)
messages.h
Declarations having to do with the four output streams.
my_basic_string< char >
double_imanip::operator>>
std::istream & operator>>(const double_imanip &) const
Definition: file.h:123
double_imanip
Input manipulator class for doubles to enable nan and inf parsing.
Definition: file.h:117
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.
Definition: file.cc:225
open_input_file
void open_input_file(std::ifstream &file, const String &name)
Verbosity
Definition: messages.h:49
double_istream::in
std::istream & in
Definition: file.h:113
double_istream::double_istream
double_istream(std::istream &i)
Definition: file.h:95
expand_path
String expand_path(const String &path)
Definition: file.cc:480
find_xml_file
void find_xml_file(String &filename, const Verbosity &verbosity)
Find an xml file.
Definition: file.cc:414
double_imanip::in
std::istream * in
Definition: file.h:129
check_newline
int check_newline(const String &s)
Checks if there is exactly one newline character at the end of the string.
Definition: file.cc:271
get_dirname
void get_dirname(String &dirname, const String &path)
Return the parent directory of a path.
Definition: file.cc:519
operator>>
const double_imanip & operator>>(std::istream &in, const double_imanip &dm)
Definition: file.cc:655
double_istream
Input stream class for doubles that correctly handles nan and inf.
Definition: file.h:93
read_text_from_stream
void read_text_from_stream(ArrayOfString &text, std::istream &is)
Read an ASCII stream and append the contents to the String array text.
Definition: file.cc:190
double_istream::parse_on_fail
double_istream & parse_on_fail(double &x, bool neg)
Definition: file.cc:602
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
filename_ascii
void filename_ascii(String &filename, const String &varname)
Gives the default file name for the ASCII formats.
Definition: file.cc:73
add_basedir
String add_basedir(const String &path)
Definition: file.cc:499
find_file
bool find_file(ArrayOfString &matches, const String &filename, const ArrayOfString &paths, const ArrayOfString &extensions={""})
Searches through paths for a file with a matching name.
Definition: file.cc:353
file_exists
bool file_exists(const String &filename)
Checks if the given file exists.
Definition: file.cc:303
mystring.h
This file contains the definition of String, the ARTS string class.