COOPY » Guide
version 0.6.5
|
00001 #include <stdio.h> 00002 #include <string.h> 00003 00004 #include <coopy/Dbg.h> 00005 #include <coopy/CsvSheet.h> 00006 #include <coopy/GnumericSheet.h> 00007 #include <coopy/Stringer.h> 00008 00009 extern "C" { 00010 #include "coopy/gnumeric_link.h" 00011 } 00012 00013 using namespace coopy::store; 00014 using namespace std; 00015 00016 // This code is just used in regression tests, so it is ok 00017 // to be brain-dead-ed-ly inefficient 00018 string ingest(const char *fname, string def) { 00019 FILE *fin1 = fopen(fname,"r"); 00020 if (fin1==NULL) { 00021 fprintf(stderr,"Failed to open %s\n", fname); 00022 return def; 00023 } 00024 int ch = 0; 00025 string result = ""; 00026 while (ch!=-1) { 00027 ch = fgetc(fin1); 00028 if (ch!=-1 && ch!='\r') { 00029 result += ch; 00030 } 00031 } 00032 fclose(fin1); 00033 return result; 00034 } 00035 00036 int main(int argc, char *argv[]) { 00037 bool compare = false; 00038 while (argc>1) { 00039 if (string(argv[1])=="--verbose") { 00040 coopy_set_verbose(true); 00041 argc--; 00042 argv++; 00043 } 00044 else if (string(argv[1])=="--compare") { 00045 compare = true; 00046 argc--; 00047 argv++; 00048 } else { 00049 break; 00050 } 00051 } 00052 if (argc<3+compare?1:0) { 00053 printf("Call as:\n"); 00054 printf(" ss2html [--verbose] src.xls dest.html\n"); 00055 printf(" ss2html [--verbose] [--compare] src.xls dest.html ref.html\n"); 00056 return 1; 00057 } 00058 const char *template_name = argv[1]; 00059 const char *target_name = argv[2]; 00060 00061 gnumeric_init(); 00062 00063 GnumericWorkbookPtr book = gnumeric_load(template_name); 00064 00065 if (!book) exit(1); 00066 00067 FILE *fout = fopen(target_name,"w"); 00068 if (!fout) exit(1); 00069 00070 int len = gnumeric_get_sheet_count(book); 00071 for (int k=0; k<len; k++) { 00072 GnumericSheetPtr s = gnumeric_get_sheet(book,k); 00073 if (!s) exit(1); 00074 fprintf(fout,"<h2>%s</h2>\n",gnumeric_sheet_get_name(s)); 00075 int w, h; 00076 gnumeric_sheet_get_size(s,&w,&h); 00077 fprintf(fout,"<table>\n"); 00078 for (int y=0; y<h; y++) { 00079 fprintf(fout," <tr>"); 00080 for (int x=0; x<w; x++) { 00081 fprintf(fout,"<td>"); 00082 int r, g, b; 00083 gnumeric_sheet_get_cell_font_color(s,x,y,&r,&g,&b); 00084 bool color = false; 00085 if (r!=0||g!=0||b!=0) { 00086 color = true; 00087 fprintf(fout,"<font color='#%02x%02x%02x'>", 00088 r,g,b); 00089 } 00090 char *str = gnumeric_sheet_get_cell_as_string(s,x,y); 00091 if (str) { 00092 fprintf(fout,"%s",str); 00093 gnumeric_free_string(str); 00094 } else { 00095 fprintf(fout,""); 00096 } 00097 if (color) { 00098 fprintf(fout,"</font>"); 00099 } 00100 fprintf(fout,"</td>"); 00101 } 00102 fprintf(fout,"</tr>\n"); 00103 } 00104 fprintf(fout,"</table>\n"); 00105 } 00106 00107 fclose(fout); 00108 fout = NULL; 00109 00110 // Exporter is insufficiently stable for using in tests 00111 // gnumeric_save(book,target_name,"Gnumeric_html:html40frag"); 00112 00113 00114 gnumeric_free(book); 00115 gnumeric_fini(); 00116 00117 if (compare) { 00118 string str1 = ingest(target_name,"1"); 00119 string str2 = ingest(argv[3],"2"); 00120 return (str1==str2)?0:1; 00121 } 00122 00123 return 0; 00124 } 00125