GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/format.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/glocale.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 
6 #include <grass/config.h>
7 
8 #ifdef HAVE_UNISTD_H
9 #include <unistd.h>
10 #endif
11 
12 #include "G.h"
13 
53 /**********************************************************************
54  *
55  * G__check_format(int fd)
56  *
57  * Check to see if map with file descriptor "fd" is in compressed
58  * format. If it is, the offset table at the beginning of the
59  * file (which gives seek addresses into the file where code for
60  * each row is found) is read into the File Control Buffer (FCB).
61  * The compressed flag in the FCB is appropriately set.
62  *
63  * returns: 1 if row pointers were read successfully, -1 otherwise
64  **********************************************************************/
65 
67 {
68  struct fileinfo *fcb = &G__.fileinfo[fd];
69  unsigned char compress[4];
70 
71  /*
72  * Check to see if the file is in compress mode
73  * 4 possibilites
74  * compressed flag in cellhd is negative (meaning pre 3.0 cell file)
75  * compression flag is first 3 bytes of cell file
76  * compression flag is 0 - not compressed
77  * compression flag is 1 - compressed using RLE (int) or zlib (FP)
78  * compression flag is 2 - compressed using zlib
79  */
80 
81  if (fcb->cellhd.compressed < 0) {
82  if (read(fd, compress, 3) != 3
83  || compress[0] != 251 || compress[1] != 255 || compress[2] != 251)
84  fcb->cellhd.compressed = 0;
85  }
86 
87  if (!fcb->cellhd.compressed)
88  return fd;
89 
90  /* allocate space to hold the row address array */
91  fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
92 
93  /* read the row address array */
94  return G__read_row_ptrs(fd);
95 }
96 
98 {
99  struct fileinfo *fcb = &G__.fileinfo[fd];
100  int nrows = fcb->cellhd.rows;
101  unsigned char nbytes;
102  unsigned char *buf, *b;
103  int n;
104  int row;
105 
106  /*
107  * pre3.0 row addresses were written directly from the array of off_t's
108  * (this makes them machine dependent)
109  */
110 
111  if (fcb->cellhd.compressed < 0) {
112  n = (nrows + 1) * sizeof(off_t);
113  if (read(fd, fcb->row_ptr, n) != n)
114  goto badread;
115  return 1;
116  }
117 
118  /*
119  * 3.0 row address array is in a machine independent format
120  * (warning - the format will work even if the sizeof(off_t) is
121  * not the same from machine to machine, as long as the
122  * actual values do not exceed the capability of the off_t)
123  */
124 
125  if (read(fd, &nbytes, 1) != 1)
126  goto badread;
127  if (nbytes == 0)
128  goto badread;
129 
130  n = (nrows + 1) * nbytes;
131  buf = G_malloc(n);
132  if (read(fd, buf, n) != n)
133  goto badread;
134 
135  for (row = 0, b = buf; row <= nrows; row++) {
136  off_t v = 0;
137 
138  for (n = 0; n < (int)nbytes; n++) {
139  unsigned char c = *b++;
140 
141  if (nbytes > sizeof(off_t) && n < nbytes - sizeof(off_t) &&
142  c != 0)
143  goto badread;
144 
145  v <<= 8;
146  v += c;
147  }
148 
149  fcb->row_ptr[row] = v;
150  }
151 
152  G_free(buf);
153 
154  return 1;
155 
156  badread:
157  G_warning(_("Fail of initial read of compressed file [%s in %s]"),
158  fcb->name, fcb->mapset);
159  return -1;
160 }
161 
163 {
164  struct fileinfo *fcb = &G__.fileinfo[fd];
165  int nrows = fcb->cellhd.rows;
166  int nbytes = sizeof(off_t);
167  unsigned char *buf, *b;
168  int len, row, result;
169 
170  lseek(fd, 0L, SEEK_SET);
171 
172  len = (nrows + 1) * nbytes + 1;
173  b = buf = G_malloc(len);
174  *b++ = nbytes;
175 
176  for (row = 0; row <= nrows; row++) {
177  off_t v = fcb->row_ptr[row];
178  int i;
179 
180  for (i = nbytes - 1; i >= 0; i--) {
181  b[i] = v & 0xff;
182  v >>= 8;
183  }
184 
185  b += nbytes;
186  }
187 
188  result = (write(fd, buf, len) == len);
189  G_free(buf);
190 
191  return result;
192 }
int nbytes
Definition: G.h:58
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G__read_row_ptrs(int fd)
Definition: gis/format.c:97
float b
Definition: named_colr.c:8
int G__check_format(int fd)
Definition: gis/format.c:66
char * name
Definition: G.h:63
off_t * row_ptr
Definition: G.h:51
FILE * fd
Definition: g3dcolor.c:368
int G__write_row_ptrs(int fd)
Definition: gis/format.c:162
Definition: G.h:74
char * mapset
Definition: G.h:64
int
Definition: g3dcolor.c:48
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
struct fileinfo * fileinfo
Definition: G.h:95
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
struct Cell_head cellhd
Definition: G.h:44
Definition: G.h:41
int n
Definition: dataquad.c:291