]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/sheet.c
Recreated CVS repository from working copy
[oss/catdoc.git] / src / sheet.c
1 /*****************************************************************/
2 /* Representation and handling of Excell worksheets in memory    */
3 /*                                                               */
4 /* This file is part of catdoc project                           */
5 /* (c) Victor Wagner 1998-2003, (c) Alex Ott 2003                    */
6 /*****************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include "xls.h"
14 struct rowdescr *rowptr=NULL;
15 int startrow=0,lastrow=0;
16 char cell_separator = ',';
17 int quote_mode = QUOTE_ALL_STRINGS;
18 char *sheet_separator = "\f";
19 /* 
20  * Allocates cell for given row and col and returns pointer to poitrer
21  * of cell contents
22  */ 
23 unsigned char **allocate (int row,int col) {
24         unsigned int newrow,newcol;
25         if (row>=lastrow) {
26                 newrow=(row/16+1)*16;
27                 rowptr=realloc(rowptr,newrow*sizeof(struct rowdescr));
28                 if (rowptr == NULL) {
29                         perror("allocating sheet ");
30                         exit(1);
31                 }       
32                 memset(rowptr+lastrow,0,(newrow-lastrow)*sizeof(struct rowdescr));
33                 lastrow=newrow;
34         }
35         if (col>=rowptr[row].end) {
36                 newcol=(col/16+1)*16;
37                 rowptr[row].cells=realloc(rowptr[row].cells,newcol*sizeof(char *));
38                 if (rowptr[row].cells == NULL) {
39                         perror("allocating row");
40                         exit(1);
41                 }       
42                 memset(rowptr[row].cells+rowptr[row].end,0,(newcol-rowptr[row].end)
43                                 *sizeof(char *));
44                 rowptr[row].end=newcol;
45         }  
46         if (col>rowptr[row].last) rowptr[row].last=col;
47         return (rowptr[row].cells+col);
48 }
49 /*
50  * Frees up all memory used by sheet
51  */ 
52 void free_sheet(void) {
53         int i,j;
54         struct rowdescr *row;
55         unsigned char **col;
56         for (row=rowptr,i=0;i<lastrow;i++,row++) {
57                 if (!row->cells) continue;
58                 for (col=row->cells,j=0;j<row->end;j++,col++) {
59                         if (*col) {
60                                 free(*col);
61                         }
62                 }
63                 free (row->cells);
64         }
65         free (rowptr);
66         rowptr=NULL;
67         lastrow=0;
68 }
69
70 /*
71  * prints out one value with quoting
72  * uses global variable quote_mode
73  */ 
74 void print_value(unsigned char *value) 
75 {
76         int i,len;
77         int quotes=0;
78         if (value != NULL) {
79                 len=strlen((char *)value);
80         } else {
81                 len = 0;
82         }
83         switch (quote_mode) {
84                 case QUOTE_NEVER:
85                         break;
86                 case QUOTE_SPACES_ONLY:   
87                         for (i=0;i<len;i++) {
88                                 if (isspace(value[i]) || value[i]==cell_separator || 
89                                                 value[i]=='"') {
90                                         quotes=1;
91                                         break;
92                                 }
93                         }    
94                         break;
95                 case QUOTE_ALL_STRINGS:
96                         { char *endptr;
97                           strtod(value,&endptr);
98                           quotes=(*endptr != '0');
99                         break;
100                         }  
101                 case QUOTE_EVERYTHING:
102                         quotes = 1;
103                         break;     
104         }         
105         if (quotes) {
106                 fputc('\"',stdout);
107                 for (i=0;i<len;i++) {
108                         if (value[i]=='\"') {
109                                 fputc('\"',stdout);
110                                 fputc('\"',stdout);
111                         } else {
112                                 fputc(value[i],stdout);
113                         }
114                 }   
115                 fputc('\"',stdout);
116         } else {
117                 fputs((char *)value,stdout);
118         }
119 }    
120 /*
121  * Prints sheet to stdout. Uses global variable cell_separator
122  */ 
123 void print_sheet(void) {
124         int i,j,printed=0;
125         struct rowdescr *row;
126         unsigned char **col;
127         lastrow--;
128         while (lastrow>0&&!rowptr[lastrow].cells) lastrow--;
129         for(i=0,row=rowptr;i<=lastrow;i++,row++) {
130                 if (row->cells) {
131                         for (j=0,col=row->cells;j<=row->last;j++,col++) {
132                                 if (j){
133                                         fputc(cell_separator,stdout);
134                                         printed=1;
135                                 }
136                                 if (*col) {
137                                         print_value(*col);
138                                         printed=1;
139                                 }
140                         }
141                         if (printed) {
142                                 fputc('\n',stdout);
143                                 printed=0;
144                         }
145                 }
146         }
147         fputs(sheet_separator,stdout);
148 }