teapot-spreadsheet/src/common/context.c

148 lines
4.2 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 "context.h"
#include "main.h"
#include "misc.h"
/*}}}*/
/* savecontext -- save as ConTeXt table */ /*{{{*/
const char *savecontext(Sheet *sheet, const char *name, int body,
const Location beg, const Location end,
unsigned int *count)
{
/* variables */ /*{{{*/
FILE *fp=(FILE*)0; /* cause runtime error */
Location w;
char buf[1024];
char num[20];
char fullname[PATH_MAX];
Cell *cell;
/*}}}*/
/* asserts */ /*{{{*/
assert(sheet != (Sheet*)0);
assert(name != (const char*)0);
/*}}}*/
*count=0;
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(CELL_AT(sheet,w))) return _("Shadowed cells in first column");
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 */ /*{{{*/
{
sprintf(num, ".%d", w[Z]);
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 (w[Z] == beg[Z])
{
if (fputs_close("\\starttext\n",fp)==EOF) return strerror(errno);
}
else
{
if (fputs_close("\\page\n",fp)==EOF) return strerror(errno);
}
/*}}}*/
/* print bogus format */ /*{{{*/
fprintf(fp,"\\starttable[");
for (w[X]=beg[X]; w[X]<=end[X]; ++(w[X]))
if (fputs_close("|l",fp)==EOF) return strerror(errno);
fprintf(fp,"|]\n");
/*}}}*/
for (w[Y]=beg[Y]; w[Y]<=end[Y]; ++(w[Y]))
/* print contents */ /*{{{*/
{
for (w[X]=beg[X]; w[X]<=end[X]; )
{
int multicols;
char *bufp;
if (w[X] > beg[X] && fputs_close("\\NC",fp)==EOF) return strerror(errno);
for (multicols=w[X]+1; multicols<sheet->dimx && SHADOWEDC(sheet,multicols,w[Y],w[Z]); ++multicols);
multicols=multicols-w[X];
if (multicols>1) fprintf(fp,"\\use{%d}",multicols);
cell = CELL_AT(sheet, w);
switch (getadjust(cell))
{
case LEFT: if (fputs_close("\\JustLeft ",fp)==EOF) return strerror(errno); break;
case RIGHT: if (fputs_close("\\JustRight ",fp)==EOF) return strerror(errno); break;
case CENTER: if (fputs_close("\\JustCenter ",fp)==EOF) return strerror(errno); break;
default: assert(0);
}
printvalue(buf, sizeof(buf), 0, DIRECT_STRING, getfltformat(cell),
getprecision(cell), sheet, w);
/* if (fputs_close("}{",fp)==EOF) return strerror(errno);*/
if (transparent(cell))
{
if (fputs_close(buf,fp)==EOF) return strerror(errno);
}
else for (bufp=buf; *bufp; ++bufp) switch (*bufp)
{
case '%':
case '$':
case '&':
case '#':
case '_':
case '{':
case '}':
case '~':
case '^': if (fputc_close('\\',fp)==EOF || fputc_close(*bufp,fp)==EOF) return strerror(errno); break;
case '\\': if (fputs_close("\\backslash ",fp)==EOF) return strerror(errno); break;
default: if (fputc_close(*bufp,fp)==EOF) return strerror(errno);
}
/* if (fputc_close('}',fp)==EOF) return strerror(errno);*/
w[X] += multicols;
++*count;
}
if (fputs_close(w[Y]<end[Y] ? "\\MR\n" : "\n\\stoptable\n",fp)==EOF) return strerror(errno);
}
/*}}}*/
if (body)
{
if (fclose(fp)==EOF) return strerror(errno);
}
else
{
if (fputs_close("\n",fp)==EOF) return strerror(errno);
}
}
if (!body)
{
if (fputs_close("\\stoptext\n",fp)==EOF) return strerror(errno);
if (fclose(fp)==EOF) return strerror(errno);
}
return (const char*)0;
}
/*}}}*/