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,
131 if (writable == true) {
137 if (file_name_with_path != NULL) {
138 if (access(file_name_with_path, acl_mode) == 0) {
145 bool FileUtil::dir_exist(const char *dir_name_with_path,
152 if (stat(dir_name_with_path, &st) == 0) {
153 is_dir = S_ISDIR(st.st_mode);
154 if (is_dir == true) {
155 if (writable == false) {
156 // check only the read permission
157 ret_val = ((st.st_mode) & S_IRUSR);
161 // check also the write permission
162 ret_val = ((st.st_mode) & S_IWUSR);
166 if (writable == true) {
172 if (dir_name_with_path != NULL) {
173 if (access(dir_name_with_path, acl_mode) == 0) {
182 bool FileUtil::mkfile(const char *file_name_with_path, bool writable) {
192 if (writable == true) {
198 if (file_name_with_path != NULL) {
199 if (access(file_name_with_path, F_OK) != 0) {
200 // file does not exist, create it
201 path_name = parse_directory_path(file_name_with_path);
203 if (access(path_name.c_str(), F_OK) != 0) {
204 // path to file does not exist, create it
205 succ_bool = mkdirs(path_name.c_str());
207 if (succ_bool == true) {
208 // create file with read or write acl rights
209 fp = fopen(file_name_with_path, acl);
217 // file already exist, check access rights and change if needed
219 if (access(file_name_with_path, acl_mode) != 0) {
220 // file does not have specified acl righs, try to grant them
221 fp = fopen(file_name_with_path, acl);
232 path_name = parse_directory_path(file_name_with_path)
233 if (cfg_dir.empty() == false) {
234 if (access(path_name.c_str(), F_OK) != 0) {
235 // config dir does not exist, create it
236 FileUtil::mkdirs(cfg_dir.c_str());
238 if (access(cfg_dir.c_str(), R_OK) != 0) {
239 // config dir does not have read access, add it
240 fp = fopen(cfg_dir.c_str(), "r+");
244 fname_full = get_config_file_name();
245 if (access(fname_full.c_str(), F_OK) != 0) {
246 // config file does not exist, create empty one
247 fp = fopen(fname_full.c_str(), "w+");
251 if (access(fname_full.c_str(), R_OK) != 0) {
252 // config file does not have read access, add it
253 fp = fopen(fname_full.c_str(), "r+");
257 if (access(fname_full.c_str(), R_OK) == 0) {
263 std::ofstream *FileUtil::open_for_writing(const char *f_path) {
271 if (f_path != NULL) {
272 b_count = strlen(f_path);
273 if ((f_path[b_count - 1] != '/') &&
274 (f_path[b_count - 1] != '\\')) {
275 ret_val = new ofstream();
276 ret_val->open(f_path, ios::app);
277 if (ret_val->is_open() == false) {
278 path_name = parse_directory_path(f_path);
279 if (path_name.empty() == false) {
280 b_flg = mkdirs(path_name.c_str());
283 ret_val->open(f_path, ios::app);
288 log_error("Could not open file, invalid file name. (%s)\n", f_path);
292 log_error("Could not open file, invalid file name. (= NULL)\n");
297 string FileUtil::concat_paths(string path_start, string path_end) {
298 return concat_paths(path_start.c_str(), path_end.c_str());
301 string FileUtil::concat_paths(const char *path_start, const char *path_end) {
307 if (path_start != NULL) {
308 ret_val = path_start;
309 if (path_end != NULL) {
311 b_count = ret_val.length();
312 pos = ret_val.find_last_of("/");
313 if (pos == (b_count -1)) {
314 ret_val.append(end_str);
317 pos = end_str.find_first_of("/");
319 ret_val.append(end_str);
323 ret_val.append(end_str);
329 if (path_end != NULL) {
336 bool FileUtil::is_subdirectory(const char *path, dirent *direntry) {
338 struct stat stat_info;
342 if (direntry != NULL) {
343 if ((strcmp(direntry->d_name, ".") == 0) ||
344 (strcmp(direntry->d_name, "..") == 0)) {
348 fname = concat_paths(path, direntry->d_name);
349 stat(fname.c_str(), &stat_info);
350 ret_val = S_ISDIR(stat_info.st_mode);
351 //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
357 bool FileUtil::is_datafile(const char *path, dirent *direntry) {
359 struct stat stat_info;
365 if (direntry != NULL) {
366 name = direntry->d_name;
367 pos = name.find(DATAFILE_SUFFIX);
369 fname = concat_paths(path, direntry->d_name);
370 stat(fname.c_str(), &stat_info);
371 ret_val = S_ISREG(stat_info.st_mode);
378 * get sub-directories sorted in alphabetical order.
380 vector<string> FileUtil::get_subdirectories(const string& path) {
384 vector<string> ret_val;
386 //log_debug("scanning path: %s\n", path.c_str());
388 if (path.empty() == false) {
389 dir = opendir(path.c_str());
393 direntry = readdir(dir);
394 if (direntry != NULL) {
395 bool_flg = is_subdirectory(path.c_str(), direntry);
396 if (bool_flg == true) {
397 ret_val.push_back(string(direntry->d_name));
398 //log_debug("added dir: %s\n", direntry->d_name);
406 sort(ret_val.begin(), ret_val.end());
413 * get sub-directories sorted in alphabetical order.
415 vector<string> FileUtil::get_data_files(const string& path) {
418 vector<string> ret_val;
421 if (path.empty() == false) {
422 dir = opendir(path.c_str());
426 direntry = readdir(dir);
427 if (direntry != NULL) {
428 if (is_datafile(path.c_str(), direntry) == true) {
429 ret_val.push_back(string(direntry->d_name));
437 sort(ret_val.begin(), ret_val.end());