teapot-spreadsheet/src/common/htmlio.c

117 lines
3.8 KiB
C

/* #includes */ /*{{{C}}}*//*{{{*/
#ifndef NO_POSIX_SOURCE
#undef _POSIX_SOURCE
#define _POSIX_SOURCE 1
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 2
#endif
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "htmlio.h"
#include "main.h"
#include "misc.h"
/*}}}*/
/* savehtml -- save as HTML table */ /*{{{*/
const char *savehtml(Sheet *sheet, const char *name, int body,
const Location beg, const Location end,
unsigned int *count)
{
assert(sheet != (Sheet*)0);
assert(name != NULL);
*count = 0;
Location w;
w[X] = beg[X];
for (w[Z]=beg[Z]; w[Z]<=end[Z]; ++(w[Z]))
for (w[Y]=beg[Y]; w[Y]<=end[Y]; ++(w[Y]))
if (shadowed(sheet, w)) return _("Shadowed cells in first column");
FILE *fp = (FILE*)0; /* cause runtime error */
if (!body && (fp = fopen(name,"w")) == (FILE*)0) return strerror(errno);
for (w[Z]=beg[Z]; w[Z]<=end[Z]; ++(w[Z]))
{
if (body) /* open new file */ /*{{{*/
{
char num[20];
sprintf(num, ".%d", w[Z]);
char fullname[PATH_MAX];
fullname[sizeof(fullname)-strlen(num)-1]='\0';
(void)strncpy(fullname,name,sizeof(fullname)-strlen(num)-1);
fullname[sizeof(fullname)-1]='\0';
(void)strncat(fullname,num,sizeof(fullname)-strlen(num)-1);
fullname[sizeof(fullname)-1]='\0';
if ((fp=fopen(fullname,"w"))==(FILE*)0) return strerror(errno);
}
/*}}}*/
else /* print header */ /*{{{*/
if (fputs_close("<html>\n<head>\n<title>\n</title>\n</head>\n<body>\n",fp)==EOF) return strerror(errno);
/*}}}*/
if (fputs_close("<table>\n",fp)==EOF) return strerror(errno);
for (w[Y]=beg[Y]; w[Y]<=end[Y]; ++(w[Y])) /* print contents */ /*{{{*/
{
if (fputs_close("<tr>",fp)==EOF) return strerror(errno);
for (w[X]=beg[X]; w[X]<=end[X]; )
{
Location mw; LOCATION_GETS(mw, w);
for (++(mw[X]); (size_t)(mw[X]) < sheet->dim[X] && shadowed(sheet, mw);
++(mw[X]));
int multicols = mw[X] - w[X];
if (multicols > 1) fprintf(fp,"<td colspan=%d",multicols);
else fprintf(fp,"<td");
Style cs = getstyle(sheet, w);
switch (cs.adjust)
{
case LEFT: if (fputs_close(" align=left>",fp)==EOF) return strerror(errno); break;
case RIGHT: if (fputs_close(" align=right>",fp)==EOF) return strerror(errno); break;
case CENTER: if (fputs_close(" align=center>",fp)==EOF) return strerror(errno); break;
default: assert(0);
}
char buf[1024];
printvalue(buf, sizeof(buf), 0, DIRECT_STRING, cs.fform, cs.precision,
sheet, w);
if (cs.transparent)
{
if (fputs_close(buf,fp) == EOF) return strerror(errno);
}
else for (char *bufp = buf; *bufp; ++bufp) switch (*bufp)
{
case '<': if (fputs_close("&lt;",fp)==EOF) return strerror(errno); break;
case '>': if (fputs_close("&gt;",fp)==EOF) return strerror(errno); break;
case '&': if (fputs_close("&amp;",fp)==EOF) return strerror(errno); break;
case '"': if (fputs_close("&quot;",fp)==EOF) return strerror(errno); break;
default: if (fputc_close(*bufp,fp)==EOF) return strerror(errno);
}
if (fputs_close("</td>",fp)==EOF) return strerror(errno);
w[X] += multicols;
++*count;
}
if (fputs_close("</tr>\n",fp)==EOF) return strerror(errno);
}
/*}}}*/
if (fputs_close("</table>\n",fp)==EOF) return strerror(errno);
if (body)
{
if (fclose(fp) == EOF) return strerror(errno);
}
}
if (!body)
{
if (fputs_close("</body>\n</html>\n",fp) == EOF) return strerror(errno);
if (fclose(fp) == EOF) return strerror(errno);
}
return NULL;
}
/*}}}*/