COOPY » Guide
version 0.6.5
|
00001 00002 #include <stdio.h> 00003 #include <getopt.h> 00004 00005 #include <coopy/PolyBook.h> 00006 #include <coopy/Dbg.h> 00007 #include <coopy/FoldTool.h> 00008 #include <coopy/CsvTextBook.h> 00009 00010 using namespace coopy::store; 00011 using namespace coopy::fold; 00012 using namespace std; 00013 00014 int main(int argc, char *argv[]) { 00015 std::string recipe_file = ""; 00016 std::string table_name = ""; 00017 bool verbose = false; 00018 bool unfold = false; 00019 bool fold = false; 00020 FoldOptions options; 00021 00022 while (true) { 00023 int option_index = 0; 00024 static struct option long_options[] = { 00025 {"fold", 0, 0, 'f'}, 00026 {"unfold", 0, 0, 'u'}, 00027 {"verbose", 0, 0, 'v'}, 00028 00029 {"recipe", 1, 0, 'r'}, 00030 {"table", 1, 0, 't'}, 00031 00032 {"drop", 1, 0, 'd'}, 00033 00034 {0, 0, 0, 0} 00035 }; 00036 00037 int c = getopt_long(argc, argv, "", 00038 long_options, &option_index); 00039 if (c==-1) break; 00040 switch (c) { 00041 case 'v': 00042 verbose = true; 00043 break; 00044 case 'f': 00045 fold = true; 00046 break; 00047 case 'u': 00048 unfold = true; 00049 break; 00050 00051 case 'r': 00052 recipe_file = optarg; 00053 break; 00054 case 't': 00055 table_name = optarg; 00056 break; 00057 00058 case 'd': 00059 options.drops.insert(optarg); 00060 break; 00061 00062 default: 00063 fprintf(stderr, "Unrecognized option\n"); 00064 return 1; 00065 } 00066 } 00067 00068 if (optind<argc-3) { 00069 fprintf(stderr, "Options not understood\n"); 00070 return 1; 00071 } 00072 argc -= optind; 00073 argv += optind; 00074 00075 if (argc<2) { 00076 printf("Fold or flatten tables:\n"); 00077 printf(" ssfold [--table TABLE] [--recipe RECIPE] [--unfold|--fold] SOURCE DESTINATION\n"); 00078 printf(" ssfold --recipe folds.csv --table main --unfold tables.sqlite sheet.csv\n"); 00079 printf(" ssfold --drop length bridges.csv partial.csv # removes length column\n"); 00080 return 1; 00081 } 00082 00083 if (verbose) { 00084 coopy_set_verbose(verbose); 00085 } 00086 00087 if (fold&&unfold) { 00088 fprintf(stderr,"Cannot both fold and unfold\n"); 00089 return 1; 00090 } 00091 fold = !unfold; 00092 00093 PolyBook local; 00094 PolyBook remote; 00095 PolyBook recipe; 00096 00097 if (!local.read(argv[0])) { 00098 fprintf(stderr,"Failed to read %s\n", argv[0]); 00099 return 1; 00100 } 00101 if (recipe_file!="") { 00102 if (!recipe.read(recipe_file.c_str())) { 00103 fprintf(stderr,"Failed to read %s\n", recipe_file.c_str()); 00104 return 1; 00105 } 00106 } 00107 if (!remote.attach(argv[1])) { 00108 fprintf(stderr,"Failed to attach %s\n", argv[1]); 00109 return 1; 00110 } 00111 00112 dbg_printf("ssfold: %s\n", fold?"folding":"unfolding"); 00113 options.tableName = table_name; 00114 options.fold = fold; 00115 options.recipe = recipe; 00116 FoldTool tool; 00117 00118 bool ok = false; 00119 if (fold) { 00120 ok = tool.fold(local,remote,options); 00121 } else { 00122 ok = tool.unfold(local,remote,options); 00123 } 00124 if (!ok) { 00125 fprintf(stderr,"Operation failed.\n"); 00126 return 1; 00127 } 00128 //if (!CsvTextBook::write(argv[1],&remote,true)) { 00129 if (!remote.flush()) { 00130 fprintf(stderr,"Failed to write %s\n", argv[1]); 00131 return 1; 00132 } 00133 return 0; 00134 } 00135