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() {
41 char *FileUtil::parse_directory_path(const char *file_path) {
48 if (file_path != NULL) {
50 b_count = strlen(file_path);
51 for (p = &((char *)file_path)[b_count]; p != (char *)file_path; p--) {
55 b_count = (b_count - f_size) + 1;
56 ret_val = strndup(file_path, b_count);
57 log_debug("dir: %s\n", ret_val);
65 bool FileUtil::mkdirs(const char *path) {
72 // go through each directory one by and and create if not exist
73 for (p = (char *)path; *p; p++) {
78 // if dir does not exist, create it
79 if (access(path, F_OK) != 0) {
80 //log_debug("trying to create directory: %s\n", path);
81 err_flg = mkdir(path, S_IRWXU);
83 log_error("Could not create directory: %s\n", path);
91 if (ret_val == true) {
92 // if dir does not exist, create it
93 if (access(path, F_OK) != 0) {
94 //log_debug("trying to create directory: %s\n", path);
95 err_flg = mkdir(path, S_IRWXU);
97 log_error("Could not create directory: %s\n", path);
105 log_error("Could not create NULL directory\n");
110 std::ofstream *FileUtil::open_for_writing(const char *f_path) {
118 if (f_path != NULL) {
119 b_count = strlen(f_path);
120 if ((f_path[b_count - 1] != '/') &&
121 (f_path[b_count - 1] != '\\')) {
122 ret_val = new ofstream();
123 ret_val->open(f_path, ios::app);
124 if (ret_val->is_open() == false) {
125 d_path = parse_directory_path(f_path);
126 if (d_path != NULL) {
127 b_flg = mkdirs(d_path);
131 ret_val->open(f_path, ios::app);
136 log_error("Could not open file, invalid file name. (%s)\n", f_path);
140 log_error("Could not open file, invalid file name. (= NULL)\n");
145 string FileUtil::concat_paths(string path_start, string path_end) {
146 return concat_paths(path_start.c_str(), path_end.c_str());
149 string FileUtil::concat_paths(const char *path_start, const char *path_end) {
155 if (path_start != NULL) {
156 ret_val = path_start;
157 if (path_end != NULL) {
159 b_count = ret_val.length();
160 pos = ret_val.find_last_of("/");
161 if (pos == (b_count -1)) {
162 ret_val.append(end_str);
165 pos = end_str.find_first_of("/");
167 ret_val.append(end_str);
171 ret_val.append(end_str);
177 if (path_end != NULL) {
184 bool FileUtil::is_subdirectory(const char *path, dirent *direntry) {
186 struct stat stat_info;
190 if (direntry != NULL) {
191 if ((strcmp(direntry->d_name, ".") == 0) ||
192 (strcmp(direntry->d_name, "..") == 0)) {
196 fname = concat_paths(path, direntry->d_name);
197 stat(fname.c_str(), &stat_info);
198 ret_val = S_ISDIR(stat_info.st_mode);
199 //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
205 bool FileUtil::is_datafile(const char *path, dirent *direntry) {
207 struct stat stat_info;
213 if (direntry != NULL) {
214 name = direntry->d_name;
215 pos = name.find(DATAFILE_SUFFIX);
217 fname = concat_paths(path, direntry->d_name);
218 stat(fname.c_str(), &stat_info);
219 ret_val = S_ISREG(stat_info.st_mode);
226 * get sub-directories sorted in alphabetical order.
228 vector<string> FileUtil::get_subdirectories(const string& path) {
232 vector<string> ret_val;
234 //log_debug("scanning path: %s\n", path.c_str());
236 if (path.empty() == false) {
237 dir = opendir(path.c_str());
241 direntry = readdir(dir);
242 if (direntry != NULL) {
243 bool_flg = is_subdirectory(path.c_str(), direntry);
244 if (bool_flg == true) {
245 ret_val.push_back(string(direntry->d_name));
246 //log_debug("added dir: %s\n", direntry->d_name);
254 sort(ret_val.begin(), ret_val.end());
261 * get sub-directories sorted in alphabetical order.
263 vector<string> FileUtil::get_data_files(const string& path) {
266 vector<string> ret_val;
269 if (path.empty() == false) {
270 dir = opendir(path.c_str());
274 direntry = readdir(dir);
275 if (direntry != NULL) {
276 if (is_datafile(path.c_str(), direntry) == true) {
277 ret_val.push_back(string(direntry->d_name));
285 sort(ret_val.begin(), ret_val.end());