/* depend: */
/* cflags: */
-/* linker: code.o debug.o */
+/* linker: code.o debug.o fprintf.o */
#include <malloc.h>
#include <stdio.h>
#include "code.h"
#include "debug.h"
+#include "fprintf.h"
/* constants */
void usage (int ret)
{
FILE *fd = ret ? stderr : stdout;
- fprintf (fd, "usage: %s\n", progname);
- fprintf (fd, " -h : help message\n");
- fprintf (fd, " -i <file>: input file\n");
- fprintf (fd, " -o <file>: 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 <file>: input file\n");
+ _fprintf (fd, " -o <file>: output file\n");
+ _fprintf (fd, " -v : verbose level (%d)\n", verbose);
exit (ret);
}
/* 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)) {
{
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]);
}
}
}
/* 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;
}
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];
/* 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];
/* 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;
}
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);
}
}
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 */
/* 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];
/* 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);
}
fclose (fid);
if (mode == 0) {
- VERBOSE (ERROR, printf ("incorrect file\n"));
+ VERBOSE (ERROR, _fprintf (stdout, "incorrect file\n"));
return NULL;
}
}
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;
}
/* 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;
}
/* 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;
while (argc-- > 1) {
arg = *(++argv);
if (arg[0] != '-') {
- fprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
+ _fprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
usage (1);
}
c = arg[1];
case 'v':
arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
if (arg == NULL) {
- fprintf (stderr, "%s: missing verbose level\n", progname);
+ _fprintf (stderr, "%s: missing verbose level\n", progname);
usage (1);
}
verbose = atoi (arg);
- VERBOSE (INFO, printf ("verbose: %d\n", verbose));
+ VERBOSE (INFO, _fprintf (stdout, "verbose: %d\n", verbose));
break;
case 'h':
default:
}
}
if ((input == NULL) || (output == NULL)) {
- fprintf (stderr, "%s: missing file\n", progname);
+ _fprintf (stderr, "%s: missing file\n", progname);
usage (1);
}
VERBOSE (DEBUG, PRINTF ("end processing arguments\n"));
// 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 */