]> www.wagner.pp.ru Git - oss/catdoc.git/blob - src/sheet.c
Add detection of ZIP-archive and report that this type of file (i.e. OOXML or OpenDoc...
[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=
38                    (unsigned char**)realloc(rowptr[row].cells,
39                                             newcol*sizeof(unsigned char *));
40                 if (rowptr[row].cells == NULL) {
41                         perror("allocating row");
42                         exit(1);
43                 }       
44                 memset(rowptr[row].cells+rowptr[row].end,0,(newcol-rowptr[row].end)
45                                 *sizeof(char *));
46                 rowptr[row].end=newcol;
47         }  
48         if (col>rowptr[row].last) rowptr[row].last=col;
49         return (rowptr[row].cells+col);
50 }
51 /*
52  * Frees up all memory used by sheet
53  */ 
54 void free_sheet(void) {
55         int i,j;
56         struct rowdescr *row;
57         unsigned char **col;
58         for (row=rowptr,i=0;i<lastrow;i++,row++) {
59                 if (!row->cells) continue;
60                 for (col=row->cells,j=0;j<row->end;j++,col++) {
61                         if (*col) {
62                                 free(*col);
63                         }
64                 }
65                 free (row->cells);
66         }
67         free (rowptr);
68         rowptr=NULL;
69         lastrow=0;
70 }
71
72 /*
73  * prints out one value with quoting
74  * uses global variable quote_mode
75  */ 
76 void print_value(unsigned char *value) 
77 {
78         int i,len;
79         int quotes=0;
80         if (value != NULL) {
81                 len=strlen((char *)value);
82         } else {
83                 len = 0;
84         }
85         switch (quote_mode) {
86                 case QUOTE_NEVER:
87                         break;
88                 case QUOTE_SPACES_ONLY:   
89                         for (i=0;i<len;i++) {
90                                 if (isspace(value[i]) || value[i]==cell_separator || 
91                                                 value[i]=='"') {
92                                         quotes=1;
93                                         break;
94                                 }
95                         }    
96                         break;
97                 case QUOTE_ALL_STRINGS:
98                         { char *endptr;
99                           strtod((char *)value,&endptr);
100                           quotes=(*endptr != '0');
101                         break;
102                         }  
103                 case QUOTE_EVERYTHING:
104                         quotes = 1;
105                         break;     
106         }         
107         if (quotes) {
108                 fputc('\"',stdout);
109                 for (i=0;i<len;i++) {
110                         if (value[i]=='\"') {
111                                 fputc('\"',stdout);
112                                 fputc('\"',stdout);
113                         } else {
114                                 fputc(value[i],stdout);
115                         }
116                 }   
117                 fputc('\"',stdout);
118         } else {
119                 fputs((char *)value,stdout);
120         }
121 }    
122 /*
123  * Prints sheet to stdout. Uses global variable cell_separator
124  */ 
125 void print_sheet(void) {
126         int i,j,printed=0;
127         struct rowdescr *row;
128         unsigned char **col;
129         lastrow--;
130         while (lastrow>0&&!rowptr[lastrow].cells) lastrow--;
131         for(i=0,row=rowptr;i<=lastrow;i++,row++) {
132                 if (row->cells) {
133                         for (j=0,col=row->cells;j<=row->last;j++,col++) {
134                                 if (j){
135                                         fputc(cell_separator,stdout);
136                                         printed=1;
137                                 }
138                                 if (*col) {
139                                         print_value(*col);
140                                         printed=1;
141                                 }
142                         }
143                         if (printed) {
144                                 fputc('\n',stdout);
145                                 printed=0;
146                         }
147                 }
148         }
149         fputs(sheet_separator,stdout);
150 }