diff options
author | andmarti1424 <scim.spreadsheet@gmail.com> | 2016-03-20 13:26:33 -0300 |
---|---|---|
committer | andmarti1424 <scim.spreadsheet@gmail.com> | 2016-03-20 13:26:33 -0300 |
commit | 1733b6403bf1cfbd80b82ac2e55f41b8ad5be44d (patch) | |
tree | 41380b1276f16da4c790954463816d2fead957f9 | |
parent | a648ab46609f3e2a25d7bec5cd503a87ef54a7f3 (diff) |
export to plain text
-rw-r--r-- | src/cmds_command.c | 8 | ||||
-rw-r--r-- | src/doc | 9 | ||||
-rw-r--r-- | src/file.c | 90 | ||||
-rw-r--r-- | src/file.h | 1 | ||||
-rw-r--r-- | src/screen.c | 65 | ||||
-rw-r--r-- | src/screen.h | 1 |
6 files changed, 136 insertions, 38 deletions
diff --git a/src/cmds_command.c b/src/cmds_command.c index d56a82f..6036c00 100644 --- a/src/cmds_command.c +++ b/src/cmds_command.c @@ -44,8 +44,10 @@ static char * valid_commands[] = { "color", "e csv", "e tab", +"e txt", "e! csv", "e! tab", +"e! txt", "datefmt", "delfilter", "delfilters", @@ -533,9 +535,11 @@ void do_commandmode(struct block * sb) { } else if ( ! strncmp(inputline, "e csv" , 5) || - ! strncmp(inputline, "e! tab" , 6) || ! strncmp(inputline, "e! csv" , 6) || - ! strncmp(inputline, "e tab" , 5) ) { + ! strncmp(inputline, "e tab" , 5) || + ! strncmp(inputline, "e! tab" , 6) || + ! strncmp(inputline, "e txt" , 5) || + ! strncmp(inputline, "e! txt" , 6) ) { do_export( p == -1 ? 0 : sr->tlrow, p == -1 ? 0 : sr->tlcol, p == -1 ? maxrow : sr->brrow, p == -1 ? maxcol : sr->brcol); @@ -278,6 +278,9 @@ Commands for handling cell content: the name of the file that is created comes from the current open spreadsheet. if a range is selected, only the that range is exported. + :e txt export current spreadsheet to plain text. + if a range is selected, only the that range is exported. + :e csv name export current spreadsheet to csv file. 'name' is the name of the file to be generated @@ -291,6 +294,12 @@ Commands for handling cell content: same as previous, but forcing the rewrite of file 'name' if it exists. NOTE: if a range is selected, its content are exported. if not, the entire spreadsheet is exported + :e txt name + export current spreadsheet to plain text. 'name' is the name of the file to be generated + + :e! txt name + same as previous, but forcing the rewrite of file 'name' if it exists. + :i csv name import a csv delimited file called 'name' to SC-IM. @@ -833,7 +833,7 @@ int import_csv(char * fname, char d) { return 0; } -// Export: to CSV and TAB +// Export to CSV, TAB or plain TXT void do_export(int r0, int c0, int rn, int cn) { int force_rewrite = 0; char type_export[4] = ""; @@ -849,12 +849,14 @@ void do_export(int r0, int c0, int rn, int cn) { strcpy(type_export, "csv"); } else if (str_in_str(linea, "tab") == 0) { strcpy(type_export, "tab"); + } else if (str_in_str(linea, "txt") == 0) { + strcpy(type_export, "txt"); } // Get route and file name to write. // Use parameter if any. - if (strlen(linea) > 4) { // 'csv ' - del_range_chars(linea, 0, 3); // Remove 'csv' + if (strlen(linea) > 4) { // ex. 'csv ' + del_range_chars(linea, 0, 3); // remove 'csv' strcpy(ruta, linea); // Use curfile name and '.csv' o '.tab' extension @@ -878,16 +880,90 @@ void do_export(int r0, int c0, int rn, int cn) { // Call export routines if (strcmp(type_export, "csv") == 0) { export_delim(ruta, ',', r0, c0, rn, cn); - } if (strcmp(type_export, "tab") == 0) { + } else if (strcmp(type_export, "tab") == 0) { export_delim(ruta, '\t', r0, c0, rn, cn); + } else if (strcmp(type_export, "txt") == 0) { + export_plain(ruta, r0, c0, rn, cn); } } -// FNAME: route and name of file +// fname is the path and name of file +void export_plain(char * fname, int r0, int c0, int rn, int cn) { + FILE * f; + int row, col; + register struct ent ** pp; + int pid; + char out[FBUFLEN] = ""; + + scinfo("Writing file \"%s\"...", fname); + + if ((f = openfile(fname, &pid, NULL)) == (FILE *)0) { + scerror ("Can't create file \"%s\"", fname); + return; + } + struct ent * ent = go_end(); + if (rn > ent->row) rn = ent->row; + + for (row = r0; row <= rn; row++) { + // ignore hidden rows + if (row_hidden[row]) continue; + + for (pp = ATBL(tbl, row, col = c0); col <= cn; col++, pp++) { + // ignore hidden cols + if (col_hidden[col]) continue; + + if (*pp) { + if ((*pp)->flags & is_valid) { + if ((*pp)->cellerror) { + (void) fprintf (f, "%*s", fwidth[col], ((*pp)->cellerror == CELLERROR ? "ERROR" : "INVALID")); + } else if ((*pp)->format) { + char field[FBUFLEN]; + if (*((*pp)->format) == 'd') { // Date format + time_t v = (time_t) ((*pp)->v); + strftime(field, sizeof(field), ((*pp)->format)+1, localtime(&v)); + } else { // Numeric format + format((*pp)->format, precision[col], (*pp)->v, field, sizeof(field)); + } + (void) fprintf (f, "%*s", fwidth[col], field); + } else { //eng number format + char field[FBUFLEN] = ""; + (void) engformat(realfmt[col], fwidth[col], precision[col], (*pp)->v, field, sizeof(field)); + (void) fprintf (f, "%*s", fwidth[col], field); + } + } + + else if ((*pp)->label) { + out[0] = '\0'; + pad_text(out, pp, row, col); + //scdebug("_%s_", out); + (void) fprintf (f, "%s", out); + } else { + (void) fprintf (f, "%*s", fwidth[col], " "); + } + } else { // ! *pp + (void) fprintf (f, "%*s", fwidth[col], " "); + } + + } + (void) fprintf(f,"\n"); + } + closefile(f, pid, 0); + + if (! pid) { + scinfo("File \"%s\" written", fname); + } + +} + + + + + +// fname is the path and name of file void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn) { - FILE *f; + FILE * f; int row, col; - register struct ent **pp; + register struct ent ** pp; int pid; scinfo("Writing file \"%s\"...", fname); @@ -14,4 +14,5 @@ void print_options(FILE *f); int import_csv(char * fname, char d); void do_export(int r0, int c0, int rn, int cn); void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn); +void export_plain(char * fname, int r0, int c0, int rn, int cn); void unspecial(FILE *f, char *str, int delim); diff --git a/src/screen.c b/src/screen.c index a5853c4..ea29a94 100644 --- a/src/screen.c +++ b/src/screen.c @@ -730,10 +730,11 @@ int scstrlen(char * s) { return len; } -void show_text_content_of_cell(WINDOW * win, struct ent ** p, int row, int col, int r, int c) { - +// function to arrange padding and format of ent with text +// out would be the value to be printed with padding and format +void pad_text(char * out, struct ent ** p, int row, int col) { char value[FBUFLEN]; // the value to be printed without padding - char field[FBUFLEN] = ""; // the value with padding and alignment + //char field[FBUFLEN] = ""; // the value with padding int col_width = fwidth[col]; int flen; // current length of field int left; @@ -748,65 +749,71 @@ void show_text_content_of_cell(WINDOW * win, struct ent ** p, int row, int col, // If there isn't enough space on the screen if (str_len > col_width) { - sprintf(field, "%0*d", col_width, 0); - subst(field, '0', '*'); + //sprintf(out, "%0*d", col_width, 0); + //subst(out, '0', '*'); - // Color selected cell + /* Color selected cell if ((currow == row) && (curcol == col)) { #ifdef USECOLORS if (has_colors()) set_ucolor(win, &ucolors[CELL_SELECTION_SC]); #else wattron(win, A_REVERSE); #endif - } - strncpy(field, value, col_width); - field[col_width]='\0'; - mvwprintw(win, r, c, "%s", field); + }*/ + strncpy(out, value, col_width); + out[col_width-1]='*'; + out[col_width]='\0'; + //mvwprintw(win, r, c, "%s", out); + + //char ex[str_len+1]; + //strcpy(ex, value); + //del_range_chars(ex, 0, col_width-1); + // + // #ifdef USECOLORS + // if (has_colors()) set_ucolor(win, &ucolors[STRG]); + // #else + // wattroff(win, A_REVERSE); + // #endif + //mvwprintw(win, r, c + col_width, "%s", ex); + //wclrtoeol(win); - char ex[str_len+1]; - strcpy(ex, value); - del_range_chars(ex, 0, col_width-1); - #ifdef USECOLORS - if (has_colors()) set_ucolor(win, &ucolors[STRG]); - #else - wattroff(win, A_REVERSE); - #endif - mvwprintw(win, r, c + col_width, "%s", ex); - wclrtoeol(win); return; // Left } else if ( (*p)->label && (*p)->flags & is_leftflush ) { - strcpy(field, value); + strcpy(out, value); left = col_width - str_len; left = left < 0 ? 0 : left; flen = str_len; - while (left-- && flen++) add_char(field, ' ', strlen(field)); + while (left-- && flen++) add_char(out, ' ', strlen(out)); // Centered } else if ( (*p)->label && (*p)->flags & is_label) { left = (col_width - str_len ) / 2; left = left < 0 ? 0 : left; flen = 0; - while (left-- && ++flen) add_char(field, ' ', 0); - strcat(field, value); + while (left-- && ++flen) add_char(out, ' ', 0); + strcat(out, value); flen += str_len; left = (col_width - flen); left = left < 0 ? 0 : left; - while (left-- && ++flen) add_char(field, ' ', strlen(field)); + while (left-- && ++flen) add_char(out, ' ', strlen(out)); // Right } else if ( (*p)->label || res == 0) { left = col_width - str_len; left = left < 0 ? 0 : left; flen = 0; - while (left-- && ++flen) add_char(field, ' ', 0); - strcat(field, value); + while (left-- && ++flen) add_char(out, ' ', 0); + strcat(out, value); } +} - mvwprintw(win, r, c, "%s", field); +void show_text_content_of_cell(WINDOW * win, struct ent ** p, int row, int col, int r, int c) { + char out[FBUFLEN] = ""; + pad_text(out, p, row, col); + mvwprintw(win, r, c, "%s", out); wclrtoeol(win); - return; } diff --git a/src/screen.h b/src/screen.h index 8b37bf4..7b91034 100644 --- a/src/screen.h +++ b/src/screen.h @@ -16,6 +16,7 @@ void show_sc_row_headings(WINDOW * win, int mxrow); void show_sc_col_headings(WINDOW * win, int mxcol, int mxrow); void show_celldetails(WINDOW * win); +void pad_text(char * out, struct ent ** p, int row, int col); void show_text_content_of_cell(WINDOW * win, struct ent ** p, int row, int col, int r, int c); void show_numeric_content_of_cell(WINDOW * win, struct ent ** p, int col, int r, int c); int get_formated_value(struct ent ** p, int col, char * value); |