/[csv2latex]/csv2latex.c
ViewVC logotype

Diff of /csv2latex.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 49, Mon Nov 28 20:57:57 2016 UTC revision 55 by ben, Sun Jul 1 01:46:58 2018 UTC
# Line 34  Line 34 
34  #include <ctype.h>  #include <ctype.h>
35  #include "version.h"  #include "version.h"
36    
37    #define PTOMM(p) (0.3528 * (p)) /* pt to mm */
38    #define HEIGHT PTOMM(592) /* \textheight default in pt */
39    #define INTER 1.0 /* default interline FIXME */
40    
41  typedef struct {  typedef struct {
42          char* tab;      /* actual escapes */          char* tab;      /* actual escapes */
43          int size;       /* escape tab len */          int size;       /* escape tab len */
# Line 55  typedef struct { Line 59  typedef struct {
59          unsigned int repeat;    /* repeat table headers for each LaTeX table section or not */          unsigned int repeat;    /* repeat table headers for each LaTeX table section or not */
60          unsigned int vlines;    /* insert vertical lines between columns or not */          unsigned int vlines;    /* insert vertical lines between columns or not */
61          unsigned int hlines;    /* insert horizontal lines between rows or not */          unsigned int hlines;    /* insert horizontal lines between rows or not */
62            unsigned int paper;     /* output paper type */
63            unsigned int font;      /* expected font size */
64            unsigned int land;      /* landscape mode */
65          char* clrrow;   /* row graylevel (from 0 to 1) */          char* clrrow;   /* row graylevel (from 0 to 1) */
66          texcape* tex;   /* TeX escapes */          texcape* tex;   /* TeX escapes */
67  } config;  } config;
# Line 70  void rtfm(char* prog) { Line 77  void rtfm(char* prog) {
77     printf("     [--guess] (CSV) guess separator and block |\n"     printf("     [--guess] (CSV) guess separator and block |\n"
78                  "       [--separator <(c)omma|(s)emicolon|(t)ab|s(p)ace|co(l)on>] (CSV's comma)\n"                  "       [--separator <(c)omma|(s)emicolon|(t)ab|s(p)ace|co(l)on>] (CSV's comma)\n"
79                  "       [--block <(q)uote|(d)ouble|(n)one>] (CSV) block delimiter (e.g: none)\n");                  "       [--block <(q)uote|(d)ouble|(n)one>] (CSV) block delimiter (e.g: none)\n");
80     printf("     [--lines n] (LaTeX) rows per table: useful for long tabulars\n");     printf("     [--lines n] (LaTeX) rows per table: useful for long tabulars |\n");
81       printf("             [--font n] font size used (in pt)\n");
82     printf("     [--position <l|c|r>] (LaTeX) text align in cells\n");     printf("     [--position <l|c|r>] (LaTeX) text align in cells\n");
83     printf("     [--colorrows graylevel] (LaTeX) alternate gray rows (e.g: 0.75)\n");     printf("     [--colorrows graylevel] (LaTeX) alternate gray rows (e.g: 0.75)\n");
84     printf("     [--reduce level] (LaTeX) reduce table size (e.g: 1)\n");     printf("     [--reduce level] (LaTeX) reduce table size (e.g: 1)\n");
85       printf("     [--landscape] (LaTeX) use landscape mode\n");
86     printf("     [--repeatheader] (LaTeX) repeat table header (for long tables)\n");     printf("     [--repeatheader] (LaTeX) repeat table header (for long tables)\n");
87     printf("     [--nohlines] (LaTeX) don't put hline between table rows\n");     printf("     [--nohlines] (LaTeX) don't put hline between table rows\n");
88     printf("     [--novlines] (LaTeX) don't put vline between columns\n");     printf("     [--novlines] (LaTeX) don't put vline between columns\n");
# Line 108  config* parseOptions (config* conf, int Line 117  config* parseOptions (config* conf, int
117                  {"repeatheader", 0, NULL, 'e'},                  {"repeatheader", 0, NULL, 'e'},
118                  {"novlines",     0, NULL, 'y'},                  {"novlines",     0, NULL, 'y'},
119                  {"nohlines",     0, NULL, 'z'},                  {"nohlines",     0, NULL, 'z'},
120                    {"landscape",    0, NULL, 'a'},
121                    {"font",         1, NULL, 'f'},
122                  {NULL, 0, NULL,  0} /* marks end-of-list */                  {NULL, 0, NULL,  0} /* marks end-of-list */
123          };          };
124  #endif  #endif
125  #if defined USE_GETOPT  #if defined USE_GETOPT
126          while ((opt = getopt (argc, argv, "hvgnxteyz?b:l:p:s:c:r:")) != EOF) {          while ((opt = getopt (argc, argv, "hvgnxteyza?b:l:p:s:c:r:f:")) != EOF) {
127  #else  #else
128          while ((opt = getopt_long (argc, argv, "hvgnxteyz?b:l:p:s:c:r:", long_options, &longopt_index)) > 0) {          while ((opt = getopt_long (argc, argv, "hvgnxteyza?b:l:p:s:c:r:f:", long_options, &longopt_index)) > 0) {
129  #endif  #endif
130                  switch (opt) {                  switch (opt) {
131                          case '?':                          case '?':
# Line 200  config* parseOptions (config* conf, int Line 211  config* parseOptions (config* conf, int
211                          case 'z': /*don't draw hlines between rows*/                          case 'z': /*don't draw hlines between rows*/
212                                          conf->hlines = 0;                                          conf->hlines = 0;
213                                          break;                                          break;
214                            case 'a': /*landscape*/
215                                            conf->land = 1;
216                                            break;
217                            case 'f': /*output font size*/
218                                            if (isdigit(optarg[0])) {
219                                                    conf->font = atoi(optarg);
220                                            } else {
221                                                    fprintf(stderr,
222                                                    "option \"font\" need a positive integer value\n");
223                                                    exit(EXIT_FAILURE);
224                                            }
225                                            break;
226                  }                  }
227          }          }
228          return conf;          return conf;
229  }  }
230    
231  int guessCSV(config* conf, FILE* in) {  int guessCSV(config* conf, FILE* in) {
232  /* guess the block delimiter and the csv separator */  /* guess the block delimiter and the csv separator */
233          int token;          int token;
# Line 313  void getMaximums(config* conf, FILE* in) Line 337  void getMaximums(config* conf, FILE* in)
337          return;          return;
338  }  }
339    
340    unsigned int guessLinesPerPage(unsigned int font, double height) {
341            return  height / (PTOMM(font) + INTER);
342    }
343    
344    
345  void doTeXsub(config* conf, char newsep, FILE* in, FILE* out) {  void doTeXsub(config* conf, char newsep, FILE* in, FILE* out) {
346  /* substitutes CSV sep by LaTeX newsep and some TeX code */  /* substitutes CSV sep by LaTeX newsep and some TeX code */
347          int token = 0;          int token = 0;
# Line 330  void doTeXsub(config* conf, char newsep, Line 359  void doTeXsub(config* conf, char newsep,
359    
360          max = numcols = conf->cols;          max = numcols = conf->cols;
361          csvrows = conf->rows;          csvrows = conf->rows;
362    
363            /* optimize a bit lines per tabular */
364            if (!conf->longtable && !conf->lines) {
365                    conf->lines = guessLinesPerPage(conf->font ? conf->font : 10, conf->land ? (HEIGHT / 1.414) : HEIGHT);
366                    fprintf(stderr, "guessed %d lines per page\n", conf->lines);
367            }
368    
369          /* choose infinity when conf->lines is 0 */          /* choose infinity when conf->lines is 0 */
370          lines = (conf->lines) ? conf->lines : MAXUINT;          lines = (conf->lines) ? conf->lines : MAXUINT;
371    
# Line 496  void doTeXdoc(config* conf, FILE* in, FI Line 532  void doTeXdoc(config* conf, FILE* in, FI
532    
533          numcols = maxcols = conf->cols;          numcols = maxcols = conf->cols;
534          if (conf->header) {          if (conf->header) {
535                  fprintf(out, "\\documentclass[a4paper]{article}\n");                  if (conf->land) {
536                            if (conf->font)
537                                    fprintf(out, "\\documentclass[%dpt,a4paper,landscape]{article}\n", conf->font);
538                            else {
539                                    fprintf(out, "\\documentclass[a4paper,landscape]{article}\n");
540                                    conf->font = 10; /* pt */
541                            }
542                    } else {
543                            if (conf->font)
544                                    fprintf(out, "\\documentclass[%dpt,a4paper]{article}\n", conf->font);
545                            else {
546                                    fprintf(out, "\\documentclass[a4paper]{article}\n");
547                                    conf->font = 10; /* pt */
548                            }
549                    }
550                  fprintf(out, "\\usepackage[T1]{fontenc}\n");                  fprintf(out, "\\usepackage[T1]{fontenc}\n");
551                  fprintf(out, "\\usepackage[utf8]{inputenc}\n");                  fprintf(out, "\\usepackage[utf8]{inputenc}\n");
552                  if (conf->red) {                  if (conf->red) {
# Line 579  int main (int argc, char **argv) { Line 629  int main (int argc, char **argv) {
629          conf->repeat = 0;      /* default; do not repeat the header row */          conf->repeat = 0;      /* default; do not repeat the header row */
630          conf->vlines = 1;      /* default; draw lines between columns */          conf->vlines = 1;      /* default; draw lines between columns */
631          conf->hlines = 1;      /* default; draw lines between rows */          conf->hlines = 1;      /* default; draw lines between rows */
632            conf->font = 0;        /* default to LaTeX defaults */
633            conf->land = 0;   /* default to LaTeX defaults */
634    
635          /* TeX charaters to escape */          /* TeX charaters to escape */
636          conf->tex = (texcape*)malloc(sizeof(texcape));          conf->tex = (texcape*)malloc(sizeof(texcape));

Legend:
Removed from v.49  
changed lines
  Added in v.55

  ViewVC Help
Powered by ViewVC 1.1.26