home *** CD-ROM | disk | FTP | other *** search
/ Los Alamos National Laboratory / LANL_CD.ISO / software / compres / src / src_rle / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-17  |  2.0 KB  |  103 lines

  1. #include <sys/types.h>
  2. #include <sys/uio.h>
  3. #include <malloc.h>
  4. #include <stdio.h>
  5. long lseek();
  6.  
  7. cread(name, x, nbyte, offset_byte)
  8. char *name, *x;
  9. int nbyte, offset_byte;
  10. {  int fd, status;
  11.  
  12.    fd = open(name, 0);
  13.    if(fd == -1)
  14.    {  printf("ERROR: Error opening %s for read\n", name);
  15.       exit(-1);
  16.    }
  17.  
  18.    lseek(fd, (long)offset_byte, 1);
  19.  
  20.    status = read(fd, x, nbyte);
  21.    if(status == -1)
  22.    {  printf("ERROR: Error reading file %s\n", name);
  23.       exit(-1);
  24.    }
  25.  
  26.    status = close(fd);
  27.    if(status == -1) 
  28.    {  printf("ERROR:  Error closing %s after read\n", name);
  29.       exit(-1);
  30.    }
  31. }
  32.  
  33. /**********************************************************************/
  34.  
  35. cwrite(name, x, nbyte, offset_byte)
  36. char *name, *x;
  37. int nbyte, offset_byte;
  38. {  int fd, status;
  39.  
  40.   if((fd = open(name, 1)) == -1)
  41.       fd = creat(name, 0644);
  42.  
  43.    lseek(fd, (long)offset_byte, 1);
  44.  
  45.    status = write(fd, x, nbyte);
  46.    if(status == -1)
  47.    {  printf("ERROR: Error writing file %s\n", name);
  48.       exit(-1);
  49.    }
  50.  
  51.    status = close(fd);
  52.    if(status == -1) 
  53.    {  printf("ERROR:  Error closing %s after write\n", name);
  54.       exit(-1);
  55.    }
  56.  }
  57.  
  58. /****************************************************************/
  59.  
  60. #define PAGE_SIZE 262144
  61.  
  62. int cread2(name, x)
  63. char *name, **x;
  64. {
  65.     int fd, status, nbyte, nbyte_tot = 0;
  66.     char *xx;
  67.  
  68.     fd = open(name, 0);
  69.     if(fd == -1) {
  70.         printf("ERROR: Error opening %s for read\n", name);
  71.         exit(-1);
  72.     }
  73.  
  74.     *x = malloc(PAGE_SIZE);
  75.     while(1) {
  76.  
  77.         xx = *x + nbyte_tot;
  78.         nbyte_tot += ( nbyte = read(fd, xx, PAGE_SIZE) );
  79.  
  80.         if(nbyte < 0) {
  81.             printf("ERROR: Error reading file %s\n", name);
  82.             exit(-1);
  83.     }
  84.  
  85.         else if( nbyte < PAGE_SIZE ) {
  86.             *x = realloc(*x, (unsigned)nbyte_tot);
  87.             break;
  88.     }
  89.  
  90.         else
  91.             *x = realloc(*x, (unsigned)(nbyte_tot + PAGE_SIZE));
  92.     }
  93.  
  94.     status = close(fd);
  95.     if(status == -1) {
  96.         printf("ERROR:  Error closing %s after read\n", name);
  97.         exit(-1);
  98.     }
  99.  
  100.     return(nbyte_tot);
  101.  
  102. }
  103.