SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
FileUtil.cc
Go to the documentation of this file.
1 #include "FileUtil.hh"
2 
3 // STL
4 #include <iostream>
5 #include <stdio.h>
6 #include <fstream>
7 
8 using std::ifstream;
9 using std::ios;
10 
11 namespace slic {
12 
13 int FileUtil::removeFile(const char* filename) {
14  int ret = remove(filename);
15  return ret;
16 }
17 
18 int FileUtil::removeFile(const std::string& filename) {
19  return FileUtil::removeFile(filename.c_str());
20 }
21 
22 bool FileUtil::fileExists(const char* filename) {
23  bool exists = true;
24 
25  ifstream fin;
26  fin.open(filename, ios::in);
27 
28  // either doesn't exist or can't read
29  if (fin.fail()) {
30  exists = false;
31  }
32  fin.close();
33  return exists;
34 }
35 
36 bool FileUtil::fileExists(const std::string& filename) {
37  return FileUtil::fileExists(filename.c_str());
38 }
39 
40 std::string FileUtil::basename(std::string fn) {
41  static std::string delim = "/";
42 
43  std::string filename = fn;
44 
45  // find Unix/Cygwin delim
46  int basename_pos = filename.rfind(delim);
47 
48  // may be dealing with a Windows-style path
49 #if defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
50  if ( basename_pos == -1 ) {
51  basename_pos = filename.rfind("\\");
52  }
53 #endif
54 
55  // found a dir delimiter so erase up to it
56  if (basename_pos != -1) {
57  filename.erase(filename.begin(), filename.begin() + basename_pos + 1);
58  }
59 
60  return filename;
61 }
62 
63 std::string FileUtil::extension(std::string fn) {
64  std::string::size_type idx = fn.find_last_of(".");
65 
66  std::string ext;
67  if (idx != std::string::npos) {
68  ext = fn.substr(idx + 1, fn.size() - idx);
69  } else {
70  ext = "";
71  }
72 
73  return ext;
74 }
75 
76 std::string FileUtil::removeExtension(std::string fn) {
77  std::string::size_type idx = fn.find_last_of(".");
78 
79  std::string fname;
80  if (idx != std::string::npos) {
81  fname = fn.substr(0, idx);
82  } else {
83  fname = fn;
84  }
85  return fname;
86 }
87 }
static int removeFile(const char *filename)
Definition: FileUtil.cc:13
static std::string removeExtension(std::string filename)
Definition: FileUtil.cc:76
static bool fileExists(const char *filename)
Definition: FileUtil.cc:22
static std::string extension(std::string filename)
Definition: FileUtil.cc:63
static std::string basename(std::string filename)
Definition: FileUtil.cc:40