X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=compress.c;h=91f0d0e1d1ea993550114da81dc9664796f2576b;hb=5f83300ce6a5da27e68085bddce47b736cfdea79;hp=65d12fe7b7b248f621324e91355575334946b306;hpb=c9987f3b01b7d9c14c358b862d7a8d174476e569;p=compress.git diff --git a/compress.c b/compress.c index 65d12fe..91f0d0e 100644 --- a/compress.c +++ b/compress.c @@ -1,15 +1,12 @@ /* depend: */ /* cflags: */ -/* linker: code.o debug.o */ +/* linker: code.o debug.o fprintf.o */ -#include -#include #include #include -#include -#include #include "code.h" #include "debug.h" +#include "fprintf.h" /* constants */ @@ -26,15 +23,22 @@ char *progname = NULL; void usage (int ret) { FILE *fd = ret ? stderr : stdout; - fprintf (fd, "usage: %s\n", progname); - fprintf (fd, " -h : help message\n"); - fprintf (fd, " -i : input file\n"); - fprintf (fd, " -o : output file\n"); - fprintf (fd, " -v : verbose level (%d)\n", verbose); + _fprintf (fd, "usage: %s\n", progname); + _fprintf (fd, " -h : help message\n"); + _fprintf (fd, " -i : input file\n"); + _fprintf (fd, " -o : output file\n"); + _fprintf (fd, " -v : verbose level (%d)\n", verbose); exit (ret); } +void blkcpy (void *dst, const void *src, int len) +{ + while (len--) { + *((char *)dst++) = *((char *)src++); + } +} + /* create occurence table */ int *create_table (char *filename) @@ -49,19 +53,19 @@ int *create_table (char *filename) /* memory allocation */ table = (int *) calloc (NB_BYTES, sizeof (int)); if (table == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } - VERBOSE (INFO, printf ("memory allocated\n")); + VERBOSE (INFO, _fprintf (stdout, "memory allocated\n")); /* open file */ fid = fopen (filename, "rb"); if (fid == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s'\n", filename)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s'\n", filename)); free (table); return NULL; } - VERBOSE (INFO, printf ("file '%s' opened\n", filename)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", filename)); /* read file */ while (!feof (fid)) { @@ -86,10 +90,10 @@ void print_occ_table (int *table) { int i; - printf ("Occurence table\n"); + _fprintf (stdout, "Occurence table\n"); for (i = 0; i < NB_BYTES; i++) { if (table[i]) { - printf ("0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); + _fprintf (stdout, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); } } } @@ -124,7 +128,7 @@ leaf_t **init_forest (int *table) /* allocate memory */ leafs = (leaf_t **) calloc (nb_leafs + 1, sizeof (leaf_t *)); if (leafs == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -133,7 +137,7 @@ leaf_t **init_forest (int *table) if (table[i] > 0) { leafs[l] = (leaf_t *) calloc (1, sizeof (leaf_t)); if (leafs[l] == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } leafs[l]->occ = table[i]; @@ -191,12 +195,12 @@ leaf_t *create_tree (leaf_t **leafs) /* create branch */ if ((last == -1) || (ante == -1)) { - VERBOSE (ERROR, printf ("error during tree building\n")); + VERBOSE (ERROR, _fprintf (stdout, "error during tree building\n")); return NULL; } branch = (leaf_t *) calloc (1, sizeof (leaf_t)); if (branch == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } branch->left = leafs[last]; @@ -252,7 +256,7 @@ code_t *create_code (leaf_t *root) /* allocate table */ table = (code_t *) calloc (NB_BYTES, sizeof (code_t)); if (table == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -270,13 +274,13 @@ void print_code_table (code_t *codes) char *code; int i; - printf ("Code table\n"); + _fprintf (stdout, "Code table\n"); for (i = 0; i < NB_BYTES; i++) { code = (char *)(codes + i); if (codlen (code) == 0) { continue; } - printf ("0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code); + _fprintf (stdout, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code); } } @@ -371,7 +375,7 @@ byte_t *encode_header_table (code_t *codes, int *occ) /* allocation */ header = (byte_t *) calloc (length + 6, 1); - memcpy (header, buffer, length + 6); + blkcpy (header, buffer, length + 6); VERBOSE (DEBUG, PRINTF ("end encoding header and code table\n")); @@ -387,9 +391,9 @@ void print_header (byte_t *header) length = (header[3] << 8) + header[4]; VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length)); for (i = 0; i < length + 6; i++) { - printf ("%02x", header[i]); + _fprintf (stdout, "%02x", header[i]); } - printf ("\n"); + _fprintf (stdout, "\n"); } /* write crompressed file */ @@ -409,18 +413,18 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) /* open input file */ fin = fopen (input, "rb"); if (fin == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for reading\n", input)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } - VERBOSE (INFO, printf ("file '%s' opened\n", input)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", input)); /* open output file */ fout = fopen (output, "wb"); if (fin == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for writing\n", output)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for writing\n", output)); return 1; } - VERBOSE (INFO, printf ("file '%s' opened\n", output)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", output)); /* write header */ length = (header[3] << 8) + header[4]; @@ -495,10 +499,10 @@ code_t *read_header (char *filename) { /* open file */ fid = fopen (filename, "rb"); if (fid == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s'\n", filename)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s'\n", filename)); return NULL; } - VERBOSE (INFO, printf ("file '%s' opened\n", filename)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", filename)); /* read magic number */ nb = fread (buffer, 1, 6, fid); @@ -519,7 +523,7 @@ code_t *read_header (char *filename) { } fclose (fid); if (mode == 0) { - VERBOSE (ERROR, printf ("incorrect file\n")); + VERBOSE (ERROR, _fprintf (stdout, "incorrect file\n")); return NULL; } @@ -548,14 +552,14 @@ code_t *read_header (char *filename) { } if (((mode == 1) && (size - 256 != (l + 7) / 8)) || ((mode == 2) && (size - 2 * nb - 1 != (l + 7) / 8))) { - VERBOSE (ERROR, printf ("incorrect code table length\n")); + VERBOSE (ERROR, _fprintf (stdout, "incorrect code table length\n")); return NULL; } /* allocate table */ table = (code_t *) calloc (NB_BYTES, sizeof (code_t)); if (table == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -601,25 +605,25 @@ int write_decompress (char *output, char *input, code_t *codes) /* open file for reading */ fin = fopen (input, "rb"); if (fin == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for reading\n", input)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } - VERBOSE (INFO, printf ("file '%s' opened\n", input)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", input)); /* read magic number */ nb = fread (bufhea, 1, 6, fin); if (nb != 6) { - VERBOSE (ERROR, printf ("can't read file\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't read file\n")); fclose (fin); return 1; } size = (bufhea[3] << 8) + bufhea[4]; - VERBOSE (DEBUG, printf ("table size: %d\n", size)); + VERBOSE (DEBUG, _fprintf (stdout, "table size: %d\n", size)); rem = bufhea[5]; - VERBOSE (DEBUG, printf ("remainder: %d\n", rem)); + VERBOSE (DEBUG, _fprintf (stdout, "remainder: %d\n", rem)); nb = fread (bufhea, 1, size, fin); if (nb != size) { - VERBOSE (ERROR, printf ("can't read file\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't read file\n")); fclose (fin); return 1; } @@ -627,10 +631,10 @@ int write_decompress (char *output, char *input, code_t *codes) /* open file for writing */ fout = fopen (output, "wb"); if (fout == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for writing\n", output)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for writing\n", output)); return 2; } - VERBOSE (INFO, printf ("file '%s' opened\n", output)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", output)); /* write file */ pt = bufout; @@ -699,8 +703,15 @@ int main (int argc, char *argv[]) progname = argv[0]; int c; + char * arg; VERBOSE (DEBUG, PRINTF ("start processing arguments\n")); - while ((c = getopt(argc, argv, "cdhi:o:v:")) != EOF) { + while (argc-- > 1) { + arg = *(++argv); + if (arg[0] != '-') { + _fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + usage (1); + } + c = arg[1]; VERBOSE (DEBUG, PRINTF ("option: %c\n", c)); switch (c) { case 'c': @@ -710,24 +721,29 @@ int main (int argc, char *argv[]) mode = DECOMPRESS; break; case 'i': - input = optarg; + input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; VERBOSE (DEBUG, PRINTF ("input: %s\n", input)); break; case 'o': - output = optarg; + output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; VERBOSE (DEBUG, PRINTF ("output: %s\n", output)); break; case 'v': - verbose = atoi (optarg); - VERBOSE (INFO, printf ("verbose: %d\n", verbose)); + arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; + if (arg == NULL) { + _fprintf (stderr, "%s: missing verbose level\n", progname); + usage (1); + } + verbose = atoi (arg); + VERBOSE (INFO, _fprintf (stdout, "verbose: %d\n", verbose)); break; case 'h': default: usage (c != 'h'); } } - if (argc - optind != 0) { - fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]); + if ((input == NULL) || (output == NULL)) { + _fprintf (stderr, "%s: missing file\n", progname); usage (1); } VERBOSE (DEBUG, PRINTF ("end processing arguments\n")); @@ -779,4 +795,4 @@ int main (int argc, char *argv[]) // test: cmp compress.c tmp.c // test: rm compress.mz tmp.c -// vim: ts=4 sw=4 et +/* vim: set ts=4 sw=4 et */