--- /dev/null
+/*
+ * FileUtil.cc
+ *
+ * Created on: Mar 5, 2011
+ * Author: lamikr
+ */
+
+#include <algorithm>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <sstream>
+
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <stdbool.h>
+
+#include "log.h"
+#include "FileUtil.hh"
+#include "DeviceConfig.hh"
+
+using namespace std;
+using namespace plp;
+
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
+ const std::string& string_param,
+ std::ios_base& (*format)(std::ios_base&))
+{
+ std::istringstream iss(string_param);
+ return !(iss >> format >> result).fail();
+}
+
+FileUtil::FileUtil() {
+}
+
+FileUtil::~FileUtil() {
+}
+
+char *FileUtil::parse_directory_path(const char *file_path) {
+ char *p;
+ size_t b_count;
+ size_t f_size;
+ char *ret_val;
+
+ ret_val = NULL;
+ if (file_path != NULL) {
+ f_size = 0;
+ b_count = strlen(file_path);
+ for (p = &((char *)file_path)[b_count]; p != (char *)file_path; p--) {
+ f_size++;
+ if ((*p == '/') ||
+ (*p == '\\')) {
+ b_count = (b_count - f_size) + 1;
+ ret_val = strndup(file_path, b_count);
+ log_debug("dir: %s\n", ret_val);
+ break;
+ }
+ }
+ }
+ return ret_val;
+}
+
+bool FileUtil::mkdirs(const char *path) {
+ bool ret_val;
+ char *p;
+ int err_flg;
+
+ ret_val = true;
+ if (path != NULL) {
+ // go through each directory one by and and create if not exist
+ for (p = (char *)path; *p; p++) {
+ if ((p != path) &&
+ ((*p == '/') ||
+ (*p == '\\'))) {
+ *p = '\0';
+ // if dir does not exist, create it
+ if (access(path, F_OK) != 0) {
+ //log_debug("trying to create directory: %s\n", path);
+ err_flg = mkdir(path, S_IRWXU);
+ if (err_flg != 0) {
+ log_error("Could not create directory: %s\n", path);
+ ret_val = false;
+ break;
+ }
+ }
+ *p = '/';
+ }
+ }
+ if (ret_val == true) {
+ // if dir does not exist, create it
+ if (access(path, F_OK) != 0) {
+ //log_debug("trying to create directory: %s\n", path);
+ err_flg = mkdir(path, S_IRWXU);
+ if (err_flg != 0) {
+ log_error("Could not create directory: %s\n", path);
+ ret_val = false;
+ }
+ }
+ }
+ }
+ else {
+ ret_val = false;
+ log_error("Could not create NULL directory\n");
+ }
+ return ret_val;
+}
+
+std::ofstream *FileUtil::open_for_writing(const char *f_path) {
+ char *d_path;
+ size_t b_count;
+ ofstream *ret_val;
+ bool b_flg;
+
+ ret_val = NULL;
+ if (f_path != NULL) {
+ b_count = strlen(f_path);
+ if ((f_path[b_count - 1] != '/') &&
+ (f_path[b_count - 1] != '\\')) {
+ ret_val = new ofstream();
+ ret_val->open(f_path, ios::app);
+ if (ret_val->is_open() == false) {
+ d_path = parse_directory_path(f_path);
+ if (d_path != NULL) {
+ b_flg = mkdirs(d_path);
+ free(d_path);
+ }
+ if (b_flg == true) {
+ ret_val->open(f_path, ios::app);
+ }
+ }
+ }
+ else {
+ log_error("Could not open file, invalid file name. (%s)\n", f_path);
+ }
+ }
+ else {
+ log_error("Could not open file, invalid file name. (= NULL)\n");
+ }
+ return ret_val;
+}
+
+string FileUtil::concat_paths(string path_start, string path_end) {
+ return concat_paths(path_start.c_str(), path_end.c_str());
+}
+
+string FileUtil::concat_paths(const char *path_start, const char *path_end) {
+ string ret_val;
+ string end_str;
+ int pos;
+ int b_count;
+
+ if (path_start != NULL) {
+ ret_val = path_start;
+ if (path_end != NULL) {
+ end_str = path_end;
+ b_count = ret_val.length();
+ pos = ret_val.find_last_of("/");
+ if (pos == (b_count -1)) {
+ ret_val.append(end_str);
+ }
+ else {
+ pos = end_str.find_first_of("/");
+ if (pos == 0) {
+ ret_val.append(end_str);
+ }
+ else {
+ ret_val.append("/");
+ ret_val.append(end_str);
+ }
+ }
+ }
+ }
+ else {
+ if (path_end != NULL) {
+ ret_val = path_end;
+ }
+ }
+ return ret_val;
+}
+
+bool FileUtil::is_subdirectory(const char *path, dirent *direntry) {
+ bool ret_val;
+ struct stat stat_info;
+ string fname;
+
+ ret_val = false;
+ if (direntry != NULL) {
+ if ((strcmp(direntry->d_name, ".") == 0) ||
+ (strcmp(direntry->d_name, "..") == 0)) {
+ ret_val = false;
+ }
+ else {
+ fname = concat_paths(path, direntry->d_name);
+ stat(fname.c_str(), &stat_info);
+ ret_val = S_ISDIR(stat_info.st_mode);
+ //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
+ }
+ }
+ return ret_val;
+}
+
+bool FileUtil::is_datafile(const char *path, dirent *direntry) {
+ bool ret_val;
+ struct stat stat_info;
+ string name;
+ int pos;
+ string fname;
+
+ ret_val = false;
+ if (direntry != NULL) {
+ name = direntry->d_name;
+ pos = name.find(DATAFILE_SUFFIX);
+ if (pos > 0) {
+ fname = concat_paths(path, direntry->d_name);
+ stat(fname.c_str(), &stat_info);
+ ret_val = S_ISREG(stat_info.st_mode);
+ }
+ }
+ return ret_val;
+}
+
+/**
+ * get sub-directories sorted in alphabetical order.
+ */
+vector<string> FileUtil::get_subdirectories(const string& path) {
+ dirent *direntry;
+ DIR *dir;
+ bool bool_flg;
+ vector<string> ret_val;
+
+ //log_debug("scanning path: %s\n", path.c_str());
+ errno = 0;
+ if (path.empty() == false) {
+ dir = opendir(path.c_str());
+ if (dir) {
+ while (true) {
+ errno = 0;
+ direntry = readdir(dir);
+ if (direntry != NULL) {
+ bool_flg = is_subdirectory(path.c_str(), direntry);
+ if (bool_flg == true) {
+ ret_val.push_back(string(direntry->d_name));
+ //log_debug("added dir: %s\n", direntry->d_name);
+ }
+ }
+ else {
+ break;
+ }
+ }
+ closedir(dir);
+ sort(ret_val.begin(), ret_val.end());
+ }
+ }
+ return ret_val;
+}
+
+/**
+ * get sub-directories sorted in alphabetical order.
+ */
+vector<string> FileUtil::get_data_files(const string& path) {
+ dirent *direntry;
+ DIR *dir;
+ vector<string> ret_val;
+
+ errno = 0;
+ if (path.empty() == false) {
+ dir = opendir(path.c_str());
+ if (dir) {
+ while (true) {
+ errno = 0;
+ direntry = readdir(dir);
+ if (direntry != NULL) {
+ if (is_datafile(path.c_str(), direntry) == true) {
+ ret_val.push_back(string(direntry->d_name));
+ }
+ }
+ else {
+ break;
+ }
+ }
+ closedir(dir);
+ sort(ret_val.begin(), ret_val.end());
+ }
+ }
+ return ret_val;
+}