4 * Created on: Mar 5, 2011
21 #include "FileUtil.hh"
22 #include "DeviceConfig.hh"
27 template <class NumberDataType>
28 bool string_to_number(NumberDataType& result,
29 const std::string& string_param,
30 std::ios_base& (*format)(std::ios_base&))
32 std::istringstream iss(string_param);
33 return !(iss >> format >> result).fail();
36 FileUtil::FileUtil() {
39 FileUtil::~FileUtil() {
43 * Parses directory path from the full filename and
46 string FileUtil::parse_directory_path(const char *file_name_with_path) {
54 if (file_name_with_path != NULL) {
56 b_count = strlen(file_name_with_path);
57 for (p = &((char *)file_name_with_path)[b_count]; p != (char *)file_name_with_path; p--) {
61 b_count = (b_count - f_size) + 1;
62 ret = strndup(file_name_with_path, b_count);
63 //log_debug("path ret_val: %s\n", ret);
75 bool FileUtil::mkdirs(const char *path) {
82 if (access(path, F_OK) != 0) {
83 // path does not exist, need to create it
84 // go through each directory one by one and create them if they do not yet exist
85 for (p = (char *)path; *p; p++) {
90 // if dir does not exist, create it
91 if (access(path, F_OK) != 0) {
92 //log_debug("trying to create directory: %s\n", path);
93 err_flg = mkdir(path, S_IRWXU);
95 log_error("Could not create directory: %s\n", path);
103 if (ret_val == true) {
104 // if dir does not exist, create it
105 if (access(path, F_OK) != 0) {
106 //log_debug("trying to create directory: %s\n", path);
107 err_flg = mkdir(path, S_IRWXU);
109 log_error("Could not create directory: %s\n", path);
121 log_error("Could not create NULL directory\n");
126 bool FileUtil::file_exist(const char *file_name_with_path,
132 if (writable == true) {
138 if (file_name_with_path != NULL) {
139 if (access(file_name_with_path, acl_mode) == 0) {
146 bool FileUtil::dir_exist(const char *dir_name_with_path,
153 if (stat(dir_name_with_path, &st) == 0) {
154 is_dir = S_ISDIR(st.st_mode);
155 if (is_dir == true) {
156 if (writable == false) {
157 // check only the read permission
158 ret_val = ((st.st_mode) & S_IRUSR);
162 // check also the write permission
163 ret_val = ((st.st_mode) & S_IWUSR);
167 if (writable == true) {
173 if (dir_name_with_path != NULL) {
174 if (access(dir_name_with_path, acl_mode) == 0) {
183 bool FileUtil::mkfile(const char *file_name_with_path, bool writable) {
193 if (writable == true) {
199 if (file_name_with_path != NULL) {
200 if (access(file_name_with_path, F_OK) != 0) {
201 // file does not exist, create it
202 path_name = parse_directory_path(file_name_with_path);
204 if (access(path_name.c_str(), F_OK) != 0) {
205 // path to file does not exist, create it
206 succ_bool = mkdirs(path_name.c_str());
208 if (succ_bool == true) {
209 // create file with read or write acl rights
210 fp = fopen(file_name_with_path, acl);
218 // file already exist, check access rights and change if needed
220 if (access(file_name_with_path, acl_mode) != 0) {
221 // file does not have specified acl righs, try to grant them
222 fp = fopen(file_name_with_path, acl);
233 path_name = parse_directory_path(file_name_with_path)
234 if (cfg_dir.empty() == false) {
235 if (access(path_name.c_str(), F_OK) != 0) {
236 // config dir does not exist, create it
237 FileUtil::mkdirs(cfg_dir.c_str());
239 if (access(cfg_dir.c_str(), R_OK) != 0) {
240 // config dir does not have read access, add it
241 fp = fopen(cfg_dir.c_str(), "r+");
245 fname_full = get_config_file_name();
246 if (access(fname_full.c_str(), F_OK) != 0) {
247 // config file does not exist, create empty one
248 fp = fopen(fname_full.c_str(), "w+");
252 if (access(fname_full.c_str(), R_OK) != 0) {
253 // config file does not have read access, add it
254 fp = fopen(fname_full.c_str(), "r+");
258 if (access(fname_full.c_str(), R_OK) == 0) {
264 std::ofstream *FileUtil::open_for_writing(const char *f_path) {
272 if (f_path != NULL) {
273 b_count = strlen(f_path);
274 if ((f_path[b_count - 1] != '/') &&
275 (f_path[b_count - 1] != '\\')) {
276 ret_val = new ofstream();
277 ret_val->open(f_path, ios::app);
278 if (ret_val->is_open() == false) {
279 path_name = parse_directory_path(f_path);
280 if (path_name.empty() == false) {
281 b_flg = mkdirs(path_name.c_str());
284 ret_val->open(f_path, ios::app);
289 log_error("Could not open file, invalid file name. (%s)\n", f_path);
293 log_error("Could not open file, invalid file name. (= NULL)\n");
298 string FileUtil::concat_paths(string path_start, string path_end) {
299 return concat_paths(path_start.c_str(), path_end.c_str());
302 string FileUtil::concat_paths(const char *path_start, const char *path_end) {
308 if (path_start != NULL) {
309 ret_val = path_start;
310 if (path_end != NULL) {
312 b_count = ret_val.length();
313 pos = ret_val.find_last_of("/");
314 if (pos == (b_count -1)) {
315 ret_val.append(end_str);
318 pos = end_str.find_first_of("/");
320 ret_val.append(end_str);
324 ret_val.append(end_str);
330 if (path_end != NULL) {
337 bool FileUtil::is_subdirectory(const char *path, dirent *direntry) {
339 struct stat stat_info;
343 if (direntry != NULL) {
344 if ((strcmp(direntry->d_name, ".") == 0) ||
345 (strcmp(direntry->d_name, "..") == 0)) {
349 fname = concat_paths(path, direntry->d_name);
350 stat(fname.c_str(), &stat_info);
351 ret_val = S_ISDIR(stat_info.st_mode);
352 //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
358 bool FileUtil::is_datafile(const char *path, dirent *direntry) {
360 struct stat stat_info;
366 if (direntry != NULL) {
367 name = direntry->d_name;
368 pos = name.find(DATAFILE_SUFFIX);
370 fname = concat_paths(path, direntry->d_name);
371 stat(fname.c_str(), &stat_info);
372 ret_val = S_ISREG(stat_info.st_mode);
379 * get sub-directories sorted in alphabetical order.
381 vector<string> FileUtil::get_subdirectories(const string& path) {
385 vector<string> ret_val;
387 //log_debug("scanning path: %s\n", path.c_str());
389 if (path.empty() == false) {
390 dir = opendir(path.c_str());
394 direntry = readdir(dir);
395 if (direntry != NULL) {
396 bool_flg = is_subdirectory(path.c_str(), direntry);
397 if (bool_flg == true) {
398 ret_val.push_back(string(direntry->d_name));
399 //log_debug("added dir: %s\n", direntry->d_name);
407 sort(ret_val.begin(), ret_val.end());
414 * get sub-directories sorted in alphabetical order.
416 vector<string> FileUtil::get_data_files(const string& path) {
419 vector<string> ret_val;
422 if (path.empty() == false) {
423 dir = opendir(path.c_str());
427 direntry = readdir(dir);
428 if (direntry != NULL) {
429 if (is_datafile(path.c_str(), direntry) == true) {
430 ret_val.push_back(string(direntry->d_name));
438 sort(ret_val.begin(), ret_val.end());