/* xls2xml: Converts from Microsoft Excel files to XML. Copyright 1999 Roberto Arturo Tena Sanchez This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Roberto Arturo Tena Sanchez */ #include #include #include /* To compile this file outside xls2xml source tree, you must include here instead */ #include static xmlDocPtr xml_doc; static void print_systax (void); void print_syntax (void) { fprintf(stderr,"xls2xml: converts from xls files to xml\n"); fprintf(stderr,"xls2xml xlsFILE [xmlFILE] [--refmode=(A1|R1C1|default)] "); /* fprintf(stderr,"[--level=(all|normal|brief)]\n"); */ fprintf(stderr,"[--level=(all|normal)]\n"); fprintf(stderr,"\txlsFILE - Excel file\n"); fprintf(stderr,"\txmlFILE - Output file\n"); fprintf(stderr,"\trefmode - Cell reference mode\n"); fprintf(stderr,"\tlevel - How much will be extracted\n"); fprintf(stderr,"\n"); } int main (int argc, char **argv) { FILE *xmlFile = NULL; int refmode = REFMODE_AS_IS, level = EXTRACT_NORMAL; int has_xml = 1, i; int result; COLEFILE *colefile; COLEFS *colefilesystem; COLERRNO colerrno; if (argc < 2) { print_syntax (); exit (1); } /* Just need to know if there are xmlFile declared in command line */ if ( (argc > 2) && strncmp (argv[2],"--",2) ) { } else has_xml = 0; for (i = 2 + has_xml; i < argc; i++) { if (strcmp (argv[i], "--refmode=") >= 0) { switch (argv[i][10]) { case 'A': refmode = REFMODE_A1; break; case 'R': refmode = REFMODE_R1C1; break; case 'd': refmode = REFMODE_AS_IS; break; default: print_syntax (); exit (1); } continue; } if (strcmp (argv[i], "--level=") >= 0) { switch (argv[i][8]) { case 'a': level = EXTRACT_ALL; break; case 'n': level = EXTRACT_NORMAL; break; /* FIXME: not yet implemented brief */ /* case 'b': level = EXTRACT_BRIEF; break; */ default: print_syntax (); exit (1); } continue; } print_syntax (); exit (1); } colefilesystem = cole_mount (argv[1], &colerrno); if (colefilesystem == NULL) { switch (colerrno) { case COLE_ENOFILESYSTEM: case COLE_EINVALIDFILESYSTEM: fprintf (stderr, "xls2xml: The file is not a Excel file.\n"); break; default: cole_perror ("xls2xml", colerrno); break; } exit (1); } /* open `/Workbook' (BIFF8) stream */ colefile = cole_fopen (colefilesystem, "/Workbook", &colerrno); if (colefile == NULL) { /* open `/Book' (pre BIFF8) stream */ colefile = cole_fopen (colefilesystem, "/Book", &colerrno); if (colefile == NULL) { fprintf (stderr, "xls2xml: The file is not a Excel file.\n"); cole_umount (colefilesystem, NULL); return 1; } } result = xls2xml (colefile, &xml_doc, level, refmode); /* switch (result) { case 0: /* OK / break; case 10: /* Error allocating memory, there's no more memory / fprintf (stderr, "xls2xml: Error allocating memory: no more memory\n"); break; case 5: /* Error reading from stream file, means xls file has a faulty format / case 15: /* value readed from xls file is unknown by now, thus xls is more advanced or xls file has a faulty format / fprintf (stderr, "xls2xml: %s have a unknown Excel format (may be it's newest or it's broken)\n", argv[1]); break; case 11: /* Error reading streams files, can use perror / case 17: /* xls file is a Excel file, but it's format is unknown, too old / fprintf (stderr, "xls2xml: %s is a valid Excel file, but its format is too old to know\n", argv[1]); break; case 18: /* xls file is password protected so can't access it / fprintf (stderr, "xls2xml: %s is password protected, save it without password and try again\n", argv[1]); break; case 19: /* internal inconsistences between structures (aka, bug) / /* the bug must be reported using report_bug where produced / /* and should be tested using assert_return where produced / /* so we don't have to do anything here / break; default: /* Impossible / report_bug (xls2xml); break; } */ if (result) { /* freeing xml_doc it's NOT necessary, since it's done in process_main_stream() when anything goes wrong */ cole_fclose (colefile, NULL); cole_umount (colefilesystem, NULL); exit (1); } if (has_xml) { /* FIXME: When accepting compression flags, open with wb if compressed, and if not compressed open with wt */ xmlFile = fopen (argv[2], "wt"); if (xmlFile == NULL) { fprintf (stderr, "xls2xml: "); perror (argv[2]); } else { xmlDocDump (xmlFile, xml_doc); fclose (xmlFile); } } else xmlDocDump (stdout, xml_doc); erase_xml_doc (xml_doc); if (cole_fclose (colefile, &colerrno)) { cole_perror ("xls2xml", colerrno); cole_umount (colefilesystem, NULL); exit (1); } if (cole_umount (colefilesystem, &colerrno)) { cole_perror ("xls2xml", colerrno); exit (1); } exit (0); }