"Fossies" - the Fresh Open Source Software Archive

Member "teapot-2.3.0/context.c" (6 Feb 2012, 4096 Bytes) of package /linux/privat/old/teapot-2.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "context.c" see the Fossies "Dox" file reference documentation.

    1 /* #includes */ /*{{{C}}}*//*{{{*/
    2 #ifndef NO_POSIX_SOURCE
    3 #undef _POSIX_SOURCE
    4 #define _POSIX_SOURCE   1
    5 #undef _POSIX_C_SOURCE
    6 #define _POSIX_C_SOURCE 2
    7 #endif
    8 
    9 #ifdef DMALLOC
   10 #include "dmalloc.h"
   11 #endif
   12 
   13 #include <assert.h>
   14 #include <errno.h>
   15 #include <limits.h>
   16 #include <stdio.h>
   17 #include <stdlib.h>
   18 #include <string.h>
   19 
   20 
   21 #include "context.h"
   22 #include "main.h"
   23 #include "misc.h"
   24 /*}}}*/
   25 
   26 /* savecontext     -- save as ConTeXt table */ /*{{{*/
   27 const char *savecontext(Sheet *sheet, const char *name, int body, int x1, int y1, int z1, int x2, int y2, int z2, unsigned int *count)
   28 {
   29   /* variables */ /*{{{*/
   30   FILE *fp=(FILE*)0; /* cause runtime error */
   31   int x,y,z;
   32   char buf[1024];
   33   char num[20];
   34   char fullname[PATH_MAX];
   35   /*}}}*/
   36   
   37   /* asserts */ /*{{{*/
   38   assert(sheet!=(Sheet*)0);
   39   assert(name!=(const char*)0);
   40   /*}}}*/
   41   *count=0;
   42   for (z=z1; z<=z2; ++z) for (y=y1; y<=y2; ++y) if (shadowed(sheet,x1,y,z)) return _("Shadowed cells in first column");
   43   if (!body && (fp=fopen(name,"w"))==(FILE*)0) return strerror(errno);
   44   for (z=z1; z<=z2; ++z)
   45   {
   46     if (body)
   47     /* open new file */ /*{{{*/
   48     {
   49       sprintf(num,".%d",z);
   50 
   51       fullname[sizeof(fullname)-strlen(num)-1]='\0';
   52       (void)strncpy(fullname,name,sizeof(fullname)-strlen(num)-1);
   53       fullname[sizeof(fullname)-1]='\0';
   54       (void)strncat(fullname,num,sizeof(fullname)-strlen(num)-1);
   55       fullname[sizeof(fullname)-1]='\0';  
   56       if ((fp=fopen(fullname,"w"))==(FILE*)0) return strerror(errno);
   57     }
   58     /*}}}*/
   59     else
   60     /* print header */ /*{{{*/
   61     if (z==z1)
   62     {
   63       if (fputs_close("\\starttext\n",fp)==EOF) return strerror(errno);
   64     }
   65     else
   66     {
   67       if (fputs_close("\\page\n",fp)==EOF) return strerror(errno);
   68     }
   69     /*}}}*/
   70 
   71     /* print bogus format */ /*{{{*/
   72     fprintf(fp,"\\starttable[");
   73     for (x=x1; x<=x2; ++x) if (fputs_close("|l",fp)==EOF) return strerror(errno);
   74     fprintf(fp,"|]\n");
   75     /*}}}*/
   76     for (y=y1; y<=y2; ++y)
   77     /* print contents */ /*{{{*/
   78     {
   79       for (x=x1; x<=x2; )
   80       {
   81         int multicols;
   82         char *bufp;
   83       
   84         if (x>x1 && fputs_close("\\NC",fp)==EOF) return strerror(errno);
   85         for (multicols=x+1; multicols<sheet->dimx && shadowed(sheet,multicols,y,z); ++multicols);
   86         multicols=multicols-x;
   87         if (multicols>1) fprintf(fp,"\\use{%d}",multicols);
   88         switch (getadjust(sheet,x,y,z))
   89         {
   90           case LEFT: if (fputs_close("\\JustLeft ",fp)==EOF) return strerror(errno); break;
   91           case RIGHT: if (fputs_close("\\JustRight ",fp)==EOF) return strerror(errno); break;
   92           case CENTER: if (fputs_close("\\JustCenter ",fp)==EOF) return strerror(errno); break;
   93           default: assert(0);
   94         }
   95         printvalue(buf,sizeof(buf),0,0,getscientific(sheet,x,y,z),getprecision(sheet,x,y,z),sheet,x,y,z);
   96 /*        if (fputs_close("}{",fp)==EOF) return strerror(errno);*/
   97         if (transparent(sheet,x,y,z))
   98         {
   99           if (fputs_close(buf,fp)==EOF) return strerror(errno);
  100         }
  101         else for (bufp=buf; *bufp; ++bufp) switch (*bufp)
  102         {
  103           case '%':
  104           case '$':
  105           case '&':
  106           case '#':
  107           case '_':
  108           case '{':
  109           case '}':
  110           case '~':
  111           case '^': if (fputc_close('\\',fp)==EOF || fputc_close(*bufp,fp)==EOF) return strerror(errno); break;
  112           case '\\': if (fputs_close("\\backslash ",fp)==EOF) return strerror(errno); break;
  113           default: if (fputc_close(*bufp,fp)==EOF) return strerror(errno);
  114         }
  115 /*        if (fputc_close('}',fp)==EOF) return strerror(errno);*/
  116         x+=multicols;
  117         ++*count;
  118       }
  119       if (fputs_close(y<y2 ? "\\MR\n" : "\n\\stoptable\n",fp)==EOF) return strerror(errno);
  120     }
  121     /*}}}*/
  122     if (body)
  123     {
  124       if (fclose(fp)==EOF) return strerror(errno);
  125     }
  126     else
  127     {
  128       if (fputs_close("\n",fp)==EOF) return strerror(errno);
  129     }
  130   }
  131   if (!body)
  132   {
  133     if (fputs_close("\\stoptext\n",fp)==EOF) return strerror(errno);
  134     if (fclose(fp)==EOF) return strerror(errno);
  135   }
  136   return (const char*)0;
  137 }
  138 /*}}}*/