TPCCLIB
Loading...
Searching...
No Matches
htmlgall.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <string.h>
14#include <math.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Make simple HTML page working as a gallery of argument SVG plots.",
22 " ",
23 "Usage: @P [Options] htmlfile svgfiles",
24 " ",
25 "Options:",
26 " -cols=<Max columns>",
27 " Maximum number of plots on one row (default is 5).",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "Example:",
31 " @P study.html study*.svg",
32 " ",
33 "See also: tac2svg",
34 0};
35/*****************************************************************************/
36
37/*****************************************************************************/
38/* Turn on the globbing of the command line, since it is disabled by default in
39 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
40 In Unix&Linux wildcard command line processing is enabled by default. */
41/*
42#undef _CRT_glob
43#define _CRT_glob -1
44*/
45int _dowildcard = -1;
46/*****************************************************************************/
47
48/*****************************************************************************/
52int main(int argc, char **argv)
53{
54 int ai, help=0, version=0, verbose=1;
55 char svgfile[FILENAME_MAX], htmlfile[FILENAME_MAX];
56 int firstfile=0, fileNr=0;
57 int maxCols=5;
58
59
60 /*
61 * Get arguments
62 */
63 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
64 htmlfile[0]=(char)0;
65 /* Options */
66 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
67 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
68 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
69 if(strncasecmp(cptr, "COLS=", 5)==0) {
70 if(atoiCheck(cptr+5, &maxCols)==0 && maxCols>0) continue;
71 } else if(strncasecmp(cptr, "COL=", 4)==0) {
72 if(atoiCheck(cptr+4, &maxCols)==0 && maxCols>0) continue;
73 }
74 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
75 return(1);
76 } else break; // tac name argument may start with '-'
77
78 TPCSTATUS status; statusInit(&status);
79 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
80 status.verbose=verbose-1;
81
82 /* Print help or version? */
83 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
84 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
85 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
86
87 /* Process other arguments, starting from the first non-option */
88 if(ai<argc) {strlcpy(htmlfile, argv[ai++], FILENAME_MAX);}
89 if(ai<argc) {
90 firstfile=ai; fileNr=1;
91 strlcpy(svgfile, argv[ai++], FILENAME_MAX);
92 } else {
93 fprintf(stderr, "Error: missing filename.\n"); return(1);
94 }
95 /* More file names? */
96 int ret=0;
97 for(; ai<argc; ai++) {
98 if(access(argv[ai], 0) == -1) ret++; else fileNr++;
99 }
100
101
102 /* Is something missing or wrong? */
103 if(!htmlfile[0] || fileNr<1) {
104 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
105 return(1);
106 }
107
108 /* In verbose mode print arguments and options */
109 if(verbose>1) {
110 printf("htmlfile := %s\n", htmlfile);
111 printf("fileNr := %d\n", fileNr);
112 }
113
114 /* Set image width based on nr of columns */
115 if(fileNr<maxCols) maxCols=fileNr;
116 double iw=90./(double)maxCols;
117
118
119 /* Open output file */
120 if(verbose>1) printf("opening %s for writing\n", htmlfile);
121 FILE *fp;
122 if((fp=fopen(htmlfile, "w")) == NULL) {
123 printf("Error: cannot open %s.\n", htmlfile);
124 return(11);
125 }
126
127 /* Write HTML header */
128 int len=fprintf(fp, "<!DOCTYPE html>\n");
129 if(len<10) {fclose(fp); return(12);}
130 fprintf(fp, "<html>\n");
131 fprintf(fp, "<head>\n");
132 fprintf(fp, " <meta charset=\"UTF-8\">\n");
133 fprintf(fp, " <title>%s</title>\n", "Gallery");
134 fprintf(fp, " <style type=\"text/css\">\n");
135 fprintf(fp, " * {font-family: monospace;}\n");
136 fprintf(fp, " img {border-width: 0px; padding: 5px 0px 0px 10px; width:%g%%;}\n", iw);
137 fprintf(fp, " </style>\n");
138 fprintf(fp, "</head>\n\n");
139
140 /* Write HTML body */
141 fprintf(fp, "<body>\n");
142
143 /* Start figure */
144 fprintf(fp, "<figure>\n");
145
146 /* Add links to plots */
147 int column=0, row=0;
148 for(int ai=firstfile; ai<argc; ai++) {
149 strlcpy(svgfile, argv[ai], FILENAME_MAX);
150 /* Make link to the plot */
151 fprintf(fp, " <a href=\"%s\">", svgfile);
152 /* Make img tag */
153 fprintf(fp, "<img src=\"%s\" alt=\"%s\">", svgfile, svgfile);
154 /* Close link */
155 fprintf(fp, "</a>\n");
156 column++;
157 if(column==maxCols) {
158 if(ai<argc) fprintf(fp, " <br>\n");
159 column=0; row++;
160 }
161 }
162
163 /* Close figure */
164 fprintf(fp, "</figure>\n");
165
166 /* Close HTML */
167 fprintf(fp, "</body>\n");
168 fprintf(fp, "</html>\n");
169 /* ... and file */
170 fclose(fp);
171
172 return(0);
173}
174/*****************************************************************************/
175
176/*****************************************************************************/
int atoiCheck(const char *s, int *v)
Definition intutil.c:25
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
int verbose
Verbose level, used by statusPrint() etc.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.