ARTS  2.2.66
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 
18 
20 // File description
22 
32 #ifndef file_h
33 #define file_h
34 
35 #include <fstream>
36 #include "matpackI.h"
37 #include "mystring.h"
38 #include "messages.h"
39 #include "make_array.h"
40 
41 
43 // Default file names
45 
46 void filename_ascii(
47  String& filename,
48  const String& varname );
49 
51  String& filename,
52  const String& varname );
53 
54 
55 
57 // Functions to open and read ASCII files
59 
60 void open_output_file(std::ofstream& file, const String& name);
61 
62 void cleanup_output_file(std::ofstream& file, const String& name);
63 
64 void open_input_file(std::ifstream& file, const String& name);
65 
66 void read_text_from_stream(ArrayOfString& text, std::istream& is);
67 
68 void read_text_from_file(ArrayOfString& text, const String& name);
69 
70 void replace_all(String& s, const String& what, const String& with);
71 
72 int check_newline(const String& s);
73 
74 bool file_exists(const String& filename);
75 
76 bool find_file(ArrayOfString& matches, const String& filename, const ArrayOfString& paths,
77  const ArrayOfString& extensions = MakeArray<String>(""));
78 
79 void find_xml_file(String& filename, const Verbosity& verbosity);
80 
81 String expand_path(const String& path);
82 
83 String add_basedir(const String& path);
84 
85 void get_dirname(String& dirname, const String& path);
86 
87 void list_directory(ArrayOfString& files, String dirname);
88 
89 void make_filename_unique(String& filename, const String& extension = "");
90 
91 
93 // IO manipulation classes for parsing nan and inf
95 
96 // Not all compilers do support parsing of nan and inf.
97 // The code below is taken (and slightly modified) from the discussion at:
98 // http://stackoverflow.com/questions/11420263/is-it-possible-to-read-infinity-or-nan-values-using-input-streams
99 
102 public:
103  double_istream(std::istream& i) : in(i) {}
104 
105  double_istream& parse_on_fail(double& x, bool neg);
106 
108  bool neg = false;
109  char c;
110  if (!in.good()) return *this;
111  while (isspace(c = (char)in.peek())) in.get();
112  if (c == '-') { neg = true; }
113  in >> x;
114  if (!in.fail()) return *this;
115  return parse_on_fail(x, neg);
116  }
117 
118 private:
119  std::istream& in;
120 };
121 
122 
125 public:
126  const double_imanip& operator>>(double& x) const {
127  double_istream(*in) >> x;
128  return *this;
129  }
130  std::istream& operator>>(const double_imanip&) const {
131  return *in;
132  }
133 
134  friend const double_imanip&
135  operator>>(std::istream& in, const double_imanip& dm);
136 
137 private:
138  mutable std::istream* in;
139 };
140 
141 
142 const double_imanip&
143 operator>>(std::istream& in, const double_imanip& dm);
144 
145 
146 #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:286
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)
double_imanip::operator>>
const double_imanip & operator>>(double &x) const
Definition: file.h:126
find_file
bool find_file(ArrayOfString &matches, const String &filename, const ArrayOfString &paths, const ArrayOfString &extensions=MakeArray< String >(""))
Searches through paths for a file with a matching name.
Definition: file.cc:402
matpackI.h
make_filename_unique
void make_filename_unique(String &filename, const String &extension="")
Make filename unique.
Definition: file.cc:612
Array
This can be used to make arrays out of anything.
Definition: array.h:107
list_directory
void list_directory(ArrayOfString &files, String dirname)
Return list of files in directory.
Definition: file.cc:581
double_istream::operator>>
double_istream & operator>>(double &x)
Definition: file.h:107
filename_bin
void filename_bin(String &filename, const String &varname)
messages.h
Declarations having to do with the four output streams.
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:64
double_imanip::operator>>
std::istream & operator>>(const double_imanip &) const
Definition: file.h:130
double_imanip
Input manipulator class for doubles to enable nan and inf parsing.
Definition: file.h:124
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:252
open_input_file
void open_input_file(std::ifstream &file, const String &name)
Verbosity
Definition: messages.h:50
double_istream::in
std::istream & in
Definition: file.h:119
make_array.h
Implements the class MakeArray, which is a derived class of Array, allowing explicit initialization.
double_istream::double_istream
double_istream(std::istream &i)
Definition: file.h:103
expand_path
String expand_path(const String &path)
Definition: file.cc:506
find_xml_file
void find_xml_file(String &filename, const Verbosity &verbosity)
Find an xml file.
Definition: file.cc:464
double_imanip::in
std::istream * in
Definition: file.h:138
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:307
get_dirname
void get_dirname(String &dirname, const String &path)
Return the parent directory of a path.
Definition: file.cc:553
operator>>
const double_imanip & operator>>(std::istream &in, const double_imanip &dm)
Definition: file.cc:688
double_istream
Input stream class for doubles that correctly handles nan and inf.
Definition: file.h:101
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:213
double_istream::parse_on_fail
double_istream & parse_on_fail(double &x, bool neg)
Definition: file.cc:649
MakeArray
Explicit construction of Arrays.
Definition: make_array.h:52
filename_ascii
void filename_ascii(String &filename, const String &varname)
Gives the default file name for the ASCII formats.
Definition: file.cc:78
add_basedir
String add_basedir(const String &path)
Definition: file.cc:530
file_exists
bool file_exists(const String &filename)
Checks if the given file exists.
Definition: file.cc:343
mystring.h
This file contains the definition of String, the ARTS string class.