|
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
30 int read_stream(
const char *file_name,
const char *header,
39 int write_stream(
const char *file_name,
int flags,
const char *header,
43 void replace_timestep(std::string &file_name,
unsigned long time_step,
int width = 6);
47 const std::string &calendar,
const std::string &units,
48 const std::string &format);
94 const std::string &search_for,
95 const std::string &replace_with,
96 std::string &in_text);
100 const std::string &
path,
101 const std::string &re,
102 std::vector<std::string> &file_list);
108 std::ifstream file(
filename,std::ios::binary);
111 std::cerr <<
"ERROR: File " <<
filename <<
" could not be opened." << std::endl;
116 file.seekg(0,std::ios::end);
117 size_t flen=file.tellg();
118 file.seekg(0,std::ios::beg);
121 if (dlen*
sizeof(T)!=flen)
124 <<
"ERROR: Expected " << dlen <<
" bytes but found "
125 << flen <<
" bytes in \"" <<
filename <<
"\".";
130 file.read((
char*)buffer,flen);
139 int name_value(std::vector<std::string> &lines, std::string name, T &value)
141 size_t n_lines=lines.size();
142 for (
size_t i=0; i<n_lines; ++i)
145 std::istringstream is(lines[i]);
161 template <
typename T>
162 size_t parse_value(std::string &in,
size_t at, std::string key, T &value)
164 size_t p=in.find(key,at);
165 if (p!=std::string::npos)
170 if ((p!=0) && isalpha(in[p-1]) && isalpha(in[p+n]))
172 return std::string::npos;
175 const int max_value_len=64;
177 std::istringstream valss(in.substr(p,max_value_len));
189 line_buffer() : m_buffer(
nullptr), m_line_number(0) {}
194 int initialize(
const char *file_name);
199 return !m_lines.empty();
205 return m_lines.front();
218 return m_line_number;
222 std::deque<char*> m_lines;
223 size_t m_line_number;
void to_lower(std::string &in)
return string converted to lower case
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:15
Codes dealing with low level file system API's.
Definition: teca_file_util.h:23
int write_stream(const char *file_name, int flags, const char *header, const teca_binary_stream &stream, bool verbose=true)
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
int file_exists(const char *path)
return 0 if the file does not exist
std::string base_filename(const std::string &filename)
void replace_identifier(std::string &file_name, const std::string &id)
replace s% with the given string
std::string extension(const std::string &filename)
Returns the extension from the given filename.
int write_text(std::string &filename, std::string &text)
write the string to the named file
std::string path(const std::string &filename)
std::string filename(const std::string &filename)
int file_writable(const char *path)
return 0 if the file/directory is not writeable
int read_stream(const char *file_name, const char *header, teca_binary_stream &stream, bool verbose=true)
size_t load_bin(const char *filename, size_t dlen, T *buffer)
Load a binary file into memory.
Definition: teca_file_util.h:106
void replace_timestep(std::string &file_name, unsigned long time_step, int width=6)
replace t% with the given value
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.
size_t load_text(const std::string &filename, std::string &text)
read the file into a string
size_t parse_value(std::string &in, size_t at, std::string key, T &value)
Definition: teca_file_util.h:162
size_t load_lines(const char *filename, std::vector< std::string > &lines)
read the lines of the ascii file into a vector
Definition: teca_file_util.h:187
void replace_extension(std::string &file_name, const std::string &ext)
replace e% with the given string
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.
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:139