00001
00002
00003
00004
00005
00006
00007 #include"ConfigBuffer.h"
00008
00009 using namespace LcgConfigBuffer;
00010
00011 ConfigBuffer::ConfigBuffer(){}
00012
00013 ConfigBuffer::ConfigBuffer(const string& filename) {
00014 this->populate_from_config(filename);
00015 }
00016
00017
00018 int ConfigBuffer::populate_from_config(const string& filename){
00019
00020 std::string EntryValue;
00021 std::ifstream infile(filename.c_str());
00022
00023 if ( infile == 0 ) {
00024 throw CBException("File \""+filename+"\" not found",__FILE__,__LINE__);
00025 return FAILURE;
00026 } else {
00027
00028 std::string key,value;
00029 std::pair<string,string> KeyVal;
00030
00031
00032 while (getline(infile,EntryValue)){
00033
00034 if ( (EntryValue[0] != '#') && (EntryValue.find('=') != EntryValue.npos)) {
00035
00036
00037 key = EntryValue.substr(0,EntryValue.find ('='));
00038 value = EntryValue.substr(EntryValue.find ('=')+1);
00039
00040 key = key.erase (key.find_last_not_of (' ')+1);
00041 key = key.erase (0,key.find_first_not_of (' '));
00042
00043 value = value.erase (value.find_last_not_of (' ')+1);
00044 value = value.erase (0,value.find_first_not_of (' '));
00045
00046 KeyVal.first = key;
00047 KeyVal.second = value;
00048
00049
00050 if ( _MBuffer.find(key) == _MBuffer.end()){
00051
00052 _MBuffer.insert(KeyVal);
00053
00054 } else {
00055
00056 std::cout << "ATTRIBUTE \"" << key << "\" already exists ... skip entry" << std::endl;
00057 }
00058
00059 } else {
00060
00061 }
00062 }
00063
00064 return SUCCESS;
00065 }
00066 }
00067
00068 void ConfigBuffer::show_map(){
00069
00070 std::cout << " Map : ATTRIBUTE ==> VALUE " << std::endl;
00071
00072 map<std::string,std::string>::const_iterator it_buffer = _MBuffer.begin();
00073
00074 while (it_buffer != _MBuffer.end()){
00075
00076 std::cout << it_buffer->first << " ==> " << it_buffer->second << std::endl;
00077
00078 it_buffer++;
00079
00080 }
00081 }
00082
00083
00084 std::vector<std::string> ConfigBuffer::list_attributes(){
00085 std::vector<std::string> aux;
00086
00087 map<std::string,std::string>::const_iterator it_buffer = _MBuffer.begin();
00088 while (it_buffer != _MBuffer.end()){
00089 aux.push_back(it_buffer->first);
00090 it_buffer++;
00091 }
00092 return aux;
00093 }
00094
00095
00096 string ConfigBuffer::get_attribute_value (const string& Attribute){
00097
00098
00099 if ( _MBuffer.find(Attribute) != _MBuffer.end()){
00100
00101 return _MBuffer[Attribute];
00102
00103 } else {
00104
00105 throw CBException("Attribute does not exist",__FILE__,__LINE__);
00106 return "";
00107
00108 }
00109 }
00110
00111 REsult ConfigBuffer::set_attribute_value(const string& Attribute, const string& Value){
00112
00113 if ( _MBuffer.find(Attribute) != _MBuffer.end()){
00114
00115 _MBuffer[Attribute] = Value;
00116 return SUCCESS;
00117 } else {
00118
00119 throw CBException("Attribute does not exist",__FILE__,__LINE__);
00120 return FAILURE;
00121
00122 }
00123
00124 }
00125
00126 REsult ConfigBuffer::create_new_attribute(const string& Attribute){
00127
00128 if ( _MBuffer.find(Attribute) == _MBuffer.end()){
00129
00130 std::pair<string,string> KeyVal(Attribute,"");
00131 _MBuffer.insert(KeyVal);
00132
00133 return SUCCESS;
00134 } else {
00135
00136 throw CBException("Attribute already exists",__FILE__,__LINE__);
00137 return FAILURE;
00138 }
00139
00140
00141 }
00142
00143
00144 ConfigBuffer::~ConfigBuffer(){
00145
00146 }
00147
00148
00149
00150 string CBException::DefMess = "+CB EXCEPTION: ";
00151
00152
00153 CBException::CBException() {
00154 message=DefMess;
00155 filename="";
00156 linenumber=-1;
00157 }
00158
00159 CBException::CBException(const string& s, const string& file, const int& line){
00160
00161 message=DefMess+s;
00162 filename=file;
00163 linenumber=line;
00164 }
00165
00166 CBException::~CBException() {}
00167
00168 string CBException::what(){
00169 return message;
00170 }
00171
00172 int CBException::get_line(){
00173 return linenumber;
00174 }
00175
00176 string CBException::get_file(){
00177 return filename;
00178 }
00179
00180