Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# test3.cpp

    00001 #include <fstream>
    00002 #include <stdlib.h>
    00003 #include <iostream>
    00004 #include <string>
    00005 #include <list>
    00006 #include <utility>
    00007 #include "memorydatabase.hpp"
    00008
    00009 using namespace std;
    00010
    00011 void error_return(const char* msg)
    00012 {
    00013         cerr << msg;
    00014         exit(3);
    00015 }
    00016
    00017 class mystring
    00018 {
    00019 public:
    00020         string my;
    00021
    00022         mystring() {}
    00023         mystring(const string& s) : my(s) {}
    00024
    00025         istream& read(istream& is)
    00026         {
    00027                 size_t t;
    00028                 char *nstr;
    00029
    00030                 is.read(&t, sizeof(t));
    00031                 nstr = new char[t];
    00032                 is.read(nstr,t);
    00033                 my = string(nstr);
    00034
    00035                 delete [] nstr;
    00036         }
    00037
    00038         ostream& write(ostream& os)
    00039         {
    00040                 size_t t;
    00041
    00042                 t = my.length() + 1;
    00043
    00044                 os.write(&t,sizeof(t));
    00045                 os.write(my.c_str(),t);
    00046         }
    00047 };
    00048
    00049 int main(int argc, char *argv[])
    00050 {
    00051         int i;
    00052         ID id;
    00053         list<ID> m;
    00054         list<ID>::iterator p;
    00055         MemoryDatabase<mystring> ms;
    00056         MemoryDatabase<mystring> ps;
    00057         pair<mystring,bool> ret;
    00058         mystring s;
    00059         fstream outfile;
    00060         fstream infile;
    00061
    00062         ms.Create("Hello");
    00063         for(i=argc-1; i > 0; i--)
    00064         {
    00065                 s = string(argv[i]);
    00066                 id = ms.Add(s);
    00067
    00068                 cout << "ID: " << id << "       ";
    00069                 cout << "DATA: " << s.my << endl;
    00070
    00071                 m.push_back(id);
    00072         }
    00073
    00074         for(p = m.begin(); p != m.end(); p++)
    00075         {
    00076                 ret = ms[*p];
    00077
    00078                 if(!ret.second)
    00079                         cout << "Couldn't retrieve the record from the database" << endl;
    00080                 else
    00081                 {
    00082                         cout << "ID " << *p << "        ";
    00083                         cout << "string " << ret.first.my << endl;
    00084                 }
    00085         }
    00086
    00087         cout << endl << "Here we delete all" << endl;
    00088
    00089         ms.RemoveAll();
    00090
    00091         for(p = m.begin(); p != m.end(); p++)
    00092         {
    00093                 ret = ms[*p];
    00094
    00095                 if(!ret.second)
    00096                         cout << "Couldn't retrieve the record from the database" << endl;
    00097                 else
    00098                 {
    00099                         cout << "ID " << *p << "        ";
    00100                         cout << "string " << ret.first.my << endl;
    00101                 }
    00102         }
    00103         m.clear();
    00104
    00105         cout << endl << "Here we read them all again" << endl;
    00106
    00107         for(i=argc-1; i > 0; i--)
    00108         {
    00109                 s = string(argv[i]);
    00110                 id = ms.Add(s);
    00111
    00112                 cout << "ID: " << id << "       ";
    00113                 cout << "DATA: " << s.my << endl;
    00114
    00115                 m.push_back(id);
    00116         }
    00117
    00118         for(p = m.begin(); p != m.end(); p++)
    00119         {
    00120                 ret = ms[*p];
    00121
    00122                 if(!ret.second)
    00123                         cout << "Couldn't retrieve the record from the database" << endl;
    00124                 else
    00125                 {
    00126                         cout << "ID " << *p << "        ";
    00127                         cout << "string " << ret.first.my << endl;
    00128                 }
    00129         }
    00130
    00131         cout << endl << "ANd the second time" << endl;
    00132
    00133         for(i=argc-1; i > 0; i--)
    00134         {
    00135                 s = string(argv[i]);
    00136                 id = ms.Add(s);
    00137
    00138                 cout << "ID: " << id << "       ";
    00139                 cout << "DATA: " << s.my << endl;
    00140
    00141                 m.push_back(id);
    00142         }
    00143
    00144         for(p = m.begin(); p != m.end(); p++)
    00145         {
    00146                 ret = ms[*p];
    00147
    00148                 if(!ret.second)
    00149                         cout << "Couldn't retrieve the record from the database" << endl;
    00150                 else
    00151                 {
    00152                         cout << "ID " << *p << "        ";
    00153                         cout << "string " << ret.first.my << endl;
    00154                 }
    00155         }
    00156
    00157         cout << endl << "Here we delete all the second time" << endl;
    00158
    00159         p = m.begin();
    00160         for(; p != m.end(); p++) ms.Remove(*p);
    00161
    00162         for(p = m.begin(); p != m.end(); p++)
    00163         {
    00164                 ret = ms[*p];
    00165
    00166                 if(!ret.second)
    00167                         cout << "Couldn't retrieve the record from the database" << endl;
    00168                 else
    00169                 {
    00170                         cout << "ID " << *p << "        ";
    00171                         cout << "string " << ret.first.my << endl;
    00172                 }
    00173         }
    00174
    00175         m.clear();
    00176
    00177         cout << endl << "Here we read them all again" << endl;
    00178
    00179         for(i=argc-1; i > 0; i--)
    00180         {
    00181                 s = string(argv[i]);
    00182                 id = ms.Add(s);
    00183
    00184                 cout << "ID: " << id << "       ";
    00185                 cout << "DATA: " << s.my << endl;
    00186
    00187                 m.push_back(id);
    00188         }
    00189
    00190         for(p = m.begin(); p != m.end(); p++)
    00191         {
    00192                 ret = ms[*p];
    00193
    00194                 if(!ret.second)
    00195                         cout << "Couldn't retrieve the record from the database" << endl;
    00196                 else
    00197                 {
    00198                         cout << "ID " << *p << "        ";
    00199                         cout << "string " << ret.first.my << endl;
    00200                 }
    00201         }
    00202
    00203         cout << endl << "Here we write and read" << endl;
    00204
    00205         outfile.open("/h/11/trokhim/work/code/mdtest.dat", ios::out | ios::binary);
    00206         cout << "hey1" << endl;
    00207         if(!outfile) error_return("Couldn't open output file");
    00208         cout << "hey2" << endl;
    00209
    00210         ms.Write(outfile);
    00211         cout << "hey3" << endl;
    00212         if(!outfile) error_return("couldn't write to the output file");
    00213         cout << "hey3.1" << endl;
    00214
    00215         outfile.close();
    00216         cout << "hey4" << endl;
    00217
    00218         infile.open("/h/11/trokhim/work/code/mdtest.dat", ios::in | ios::nocreate | ios::binary);
    00219
    00220         if(!infile) error_return("Couldn't open input file");
    00221         cout << "hey5" << endl;
    00222
    00223         ps.Read(infile);
    00224         cout << "hey6" << endl;
    00225         if(!infile) error_return("couldn't read from input file");
    00226
    00227         cout << "hey7" << endl;
    00228         infile.close();
    00229         cout << "hey8" << endl;
    00230
    00231         for(p = m.begin(); p != m.end(); p++)
    00232         {
    00233                 ret = ps[*p];
    00234
    00235                 if(!ret.second)
    00236                         cout << "Couldn't retrieve the record from the database" << endl;
    00237                 else
    00238                 {
    00239                         cout << "ID " << *p << "        ";
    00240                         cout << "string " << ret.first.my << endl;
    00241                 }
    00242         }
    00243
    00244         return 0;
    00245 }