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 for(; ai<argc; ai++) {
97 if(access(argv[ai], 0) == -1) {} else fileNr++;
98 }
99
100
101 /* Is something missing or wrong? */
102 if(!htmlfile[0] || fileNr<1) {
103 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
104 return(1);
105 }
106
107 /* In verbose mode print arguments and options */
108 if(verbose>1) {
109 printf("htmlfile := %s\n", htmlfile);
110 printf("fileNr := %d\n", fileNr);
111 }
112
113 /* Set image width based on nr of columns */
114 if(fileNr<maxCols) maxCols=fileNr;
115 double iw=90./(double)maxCols;
116
117
118 /* Open output file */
119 if(verbose>1) printf("opening %s for writing\n", htmlfile);
120 FILE *fp;
121 if((fp=fopen(htmlfile, "w")) == NULL) {
122 printf("Error: cannot open %s.\n", htmlfile);
123 return(11);
124 }
125
126 /* Write HTML header */
127 int len=fprintf(fp, "<!DOCTYPE html>\n");
128 if(len<10) {fclose(fp); return(12);}
129 fprintf(fp, "<html>\n");
130 fprintf(fp, "<head>\n");
131 fprintf(fp, " <meta charset=\"UTF-8\">\n");
132 fprintf(fp, " <title>%s</title>\n", "Gallery");
133 fprintf(fp, " <style type=\"text/css\">\n");
134 fprintf(fp, " * {font-family: monospace;}\n");
135 fprintf(fp, " img {border-width: 0px; padding: 5px 0px 0px 10px; width:%g%%;}\n", iw);
136 fprintf(fp, " </style>\n");
137 fprintf(fp, "</head>\n\n");
138
139 /* Write HTML body */
140 fprintf(fp, "<body>\n");
141
142 /* Start figure */
143 fprintf(fp, "<figure>\n");
144
145 /* Add links to plots */
146 int column=0;
147 for(int ai=firstfile; ai<argc; ai++) {
148 strlcpy(svgfile, argv[ai], FILENAME_MAX);
149 /* Make link to the plot */
150 fprintf(fp, " <a href=\"%s\">", svgfile);
151 /* Make img tag */
152 fprintf(fp, "<img src=\"%s\" alt=\"%s\">", svgfile, svgfile);
153 /* Close link */
154 fprintf(fp, "</a>\n");
155 column++;
156 if(column==maxCols) {
157 if(ai<argc) fprintf(fp, " <br>\n");
158 column=0;
159 }
160 }
161
162 /* Close figure */
163 fprintf(fp, "</figure>\n");
164
165 /* Close HTML */
166 fprintf(fp, "</body>\n");
167 fprintf(fp, "</html>\n");
168 /* ... and file */
169 fclose(fp);
170
171 return(0);
172}
173/*****************************************************************************/
174
175/*****************************************************************************/
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:635
int verbose
Verbose level, used by statusPrint() etc.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.