|
TECA
The Toolkit for Extreme Climate Analysis
|
Go to the documentation of this file. 1 #ifndef teca_file_util_h
2 #define teca_file_util_h
6 #include "teca_config.h"
33 int read_stream(
const char *file_name,
const char *header,
43 int write_stream(
const char *file_name,
int flags,
const char *header,
48 void replace_timestep(std::string &file_name,
unsigned long time_step,
int width = 6);
53 const std::string &calendar,
const std::string &units,
54 const std::string &format);
113 const std::string &search_for,
114 const std::string &replace_with,
115 std::string &in_text);
120 const std::string &
path,
121 const std::string &re,
122 std::vector<std::string> &file_list);
129 std::ifstream file(
filename,std::ios::binary);
132 std::cerr <<
"ERROR: File " <<
filename <<
" could not be opened." << std::endl;
137 file.seekg(0,std::ios::end);
138 size_t flen=file.tellg();
139 file.seekg(0,std::ios::beg);
142 if (dlen*
sizeof(T)!=flen)
145 <<
"ERROR: Expected " << dlen <<
" bytes but found "
146 << flen <<
" bytes in \"" <<
filename <<
"\".";
151 file.read((
char*)buffer,flen);
161 int name_value(std::vector<std::string> &lines, std::string name, T &value)
163 size_t n_lines=lines.size();
164 for (
size_t i=0; i<n_lines; ++i)
167 std::istringstream is(lines[i]);
183 template <
typename T>
185 size_t parse_value(std::string &in,
size_t at, std::string key, T &value)
187 size_t p=in.find(key,at);
188 if (p!=std::string::npos)
193 if ((p!=0) && isalpha(in[p-1]) && isalpha(in[p+n]))
195 return std::string::npos;
198 const int max_value_len=64;
200 std::istringstream valss(in.substr(p,max_value_len));
212 line_buffer() : m_buffer(
nullptr), m_line_number(0) {}
217 int initialize(
const char *file_name);
222 return !m_lines.empty();
228 return m_lines.front();
241 return m_line_number;
245 std::deque<char*> m_lines;
246 size_t m_line_number;
TECA_EXPORT std::string base_filename(const std::string &filename)
TECA_EXPORT void replace_extension(std::string &file_name, const std::string &ext)
replace e% with the given string
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:16
Codes dealing with low level file system API's.
Definition: teca_file_util.h:25
TECA_EXPORT int replace_time(std::string &file_name, double t, const std::string &calendar, const std::string &units, const std::string &format)
replace t% with the time t in calendar with units in the strftime format
TECA_EXPORT int read_stream(const char *file_name, const char *header, teca_binary_stream &stream, bool verbose=true)
TECA_EXPORT int search_and_replace(const std::string &search_for, const std::string &replace_with, std::string &in_text)
Search and replace with in a string of text.
TECA_EXPORT size_t parse_value(std::string &in, size_t at, std::string key, T &value)
Definition: teca_file_util.h:185
TECA_EXPORT int write_text(std::string &filename, std::string &text)
write the string to the named file
TECA_EXPORT int file_exists(const char *path)
return 0 if the file does not exist
TECA_EXPORT size_t load_bin(const char *filename, size_t dlen, T *buffer)
Load a binary file into memory.
Definition: teca_file_util.h:127
TECA_EXPORT size_t load_text(const std::string &filename, std::string &text)
read the file into a string
TECA_EXPORT void replace_identifier(std::string &file_name, const std::string &id)
replace s% with the given string
TECA_EXPORT int locate_files(const std::string &path, const std::string &re, std::vector< std::string > &file_list)
Locate files in path that match a regular expression.
TECA_EXPORT int write_stream(const char *file_name, int flags, const char *header, const teca_binary_stream &stream, bool verbose=true)
TECA_EXPORT int name_value(std::vector< std::string > &lines, std::string name, T &value)
extract a name-value pair from the given set of lines.
Definition: teca_file_util.h:161
TECA_EXPORT void to_lower(std::string &in)
return string converted to lower case
TECA_EXPORT std::string filename(const std::string &filename)
TECA_EXPORT std::string extension(const std::string &filename)
Returns the extension from the given filename.
TECA_EXPORT size_t load_lines(const char *filename, std::vector< std::string > &lines)
read the lines of the ascii file into a vector
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
Definition: teca_file_util.h:210
TECA_EXPORT void replace_timestep(std::string &file_name, unsigned long time_step, int width=6)
replace t% with the given value
TECA_EXPORT int file_writable(const char *path)
return 0 if the file/directory is not writeable
TECA_EXPORT std::string path(const std::string &filename)