COOPY » Guide
version 0.6.5
|
00001 #include <coopy/FormatSniffer.h> 00002 #include <coopy/Dbg.h> 00003 00004 #include <stdio.h> 00005 #include <string.h> 00006 00007 using namespace std; 00008 using namespace coopy::format; 00009 using namespace coopy::store; 00010 00011 bool FormatSniffer::setString(const char *str) { 00012 close(); 00013 cache = str; 00014 return true; 00015 } 00016 00017 bool FormatSniffer::wrap(FileIO& fin, bool caching) { 00018 char buf[caching?32768:100]; 00019 cache = ""; 00020 size_t bytes_read; 00021 if (caching) { 00022 while ((bytes_read=fin.fread(buf,1,sizeof(buf)))>0) { 00023 cache.append(buf,bytes_read); 00024 } 00025 } else { 00026 bytes_read = fin.fread(buf,1,sizeof(buf)); 00027 cache.append(buf,bytes_read); 00028 } 00029 return true; 00030 } 00031 00032 bool FormatSniffer::open(const char *fname, bool caching) { 00033 close(); 00034 dbg_printf("Looking at %s\n", fname); 00035 Property p; 00036 if (!fio.open(fname,p)) { 00037 fprintf(stderr,"FormatSniffer: could not open %s\n", fname); 00038 return false; 00039 } 00040 wrap(fio,caching); 00041 return true; 00042 } 00043 00044 00045 bool FormatSniffer::close() { 00046 fio.close(); 00047 cache = ""; 00048 return true; 00049 } 00050 00051 00052 Format FormatSniffer::getFormat() { 00053 if (cache.substr(0,4)=="dtbl"||cache.substr(0,4)=="\"dtbl") { 00054 size_t brk = cache.find('\n'); 00055 string header = cache.substr(0,brk); 00056 bool csv = header.find("csv")!=string::npos; 00057 bool human = header.find("human")!=string::npos; 00058 //printf("HAVE brk at %d\n", (int) brk); 00059 //printf("HAVE string [%s]\n", cache.substr(0,10).c_str()); 00060 Format f; 00061 if (csv) { 00062 f.id = FORMAT_PATCH_CSV; 00063 f.name = "coopy_patch_csv"; 00064 } 00065 if (human) { 00066 f.id = FORMAT_PATCH_HUMAN; 00067 f.name = "coopy_patch_human"; 00068 } 00069 return f; 00070 } else if (cache.substr(0,7)=="# tdiff") { 00071 Format f; 00072 f.id = FORMAT_PATCH_TDIFF; 00073 f.name = "coopy_patch_tdiff"; 00074 return f; 00075 } 00076 00077 if (cache.substr(0,15)=="SQLite format 3") { 00078 Format f; 00079 f.id = FORMAT_BOOK_SQLITE; 00080 f.name = "sqlite"; 00081 return f; 00082 } 00083 00084 if (cache.substr(0,4)==" == "||cache.substr(0,3)=="== ") { 00085 Format f; 00086 f.id = FORMAT_BOOK_CSVS; 00087 f.name = "csvs"; 00088 return f; 00089 } 00090 00091 if (cache.substr(0,15).find("|")!=string::npos) { 00092 Format f; 00093 f.id = FORMAT_PATCH_TDIFF; 00094 f.name = "coopy_patch_tdiff"; 00095 return f; 00096 } 00097 00098 if (cache.find("@@@ ")!=string::npos) { 00099 Format f; 00100 f.id = FORMAT_PATCH_TDIFF; 00101 f.name = "coopy_patch_tdiff"; 00102 return f; 00103 } 00104 00105 return Format(); 00106 } 00107