Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/typechain/test.cc

Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <signal.h>
00004 #include <fstream>
00005 
00006 
00007 #include "typechain.hh"
00008 #include "typehandler.hh"
00009 
00010 void sigpipe(int signal) {
00011     printf("typetest: WARNING: recieved sigpipe!\n");
00012 }
00013 
00014 int main()
00015 {
00016     signal(SIGPIPE, sigpipe);
00017     printf("typetest...\n");
00018     string data("hello, world");
00019     string rdata("uryyb, jbeyq");
00020     string xdata("data &lt;la&gt; la &amp; la");
00021     string vanilla("identity;identity;identity;text/plain");
00022     string rot13("rot13:text/plain");
00023     string type;
00024     string base64data("SW4gWGFuYWR1IGRpZCBLdWJsYSBLaGFuCkEgc3RhdGVseSBwbGVhc3VyZS1kb21lIGRlY3Jl\n\
00025 ZToKV2hlcmUgQWxwaCwgdGhlIHNhY3JlZCByaXZlciwgcmFuClRocm91Z2ggY2F2ZXJucyBt\n\
00026 ZWFzdXJlbGVzcyB0byBtYW4KRG93biB0byBhIHN1bmxlc3Mgc2VhLgpTbyB0d2ljZSBmaXZl\n\
00027 IG1pbGVzIG9mIGZlcnRpbGUgZ3JvdW5kCldpdGggd2FsbHMgYW5kIHRvd2VycyB3ZXJlIGdp\n\
00028 cmRsZWQgcm91bmQ6CkFuZCB0aGVyZSB3ZXJlIGdhcmRlbnMgYnJpZ2h0IHdpdGggc2ludW91\n\
00029 cyByaWxscywKV2hlcmUgYmxvc3NvbWVkIG1hbnkgYW4gaW5jZW5zZS1iZWFyaW5nIHRyZWU7\n\
00030 CkFuZCBoZXJlIHdlcmUgZm9yZXN0cyBhbmNpZW50IGFzIHRoZSBoaWxscywKRW5mb2xkaW5n\n\
00031 IHN1bm55IHNwb3RzIG9mIGdyZWVuZXJ5Lgo=");
00032 
00033     printf("typetest: initializing typechain with directory \"handlers/.libs\"\n");
00034     TypeChain::initialize("handlers/.libs", 0);
00035 
00036     printf("typetest: ok. decoding identity 'encoded' text.\n");
00037     try {
00038         type = TypeChain::decode(data, vanilla);
00039         printf("identity: new type: \"%s\"  new data: \"%s\"\n",   type.c_str(), data.c_str() );
00040     } catch(runtime_error& e) {
00041         printf("ERROR: %s.\n", e.what());
00042     }
00043 
00044 
00045     cout << "decoding base64 data...\n";
00046     try {
00047         type = TypeChain::decode(base64data, "base64:text/plain");
00048         printf("\nBase64: new type=\"%s\".  new data=\"%s\".", type.c_str(), base64data.c_str() );
00049     } catch(runtime_error& e) {
00050         printf("ERROR: %s.\n", e.what());
00051     }
00052     
00053 
00054     try {
00055         printf("\nReading image.png.b64 off disk, decoding, and writing to image.png...\n");
00056         FILE* fp = fopen("image.png.b64", "r");
00057         if(fp) {
00058             string imagedata("");
00059             char buff[78];
00060             while(fgets(buff, 75, fp) != NULL) {
00061                 imagedata += (char*)buff;   // major problem if buff has null characters in it ?
00062             }
00063             fclose(fp);
00064             printf("\timage data has %ld bytes.\n", imagedata.length());
00065             type = TypeChain::decode(imagedata, "base64;image/png");
00066             printf("base64: new type=\"%s\". writing new data  to image.png...\n", type.c_str());
00067             fp = fopen("image.png", "w");
00068             for(unsigned int i = 0; i < imagedata.length(); i++)    
00069                 fprintf(fp, "%c", imagedata[i]);
00070             fclose(fp);
00071         } else {
00072             printf("\terror opening image.png.b64.\n");
00073         }
00074     } catch(runtime_error& e) {
00075         printf("ERROR: %s\n", e.what());
00076     }
00077         
00078 
00079     printf("\nGuessing type from filename \"foobar.gz.url\"...\n");
00080     string  t = TypeChain::guessTypeFromFilename("foobar.gz.url");
00081     printf("%s\n", t.c_str() );
00082 
00083     printf("\nMaking up filename  for %s...\n", t.c_str());
00084     printf("%s\n", TypeChain::makeFilename(t, "barfoo").c_str());
00085 
00086     try {
00087         printf("\nTrying URL...\n");
00088         string url("http://interreality.org");
00089         type = TypeChain::decode(url, "URL:text/html");
00090         printf("\n\n%s\n\n", url.c_str());
00091     } catch (runtime_error& e) {
00092         printf("ERROR: %s\n", e.what());
00093     }
00094 
00095 
00096     cout << "Trying gzip...\n\n";
00097     try {
00098         string data;
00099         ifstream f("testdata.txt.gz", ios::binary|ios::in);
00100         if(f.bad()) {
00101             throw runtime_error("error opening \"testdata.txt.gz\" for reading");
00102         }
00103         char c;
00104         while(f) {
00105             f.get(c);
00106             data += c;
00107         }
00108         f.close();
00109         
00110         TypeParams parm;
00111         type = TypeChain::decode(data, "gzip;text/plain", parm);
00112 
00113         cout  << "New type is: " << type << endl;
00114         cout  << "Decoded data is: " << data << endl << endl;
00115         
00116     } catch(exception& e) {
00117         cout << "Error decoding gzipped text: " << e.what() << endl;
00118     }
00119 
00120     try {
00121         printf("\nTrying deflated data...\n");
00122         ifstream f("testdata.txt.deflated", ios::in|ios::binary);
00123         if(!f.is_open() || f.bad())
00124             throw runtime_error("error opening \"testdata.txt.deflated\" for reading");
00125         f.seekg(0, ios::end);
00126         unsigned long size = f.tellg();
00127         f.seekg (0, ios::beg);
00128         char* buffer = new char[size];
00129         f.read(buffer, size);
00130         f.close();
00131         string gzdata(buffer);
00132         TypeParams parm;
00133         char dl[32]; snprintf(dl, 32, "%d", 403);    //testdata.txt is 403 chars big
00134         cerr << "Typetest: Datalen is " << dl << endl;
00135         parm["datalen"] = strdup(dl);
00136         for(map<string,string>::const_iterator i = parm.begin(); i != parm.end(); i++) {
00137             cerr << "   parm[" << i->first << "] = " << i->second << endl;
00138         }
00139 
00140 
00141         TypeChain::decode(gzdata, "deflate;text/plain", parm);
00142         cout << "Result is: \"" << gzdata << "\"" << endl;
00143     } catch(exception& e) {
00144         cout << "Error decoding deflated text: " << e.what() << endl;
00145     }
00146                 
00147     
00148     cout << "trying to encode gzipped data...\n";
00149     try {
00150         string data("hello, world");
00151         TypeChain::encode(data, "gzip:text/plain");
00152         cout << "Result is: \"" << data << "\"\n";
00153     } catch(exception& e) {
00154         cout << "Error decoding deflated text: " << e.what() << endl;
00155     }
00156 
00157     TypeChain::uninitialize();
00158     cout << "== End of typetest ==\n";
00159     return 0;
00160 }
00161 

Generated on Tue Aug 12 03:55:39 2003 for Interreality Project - VOS by doxygen 1.3.2