Main Page | Class List | File List | Class Members | File Members

database.cpp

Go to the documentation of this file.
00001 00009 #include "database.h" 00010 00011 Database::Database() {} 00012 00013 00014 Database::~Database() {} 00015 00016 00024 void Database::Load(string filename) { 00025 int i; 00026 string line, key, record; 00027 bool RecordActive=false; 00028 if (filename=="") { 00029 throw "Database Load: no filename specified\n"; 00030 }; 00031 //create and open a file-stream object 00032 ifstream file(filename.c_str()); 00033 if (!file) { 00034 throw string("Database Load: Unable to open file: "+filename+"\n"); 00035 }; 00036 while ( std::getline(file, line) ) { 00037 if (line[0]==CONTROL_CHAR) { 00038 //end processing an active record. 00039 if (RecordActive) { 00040 Insert(key, record); 00041 record = ""; 00042 RecordActive = false; 00043 }; 00044 //process a "control" line; i.e. a key or comment 00045 switch (line[1]) { 00046 case KEY_CHAR : 00047 //start a new record. 00048 // skip whitespace 00049 i = line.find_first_not_of(" \t\n\r", 2); 00050 key = line.substr(i); 00051 record = ""; 00052 RecordActive = true; 00053 break; 00054 case COMMENT_CHAR : 00055 //ignore line 00056 break; 00057 default : 00058 //unknown control line: ignore 00059 break; 00060 }; 00061 } else { 00062 if (RecordActive) { 00063 //return the \n that the getline removes... 00064 //but only between two lines. 00065 if (record !="") { 00066 record += '\n'; 00067 }; 00068 //add line to current record. 00069 record += line; 00070 }; 00071 }; 00072 }; 00073 //finish the last open record, if any 00074 if (RecordActive) { 00075 //return the \n that the getline removes... 00076 if (record !="") { 00077 record += '\n'; 00078 }; 00079 record += line; 00080 Insert(key,record); 00081 record = ""; 00082 RecordActive = false; 00083 }; 00084 } 00085 00092 void Database::Insert(string key, string data) { 00093 if (key=="") { 00094 throw "Database Insert: Cannot insert empty key\n"; 00095 }; 00096 //replace any "<ESC>" with an ESCAPE - ESC == '\x1b' 00097 data = ExpandEsc(data); 00098 DB_Map.insert( db_record(key,data) ); 00099 #ifdef DEBUG 00100 00101 cout << "\nDatabase::Insert: key: " << key << "\n with data: \n" << data << endl; 00102 #endif 00103 } 00104 00111 string Database::Find(string key) { 00112 int i,j; 00113 db_iter it; 00114 //remove whitespace from begin and end 00115 i = key.find_first_not_of(" \t\n\r"); 00116 j = key.find_last_not_of(" \t\n\r"); 00117 key = key.substr(i, j-i+1); 00118 it = DB_Map.find(key); 00119 if (it != DB_Map.end()) { 00120 return (*it).second; 00121 } else { 00122 //not found 00123 #ifdef DEBUG 00124 cerr << "\n Warning Database::Find: key: " << key << " : NOT found" << endl; 00125 #endif 00126 00127 return ""; 00128 }; 00129 } 00130 00134 void Database::Clear() { 00135 DB_Map.clear(); 00136 } 00137

Generated on Thu Jun 16 15:47:18 for pc_demo.kdevelop by doxygen 1.3.8