charon-core  0.3.1
FileTool.cpp
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15  */
24 #include <charon-core/FileTool.h>
25 #include <iostream>
26 
27 #if defined(sun) || defined(__sun) || defined(linux) \
28  || defined(__linux) || defined(__linux__) || defined(__CYGWIN__) \
29  || defined(BSD) || defined(__FreeBSD__) || defined(__OPENBSD__) \
30  || defined(__MACOSX__) || defined(__APPLE__) || defined(sgi) \
31  || defined(__sgi)
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <dirent.h>
36 #define MODE ,0711
37 #define CHARON_LINUX
38 #elif defined(_WIN32) || defined(__WIN32__)
39 #ifdef _MSC_VER
40 #include <crtdbg.h>
41 #endif /* _MSC_VER */
42 #include <direct.h>
43 #include <io.h>
44 #include <windows.h>
45 
46 #define MODE
47 #define mkdir _mkdir
48 #define unlink _unlink
49 #define chdir _chdir
50 #define getcwd _getcwd
51 #define CHARON_WINDOWS
52 #else
53 #error "Operating system not recognized!"
54 #endif
55 
56 #include <charon-core/StringTool.h>
57 #include <algorithm>
58 #include <sstream>
59 #include <fstream>
60 #include <cassert>
61 #include <stdexcept>
62 
63 #include <iostream>
64 
65 #ifdef CHARON_LINUX
66 const char FileTool::slash = '/';
67 #else
68 const char FileTool::slash = '\\';
69 #endif
70 
71 int FileTool::makePath(std::string& path) {
72  slashConvert(path);
73  std::vector<std::string> dirs;
74  StringTool::explode(path, slash, dirs);
75 
76  std::string cdir = "";
77  for (unsigned int i = 0; i < dirs.size() - 1; ++i) {
78  if (i)
79  cdir += slash + dirs[i];
80  else
81  cdir += dirs[i];
82  makeDir(cdir);
83  }
84  return makeDir(cdir + slash + dirs[dirs.size() - 1]);
85 }
86 
87 int FileTool::makeDir(const std::string& dir) {
88  return mkdir(dir.c_str() MODE);
89 }
90 
91 int FileTool::changeDir(const std::string& dir) {
92  return chdir(dir.c_str());
93 }
94 
95 std::string FileTool::getCurrentDir() {
96  char cwd[2048];
97  char* res = getcwd(cwd, 2048);
98  if (!res || (res != cwd)) {
99  throw std::runtime_error("eror during getcwd");
100  }
101  return std::string(cwd);
102 }
103 
104 std::vector<std::string> FileTool::getFilesWithSuffix(std::string suffix) {
105  std::vector<std::string> v;
106 #ifdef CHARON_LINUX
107  DIR *hdir;
108  struct dirent *entry;
109 
110  hdir = opendir(".");
111  do {
112  entry = readdir(hdir);
113  if (entry) {
114  std::string s = entry->d_name;
115  if (s.find_last_of('.') != s.npos && s.substr(s.find_last_of('.'),
116  s.length() - s.find_last_of('.')) == suffix) {
117  v.push_back(s);
118  }
119  }
120  } while (entry);
121  closedir(hdir);
122 #elif defined CHARON_WINDOWS
123  WIN32_FIND_DATA ffd;
124  HANDLE hFind;
125  //DWORD dwError=0;
126 
127  hFind = FindFirstFile(".\\*", &ffd);
128 
129  if (INVALID_HANDLE_VALUE == hFind) {
130  throw std::runtime_error("Error in opening the folder.");
131  }
132 
133  do {
134  std::string s = ffd.cFileName;
135  if (s.find_last_of('.') != s.npos && s.substr(s.find_last_of('.'),
136  s.length()) == suffix) {
137  v.push_back(s);
138  }
139  } while (FindNextFile(hFind, &ffd) != 0);
140 
141 #endif
142  return v;
143 }
144 
145 void FileTool::slashConvert(std::string& src) {
146 #ifdef CHARON_LINUX
147  char wrongSlash = '\\';
148 #else
149  char wrongSlash = '/';
150 #endif
151  replace(src.begin(), src.end(), wrongSlash, slash);
152 }
153 
154 bool FileTool::exists(const std::string& file) {
155  std::ifstream test(file.c_str());
156  return !test.fail();
157 }
158 
159 int FileTool::remove(const std::string& file) {
160  return unlink(file.c_str());
161 }
162 
163 int FileTool::rename(const std::string& oldFile, const std::string& newFile) {
164  return ::rename(oldFile.c_str(), newFile.c_str());
165 }
166 
167 std::string FileTool::readFile(const std::string& fName) {
168  std::ifstream inStream;
169  inStream.open(fName.c_str());
170  std::stringstream str;
171  std::string buffer;
172  while (!inStream.eof()) {
173  std::getline(inStream, buffer);
174  str << buffer << "\n";
175  }
176  return str.str();
177 }
std::vector< std::string > charon_core_DLL_PUBLIC getFilesWithSuffix(std::string suffix)
Search files with given suffix.
Definition: FileTool.cpp:104
Declaration of FileTool methods.
bool charon_core_DLL_PUBLIC exists(const std::string &file)
Check if file exists.
Definition: FileTool.cpp:154
int charon_core_DLL_PUBLIC makePath(std::string &path)
This function creates all directories contained in path provided they do not already exist...
Definition: FileTool.cpp:71
int charon_core_DLL_PUBLIC changeDir(const std::string &dir)
Change current working directory.
Definition: FileTool.cpp:91
void charon_core_DLL_PUBLIC explode(std::string str, char delimiter, std::vector< std::string > &result)
Use delimiter to split the string in a list of substrings.
Definition: StringTool.cpp:46
int charon_core_DLL_PUBLIC remove(const std::string &file)
Remove file.
Definition: FileTool.cpp:159
std::string charon_core_DLL_PUBLIC readFile(const std::string &fName)
read file content into std::string
Definition: FileTool.cpp:167
Declaration of StringTool methods.
std::string charon_core_DLL_PUBLIC getCurrentDir()
Get current working directory.
Definition: FileTool.cpp:95
const char charon_core_DLL_PUBLIC slash
Slash of current operating system ("/" or "\")
Definition: FileTool.cpp:68
void charon_core_DLL_PUBLIC slashConvert(std::string &src)
Convert unix to windows paths and reversed.
Definition: FileTool.cpp:145
int charon_core_DLL_PUBLIC makeDir(const std::string &dir)
Create a new directory using MODE 711 (in unix)
Definition: FileTool.cpp:87
int charon_core_DLL_PUBLIC rename(const std::string &oldFile, const std::string &newFile)
Rename file.
Definition: FileTool.cpp:163