81 #include <grass/config.h> 88 #include <grass/gis.h> 89 #include <grass/glocale.h> 93 #define G_COMPRESSED_NO (unsigned char)'0' 94 #define G_COMPRESSED_YES (unsigned char)'1' 119 if (number < 0 || number >= n_compressors)
131 if (number < 0 || number >= n_compressors) {
132 G_warning(_(
"Request for unsupported compressor"));
156 memcpy(dst, src, src_sz);
178 memcpy(dst, src, src_sz);
190 int dst_sz,
int number)
192 if (number < 0 || number >= n_compressors) {
205 int dst_sz,
int number)
207 if (number < 0 || number >= n_compressors) {
218 int bsize, nread,
err;
221 if (dst ==
NULL || nbytes <= 0) {
223 G_warning(_(
"No destination buffer allocated"));
225 G_warning(_(
"Invalid destination buffer size %d"), nbytes);
230 G_warning(_(
"Invalid read size %d"), nbytes);
237 if (
NULL == (b = (
unsigned char *)
238 G_calloc(bsize,
sizeof(
unsigned char))))
244 err = read(fd, b + nread, bsize - nread);
247 }
while (err > 0 && nread < bsize);
251 G_warning(_(
"Unable to read %d bytes: end of file"), rbytes);
253 G_warning(_(
"Unable to read %d bytes: %s"), rbytes, strerror(errno));
258 if (nread < rbytes) {
260 G_warning(
"Unable to read %d bytes, got %d bytes", rbytes, nread);
267 for (err = 0; err < nread - 1 && err < nbytes; err++)
268 dst[err] = b[err + 1];
276 G_warning(
"Read error: We're not at the start of a row");
284 err =
G_expand(b + 1, bsize - 1, dst, nbytes, number);
297 int dst_sz, nwritten,
err;
298 unsigned char *
dst, compressed;
301 if (src ==
NULL || nbytes < 0) {
305 G_warning(_(
"Invalid source buffer size %d"), nbytes);
310 if (
NULL == (dst = (
unsigned char *)
311 G_calloc(dst_sz,
sizeof(
unsigned char))))
315 err =
G_compress(src, nbytes, dst, dst_sz, number);
321 if (err > 0 && err < nbytes) {
325 if (write(fd, &compressed, 1) != 1) {
327 G_warning(_(
"Unable to write compression flag"));
332 err = write(fd, dst + nwritten, dst_sz - nwritten);
335 }
while (err > 0 && nwritten < dst_sz);
338 G_warning(_(
"Unable to write %d bytes: nothing written"), dst_sz);
340 G_warning(_(
"Unable to write %d bytes: %s"), dst_sz, strerror(errno));
348 if (write(fd, &compressed, 1) != 1) {
350 G_warning(_(
"Unable to write compression flag"));
355 err = write(fd, src + nwritten, nbytes - nwritten);
358 }
while (err > 0 && nwritten < nbytes);
361 G_warning(_(
"Unable to write %d bytes: nothing written"), nbytes);
363 G_warning(_(
"Unable to write %d bytes: %s"), nbytes, strerror(errno));
382 unsigned char compressed;
385 if (src ==
NULL || nbytes < 0)
390 if (write(fd, &compressed, 1) != 1) {
391 G_warning(_(
"Unable to write compression flag"));
398 err = write(fd, src + nwritten, nbytes - nwritten);
401 }
while (err > 0 && nwritten < nbytes);
404 G_warning(_(
"Unable to write %d bytes: nothing written"), nbytes);
406 G_warning(_(
"Unable to write %d bytes: %s"), nbytes, strerror(errno));
409 if (err < 0 || nwritten != nbytes)
int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes, int number)
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
struct compressor_list compressor[]
int G_compress(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz, int number)
char * G_compressor_name(int number)
int G_expand(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz, int number)
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
int G_no_expand(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz)
int G_write_uncompressed(int fd, const unsigned char *src, int nbytes)
int G_no_compress(unsigned char *src, int src_sz, unsigned char *dst, int dst_sz)
int G_check_compressor(int number)
int G_compressor_number(char *name)
int G_write_compressed(int fd, unsigned char *src, int nbytes, int number)
void G_free(void *buf)
Free allocated memory.
void G_warning(const char *msg,...)
Print a warning message to stderr.