X-Git-Url: http://pilppa.org/gitweb/?p=lib1wire.git;a=blobdiff_plain;f=src%2FW1Util.cc;fp=src%2FW1Util.cc;h=0000000000000000000000000000000000000000;hp=a9fc351b9755a28e3cd18c17a94f693127108aea;hb=a7002b0a87c5f17b542dfbd44f482014a191df97;hpb=da203ad44792ca37320768f4cbe68587c4806910 diff --git a/src/W1Util.cc b/src/W1Util.cc deleted file mode 100644 index a9fc351..0000000 --- a/src/W1Util.cc +++ /dev/null @@ -1,298 +0,0 @@ -/* - * W1Util.cc - * - * Created on: Nov 14, 2010 - * Author: lamikr - */ -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include "W1Util.hh" -#include "W1Configure.hh" - -using namespace std; -using namespace w1; -using namespace plp; - -template -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(); -} - -W1Util::W1Util() { -} - -W1Util::~W1Util() { -} - -char *W1Util::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 W1Util::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 *W1Util::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 W1Util::concat_paths(string path_start, string path_end) { - return concat_paths(path_start.c_str(), path_end.c_str()); -} - -string W1Util::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 W1Util::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 W1Util::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 W1Util::get_subdirectories(const string& path) { - dirent *direntry; - DIR *dir; - bool bool_flg; - vector 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 W1Util::get_data_files(const string& path) { - dirent *direntry; - DIR *dir; - vector 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; -} - -Date W1Util::parse_date_str(string date_str) { - char c; - stringstream ss(date_str); - Date ret_val; - - ss >>ret_val.year >>c >>ret_val.month >>c >>ret_val.day >>ret_val.hour >>c >>ret_val.min >>c >>ret_val.sec; - return ret_val; -}