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
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
00039
if (RecordActive) {
00040
Insert(key, record);
00041 record =
"";
00042 RecordActive =
false;
00043 };
00044
00045
switch (line[1]) {
00046
case KEY_CHAR :
00047
00048
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
00056
break;
00057
default :
00058
00059
break;
00060 };
00061 }
else {
00062
if (RecordActive) {
00063
00064
00065
if (record !=
"") {
00066 record +=
'\n';
00067 };
00068
00069 record += line;
00070 };
00071 };
00072 };
00073
00074
if (RecordActive) {
00075
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
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
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
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