MEPP2 Project
FileUtilities.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <iostream>
14 #include <fstream>
15 #include <sstream>
16 #include <string>
17 
18 #include <vector>
19 #include <algorithm>
20 #include <iterator>
21 #include <cctype> // for tolower()
22 
23 #include <boost/filesystem.hpp>
24 
25 
26 namespace FEVV {
27 namespace FileUtils {
28 
29 
34 inline
35 std::string
36 get_file_extension(const std::string &file_name)
37 {
38  return static_cast< std::string >(boost::filesystem::extension(file_name));
39 }
40 
45 inline
46 std::string
47 get_file_extension(const char *file_name)
48 {
49  return get_file_extension(std::string(file_name));
50 }
51 
56 inline
57 bool
58 has_extension(const std::string &file_name)
59 {
60  return !boost::filesystem::extension(file_name).empty();
61 }
62 
67 inline
68 bool
69 has_extension(const char *file_name)
70 {
71  return has_extension(std::string(file_name));
72 }
73 
78 inline bool
79 has_extension(const std::string &file_name,
80  const std::string &ext_name,
81  bool case_sensitive = false)
82 {
83  std::string file_ext = get_file_extension(file_name);
84 
85  if(! case_sensitive)
86  {
87  std::transform(
88  file_ext.begin(), file_ext.end(), file_ext.begin(), ::tolower);
89  }
90 
91  return ext_name.compare(file_ext) == 0;
92 }
93 
98 inline bool
99 has_extension(const char *file_name,
100  const char *ext_name,
101  bool case_sensitive = false)
102 {
103  return has_extension(
104  std::string(file_name), std::string(ext_name), case_sensitive);
105 }
106 
111 inline bool
112 has_extension(const std::string &file_name,
113  const std::vector< std::string > &ext_names,
114  bool case_sensitive = false)
115 {
116  return std::any_of(
117  ext_names.cbegin(), ext_names.cend(), [&](const std::string &ext) {
118  return has_extension(file_name, ext, case_sensitive);
119  });
120 }
121 
126 inline
127 std::string
128 get_file_name(const std::string &file_name)
129 {
130  boost::filesystem::path p(file_name);
131  boost::filesystem::path file_full_name = p.filename();
132  return static_cast< std::string >(file_full_name.stem().string());
133 }
134 
139 inline
140 std::string
141 get_file_name(const char *file_name)
142 {
143  return get_file_name(std::string(file_name));
144 }
145 
150 inline
151 std::string
152 get_file_full_name(const std::string &file_name)
153 {
154  boost::filesystem::path p(file_name);
155  return static_cast< std::string >(p.filename().string());
156 }
157 
161 inline
162 unsigned int
163 count_file_lines(const std::string &file_name)
164 {
165  std::ifstream myfile(file_name);
166  myfile.unsetf(std::ios_base::skipws);
167 
168  unsigned int line_count = static_cast< unsigned int >(
169  std::count(std::istreambuf_iterator< char >(myfile),
170  std::istreambuf_iterator< char >(),
171  '\n'));
172  return line_count + 1;
173 }
174 
178 inline
179 unsigned int
180 count_file_lines(const char *file_name)
181 {
182  return count_file_lines(std::string(file_name));
183 }
184 
189 inline
190 std::string
191 get_parent_directory(const std::string &file_name)
192 {
193  return static_cast< std::string >(
194  boost::filesystem::path(file_name).branch_path().string());
195 }
196 
206 inline
207 std::ifstream &
208 safe_getline(std::ifstream &input, std::string &str)
209 {
210  std::getline(input, str);
211 
212  // remove DOS end of line extra character
213  if((!str.empty()) && (str.back() == '\r'))
214  str.pop_back();
215 
216  return input;
217 }
218 
228 inline
229 bool
230 getline_skip_comment(std::istream &input, std::string &line)
231 {
232  // note:
233  // handling the case of empty DOS line, aka line equal to exactly one '\r'
234  // character, is enough to deal with DOS EOL as far as lines are parsed
235  // with std::istringstream operator>> which nicely handles '\r' as a word
236  // separator.
237 
238  while(std::getline(input, line))
239  {
240  // skip comment line, UNIX empty line and DOS empty line
241  if(! (line.empty() || line[0] == '#' || line[0] == '\r'))
242  break;
243  }
244 
245  return static_cast< bool >(input);
246 }
247 
258 inline
259 bool
260 getline_skip_comment(std::istream &input,
261  std::string &line,
262  std::istringstream &line_ss)
263 {
264  getline_skip_comment(input, line);
265  line_ss.clear();
266  line_ss.str(line);
267 
268  return static_cast< bool >(input);
269 }
270 
271 
279 inline
280 void
281 copy_file(const std::string &from, const std::string &to)
282 {
283  try
284  {
286  }
287  catch(const boost::filesystem::filesystem_error &e)
288  {
289  std::cout << "Copy_file WARNING: " << e.what() << std::endl;
290  }
291 }
292 
293 
299 inline
300 void
301 create_dir(const std::string &dirname)
302 {
303  boost::filesystem::create_directory(dirname);
304 }
305 
306 
312 inline
313 void
314 remove_dir(const std::string &dirname)
315 {
316  boost::filesystem::remove_all(dirname);
317 }
318 
319 
323 inline
324 std::string
325 get_wdir(void)
326 {
327  return boost::filesystem::current_path().string();
328 }
329 
330 
336 inline
337 void
338 change_wdir(const std::string &dirname)
339 {
340  boost::filesystem::current_path(dirname);
341 }
342 
343 
352 inline
353 std::string
354 load_file(const std::string &file_name)
355 {
356  std::string contents;
357 
358  std::ifstream in(file_name, std::ios::in | std::ios::binary);
359  if(in)
360  {
361  in.seekg(0, std::ios::end);
362  contents.resize(in.tellg());
363  in.seekg(0, std::ios::beg);
364  in.read(&contents[0], contents.size());
365  in.close();
366  }
367  else
368  {
369  std::cout << "FEVV::FileUtils::load_file: failed to open file '"
370  << file_name
371  << "'."
372  << std::endl;
373  }
374 
375  //throw(errno);
376  return(contents);
377 }
378 
379 
380 } // namespace FileUtils
381 } // namespace FEVV
382 
FEVV::FileUtils::load_file
std::string load_file(const std::string &file_name)
Definition: FileUtilities.hpp:354
FEVV::FileUtils::safe_getline
std::ifstream & safe_getline(std::ifstream &input, std::string &str)
Definition: FileUtilities.hpp:208
FEVV::FileUtils::create_dir
void create_dir(const std::string &dirname)
Definition: FileUtilities.hpp:301
FEVV::FileUtils::get_file_name
std::string get_file_name(const std::string &file_name)
Definition: FileUtilities.hpp:128
FEVV::FileUtils::get_file_full_name
std::string get_file_full_name(const std::string &file_name)
Definition: FileUtilities.hpp:152
FEVV::FileUtils::count_file_lines
unsigned int count_file_lines(const std::string &file_name)
Definition: FileUtilities.hpp:163
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::FileUtils::get_file_extension
std::string get_file_extension(const std::string &file_name)
Definition: FileUtilities.hpp:36
FEVV::FileUtils::change_wdir
void change_wdir(const std::string &dirname)
Definition: FileUtilities.hpp:338
FEVV::FileUtils::get_parent_directory
std::string get_parent_directory(const std::string &file_name)
Definition: FileUtilities.hpp:191
FEVV::FileUtils::copy_file
void copy_file(const std::string &from, const std::string &to)
Definition: FileUtilities.hpp:281
FEVV::FileUtils::remove_dir
void remove_dir(const std::string &dirname)
Definition: FileUtilities.hpp:314
FEVV::FileUtils::has_extension
bool has_extension(const std::string &file_name)
Definition: FileUtilities.hpp:58
FEVV::FileUtils::get_wdir
std::string get_wdir(void)
Definition: FileUtilities.hpp:325
FEVV::FileUtils::getline_skip_comment
bool getline_skip_comment(std::istream &input, std::string &line)
Definition: FileUtilities.hpp:230