COOPY » Guide
version 0.6.5
|
00001 #include <stdio.h> 00002 #include <getopt.h> 00003 00004 #include <coopy/PolyBook.h> 00005 #include <coopy/NameSniffer.h> 00006 #include <coopy/IndexSniffer.h> 00007 #include <coopy/ShortTextBook.h> 00008 #include <coopy/CsvTextBook.h> 00009 #include <coopy/Dbg.h> 00010 #include <coopy/CompareFlags.h> 00011 #include <coopy/Options.h> 00012 #include <coopy/PoolImpl.h> 00013 #include <coopy/PatchParser.h> 00014 #include <coopy/MergeOutputPool.h> 00015 #include <coopy/Highlighter.h> 00016 #include <coopy/Diff.h> 00017 00018 using namespace coopy::store; 00019 using namespace coopy::cmp; 00020 using namespace coopy::app; 00021 using namespace std; 00022 00023 int main(int argc, char *argv[]) { 00024 Options opt("ssformat"); 00025 int r = opt.apply(argc,argv); 00026 if (r!=0) return r; 00027 00028 bool help = opt.checkBool("help"); 00029 const vector<string>& core = opt.getCore(); 00030 if ((core.size()<1 && (opt.checkString("mode")=="")) || help) { 00031 opt.beginHelp(); 00032 opt.addUsage("ssformat [options] FILE"); 00033 opt.addUsage("ssformat [options] FILE1 FILE2"); 00034 opt.addDescription("Reformat tables/databases/spreadsheets."); 00035 opt.showOptions(OPTION_FOR_FORMAT); 00036 opt.addExample("ssformat numbers.csv numbers_converted.sqlite", 00037 "Convert CSV format table to an Sqlite database table.").require("numbers.csv"); 00038 opt.addExample("ssformat numbers.sqlite numbers_converted.csv", 00039 "Convert Sqlite database table to a CSV format table.").require("numbers.sqlite"); 00040 opt.addExample("ssformat numbers.sqlite -", 00041 "Display contents of an Sqlite database table.").require("numbers.sqlite"); 00042 opt.endHelp(); 00043 return help?0:1; 00044 } 00045 00046 Diff diff; 00047 return diff.apply(opt); 00048 00049 /* 00050 00051 // old implementation of ssformat 00052 // delete when sure nothing of importance was left unreimplemented 00053 00054 Options opt("ssformat"); 00055 int r = opt.apply(argc,argv); 00056 if (r!=0) return r; 00057 00058 bool extractHeader = opt.checkBool("header"); 00059 bool omitHeader = opt.checkBool("omit-header"); 00060 bool extractIndex = opt.checkBool("index"); 00061 bool verbose = opt.checkBool("verbose"); 00062 bool help = opt.checkBool("help"); 00063 string inputFormat = opt.checkString("input-format"); 00064 string outputFormat = opt.checkString("output-format"); 00065 const vector<string>& include_columns = opt.getCompareFlags().include_columns; 00066 const vector<string>& exclude_columns = opt.getCompareFlags().exclude_columns; 00067 bool have_includes = (include_columns.size() > 0); 00068 bool have_excludes = (exclude_columns.size() > 0); 00069 string sheetSelection = ""; 00070 00071 if (opt.getCompareFlags().tables.size()>1) { 00072 fprintf(stderr,"sorry, can only select one table right now\n"); 00073 return 1; 00074 } 00075 if (opt.getCompareFlags().tables.size()==1) { 00076 sheetSelection = *(opt.getCompareFlags().tables.begin()); 00077 } 00078 00079 const vector<string>& core = opt.getCore(); 00080 00081 if (core.size()>2) { 00082 fprintf(stderr, "Options not understood\n"); 00083 return 1; 00084 } 00085 00086 if (core.size()<1||help) { 00087 opt.beginHelp(); 00088 opt.addUsage("ssformat [options] FILE"); 00089 opt.addUsage("ssformat [options] FILE1 FILE2"); 00090 opt.addDescription("Reformat tables/databases/spreadsheets."); 00091 opt.showOptions(OPTION_FOR_FORMAT); 00092 opt.addExample("ssformat numbers.csv numbers_converted.sqlite", 00093 "Convert CSV format table to an Sqlite database table.").require("numbers.csv"); 00094 opt.addExample("ssformat numbers.sqlite numbers_converted.csv", 00095 "Convert Sqlite database table to a CSV format table.").require("numbers.sqlite"); 00096 opt.addExample("ssformat numbers.sqlite -", 00097 "Display contents of an Sqlite database table.").require("numbers.sqlite"); 00098 opt.endHelp(); 00099 return help?0:1; 00100 } 00101 00102 string out_file = "-"; 00103 if (core.size()==2) { 00104 out_file = core[1]; 00105 } 00106 00107 PolyBook src; 00108 if (!src.readAndWillWrite(core[0].c_str(),inputFormat.c_str(), 00109 out_file.c_str(),outputFormat.c_str())) { 00110 fprintf(stderr,"Failed to read %s\n", core[0].c_str()); 00111 return 1; 00112 } 00113 00114 CompareFlags mflags; 00115 std::string meta_file = opt.checkString("meta"); 00116 PoolImpl pool; 00117 mflags.pool = &pool; 00118 if (meta_file!="") { 00119 MergeOutputPool pooler; 00120 pooler.attachBook(src); 00121 pooler.startOutput("-",mflags); 00122 pooler.setFlags(mflags); 00123 PatchParser parser(&pooler,meta_file,mflags); 00124 bool ok = parser.apply(); 00125 pooler.stopOutput("-",mflags); 00126 if (!ok) { 00127 fprintf(stderr,"Failed to read %s\n", meta_file.c_str()); 00128 return 1; 00129 } 00130 } 00131 00132 if (sheetSelection!="") { 00133 CsvTextBook *book = new CsvTextBook(true); 00134 if (book==NULL) { 00135 fprintf(stderr,"Failed to allocate output\n"); 00136 return 1; 00137 } 00138 Property p; 00139 p.put("sheet",sheetSelection.c_str()); 00140 book->copy(src,p); 00141 src.take(book); 00142 00143 //vector<string> names = book->getNames(); 00144 //printf("have %d\n", names.size()); 00145 } 00146 if (extractHeader) { 00147 PolySheet sheet = src.readSheetByIndex(0); 00148 CompareFlags flags; 00149 NameSniffer sniff(sheet,flags); 00150 ShortTextBook *book = new ShortTextBook(); 00151 if (book==NULL) { 00152 fprintf(stderr,"Failed to allocate output\n"); 00153 return 1; 00154 } 00155 for (int i=0; i<sheet.width(); i++) { 00156 book->sheet.addField(sniff.suggestColumnName(i).c_str(),false); 00157 } 00158 book->sheet.addRecord(); 00159 src.take(book); 00160 } 00161 if (extractIndex) { 00162 PolySheet sheet = src.readSheetByIndex(0); 00163 CompareFlags flags; 00164 NameSniffer nsniff(sheet,flags); 00165 IndexSniffer sniff(sheet,flags,nsniff); 00166 vector<int> indexes = sniff.suggestIndexes(); 00167 dbg_printf("Index count %d\n", (int)indexes.size()); 00168 ShortTextBook *book = new ShortTextBook(); 00169 if (book==NULL) { 00170 fprintf(stderr,"Failed to allocate output\n"); 00171 return 1; 00172 } 00173 book->sheet.copy(sheet); 00174 int at = 0; 00175 for (int i=0; i<sheet.width(); i++) { 00176 if (indexes[i]==0) { 00177 book->sheet.deleteColumn(at); 00178 } else { 00179 at++; 00180 } 00181 } 00182 src.take(book); 00183 } 00184 00185 src.setPool(&pool); 00186 00187 if (have_includes||have_excludes) { 00188 printf("INPLACE? %d\n", src.inplace()); 00189 00190 map<string,int> includes; 00191 map<string,int> excludes; 00192 for (int i=0; i<(int)include_columns.size(); i++) { 00193 includes[include_columns[i]] = 1; 00194 } 00195 for (int i=0; i<(int)exclude_columns.size(); i++) { 00196 excludes[exclude_columns[i]] = 1; 00197 } 00198 for (int i=0; i<src.getSheetCount(); i++) { 00199 PolySheet sheet = src.readSheetByIndex(i); 00200 CompareFlags flags; 00201 NameSniffer sniff(sheet,flags); 00202 const std::vector<std::string>& names = sniff.suggestNames(); 00203 int at = 0; 00204 for (int j=0; j<(int)names.size(); j++) { 00205 string name = names[j]; 00206 bool included = includes.find(name) != includes.end(); 00207 bool excluded = excludes.find(name) != excludes.end(); 00208 if (have_excludes && excluded) { 00209 ColumnRef col(at); 00210 sheet.deleteColumn(at); 00211 continue; 00212 } 00213 if (have_includes && !included) { 00214 ColumnRef col(at); 00215 sheet.deleteColumn(at); 00216 continue; 00217 } 00218 at++; 00219 } 00220 } 00221 } 00222 00223 if (omitHeader) { 00224 for (int i=0; i<src.getSheetCount(); i++) { 00225 PolySheet sheet = src.readSheetByIndex(i); 00226 CompareFlags flags; 00227 NameSniffer sniff(sheet,flags); 00228 if (sniff.isEmbedded()) { 00229 RowRef row0(0); 00230 RowRef rowh(sniff.getHeaderHeight()-1); 00231 sheet.deleteRows(row0,rowh); 00232 } 00233 } 00234 } 00235 00236 if (opt.checkBool("paint")) { 00237 if (!src.attach(out_file.c_str())) { 00238 fprintf(stderr,"Failed to attach %s\n", out_file.c_str()); 00239 return 1; 00240 } 00241 Highlighter h; 00242 h.apply(src); 00243 src.flush(); 00244 } else { 00245 if (!src.write(out_file.c_str(),outputFormat.c_str())) { 00246 fprintf(stderr,"Failed to write %s\n", out_file.c_str()); 00247 return 1; 00248 } 00249 } 00250 00251 return 0; 00252 */ 00253 }