1 #ifndef teca_file_util_h
2 #define teca_file_util_h
20 namespace teca_file_util
26 int read_stream(
const char *file_name,
const char *header,
34 int write_stream(
const char *file_name,
int flags,
const char *header,
38 void replace_timestep(std::string &file_name,
unsigned long time_step,
int width = 6);
41 int replace_time(std::string &file_name,
double t,
42 const std::string &calendar,
const std::string &units,
43 const std::string &format);
46 void replace_extension(std::string &file_name,
const std::string &ext);
49 void replace_identifier(std::string &file_name,
const std::string &
id);
52 void to_lower(std::string &in);
55 int file_exists(
const char *path);
58 int file_writable(
const char *path);
63 std::string path(
const std::string &filename);
67 std::string base_filename(
const std::string &filename);
71 std::string filename(
const std::string &filename);
74 std::string extension(
const std::string &filename);
77 size_t load_lines(
const char *filename, std::vector<std::string> &lines);
80 size_t load_text(
const std::string &filename, std::string &text);
83 int write_text(std::string &filename, std::string &text);
85 int search_and_replace(
86 const std::string &search_for,
87 const std::string &replace_with,
88 std::string &in_text);
91 const std::string &path,
92 const std::string &re,
93 std::vector<std::string> &file_list);
97 size_t load_bin(
const char *filename,
size_t dlen, T *buffer)
99 std::ifstream file(filename,std::ios::binary);
102 std::cerr <<
"ERROR: File " << filename <<
" could not be opened." << std::endl;
107 file.seekg(0,std::ios::end);
108 size_t flen=file.tellg();
109 file.seekg(0,std::ios::beg);
112 if (dlen*
sizeof(T)!=flen)
115 <<
"ERROR: Expected " << dlen <<
" bytes but found "
116 << flen <<
" bytes in \"" << filename <<
"\".";
121 file.read((
char*)buffer,flen);
132 int name_value(std::vector<std::string> &lines, std::string name, T &value)
134 size_t n_lines=lines.size();
135 for (
size_t i=0; i<n_lines; ++i)
138 std::istringstream is(lines[i]);
157 template <
typename T>
158 size_t parse_value(std::string &in,
size_t at, std::string key, T &value)
160 size_t p=in.find(key,at);
161 if (p!=std::string::npos)
166 if ((p!=0) && isalpha(in[p-1]) && isalpha(in[p+n]))
168 return std::string::npos;
171 const int max_value_len=64;
173 std::istringstream valss(in.substr(p,max_value_len));
184 line_buffer() : m_buffer(
nullptr), m_line_number(0) {}
189 int initialize(
const char *file_name);
194 return !m_lines.empty();
200 return m_lines.front();
213 return m_line_number;
217 std::deque<char*> m_lines;
218 size_t m_line_number;