propertyInfo.cpp
00001
00002
00003
00004
00005
00006
00007 #include "propertyInfo.h"
00008 #include "fileUtils.h"
00009 #include "detect_os.h"
00010
00011
00012 PropertyInfo::PropertyInfo(void)
00013 : before_read(true), file_name(""), read_fail(false) {
00014 }
00015
00016
00017 PropertyInfo::~PropertyInfo(void) {
00018 }
00019
00020
00021 bool PropertyInfo::load(const char* fname, const char* path[]) {
00022
00023 file_name = VXV::searchFile(fname, path);
00024 if (!file_name.empty()) {
00025 return firstRead();
00026 }
00027
00028 int last_index = 0;
00029 while (path[++last_index] != NULL)
00030 ;
00031 --last_index;
00032 file_name = std::string(path[last_index]) + "/" + fname;
00033 firstRead();
00034 return false;
00035 }
00036
00037
00038 bool PropertyInfo::firstRead(void) {
00039 if (!before_read) {
00040 return false;
00041 }
00042
00043
00044 fd.open(file_name.c_str(), std::ios::in);
00045 evaluate();
00046 fd.close();
00047
00048 before_read = false;
00049 if (read_fail) {
00050
00051
00052 read_fail = false;
00053 }
00054 return true;
00055 }
00056
00057
00058 void PropertyInfo::save(void) {
00059 before_read = false;
00060
00061
00062 for (int i = 0; i < 2; ++i) {
00063 fd.open(file_name.c_str(), std::ios::out);
00064 if (fd.is_open()) {
00065 break;
00066 }
00067 if (i > 0) {
00068 return;
00069 }
00070 VXV::createDirs(file_name.substr(0, file_name.find_last_of('/')));
00071 }
00072
00073 evaluate();
00074 fd.close();
00075 }
00076
00077
00078 void PropertyInfo::value(int* int_num, const char* key, int def_num) {
00079 if (!fd.is_open()) {
00080 *int_num = def_num;
00081
00082 } else if (before_read) {
00083
00084 std::string key_actual;
00085 fd >> key_actual;
00086
00087 if (key_actual.compare(key)) {
00088 *int_num = def_num;
00089 read_fail = true;
00090
00091 } else {
00092 std::string line;
00093 getline(fd, line);
00094 *int_num = atoi(line.c_str());
00095 }
00096 } else {
00097
00098 fd << key << ' ' << *int_num << std::endl;
00099 }
00100 }
00101
00102
00103 void PropertyInfo::value(std::string& str, const char* key,
00104 const char* def_str) {
00105 if (!fd.is_open()) {
00106 str = def_str;
00107
00108 } else if (before_read) {
00109
00110 std::string key_actual;
00111 fd >> key_actual;
00112 if (key_actual.compare(key)) {
00113 str = def_str;
00114 read_fail = true;
00115
00116 } else {
00117 getline(fd, str);
00118 str.erase(0, str.find_first_not_of(" \t") + 1);
00119 str.erase(str.end() -1);
00120 }
00121 } else {
00122
00123 fd << key << " \"" << str << '"' << std::endl;
00124 }
00125 }
00126