Attachment 'fio.c'

Download

   1 //
   2 // fio.c
   3 //
   4 // Warning: Do not edit the following four lines.  CVS maintains them.
   5 // Revision Author: $Author: nicks $
   6 // Revision Date  : $Date: 2006/04/19 22:47:33 $
   7 // Revision       : $Revision: 1.29 $
   8 //
   9 ////////////////////////////////////////////////////////////////////
  10 
  11 #include <string.h>
  12 #include <stdio.h>
  13 #include <stdlib.h>
  14 #include <math.h>
  15 #include <sys/types.h>
  16 #include <sys/stat.h>
  17 #include <unistd.h>
  18 #include "bfileio.h"
  19 #include "fio.h"
  20 #include "machine.h"
  21 #include "proto.h"
  22 #include "error.h"
  23 #include "mghendian.h"
  24 #include "utils.h" // strcpyalloc
  25 
  26 #define FIO_NPUSHES_MAX 100
  27 int  fio_npushes = -1;
  28 char fio_dirstack[FIO_NPUSHES_MAX][1000];
  29 
  30 
  31 FILE *MGHopen_file(char *fname, char *rwmode)
  32 {
  33   FILE *f1;
  34 
  35   if ((f1 = fopen(fname,rwmode)) == NULL)
  36   {
  37     printf("Can't open %s\n",fname);
  38     exit(1);
  39   }
  40   return f1;
  41 }
  42 
  43 int
  44 putf(float f, FILE *fp)
  45 {
  46 #if (BYTE_ORDER == LITTLE_ENDIAN)
  47   f = swapFloat(f) ;
  48 #endif
  49   return(fwrite(&f,4,1,fp));
  50 }
  51 
  52 float
  53 getf(FILE *fp)
  54 {
  55   float f;
  56 
  57   fread(&f,4,1,fp);
  58 #if (BYTE_ORDER == LITTLE_ENDIAN)
  59   f = swapFloat(f) ;
  60 #endif
  61   return f;
  62 }
  63 
  64 int
  65 fread1(int *v, FILE *fp)
  66 {
  67   unsigned char c;
  68   int  ret ;
  69 
  70   ret = fread(&c,1,1,fp);
  71   *v = c;
  72   return(ret) ;
  73 }
  74 
  75 
  76 int
  77 fread2(int *v, FILE *fp)
  78 {
  79   short s;
  80   int   ret ;
  81 
  82   ret = fread(&s,2,1,fp);
  83 #if (BYTE_ORDER == LITTLE_ENDIAN)
  84   s = swapShort(s) ;
  85 #endif
  86   *v = s;
  87   return(ret) ;
  88 }
  89 
  90 int
  91 fread3(int *v, FILE *fp)
  92 {
  93   unsigned int i = 0;
  94   int  ret ;
  95 
  96   ret = fread(&i,3,1,fp);
  97 #if (BYTE_ORDER == LITTLE_ENDIAN)
  98   i = (unsigned int)swapInt(i) ;
  99 #endif
 100   *v = ((i>>8) & 0xffffff);
 101   return(ret) ;
 102 }
 103 
 104 int
 105 fread4(float *v, FILE *fp)
 106 {
 107   float f;
 108   int   ret ;
 109 
 110   ret = fread(&f,4,1,fp);
 111 #if (BYTE_ORDER == LITTLE_ENDIAN)
 112   f = swapFloat(f) ;
 113 #endif
 114   *v = f;
 115   return(ret) ;
 116 }
 117 
 118 int
 119 fwrite1(int v,FILE *fp)
 120 {
 121   unsigned char c = (unsigned char)v;
 122 
 123   return(fwrite(&c,1,1,fp));
 124 }
 125 
 126 int
 127 fwrite2(int v, FILE *fp)
 128 {
 129   short s ;
 130 
 131   if (v > 0x7fff)    /* don't let it overflow */
 132     v = 0x7fff ;
 133   else if (v < -0x7fff)
 134     v = -0x7fff ;
 135   s = (short)v;
 136 #if (BYTE_ORDER == LITTLE_ENDIAN)
 137   s = swapShort(s) ;
 138 #endif
 139   return(fwrite(&s,2,1,fp));
 140 }
 141 
 142 int
 143 fwrite3(int v, FILE *fp)
 144 {
 145   unsigned int i = (unsigned int)(v<<8);
 146 
 147 #if (BYTE_ORDER == LITTLE_ENDIAN)
 148   i = (unsigned int)swapInt(i) ;
 149 #endif
 150   return(fwrite(&i,3,1,fp));
 151 }
 152 
 153 int
 154 fwrite4(int v,FILE *fp)
 155 {
 156 #if (BYTE_ORDER == LITTLE_ENDIAN)
 157   v = swapInt(v) ;
 158 #endif
 159   return(fwrite(&v,4,1,fp));
 160 }
 161 
 162 int
 163 fwriteShort(short s, FILE *fp)
 164 {
 165 #if (BYTE_ORDER == LITTLE_ENDIAN)
 166   s = swapShort(s) ;
 167 #endif
 168   return(fwrite(&s, sizeof(short), 1, fp)) ;
 169 }
 170 double
 171 freadDouble(FILE *fp)
 172 {
 173   double d;
 174   int   ret ;
 175 
 176   ret = fread(&d,sizeof(double),1,fp);
 177 #if (BYTE_ORDER == LITTLE_ENDIAN)
 178   d = swapDouble(d) ;
 179 #endif
 180   if (ret != 1)
 181     ErrorPrintf(ERROR_BADFILE, "freadDouble: fread failed") ;
 182   return(d) ;
 183 }
 184 
 185 int
 186 freadInt(FILE *fp)
 187 {
 188   int  i, nread ;
 189 
 190   nread = fread(&i,sizeof(int),1,fp);
 191 #if (BYTE_ORDER == LITTLE_ENDIAN)
 192   i = swapInt(i) ;
 193 #endif
 194   return(i) ;
 195 }
 196 long long
 197 freadLong(FILE *fp)
 198 {
 199   int  nread ;
 200     long long i ;
 201 
 202   nread = fread(&i,sizeof(long long),1,fp);
 203 #if (BYTE_ORDER == LITTLE_ENDIAN)
 204   i = swapLong64(i) ;
 205 #endif
 206   return(i) ;
 207 }
 208 
 209 
 210 short
 211 freadShort(FILE *fp)
 212 {
 213   int   nread ;
 214   short s ;
 215 
 216   nread = fread(&s,sizeof(short),1,fp);
 217 #if (BYTE_ORDER == LITTLE_ENDIAN)
 218   s = swapShort(s) ;
 219 #endif
 220   if (nread != 1)
 221     ErrorPrintf(ERROR_BADFILE, "freadShort: fread failed") ;
 222   return(s) ;
 223 }
 224 
 225 /*******************************************************/
 226 /* read routines which can be used for fread           */
 227 /* the usage is                                        */
 228 /*                                                     */
 229 /* while (fread..Ex(., fp))                            */
 230 /*   dosomething();                                    */
 231 /*******************************************************/
 232 int freadFloatEx(float *pf, FILE *fp)
 233 {
 234   int   ret ;
 235   ret = fread(pf,sizeof(float),1,fp);
 236 #if (BYTE_ORDER == LITTLE_ENDIAN)
 237   *pf = swapFloat(*pf) ;
 238 #endif
 239   return ret;
 240 }
 241 
 242 int freadDoubleEx(double *pd, FILE *fp)
 243 {
 244   int   ret ;
 245   ret = fread(pd,sizeof(double),1,fp);
 246 #if (BYTE_ORDER == LITTLE_ENDIAN)
 247   *pd = swapDouble(*pd) ;
 248 #endif
 249   return ret;
 250 }
 251 
 252 int freadIntEx(int *pi, FILE *fp)
 253 {
 254   int nread ;
 255   nread = fread(pi,sizeof(int),1,fp);
 256 #if (BYTE_ORDER == LITTLE_ENDIAN)
 257   *pi = swapInt(*pi) ; /* swapInt(int i) */
 258 #endif
 259   return(nread);
 260 }
 261 
 262 int freadShortEx(short *ps, FILE *fp)
 263 {
 264   int   nread ;
 265   nread = fread(ps,sizeof(short),1,fp);
 266 #if (BYTE_ORDER == LITTLE_ENDIAN)
 267   *ps = swapShort(*ps) ;
 268 #endif
 269   return(nread) ;
 270 }
 271 
 272 /******************************************************/
 273 int
 274 fwriteInt(int v, FILE *fp)
 275 {
 276 #if (BYTE_ORDER == LITTLE_ENDIAN)
 277   v = swapInt(v) ;
 278 #endif
 279   return(fwrite(&v,sizeof(int),1,fp));
 280 }
 281 
 282 int
 283 fwriteLong(long long v, FILE *fp)
 284 {
 285 #if (BYTE_ORDER == LITTLE_ENDIAN)
 286   v = swapLong64(v) ;
 287 #endif
 288   return(fwrite(&v,sizeof(long long),1,fp));
 289 }
 290 
 291 /*----------------------------------------*/
 292 float freadFloat(FILE *fp)
 293 {
 294   char  buf[4];
 295   float f;
 296   int   ret ;
 297 
 298   ret = fread(buf,4,1,fp);
 299   //ret = fread(&f,4,1,fp); // old way
 300   if (ret != 1) ErrorPrintf(ERROR_BADFILE, "freadFloat: fread failed") ;
 301 #if (BYTE_ORDER == LITTLE_ENDIAN)
 302   byteswapbuffloat(buf,1);
 303   //f = swapFloat(f);  // old way
 304 #endif
 305   f = *((float*)buf);
 306   return(f) ;
 307 }
 308 /*----------------------------------------*/
 309 int fwriteFloat(float f, FILE *fp)
 310 {
 311   int ret;
 312   char  buf[4];
 313   memcpy(buf,&f,4);
 314 #if (BYTE_ORDER == LITTLE_ENDIAN)
 315   byteswapbuffloat(buf,1);
 316   //f = swapFloat(f);  // old way
 317 #endif
 318   ret = fwrite(buf,sizeof(float),1,fp);
 319   //ret = fwrite(&f,sizeof(float),1,fp);  // old way
 320   return(ret);
 321 }
 322 /*----------------------------------------*/
 323 int
 324 fwriteDouble(double d, FILE *fp)
 325 {
 326 #if (BYTE_ORDER == LITTLE_ENDIAN)
 327   d = swapDouble(d) ;
 328 #endif
 329   return(fwrite(&d,sizeof(double),1,fp));
 330 }
 331 
 332 /*------------------------------------------------------
 333   fio_dirname() - function to replicate the functionality
 334   of the unix dirname.
 335   Author: Douglas Greve, 9/10/2001
 336   ------------------------------------------------------*/
 337 char *fio_dirname(char *pathname)
 338 {
 339   int l,n;
 340   char *dirname;
 341 
 342   if(pathname == NULL) return(NULL);
 343 
 344   l = strlen(pathname);
 345 
 346   /* strip off leading forward slashes */
 347   while(l > 0 && pathname[l-1] == '/'){
 348     pathname[l-1] = '\0';
 349     l = strlen(pathname);
 350   }
 351 
 352   if(l < 2){
 353     /* pathname is / or . or single character */
 354     dirname = (char *) calloc(2,sizeof(char));
 355     if(l==0 || pathname[0] == '/') dirname[0] = '/';
 356     else                           dirname[0] = '.';
 357     return(dirname);
 358   }
 359 
 360   /* Start at the end of the path name and step back
 361      until a forward slash is found */
 362   for(n=l; n >= 0; n--)if(pathname[n] == '/') break;
 363 
 364   if(n < 0){
 365     /* no forward slash found */
 366     dirname = (char *) calloc(2,sizeof(char));
 367     dirname[0] = '.';
 368     return(dirname);
 369   }
 370 
 371   if(n == 0){
 372     /* first forward slash is the first character */
 373     dirname = (char *) calloc(2,sizeof(char));
 374     dirname[0] = '/';
 375     return(dirname);
 376   }
 377 
 378   dirname = (char *) calloc(n+1,sizeof(char));
 379   memcpy(dirname,pathname,n);
 380   return(dirname);
 381 }
 382 /*------------------------------------------------------
 383   fio_basename() - function to replicate the functionality
 384   of the unix basename.
 385   Author: Douglas Greve, 9/10/2001
 386   ------------------------------------------------------*/
 387 char *fio_basename(char *pathname, char *ext)
 388 {
 389   int l,n,lext;
 390   char *basename, *tmp;
 391 
 392   if(pathname == NULL) return(NULL);
 393 
 394   l = strlen(pathname);
 395   tmp = strcpyalloc(pathname); // keep a copy
 396 
 397   /* strip off the extension if it matches ext */
 398   if(ext != NULL){
 399     lext = strlen(ext);
 400     if(lext < (l + 2)){
 401       if( strcmp(ext,&(pathname[l-lext]) ) == 0){
 402     memset(&(pathname[l-lext]),'\0',lext+1);
 403     l = strlen(pathname);
 404       }
 405     }
 406   }
 407 
 408   /* strip off leading forward slashes */
 409   while(l > 0 && pathname[l-1] == '/'){
 410     pathname[l-1] = '\0';
 411     l = strlen(pathname);
 412   }
 413   
 414   if(l < 2){
 415     /* basename is / or . or single character */
 416     basename = (char *) calloc(2,sizeof(char));
 417     if(l==0) basename[0] = '/';
 418     else     basename[0] = pathname[0];
 419     memcpy(pathname,tmp,strlen(tmp));
 420     free(tmp);
 421     return(basename);
 422   }
 423 
 424   /* Start at the end of the path name and step back
 425      until a forward slash is found */
 426   for(n=l; n >= 0; n--) if(pathname[n] == '/') break;
 427 
 428   basename = (char *) calloc(l-n,sizeof(char));
 429   memcpy(basename,&(pathname[n+1]),l-n);
 430 
 431   // Make sure that pathname does not change
 432   memcpy(pathname,tmp,strlen(tmp));
 433   free(tmp);
 434 
 435   return(basename);
 436 }
 437 /*--------------------------------------------------------------
 438   fio_extension() - returns the extension of the given filename.
 439   Author: Douglas Greve, 1/30/2002
 440   -------------------------------------------------------------*/
 441 char *fio_extension(char *pathname)
 442 {
 443   int lpathname,n, lext;
 444   char *ext;
 445 
 446   if(pathname == NULL) return(NULL);
 447 
 448   lpathname = strlen(pathname);
 449 
 450   lext = 0;
 451   n = lpathname - 1;
 452   while(n >= 0 && pathname[n] != '.') {
 453     n--;
 454     lext++;
 455   }
 456 
 457   /* A dot was not found, return NULL */
 458   if(n < 0) return(NULL);
 459 
 460   /* A dot was not found at the end of the file name */
 461   if(lext == 0) return(NULL);
 462 
 463   ext = (char *) calloc(sizeof(char),lext+1);
 464   memcpy(ext,&(pathname[n+1]),lext);
 465 
 466   return(ext);
 467 }
 468 /* -----------------------------------------------------
 469   fio_DirIsWritable(char *dirname, int fname) -- tests
 470   whether the given directory is writable by creating
 471   and deleting a junk file there. If fname != 0, then
 472   dirname is treated as path to a filename. It will
 473   return 0 if the directory does not exist.
 474   ----------------------------------------------------- */
 475 int fio_DirIsWritable(char *dirname, int fname)
 476 {
 477   FILE *fp;
 478   char tmpstr[2000];
 479 
 480   if(fname != 0)
 481     sprintf(tmpstr,"%s.junk.54_-_sdfklj",dirname);
 482   else
 483     sprintf(tmpstr,"%s/.junk.54_-_sdfklj",dirname);
 484   
 485   fp = fopen(tmpstr,"w");
 486   if(fp == NULL) return(0);
 487 
 488   fclose(fp);
 489   unlink(tmpstr);
 490 
 491   return(1);
 492 }
 493 /*-----------------------------------------------------
 494   fio_FileExistsReadable() - file exists and is readable
 495   -----------------------------------------------------*/
 496 int fio_FileExistsReadable(char *fname)
 497 {
 498   FILE *fp;
 499 
 500   fp = fopen(fname,"r");
 501   if(fp != NULL){
 502     fclose(fp);
 503     return(1);
 504   }
 505   return(0);
 506 }
 507 /*-----------------------------------------------------
 508   fio_IsDirectory(fname) - fname exists and is a directory
 509   -----------------------------------------------------*/
 510 int fio_IsDirectory(char *fname)
 511 {
 512   FILE *fp;
 513   struct stat buf;
 514   int err;
 515 
 516   fp = fopen(fname,"r");
 517   if(fp == NULL) return(0);
 518   fclose(fp);
 519   err = stat(fname, &buf);
 520   if(err != 0) return(0);
 521   return(S_ISDIR(buf.st_mode));
 522 }
 523 /*------------------------------------------------------------
 524   fio_NLines() - get the number of lines. The line length
 525   should not exceed 4000 characters.
 526   ------------------------------------------------------------*/
 527 int fio_NLines(char *fname)
 528 {
 529   FILE *fp;
 530   int nrows;
 531   char tmpstring[4001];
 532 
 533   fp = fopen(fname,"r");
 534   if(fp == NULL){
 535     printf("ERROR: cannot open %s\n",fname);
 536     return(-1);
 537   }
 538 
 539   nrows = 0;
 540   while(fgets(tmpstring,4000,fp) != NULL)  nrows ++;
 541   fclose(fp);
 542 
 543   return(nrows);
 544 }
 545 
 546 
 547 /*------------------------------------------------------------------------*/
 548 int fio_pushd(char *dir)
 549 {
 550   extern int fio_npushes;
 551   extern char fio_dirstack[FIO_NPUSHES_MAX][1000];
 552   int err;
 553 
 554   fio_npushes ++;
 555   if(fio_npushes == FIO_NPUSHES_MAX){
 556     printf("ERROR: fio_pushd: maximum number of pushes reached\n");
 557     return(1);
 558   }
 559   getcwd(fio_dirstack[fio_npushes],1000);
 560   err = chdir(dir);
 561   if(err){
 562     printf("ERROR: fio_pushd: %s\n",dir);
 563     fio_npushes --;
 564     return(1);
 565   }
 566 
 567   //printf("fio_pushd: %d %s\n",fio_npushes+1,dir);
 568 
 569   return(0);
 570 }
 571 /*------------------------------------------------------------------------*/
 572 int fio_popd(void)
 573 {
 574   extern int fio_npushes; 
 575   extern char fio_dirstack[FIO_NPUSHES_MAX][1000];
 576   int err;
 577 
 578   if(fio_npushes == -1){
 579     printf("ERROR: fio_popd: dir stack is empty\n");
 580     return(1);
 581   }
 582 
 583   err = chdir(fio_dirstack[fio_npushes]);
 584   if(err){
 585     printf("ERROR: fio_popd: %d %s\n",fio_npushes,fio_dirstack[fio_npushes]);
 586     return(1);
 587   }
 588   fio_npushes --;
 589   //printf("fio_popd: %d %s\n",fio_npushes+1,fio_dirstack[fio_npushes+1]);
 590 
 591   return(0);
 592 }
 593 
 594 
 595 /*--------------------------------------------------------------------
 596   fio_fullpath() - gets full path to a file. The file must exist. Works
 597   by pushing into the file dir, getting the cwd, appending the file
 598   basename to the cwd to get the full path, then popping the stack.
 599   -------------------------------------------------------------------*/
 600 char *fio_fullpath(char *fname)
 601 {
 602   static char cwd[1000];
 603   char *dirname, *basename;
 604   char *fullpath;
 605   int err;
 606 
 607   basename = fio_basename(fname,NULL);
 608   dirname  = fio_dirname(fname);
 609 
 610   err = fio_pushd(dirname);
 611   if(err){
 612     free(dirname);
 613     free(basename);
 614     return(NULL);
 615   }
 616   getcwd(cwd,1000);
 617   fio_popd();
 618 
 619   sprintf(cwd,"%s/%s",cwd,basename);
 620   fullpath = strcpyalloc(cwd);
 621 
 622   free(dirname);
 623   free(basename);
 624   
 625   return(fullpath);
 626 }
 627 
 628 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.