home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / word2x0a.zip / source / html-table.cc < prev    next >
C/C++ Source or Header  |  1997-04-22  |  6KB  |  285 lines

  1. /* $Id: html-table.cc,v 1.4 1997/04/01 01:02:28 dps Exp $ */
  2. /* Html table layout */
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "tblock.h"
  8. #include "html-table.h"
  9. #define __EXCLUDE_READER_CLASSES
  10. #include "lib.h"
  11.  
  12. struct rdata
  13. {
  14.     struct wd_info w;
  15.     const char *data;
  16. };
  17.  
  18.  
  19.  
  20. /* Print row after folding of columns has been done */
  21. static void basic_print_row(int ncols, const struct rdata *cols,
  22.                 int maxw, FILE *out)
  23. {    
  24.     const char *s;
  25.     int i, nsp, nz;
  26.     align_t al;
  27.     num_info n;
  28.     tblock output, *outp;
  29.  
  30.     /* Print the content */
  31.     output.add("<TR>");
  32.     for (i=0; i<ncols; i++)
  33.     {
  34.       output.add("<TD>");
  35.  
  36.     s=cols[i].data;
  37.     al=cols[i].w.align;
  38.     if (cols[i].w.dp_col>=0)
  39.     {
  40.         n=scan_num(s);
  41.         if (n.dot_pos!=-1)
  42.         {
  43.         nz=cols[i].w.dp_col-n.dot_pos;
  44.         if ((!n.has_sign) && (cols[i].w.has_sign))
  45.         {
  46.           /*
  47.             output.add("\\phantom{$-$");
  48.             if (nz>0)
  49.             {
  50.             while (nz--)
  51.                 output.add('0');
  52.             }
  53.             output.add("}");
  54.             */
  55.         }
  56.         else
  57.         {
  58.             if (n.has_sign)
  59.             nz++;    // - does not count as a digit.
  60.             if (nz>0)
  61.             {
  62.               /*
  63.             output.add("\\phantom{");
  64.             while (nz--)
  65.                 output.add('0');
  66.             output.add("}");
  67.             */
  68.             }
  69.         }
  70.         if (n.has_sign)
  71.         {
  72.             /* Typeset the sign in math mode */
  73.             output.add(*s);
  74.             s++;
  75.         }
  76.         output.add(s);
  77.         if (nsp-cols[i].w.dp_col+n.dot_pos>0
  78.             && cols[i].w.align!=ALIGN_LEFT)
  79.             output.add(""); /* was hfill */
  80.         al=ALIGN_DP;
  81.         }
  82.         }
  83.  
  84.     switch(al)
  85.     {
  86.     case ALIGN_DP:
  87.         break;
  88.         
  89.     case ALIGN_LEFT:
  90.     case ALIGN_CENTER:
  91.     case ALIGN_RIGHT:
  92.         output.add(s);
  93.         break;
  94.         
  95.     default:
  96.         fprintf(stderr,"basic_print_row: Invalid alignment\n");
  97.         break;
  98.     }
  99.     
  100.     output.add(" </TD> "); // Seperate columns
  101.     }
  102.     output.add(" \n");        // converted to \\ \n by word_wrap.
  103.     outp=word_wrap((const char *) output, "\n", "</TR>\n", maxw, 0);
  104.     fputs((const char *) (*outp), out);
  105.     delete(outp);
  106. }
  107.  
  108. extern tblock *__html_do_map(const char *);
  109. /* Split the elements of a row */
  110. static void print_row(int ncols, const struct rdata *dp, int maxw, FILE *out)
  111. {
  112.     static const char null_string[]={'\0'};
  113.     struct rdata *rd; 
  114.     tblock *d;
  115.     int i;
  116.  
  117.     /* Allocate structures */
  118.     if ((rd=(struct rdata *) alloca(ncols*sizeof(rdata)))==NULL)
  119.     {
  120.     fprintf(stderr, "print_row: fatal alloca failure\n");
  121.     exit(1);
  122.     }
  123.     
  124.     /* Convert data to *TeX */
  125.     for (i=0; i<ncols; i++)
  126.     {
  127.     rd[i].w=dp[i].w;
  128.     if (dp[i].data==NULL)
  129.     {
  130.         rd[i].data=null_string; // Avoid complication of null in
  131.                     // basic_print_row
  132.         continue;            // Move on to next item
  133.     }
  134.  
  135.     d=__html_do_map(dp[i].data);
  136.     if ((rd[i].data=strdup(*d))==NULL)
  137.     {
  138.         fprintf(stderr, "html_table::print_row: fatal alloca failure\n");
  139.         exit(1);
  140.     }
  141.     delete(d);
  142.     }
  143.     basic_print_row(ncols, rd, maxw, out); // Printing the rows is actually
  144.                        // quite complex.
  145.     for (i=0; i<ncols; i++)
  146.     {
  147.     if (rd[i].data!=null_string)
  148.         free((void *) rd[i].data);    // Free data
  149.     }
  150. }
  151.  
  152.  
  153.     
  154. /* Returns NULL or text message */
  155. const char *html_table::print_table(int wd, FILE *out, const int ruled=1)
  156. {
  157.     int i,j;
  158.     struct rdata *d;
  159.     const struct col_info *col;
  160.  
  161.     if ((d=(struct rdata *) alloca(cols*sizeof(struct rdata)))==NULL)
  162.     {
  163.     cerr<<"html_table::print_table alloca failute (fatal)\n";
  164.     return "[Table omitted due to lack of memory]";
  165.     }
  166.     if (cols==0 || rows==0)
  167.     {
  168.     fputs("[empty tabel ignored]\n", out);
  169.     return "[Ignored empty table]";
  170.     }
  171.  
  172.     for (i=0, col=cdata; col!=NULL; i++, col=col->next)
  173.     d[i].w=find_width(rows, col->data);
  174.  
  175.     if (ruled)
  176.       fputs("<TABLE BORDER=1>", out);
  177.     else
  178.       fputs("<TABLE>", out);
  179.  
  180.     for (i=0; i<cols; i++)
  181.     {
  182.     switch(d[i].w.align)
  183.     {
  184.     default:
  185.        cerr<<"Alignment "<<d[i].w.align<<" unsupported\n";
  186.        /* Fall through */
  187.  
  188.        case ALIGN_LEFT:
  189.      /*       fputc('l', out); */
  190.        break;
  191.  
  192.        case ALIGN_CENTER:
  193.      /* fputc('c', out); */
  194.        break;
  195.  
  196.        case ALIGN_RIGHT:
  197.      /* fputc('r', out); */
  198.        break;
  199.        }
  200.     }
  201.     fprintf(out, "\n");
  202.  
  203.     for (i=0; i<rows; i++)
  204.     {
  205.     for (j=0, col=cdata; col!=NULL; j++, col=col->next)
  206.         d[j].data=(col->data)[i];
  207.     print_row(cols, d, wd, out);
  208.     if (i==0)
  209.         fputs("\\hline\n", out);
  210.     }
  211.  
  212.     fprintf(out, "</TABLE>\n");
  213.     return NULL;
  214. }
  215.  
  216. /* Set */
  217. int html_table::set(int c, int r, const char *s)
  218. {
  219.     struct col_info *col;
  220.     int i;
  221.  
  222.     if (c<0 || c>=cols || r<0 || r>=rows)
  223.     {
  224.     cerr<<"Invalid request to set "<<c<<','<<r<<" in "
  225.         <<cols<<'x'<<rows<<" table (value "<<s<<")\n";
  226.     return 0;
  227.     }
  228.  
  229.     for (col=cdata, i=0; i<c && col!=NULL; i++, col=col->next) ;   
  230.     if (col!=NULL)
  231.     {
  232.     if (col->data[r]!=NULL)
  233.         free((void *) col->data[r]);
  234.     col->data[r]=strdup(s);
  235.     }
  236.     return 1;
  237. }
  238.  
  239.     
  240. /* Constructor */
  241. html_table::html_table(int c, int r)
  242. {
  243.     int i, j;
  244.     struct col_info *col, **nptr;
  245.  
  246.     cols=c;
  247.     rows=r;
  248.     cdata=NULL;            // Hardenning against cols=0
  249.     for (nptr=&cdata, i=0; i<cols; i++)
  250.     {
  251.     col=new(struct col_info);
  252.     *nptr=col;
  253.     col->next=NULL;
  254.     nptr=&(col->next);
  255.     if ((col->data=
  256.          (const char **) malloc(rows*(sizeof(const char *))))==NULL)
  257.     {
  258.         cerr<<"html_table::constructor: malloc failure (fatal)\n";
  259.         exit(1);
  260.     }
  261.     for (j=0; j<rows; j++)
  262.         (col->data)[j]=NULL;
  263.     }
  264. }
  265.  
  266. /* Destructor */
  267. html_table::~html_table(int c, int r)
  268. {
  269.     int i;
  270.     struct col_info *col, *nxt;
  271.     
  272.     for (col=cdata; col!=NULL;)
  273.     {
  274.     for (i=0; i<rows; i++)
  275.         if (col->data[i]==NULL)
  276.         free((void *) col->data[i]);
  277.     nxt=col->next;
  278.     free(col);
  279.     col=nxt;
  280.     }
  281. }
  282.  
  283.  
  284.  
  285.