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) {
117 if (f_path != NULL) {
118 b_count = strlen(f_path);
119 if ((f_path[b_count - 1] != '/') &&
120 (f_path[b_count - 1] != '\\')) {
121 ret_val = new ofstream();
122 ret_val->open(f_path, ios::app);
123 if (ret_val->is_open() == false) {
124 d_path = parse_directory_path(f_path);
125 if (d_path != NULL) {
126 b_flg = mkdirs(d_path);
130 ret_val->open(f_path, ios::app);
135 log_error("Could not open file, invalid file name. (%s)\n", f_path);
139 log_error("Could not open file, invalid file name. (= NULL)\n");
144 string FileUtil::concat_paths(string path_start, string path_end) {
145 return concat_paths(path_start.c_str(), path_end.c_str());
148 string FileUtil::concat_paths(const char *path_start, const char *path_end) {
154 if (path_start != NULL) {
155 ret_val = path_start;
156 if (path_end != NULL) {
158 b_count = ret_val.length();
159 pos = ret_val.find_last_of("/");
160 if (pos == (b_count -1)) {
161 ret_val.append(end_str);
164 pos = end_str.find_first_of("/");
166 ret_val.append(end_str);
170 ret_val.append(end_str);
176 if (path_end != NULL) {
183 bool FileUtil::is_subdirectory(const char *path, dirent *direntry) {
185 struct stat stat_info;
189 if (direntry != NULL) {
190 if ((strcmp(direntry->d_name, ".") == 0) ||
191 (strcmp(direntry->d_name, "..") == 0)) {
195 fname = concat_paths(path, direntry->d_name);
196 stat(fname.c_str(), &stat_info);
197 ret_val = S_ISDIR(stat_info.st_mode);
198 //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
204 bool FileUtil::is_datafile(const char *path, dirent *direntry) {
206 struct stat stat_info;
212 if (direntry != NULL) {
213 name = direntry->d_name;
214 pos = name.find(DATAFILE_SUFFIX);
216 fname = concat_paths(path, direntry->d_name);
217 stat(fname.c_str(), &stat_info);
218 ret_val = S_ISREG(stat_info.st_mode);
225 * get sub-directories sorted in alphabetical order.
227 vector<string> FileUtil::get_subdirectories(const string& path) {
231 vector<string> ret_val;
233 //log_debug("scanning path: %s\n", path.c_str());
235 if (path.empty() == false) {
236 dir = opendir(path.c_str());
240 direntry = readdir(dir);
241 if (direntry != NULL) {
242 bool_flg = is_subdirectory(path.c_str(), direntry);
243 if (bool_flg == true) {
244 ret_val.push_back(string(direntry->d_name));
245 //log_debug("added dir: %s\n", direntry->d_name);
253 sort(ret_val.begin(), ret_val.end());
260 * get sub-directories sorted in alphabetical order.
262 vector<string> FileUtil::get_data_files(const string& path) {
265 vector<string> ret_val;
268 if (path.empty() == false) {
269 dir = opendir(path.c_str());
273 direntry = readdir(dir);
274 if (direntry != NULL) {
275 if (is_datafile(path.c_str(), direntry) == true) {
276 ret_val.push_back(string(direntry->d_name));
284 sort(ret_val.begin(), ret_val.end());