4 * Created on: Mar 5, 2011
20 #include "FileUtil.hh"
21 #include "DeviceConfig.hh"
26 template <class NumberDataType>
27 bool string_to_number(NumberDataType& result,
28 const std::string& string_param,
29 std::ios_base& (*format)(std::ios_base&))
31 std::istringstream iss(string_param);
32 return !(iss >> format >> result).fail();
35 FileUtil::FileUtil() {
38 FileUtil::~FileUtil() {
42 * Parses directory path from the full filename and
45 string FileUtil::parse_directory_path(const char *file_name_with_path) {
53 if (file_name_with_path != NULL) {
55 b_count = strlen(file_name_with_path);
56 for (p = &((char *)file_name_with_path)[b_count]; p != (char *)file_name_with_path; p--) {
60 b_count = (b_count - f_size) + 1;
61 ret = strndup(file_name_with_path, b_count);
62 //log_debug("path ret_val: %s\n", ret);
74 bool FileUtil::mkdirs(const char *path) {
81 if (access(path, F_OK) != 0) {
82 // path does not exist, need to create it
83 // go through each directory one by one and create them if they do not yet exist
84 for (p = (char *)path; *p; p++) {
89 // if dir does not exist, create it
90 if (access(path, F_OK) != 0) {
91 //log_debug("trying to create directory: %s\n", path);
92 err_flg = mkdir(path, S_IRWXU);
94 log_error("Could not create directory: %s\n", path);
102 if (ret_val == true) {
103 // if dir does not exist, create it
104 if (access(path, F_OK) != 0) {
105 //log_debug("trying to create directory: %s\n", path);
106 err_flg = mkdir(path, S_IRWXU);
108 log_error("Could not create directory: %s\n", path);
120 log_error("Could not create NULL directory\n");
125 bool FileUtil::file_exist(const char *file_name_with_path, bool writable) {
130 if (writable == true) {
136 if (file_name_with_path != NULL) {
137 if (access(file_name_with_path, acl_mode) == 0) {
144 bool FileUtil::mkfile(const char *file_name_with_path, bool writable) {
154 if (writable == true) {
160 if (file_name_with_path != NULL) {
161 if (access(file_name_with_path, F_OK) != 0) {
162 // file does not exist, create it
163 path_name = parse_directory_path(file_name_with_path);
165 if (access(path_name.c_str(), F_OK) != 0) {
166 // path to file does not exist, create it
167 succ_bool = mkdirs(path_name.c_str());
169 if (succ_bool == true) {
170 // create file with read or write acl rights
171 fp = fopen(file_name_with_path, acl);
179 // file already exist, check access rights and change if needed
181 if (access(file_name_with_path, acl_mode) != 0) {
182 // file does not have specified acl righs, try to grant them
183 fp = fopen(file_name_with_path, acl);
194 path_name = parse_directory_path(file_name_with_path)
195 if (cfg_dir.empty() == false) {
196 if (access(path_name.c_str(), F_OK) != 0) {
197 // config dir does not exist, create it
198 FileUtil::mkdirs(cfg_dir.c_str());
200 if (access(cfg_dir.c_str(), R_OK) != 0) {
201 // config dir does not have read access, add it
202 fp = fopen(cfg_dir.c_str(), "r+");
206 fname_full = get_config_file_name();
207 if (access(fname_full.c_str(), F_OK) != 0) {
208 // config file does not exist, create empty one
209 fp = fopen(fname_full.c_str(), "w+");
213 if (access(fname_full.c_str(), R_OK) != 0) {
214 // config file does not have read access, add it
215 fp = fopen(fname_full.c_str(), "r+");
219 if (access(fname_full.c_str(), R_OK) == 0) {
225 std::ofstream *FileUtil::open_for_writing(const char *f_path) {
233 if (f_path != NULL) {
234 b_count = strlen(f_path);
235 if ((f_path[b_count - 1] != '/') &&
236 (f_path[b_count - 1] != '\\')) {
237 ret_val = new ofstream();
238 ret_val->open(f_path, ios::app);
239 if (ret_val->is_open() == false) {
240 path_name = parse_directory_path(f_path);
241 if (path_name.empty() == false) {
242 b_flg = mkdirs(path_name.c_str());
245 ret_val->open(f_path, ios::app);
250 log_error("Could not open file, invalid file name. (%s)\n", f_path);
254 log_error("Could not open file, invalid file name. (= NULL)\n");
259 string FileUtil::concat_paths(string path_start, string path_end) {
260 return concat_paths(path_start.c_str(), path_end.c_str());
263 string FileUtil::concat_paths(const char *path_start, const char *path_end) {
269 if (path_start != NULL) {
270 ret_val = path_start;
271 if (path_end != NULL) {
273 b_count = ret_val.length();
274 pos = ret_val.find_last_of("/");
275 if (pos == (b_count -1)) {
276 ret_val.append(end_str);
279 pos = end_str.find_first_of("/");
281 ret_val.append(end_str);
285 ret_val.append(end_str);
291 if (path_end != NULL) {
298 bool FileUtil::is_subdirectory(const char *path, dirent *direntry) {
300 struct stat stat_info;
304 if (direntry != NULL) {
305 if ((strcmp(direntry->d_name, ".") == 0) ||
306 (strcmp(direntry->d_name, "..") == 0)) {
310 fname = concat_paths(path, direntry->d_name);
311 stat(fname.c_str(), &stat_info);
312 ret_val = S_ISDIR(stat_info.st_mode);
313 //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
319 bool FileUtil::is_datafile(const char *path, dirent *direntry) {
321 struct stat stat_info;
327 if (direntry != NULL) {
328 name = direntry->d_name;
329 pos = name.find(DATAFILE_SUFFIX);
331 fname = concat_paths(path, direntry->d_name);
332 stat(fname.c_str(), &stat_info);
333 ret_val = S_ISREG(stat_info.st_mode);
340 * get sub-directories sorted in alphabetical order.
342 vector<string> FileUtil::get_subdirectories(const string& path) {
346 vector<string> ret_val;
348 //log_debug("scanning path: %s\n", path.c_str());
350 if (path.empty() == false) {
351 dir = opendir(path.c_str());
355 direntry = readdir(dir);
356 if (direntry != NULL) {
357 bool_flg = is_subdirectory(path.c_str(), direntry);
358 if (bool_flg == true) {
359 ret_val.push_back(string(direntry->d_name));
360 //log_debug("added dir: %s\n", direntry->d_name);
368 sort(ret_val.begin(), ret_val.end());
375 * get sub-directories sorted in alphabetical order.
377 vector<string> FileUtil::get_data_files(const string& path) {
380 vector<string> ret_val;
383 if (path.empty() == false) {
384 dir = opendir(path.c_str());
388 direntry = readdir(dir);
389 if (direntry != NULL) {
390 if (is_datafile(path.c_str(), direntry) == true) {
391 ret_val.push_back(string(direntry->d_name));
399 sort(ret_val.begin(), ret_val.end());