intreadxls.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include "machine.h"
00005 #include "MALLOC.h"
00006 #include "stack-c.h"
00007 #include "ole.h"
00008 #include "ripole.h"
00009 #include "files.h"
00010 #include "gw_fileio.h"
00011 #include "intreadxls.h"
00012 
00013 /*---------------------------------------------------------------
00014   Authors Pierrick Mode, Serge Steer INRIA 2005, Copyright INRIA
00015   --------------------------------------------------------------*/
00016 
00017 extern void GetenvB( char *name,char *env, int len);
00018 extern int C2F(cluni0)(char *in_name, char *out_name, int *out_n, long int lin, long int lout);
00019 
00020 /*Prototype*/
00021 void xls_read(int *fd, int *cur_pos,double **data, int **chainesind, int *N, int *M, int *err);
00022 void xls_open(int *err, int *fd, char ***sst, int *ns, char ***Sheetnames, int** Abspos,int *nsheets);
00023 /*------------------------------------------------------------------*/
00024 /*------------------------------------------------------------------*/
00025 
00026 
00027 
00028 int C2F(intreadxls)(char *fname, long lfn)
00029 {
00030   int m1,n1,l1,zero=0,ierr;
00031   double *data=NULL;
00032   int *ind=NULL;
00033   int M,N,MN;
00034   int pos,fd;
00035 
00036   CheckLhs(2,2);
00037   CheckRhs(2,2);
00038 
00039   /*  checking variable Pos */
00040   GetRhsVar(1,"d",&m1,&n1,&l1);
00041   fd=(int)*stk(l1);
00042   GetRhsVar(2,"d",&m1,&n1,&l1);
00043   pos=(int)*stk(l1);
00044 
00045   xls_read(&fd,&pos,&data, &ind, &N, &M,  &ierr);
00046   if (ierr == 1)
00047     {
00048       Scierror(999,"%s :Not enough memory to allocate results \r\n",fname);
00049       return 0;
00050     }
00051   else if(ierr == 2)
00052     {
00053       Scierror(999,"%s :Failed to read expected data, may be invalid xls file \r\n",fname);
00054       return 0;
00055     }
00056   else if(ierr == 3)
00057     {
00058       Scierror(999,"%s :End of File \r\n",fname);
00059       return 0;
00060     }
00061 
00062   /*  sciprint("readxls %d %d %d\n",M,N,ierr);*/
00063 
00064   MN=M*N;
00065   if (MN==0)
00066     {
00067       CreateVar(Rhs+1, "d", &zero,&zero, &l1);
00068       CreateVar(Rhs+2, "d", &zero,&zero, &l1);
00069     }
00070 
00071   else
00072     {
00073       CreateVarFromPtr(Rhs+1, "d", &N,&M, &data);
00074       CreateVarFromPtr(Rhs+2, "i", &N,&M, &ind);
00075       FREE(data);
00076       FREE(ind);
00077     }
00078 
00079 
00080   LhsVar(1)= Rhs+1;
00081   LhsVar(2)= Rhs+2;
00082   C2F(putlhsvar)();
00083 
00084   return 0;
00085 }
00086 
00087 
00088 static char *xls_basename (name)
00089 char *name;
00090 {
00091   char *base=NULL;
00092 #ifdef _MSC_VER
00093   base = strrchr (name, '\\');
00094 #else
00095   base = strrchr (name, '/');
00096 #endif
00097 
00098   return base ? base + 1 : name;
00099 }
00100 
00101 int C2F(intopenxls)(char *fname, long lfn)
00102 {
00103 #undef IN
00104   int i,k,m1,n1,l1,l2,one=1,fd,f_swap=0;
00105   int ierr,ns,result;
00106   double res;
00107   char **sst=NULL;
00108   char **Sheetnames=NULL;
00109   int *Abspos=NULL;
00110   int nsheets;
00111   char IN[256],TMP[256];
00112   char sep[2];
00113 #ifdef _MSC_VER
00114   sep[0]='\\';
00115 #else
00116   sep[0]='/';
00117 #endif
00118   sep[1]='\0';
00119 
00120   CheckLhs(4,4);
00121   CheckRhs(1,1);
00122 
00123   /*  checking variable file */
00124   GetRhsVar(1,"c",&m1,&n1,&l1);
00125   C2F(cluni0)(cstk(l1), IN, &ns,(long int) (m1*n1),256L);
00126 
00127   strcpy(TMP,getenv("TMPDIR"));
00128   
00129   strcat(TMP,sep);
00130   strcat(TMP,xls_basename(IN));
00131   result=ripole(IN, TMP, 0, 0);
00132   if (result != OLE_OK) {
00133     if (result == OLEER_NO_INPUT_FILE)
00134       Scierror(999,"%s :There is no  file %s \r\n",fname,IN);
00135     else if(result == OLEER_NOT_OLE_FILE || 
00136             result == OLEER_INSANE_OLE_FILE || 
00137             result == OLEER_LOADFAT_BAD_BOUNDARY || 
00138             result == OLEER_MINIFAT_READ_FAIL || 
00139             result == OLEER_PROPERTIES_READ_FAIL)
00140       Scierror(999,"%s :file %s is not an ole2 file\r\n",fname,IN);
00141     else if(result == -1)
00142       Scierror(999,"%s :file %s  cannot be opened\r\n",fname,IN);
00143 
00144     return 0;
00145   }
00146   strcat(TMP,sep);
00147   strcat(TMP,"Workbook");
00148   C2F(mopen)(&fd, TMP,"rb", &f_swap, &res, &ierr);
00149   if (ierr != 0)
00150     {
00151       Scierror(999,"%s :There is no xls stream in the ole2 file %s \r\n",fname,IN);
00152       return 0;
00153     }
00154   CreateVar(Rhs+1,"i",&one,&one,&l2);
00155   *istk(l2)=fd; /* logical unit */
00156 
00157   xls_open(&ierr, &fd, &sst ,&ns, &Sheetnames, &Abspos,&nsheets);
00158   /*return *err:
00159     0 = OK
00160     1 = not an OLE file
00161     2 = no Workbook included
00162     3 = memory allocation problem
00163     4 = incorrect file
00164     5 = not a BIFF8 xls file
00165    */
00166   if (ierr == 1)
00167     {
00168       Scierror(999,"%s :Not an ole2 file \r\n",fname);
00169       return 0;
00170     }
00171   else  if (ierr == 2)
00172     {
00173       Scierror(999,"%s : the file has no Workbook directory \r\n",fname);
00174       return 0;
00175     }
00176  else  if (ierr == 3)
00177     {
00178       Scierror(999,"%s : impossible to alloc enough memory \r\n",fname);
00179       return 0;
00180     }
00181   else  if (ierr == 4)
00182     {
00183       Scierror(990,"%s : incorrect or corrupted file \r\n",fname);
00184       return 0;
00185     }
00186   else  if (ierr == 3)
00187     {
00188       Scierror(999,"%s : Only BIFF8 file format is handled \r\n",fname);
00189       return 0;
00190     }
00191 
00192   if (ns != 0) 
00193   {
00194     /* Create a typed list to return the properties */
00195     CreateVarFromPtr(Rhs+2,"S", &one, &ns, sst);
00196     for (k=0;k<ns;k++) 
00197         {
00198                 if ( (sst) && (sst[k]))
00199                 {
00200                         FREE(sst[k]);
00201                         sst[k]=NULL;
00202                 }
00203         }
00204         if (sst)
00205         {
00206                 FREE(sst);
00207                 sst=NULL;
00208         }
00209   }
00210   else
00211     CreateVar(Rhs+2,"d",&ns,&ns,&l2);
00212 
00213   if (nsheets != 0) 
00214   {
00215     /* Create a typed list to return the properties */
00216     CreateVarFromPtr(Rhs+3,"S", &one, &nsheets, Sheetnames);
00217         
00218         if (Sheetnames)
00219         {
00220                 for (k=0;k<nsheets;k++) 
00221                 {
00222                         if (Sheetnames[k])
00223                         {
00224                                 FREE(Sheetnames[k]);
00225                                 Sheetnames[k]=NULL;
00226                         }
00227                 }
00228                 FREE(Sheetnames);
00229                 Sheetnames=NULL;
00230         }
00231 
00232     
00233     
00234     CreateVar(Rhs+4,"d", &one, &nsheets, &l2);
00235     for (i=0;i<nsheets;i++) *stk(l2+i)=Abspos[i];
00236         if (Abspos)
00237         {
00238                 FREE(Abspos);
00239                 Abspos=NULL;
00240         }
00241   }
00242   else {
00243     CreateVar(Rhs+3,"d",&nsheets,&nsheets,&l2);
00244     CreateVar(Rhs+4,"d",&nsheets,&nsheets,&l2);
00245   }
00246 
00247   LhsVar(1)= Rhs+1;
00248   LhsVar(2)= Rhs+2;
00249   LhsVar(3)= Rhs+3;
00250   LhsVar(4)= Rhs+4;
00251   C2F(putlhsvar)();
00252   return 0;
00253 }
00254 

Generated on Sun Mar 4 15:03:49 2007 for Scilab [trunk] by  doxygen 1.5.1