📄 xlhtml.c~
字号:
{ int d1, d2; if (sscanf(argv[i] + 4, "%d-%d", &d1, &d2) != 2) { fprintf(stderr, "column range %s not valid, expected -xc:FIRST-LAST\n", argv[i] + 4); display_usage(); } xc1 = (S16)d1; xc2 = (S16)d2; Xtract = 1; if (xc1 > xc2) { fprintf(stderr, "last column must be >= the first\n"); exit(1); } } else if (strncmp(argv[i], "-xp:", 4) == 0) { Xtract = 1; xp = (S16)atoi(&(argv[i][4])); if (xp < 0) { fprintf(stderr, "Negative numbers are illegal.\n"); exit(1); } } else if (strncmp(argv[i], "-xr:", 4) == 0) { char *ptr, *buf; Xtract = 1; buf = strdup(argv[i]); ptr = strrchr(buf, '-'); xr2 = (S16)atoi(ptr+1); *ptr = 0; ptr = strchr(buf, ':'); xr1 = (S16)atoi(ptr+1); free(buf); if (xr1 > xr2) { fprintf(stderr, "row's 2nd digit must be >= the first\n"); exit(1); } } else display_usage(); } if (strcmp(filename, "-v") == 0) { print_version(); exit(0); } if (strcmp(filename, "--version") == 0) { print_version(); exit(0); } if (strcmp(filename, "--help") == 0) display_usage(); if (strcmp(filename, "-?") == 0) display_usage(); } if (Ascii) { /* Disable it if DumpPage or Xtract isn't used... */ if (!(DumpPage||Xtract)) Ascii = 0; } if (Xtract) trim_edges = 0; /* No trimming when extracting... */ if (OutputXML) aggressive = 0; /* Init arrays... */ ws_array = (work_sheet **)malloc(max_worksheets * sizeof(work_sheet*)); for (i=0; i<(int)max_worksheets; i++) ws_array[i] = 0; str_array = (uni_string **)malloc(max_strings*sizeof(uni_string *)); for (i=0; i<(int)max_strings; i++) str_array[i] = 0; font_array = (font_attr **)malloc(max_fonts * sizeof(font_attr *)); f_cnt = (fnt_cnt *)malloc(max_fonts * sizeof(fnt_cnt)); for (i=0; i<(int)max_fonts; i++) { /* I assume these won't fail since we are just starting up... */ font_array[i] = 0; f_cnt[i].name = 0; } xf_array = (xf_attr **)malloc(max_xformats * sizeof(xf_attr *)); for (i=0; i<(int)max_xformats; i++) xf_array[i] = 0; uni_string_clear(&author); uni_string_clear(&default_font); umask(GLOBAL_UMASK);#if defined( __WIN32__ ) || defined( __BORLANDC__ ) { char *ptr = strchr(filename, ':'); if (ptr) { int len; char new_drive[MAXPATH]; fnsplit(filename, new_drive, 0, 0, 0); if (new_drive[0] >= 'a') setdisk(new_drive[0] - 'a'); else setdisk(new_drive[0] - 'A'); ptr++; /* Get past the colon */ len = strlen(ptr); memmove(filename, ptr, len); filename[len] = 0; } }#endif /* If successful, this calls scan_file to extract the work book... */ cfs = cole_mount(filename, &colerrno); if (cfs == NULL) { cole_perror (NULL, colerrno); exit(1); } while (cole_locate_filename (cfs, SectionName[f_ptr], NULL, scan_file, &colerrno)) { if (f_ptr) { /* Two strikes...we're out! */ cole_perror (PRGNAME, colerrno); if (colerrno == COLE_EFILENOTFOUND) fprintf(stderr, "Section: Workbook\n"); break; } else f_ptr++; } if (cole_umount (cfs, &colerrno)) { cole_perror (PRGNAME, colerrno); exit(1); } /* For some reason, this loop core dumps on some */ /* files from Central Europe...so it's commented out for now *//* for (i=0; i<max_strings; i++) { if (str_array[i]) { if (str_array[i]->str) free(str_array[i]->str); free(str_array[i]); } } */ for (i=0; i<(int)max_fonts; i++) { if (font_array[i]) { if (font_array[i]->name.str) free(font_array[i]->name.str); free(font_array[i]); if (f_cnt[i].name) { if (f_cnt[i].name->str) free(f_cnt[i].name->str); free(f_cnt[i].name); } } } free(font_array); free(f_cnt); for (i=0; i<(int)max_worksheets; i++) { if (ws_array[i]) { if (ws_array[i]->ws_title.str) free(ws_array[i]->ws_title.str); if (ws_array[i]->c_array) { for (j=0; j<ws_array[i]->max_rows; j++) { for (k=0; k<ws_array[i]->max_cols; k++) { if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]) { if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->ustr.str) free(ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->ustr.str); if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->ustr.fmt_run) free(ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->ustr.fmt_run); if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->h_link.str) free(ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->h_link.str); free(ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]); } } } free(ws_array[i]->c_array); } free(ws_array[i]); } } for (i=0; i<(int)max_xformats; i++) { if (xf_array[i]) free(xf_array[i]); } free(xf_array); if (numCustomColors) { for (i=0; i<numCustomColors; i++) free(customColors[i]); free(customColors); } if (default_font.str) free(default_font.str); if (author.str) free(author.str); if (title) free(title); if (lastUpdated) free(lastUpdated); return 0;}static void print_version(void){ printf("xlhtml %s\nCopyright (c) 1999-2002, Charles Wyble\n" "Released under GPL.\n", VERSION); exit(1);}void scan_file(COLEDIRENT *cde, void *_info){ U32 count = 0; U16 length=0, target=0, opcode=0, version=0; U8 buf[16]; COLEFILE *cf; COLERRNO err; cf = cole_fopen_direntry(cde, &err); if (cf == 0) { /* error abort processing */ cole_perror (PRGNAME, err); return; } /* Read & process the file... */ while (cole_fread(cf, buf, 1, &err)) { if (count > 3) main_line_processor(opcode, version, count-4, target, buf[0]); else if (count == 0) { /* Init everything */ length = 0; opcode = (U16)buf[0]; target = 80; /* ficticious number */ } else if (count == 1) version = (U16)buf[0]; else if (count == 2) length = (U16)buf[0]; else if (count == 3) { length |= (U16)(buf[0]<<8); target = length; } if (count == (U32)(target+3)) count = 0; else count++; if (MaxColExceeded || MaxRowExceeded || MaxWorksheetsExceeded) break; /* We're outta memory and therefore...done */ } cole_fclose(cf, &err); if (Ascii) { if (DumpPage) { /* Output the XLS Parameters */ int i; printf("There are %d pages total.\n", sheet_count+1); for (i=0; i<=sheet_count; i++) { printf("Page:%d Name:%s MaxRow:%ld MaxCol:%d\n", i, ws_array[i]->ws_title.str ? (char *)ws_array[i]->ws_title.str : "(Unknown Page)", ws_array[i]->biggest_row, ws_array[i]->biggest_col); } } else if (Xtract) OutputPartialTableAscii(); } else { if (DumpPage) { /* Output the XLS Parameters */ int i; output_header(); printf("<p>There are %d pages total.</p>\n", sheet_count+1); for (i=0; i<=sheet_count; i++) { printf("<p>Page:%d Name:%s MaxRow:%ld MaxCol:%d</p>\n", i, ws_array[i]->ws_title.str ? (char *)ws_array[i]->ws_title.str : "(Unknown Page)", ws_array[i]->biggest_row, ws_array[i]->biggest_col); } output_footer(); } else { if( OutputXML ) OutputTableXML(); else OutputTableHTML(); } }}static void OutputPartialTableAscii(void){ int i, j, k; SetupExtraction(); /* Here's where we dump the Html Page out */ for (i=first_sheet; i<=last_sheet; i++) /* For each worksheet */ { if (ws_array[i] == 0) continue; if ((ws_array[i]->biggest_row == -1)||(ws_array[i]->biggest_col == -1)) continue; if (ws_array[i]->c_array == 0) continue; /* Now dump the table */ for (j=ws_array[i]->first_row; j<=ws_array[i]->biggest_row; j++) { for (k=ws_array[i]->first_col; k<=ws_array[i]->biggest_col; k++) { int safe, numeric=0; cell *c = ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]; /* This stuff happens for each cell... */ if (c) { numeric = IsCellNumeric(c); if (!numeric && Csv) printf("\""); safe = IsCellSafe(c); if (c->ustr.str) { if (safe) output_formatted_data(&(c->ustr), xf_array[c->xfmt]->fmt_idx, numeric, IsCellFormula(c)); else OutputString(&(c->ustr)); } else if (!Csv) printf(" "); /* Empty cell... */ } else { /* Empty cell... */ if (!Csv) printf(" "); else printf("\""); } if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]) /* Honor Column spanning ? */ { if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->colspan != 0) k += ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->colspan-1; } if (!numeric && Csv) printf("\""); if (Csv && (k < ws_array[i]->biggest_col)) { /* big cheat here: quoting everything! */ putchar(','); /* Csv Cell Separator */ } else { if (( !Csv )&&( k != ws_array[i]->biggest_col )) putchar('\t'); /* Ascii Cell Separator */ } } if (Csv) printf("\r\n"); else putchar(0x0A); /* Row Separator */ } if (!Csv) printf("\n\n"); /* End of Table 2 LF-CR */ }}static void do_cr(void){ if (!aggressive) putchar('\n');}static void OutputTableHTML(void){ int i, j, k; output_header(); if (center_tables) { printf("<CENTER>"); do_cr(); } SetupExtraction(); /* Here's where we dump the Html Page out */ for (i=first_sheet; i<=last_sheet; i++) /* For each worksheet */ { update_default_font(i); if (ws_array[i] == 0) continue; if ((ws_array[i]->biggest_row == -1)||(ws_array[i]->biggest_col == -1)) continue; if (ws_array[i]->c_array == 0) continue; trim_sheet_edges(i); /* Print its name */ if (next_ws_title > 0) { if (ws_array[i]->ws_title.str) { printf("<H1><CENTER>"); OutputString(&ws_array[i]->ws_title); printf("</CENTER></H1><br>"); do_cr(); } else { printf("<H1><CENTER>(Unknown Page)</CENTER></H1><br>"); do_cr(); } } /* Now dump the table */ printf("<FONT FACE=\""); OutputString(&default_font); if (default_fontsize != 3) printf("\" SIZE=\"%d", default_fontsize); printf("\">"); do_cr(); printf("<TABLE BORDER=\"1\" CELLSPACING=\"2\">"); do_cr(); for (j=ws_array[i]->first_row; j<=ws_array[i]->biggest_row; j++) { update_default_alignment(i, j); printf("<TR"); if (null_string((U8 *)default_alignment)) printf(">"); else { if (strcmp(default_alignment, "left") != 0) printf(" ALIGN=\"%s\"", default_alignment); if (!aggressive) printf(" VALIGN=\"bottom\">\n"); else printf(">"); } for (k=ws_array[i]->first_col; k<=ws_array[i]->biggest_col; k++) { output_cell(ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k],0); /* This stuff happens for each cell... */ if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]) { if (ws_array[i]->c_array[(j*ws_array[i]->max_cols)+k]->colspan != 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -