Properties.cpp
Go to the documentation of this file.
00001 #include <face_contour_detector/Properties.h>
00002 
00003 # include <map>
00004 # include <sstream>
00005 # include <iostream>
00006 # include <fstream>
00007 # include <string>
00008 # include <stdio.h>
00009 # include <stdlib.h>
00010 
00011 namespace face_contour_detector {
00012 
00013         Properties::Properties() {}
00014 
00015         Properties::Properties(const Properties& other) : m_m(other.m_m) {}
00016 
00017         void Properties::LoadFromTxtFile(const std::string& file) {
00018                 std::ifstream fstream;
00019                 fstream.open(file.c_str());
00020                 if (!fstream.is_open()) {
00021                         std::stringstream s;
00022                         s<<file<<" could not be opened"<<std::endl;
00023                         throw s.str();
00024                 }
00025                 LoadFromStream(fstream);
00026                 fstream.close();
00027         }
00028 
00029         void Properties::LoadFromStream(std::istream& s) {
00030                 std::string line;
00031                 int lnumber = 0;
00032                 while (s.good()) {
00033                         getline(s, line);
00035                         std::stringstream name;
00036                         std::stringstream value;
00037                         bool correct = true;
00038                         bool eq = false;
00039                         bool spaced = false;
00040                         bool escaped = true;
00041                         for (std::string::iterator it = line.begin(); it < line.end(); it++) {
00042                                 //name_
00043                                 if (!eq && ((*it) == ' ' || (*it) == '\t')) {
00044                                         spaced = true;
00045                                 //name_=
00046                                 } else if (!eq && (*it) == ':') {
00047                                         spaced = false;
00048                                         eq = true;
00049                                 //name_asdf
00050                                 } else if (!eq && spaced) {
00051                                         correct = false;
00052                                         break;
00053                                 //na
00054                                 } else if (!eq && !spaced){
00055                                         name<<(*it);
00056                                 //name_=_
00057                                 } else if (eq && !spaced && ((*it) == ' ' || (*it) == '\t')) {
00058                                         spaced = true;
00059                                 //name_=_val
00060                                 } else if (eq && spaced) {
00061                                         if (!escaped && (*it) == '\\') {
00062                                                 escaped = true;
00063                                         } else if (escaped && (*it) == 'n') {
00064                                                 value<<"\n";
00065                                                 escaped = false;
00066                                         } else {
00067                                                 value<<(*it);
00068                                                 escaped = false;
00069                                         }
00070                                 }
00071                         }
00072                         if (correct && eq && !escaped) {
00073                                 m_m[name.str()] = value.str();
00074                         } else {
00075                                 if (line.compare("") != 0) {
00076                                         if (!eq) {
00077                                                 std::cerr<<"found no ':' in line "<<lnumber<<", ignored it"<<std::endl;
00078                                         } else if (escaped) {
00079                                                 std::cerr<<"there was a '\\' at the end of the line "<<lnumber<<", ignored the line"<<std::endl;
00080                                         } else if (!correct) {
00081                                                 std::cerr<<"there was an error reading line "<<lnumber<<", ignored it (maybe no spaces between the '=' ?)"<<std::endl;
00082                                         }
00083                                 }
00084 
00085                         }
00086                         lnumber++;
00087                 }
00088         }
00089 
00090         void Properties::LoadFromStreamNoOverwrite(std::istream& s, std::stringstream& notReadable) {
00091                 std::string line;
00092                 int lnumber = 0;
00093                 while (s.good()) {
00094                         getline(s, line);
00096                         std::stringstream name;
00097                         std::stringstream value;
00098                         bool correct = true;
00099                         bool eq = false;
00100                         bool spaced = false;
00101                         bool escaped = true;
00102                         for (std::string::iterator it = line.begin(); it < line.end(); it++) {
00103                                 //name_
00104                                 if (!eq && ((*it) == ' ' || (*it) == '\t')) {
00105                                         spaced = true;
00106                                 //name_=
00107                                 } else if (!eq && (*it) == ':') {
00108                                         spaced = false;
00109                                         eq = true;
00110                                 //name_asdf
00111                                 } else if (!eq && spaced) {
00112                                         correct = false;
00113                                         break;
00114                                 //na
00115                                 } else if (!eq && !spaced){
00116                                         name<<(*it);
00117                                 //name_=_
00118                                 } else if (eq && !spaced && ((*it) == ' ' || (*it) == '\t')) {
00119                                         spaced = true;
00120                                 //name_=_val
00121                                 } else if (eq && spaced) {
00122                                         if (!escaped && (*it) == '\\') {
00123                                                 escaped = true;
00124                                         } else if (escaped && (*it) == 'n') {
00125                                                 value<<"\n";
00126                                                 escaped = false;
00127                                         } else {
00128                                                 value<<(*it);
00129                                                 escaped = false;
00130                                         }
00131                                 }
00132                         }
00133                         if (correct && eq && !Exists(name.str())) {
00134                                 m_m[name.str()] = value.str();
00135                         } else if (!Exists(name.str()) && line.compare("") != 0) {
00136                                 notReadable<<line<<std::endl;
00137                                 if (!eq) {
00138                                         std::cerr<<"found no ':' in line "<<lnumber<<", ignored it"<<std::endl;
00139                                 } else if (escaped) {
00140                                         std::cerr<<"there was a '\\' at the end of the line "<<lnumber<<", ignored the line"<<std::endl;
00141                                 } else if (!correct) {
00142                                         std::cerr<<"there was an error reading line "<<lnumber<<", ignored it (maybe no spaces between the '=' ?)"<<std::endl;
00143                                 }
00144                         }
00145                         lnumber++;
00146                 }
00147         }
00148 
00149         void Properties::SaveToTxtFile(const std::string& file) {
00150                 std::ifstream ifstream;
00151                 ifstream.open(file.c_str());
00152 
00153                 std::string notReadable;
00154 
00155                 if (ifstream.is_open()) {
00156 
00157                         //Read file before overwriting it
00158                         std::stringstream notReadableStream;
00159 
00160                         LoadFromStreamNoOverwrite(ifstream, notReadableStream);
00161 
00162                         ifstream.close();
00163 
00164                         //Save unknown stuff for saving later
00165                         notReadable = notReadableStream.str();
00166                 }
00167 
00168                 //Delete file content
00169                 std::ofstream ofstream;
00170                 ofstream.open(file.c_str(), std::ios_base::out | std::ios_base::trunc);
00171                 if (!ofstream.is_open()) {
00172                         std::stringstream s;
00173                         s<<file<<" could not be opened for writing"<<std::endl;
00174                         throw s.str();
00175                 }
00176 
00177                 //Save it
00178                 AppendSaveToStream(ofstream);
00179 
00180                 //Add prev not readable
00181                 ofstream<<notReadable;
00182                 ofstream.close();
00183         }
00184 
00185         void Properties::AppendSaveToStream(std::ostream& s) {
00186                 for (std::map<std::string, std::string>::iterator it=m_m.begin() ; it != m_m.end(); it++ ) {
00187                         //name
00188                         std::string name = (*it).first;
00189                         std::string value = (*it).second;
00190                         s << M_Replace(M_Replace(name, "\\", "\\\\"), "\n", "\\n")<<
00191                         // =
00192                                 ": "<<
00193                         //value
00194                                 M_Replace(M_Replace(value, "\\", "\\\\"), "\n", "\\n")<<
00195                                 std::endl;
00196                 }
00197         }
00198 
00199         bool Properties::Exists(const std::string& key) const { return m_m.find(key) != m_m.end(); }
00200 
00201         std::string Properties::ReadString(const std::string& key) const { return std::string(m_m.find(key)->second); }
00202 
00203         float Properties::ReadFloat(const std::string& key) const { return atof(m_m.find(key)->second.c_str()); }
00204 
00205         double Properties::ReadDouble(const std::string& key) const { return atof(m_m.find(key)->second.c_str()); }
00206 
00207         int Properties::ReadInt(const std::string& key) const { return atoi(m_m.find(key)->second.c_str()); }
00208 
00209         bool Properties::ReadBool(const std::string& key) const {
00210                 if (m_m.find(key)->second.compare("true")  == 0)
00211                         return true;
00212                 if (m_m.find(key)->second.compare("1") == 0)
00213                         return true;
00214                 return false;
00215         }
00216 
00217         void Properties::Set(const std::string& key, const std::string& value) {
00218                 m_m[key] = std::string(value);
00219         }
00220 
00221         void Properties::Set(const std::string& key, const char* value) {
00222                 m_m[key] = std::string(value);
00223         }
00224 
00225         void Properties::Set(const std::string& key, int value) {
00226                 std::stringstream s;
00227                 s<<value;
00228                 m_m[key] = s.str();
00229         }
00230 
00231         void Properties::Set(const std::string& key, float value) {
00232                 std::stringstream s;
00233                 s<<value;
00234                 m_m[key] = s.str();
00235         }
00236 
00237         void Properties::Set(const std::string& key, double value) {
00238                 std::stringstream s;
00239                 s<<value;
00240                 m_m[key] = s.str();
00241         }
00242 
00243         void Properties::Set(const std::string& key, bool value) {
00244                 m_m[key] = value?"true":"false";
00245         }
00246 
00247         void Properties::Clear() { m_m.clear(); }
00248 
00249 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends


face_contour_detector
Author(s): Fabian Wenzelmann and Julian Schmid
autogenerated on Wed Dec 26 2012 16:18:17