-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlibbif.c
447 lines (421 loc) · 10 KB
/
libbif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Here is a very simple set of routines to write an Excel worksheet
* Microsoft BIFF format. The Excel version is set to 2.0 so that it
* will work with all versions of Excel.
*
* Author: Don Capps
*/
/*
* Note: rows and colums should not exceed 255 or this code will
* act poorly
*/
#include <sys/types.h>
#include <stdio.h>
#include <sys/file.h>
#if defined(__AIX__) || defined(linux) || defined(__FreeBSD__) || defined(__DragonFly__)
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif
#if defined(OSV5) || defined(linux) || defined (__FreeBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__APPLE__) || defined(__DragonFly__)
#include <string.h>
#endif
#if defined(linux) || defined(__DragonFly__) || defined(macosx)
#include <unistd.h>
#include <stdlib.h>
#endif
#if ((defined(solaris) && defined( __LP64__ )) || defined(__s390x__))
/* If we are building for 64-bit Solaris, all functions that return pointers
* must be declared before they are used; otherwise the compiler will assume
* that they return ints and the top 32 bits of the pointer will be lost,
* causing segmentation faults. The following includes take care of this.
* It should be safe to add these for all other OSs too, but we're only
* doing it for Solaris now in case another OS turns out to be a special case.
*/
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#endif
/* Little Endian */
#define ENDIAN_1 1
/* Big Endian */
#define ENDIAN_2 2
/* Middle Endian */
#define ENDIAN_3 3
/* Middle Endian */
#define ENDIAN_4 4
#ifdef HAVE_ANSIC_C
/************************************************************************/
/* Here is the API... Enjoy */
/************************************************************************/
/* Create worksheet */
int create_xls(char *);
/* Args: Filename */
/* */
/* Close worksheet */
void close_xls(int);
/* Args: file descriptor */
/* */
/* Put a 16 bit integer in worksheet */
void do_int(int,int,int,int);
/* Args: file descriptor, */
/* value, */
/* row, */
/* column */
/* Put a double in 8 byte float */
void do_float(int,double,int,int);
/* Args: file descriptor, */
/* value, */
/* row, */
/* column */
/* Put a string in worksheet */
void do_label(int,char *,int,int);
/* Args: file descriptor, */
/* string, */
/* row, */
/* column */
/************************************************************************/
char libbif_version[] = "Libbif Version $Revision: 3.22 $";
void do_eof(int ); /* Used internally */
void do_header(int ); /* Used internally */
int endian(void);
#endif
#define BOF 0x9
#define INTEGER 0x2
#define FLOAT 0x3
#define LABEL 0x4
#define EXCEL_VERS 0x2
#define WORKSHEET 0x10
struct bof_record{ /* Beginning of file */
char hi_opcode;
char lo_opcode;
char hi_length;
char lo_length;
char hi_version; /* Excel version */
char lo_version;
char hi_filetype;
char lo_filetype;
};
struct int_record {
char hi_opcode; /* Type 2 of record */
char lo_opcode;
char hi_length;
char lo_length;
char hi_row;
char lo_row;
char hi_column;
char lo_column;
char rgbhi;
char rgbmed;
char rgblo;
char hi_data;
char lo_data;
};
struct label_record {
char hi_opcode; /* Type 4 of record */
char lo_opcode;
char hi_length;
char lo_length;
char hi_row;
char lo_row;
char hi_column;
char lo_column;
char rgbhi;
char rgbmed;
char rgblo;
char string_length;
char str_array[256];
};
struct float_record { /* Type 3 record */
char hi_opcode;
char lo_opcode;
char hi_length;
char lo_length;
char hi_row;
char lo_row;
char hi_column;
char lo_column;
char rgbhi;
char rgbmed;
char rgblo;
double data;
};
/*
* Write the EOF and close the file
*/
#ifdef HAVE_ANSIC_C
void
close_xls(int fd)
{
#else
close_xls(fd)
int fd;
{
#endif
do_eof(fd);
close(fd);
}
/*
* Create xls worksheet. Create file and put the BOF record in it.
*/
#ifdef HAVE_ANSIC_C
int
create_xls(char *name)
{
#else
create_xls(name)
char *name;
{
#endif
int fd;
unlink(name);
#ifdef Windows
fd=open(name,O_BINARY|O_CREAT|O_RDWR,0666);
#else
fd=open(name,O_CREAT|O_RDWR,0666);
#endif
if(fd<0)
{
printf("Error opening file %s\n",name);
exit(-1);
}
do_header(fd);
return(fd);
}
#ifdef HAVE_ANSIC_C
void
do_header(int fd) /* Stick the BOF at the beginning of the file */
{
#else
do_header(fd)
int fd;
{
#endif
struct bof_record bof;
bof.hi_opcode=BOF;
bof.lo_opcode = 0x0;
bof.hi_length=0x4;
bof.lo_length=0x0;
bof.hi_version=EXCEL_VERS;
bof.lo_version=0x0;
bof.hi_filetype=WORKSHEET;
bof.lo_filetype=0x0;
write(fd,&bof,sizeof(struct bof_record));
}
/*
* Put an integer (16 bit) in the worksheet
*/
#ifdef HAVE_ANSIC_C
void
do_int(int fd,int val, int row, int column)
{
#else
do_int(fd,val,row,column)
int fd,val,row,column;
{
#endif
struct int_record intrec;
short s_row,s_column;
s_row=(short)row;
s_column=(short)column;
intrec.hi_opcode=INTEGER;
intrec.lo_opcode=0x00;
intrec.hi_length=0x09;
intrec.lo_length=0x00;
intrec.rgbhi=0x0;
intrec.rgbmed=0x0;
intrec.rgblo=0x0;
intrec.hi_row=(char)s_row&0xff;
intrec.lo_row=(char)(s_row>>8)&0xff;
intrec.hi_column=(char)(s_column&0xff);
intrec.lo_column=(char)(s_column>>8)&0xff;
intrec.hi_data=(val & 0xff);
intrec.lo_data=(val & 0xff00)>>8;
write(fd,&intrec,13);
}
/* Note: This routine converts Big Endian to Little Endian
* and writes the record out.
*/
/*
* Put a double in the worksheet as 8 byte float in IEEE format.
*/
#ifdef HAVE_ANSIC_C
void
do_float(int fd, double value, int row, int column)
{
#else
do_float(fd, value, row, column)
int fd;
double value;
int row,column;
{
#endif
struct float_record floatrec;
short s_row,s_column;
unsigned char *sptr,*dptr;
s_row=(short)row;
s_column=(short)column;
floatrec.hi_opcode=FLOAT;
floatrec.lo_opcode=0x00;
floatrec.hi_length=0xf;
floatrec.lo_length=0x00;
floatrec.rgbhi=0x0;
floatrec.rgbmed=0x0;
floatrec.rgblo=0x0;
floatrec.hi_row=(char)(s_row&0xff);
floatrec.lo_row=(char)((s_row>>8)&0xff);
floatrec.hi_column=(char)(s_column&0xff);
floatrec.lo_column=(char)((s_column>>8)&0xff);
sptr =(unsigned char *) &value;
dptr =(unsigned char *) &floatrec.data;
if(endian()==ENDIAN_2) /* Big Endian */
{
dptr[0]=sptr[7]; /* Convert to Little Endian */
dptr[1]=sptr[6];
dptr[2]=sptr[5];
dptr[3]=sptr[4];
dptr[4]=sptr[3];
dptr[5]=sptr[2];
dptr[6]=sptr[1];
dptr[7]=sptr[0];
}
if(endian()==ENDIAN_3) /* Middle Endian */
{
dptr[0]=sptr[4]; /* 16 bit swapped ARM */
dptr[1]=sptr[5];
dptr[2]=sptr[6];
dptr[3]=sptr[7];
dptr[4]=sptr[0];
dptr[5]=sptr[1];
dptr[6]=sptr[2];
dptr[7]=sptr[3];
}
if(endian()==ENDIAN_1) /* Little Endian */
{
dptr[0]=sptr[0]; /* Do not convert to Little Endian */
dptr[1]=sptr[1];
dptr[2]=sptr[2];
dptr[3]=sptr[3];
dptr[4]=sptr[4];
dptr[5]=sptr[5];
dptr[6]=sptr[6];
dptr[7]=sptr[7];
}
if(endian()==-1) /* Unsupported architecture */
{
dptr[0]=0;
dptr[1]=0;
dptr[2]=0;
dptr[3]=0;
dptr[4]=0;
dptr[5]=0;
dptr[6]=0;
dptr[7]=0;
printf("Excel output not supported on this architecture.\n");
}
write(fd,&floatrec,11); /* Don't write floatrec. Padding problems */
write(fd,&floatrec.data,8); /* Write value seperately */
}
/*
* Put a string as a label in the worksheet.
*/
#ifdef HAVE_ANSIC_C
void
do_label(int fd, char *string, int row, int column)
{
#else
do_label(fd, string, row, column)
int fd;
char *string;
int row,column;
{
#endif
struct label_record labelrec;
short s_row,s_column;
int i;
for(i=0;i<255;i++)
labelrec.str_array[i]=0;
s_row=(short)row;
s_column=(short)column;
i=strlen(string);
labelrec.hi_opcode=LABEL;
labelrec.lo_opcode=0x00;
labelrec.hi_length=0x08; /* 264 total bytes */
labelrec.lo_length=0x01;
labelrec.rgblo=0x0;
labelrec.rgbmed=0x0;
labelrec.rgbhi=0x0;
labelrec.hi_row=(char)(s_row&0xff);
labelrec.lo_row=(char)((s_row>>8)&0xff);
labelrec.hi_column=(char)(s_column&0xff);
labelrec.lo_column=(char)((s_column>>8)&0xff);
labelrec.string_length=i;
if(i > 255) /* If too long then terminate it early */
string[254]=0;
i=strlen(string);
strcpy(labelrec.str_array,string);
write(fd,&labelrec,sizeof(struct label_record));
}
/*
* Write the EOF in the file
*/
#ifdef HAVE_ANSIC_C
void
do_eof(int fd)
{
#else
do_eof(fd)
int fd;
{
#endif
char buf[]={0x0a,0x00,0x00,0x00};
write(fd,buf,4);
}
/*
* Routine to determine the Endian-ness of the system. This
* is needed for Iozone to convert doubles (floats) into
* Little-endian format. This is needed for Excel to be
* able to interpret the file
*/
int
endian(void)
{
long long foo = 0x0102030405060708LL;
long foo1 = 0x012345678;
unsigned char *c,c1,c2,c3,c4,c5,c6,c7,c8;
c=(unsigned char *)&foo;
c1=*c++;
c2=*c++;
c3=*c++;
c4=*c++;
c5=*c++;
c6=*c++;
c7=*c++;
c8=*c;
/*--------------------------------------------------------------*/
/* printf("%x %x %x %x %x %x %x %x\n",c1,c2,c3,c4,c5,c6,c7,c8); */
/*--------------------------------------------------------------*/
/* Little Endian format ? ( Intel ) */
if( (c1==0x08) && (c2==0x07) && (c3==0x06) && (c4==0x05) &&
(c5==0x04) && (c6==0x03) && (c7==0x02) && (c8==0x01) )
return(ENDIAN_1);
/* Big Endian format ? ( Sparc, Risc... */
if( (c1==0x01) && (c2==0x02) && (c3==0x03) && (c4==0x04) &&
(c5==0x05) && (c6==0x06) && (c7==0x07) && (c8==0x08) )
return(ENDIAN_2);
/* Middle Endian format ? ( ARM ... ) */
if( (c1==0x04) && (c2==0x03) && (c3==0x02) && (c4==0x01) &&
(c5==0x08) && (c6==0x07) && (c7==0x06) && (c8==0x05) )
return(ENDIAN_3);
c=(unsigned char *)&foo1;
c1=*c++;
c2=*c++;
c3=*c++;
c4=*c++;
/* Another middle endian format ? ( PDP-11 ... ) */
if( (c1==0x34) && (c2==0x12) && (c3==0x78) && (c4==0x56))
return(ENDIAN_4);
return(-1);
}