/* 
 * 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(__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


#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.21 $";
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;
	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);

	return(-1);
}
