From 5e0c5bc840f7c64e93c7860cf89a058718dfeedc Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 5 Dec 2022 13:58:15 +0100 Subject: [PATCH] change private function calls --- code.c | 2 +- compress.c | 87 +++++++++++++++++++++++++++--------------------------- debug.h | 2 +- fprintf.c | 2 +- fprintf.h | 2 +- 5 files changed, 48 insertions(+), 47 deletions(-) diff --git a/code.c b/code.c index 6f04c18..70b170e 100644 --- a/code.c +++ b/code.c @@ -46,7 +46,7 @@ int codcpy (char *dst, size_t n, char *src) return i; } } - VERBOSE (ERROR, _fprintf (stdout, "Buffer too short\n")); + VERBOSE (ERROR, myfprintf (stdout, "Buffer too short\n")); return -1; } diff --git a/compress.c b/compress.c index 91f0d0e..26e922e 100644 --- a/compress.c +++ b/compress.c @@ -1,9 +1,10 @@ /* depend: */ /* cflags: */ -/* linker: code.o debug.o fprintf.o */ +/* linker: atoi.o code.o debug.o fprintf.o */ #include #include +#include "atoi.h" #include "code.h" #include "debug.h" #include "fprintf.h" @@ -23,11 +24,11 @@ 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); + myfprintf (fd, "usage: %s\n", progname); + myfprintf (fd, " -h : help message\n"); + myfprintf (fd, " -i : input file\n"); + myfprintf (fd, " -o : output file\n"); + myfprintf (fd, " -v : verbose level (%d)\n", verbose); exit (ret); } @@ -53,19 +54,19 @@ int *create_table (char *filename) /* memory allocation */ table = (int *) calloc (NB_BYTES, sizeof (int)); if (table == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n")); return NULL; } - VERBOSE (INFO, _fprintf (stdout, "memory allocated\n")); + VERBOSE (INFO, myfprintf (stdout, "memory allocated\n")); /* open file */ fid = fopen (filename, "rb"); if (fid == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s'\n", filename)); + VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename)); free (table); return NULL; } - VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", filename)); + VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename)); /* read file */ while (!feof (fid)) { @@ -90,10 +91,10 @@ void print_occ_table (int *table) { int i; - _fprintf (stdout, "Occurence table\n"); + myfprintf (stdout, "Occurence table\n"); for (i = 0; i < NB_BYTES; i++) { if (table[i]) { - _fprintf (stdout, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); + myfprintf (stdout, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); } } } @@ -128,7 +129,7 @@ leaf_t **init_forest (int *table) /* allocate memory */ leafs = (leaf_t **) calloc (nb_leafs + 1, sizeof (leaf_t *)); if (leafs == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -137,7 +138,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, _fprintf (stdout, "can't allocate memory\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n")); return NULL; } leafs[l]->occ = table[i]; @@ -195,12 +196,12 @@ leaf_t *create_tree (leaf_t **leafs) /* create branch */ if ((last == -1) || (ante == -1)) { - VERBOSE (ERROR, _fprintf (stdout, "error during tree building\n")); + VERBOSE (ERROR, myfprintf (stdout, "error during tree building\n")); return NULL; } branch = (leaf_t *) calloc (1, sizeof (leaf_t)); if (branch == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n")); return NULL; } branch->left = leafs[last]; @@ -256,7 +257,7 @@ code_t *create_code (leaf_t *root) /* allocate table */ table = (code_t *) calloc (NB_BYTES, sizeof (code_t)); if (table == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -274,13 +275,13 @@ void print_code_table (code_t *codes) char *code; int i; - _fprintf (stdout, "Code table\n"); + myfprintf (stdout, "Code table\n"); for (i = 0; i < NB_BYTES; i++) { code = (char *)(codes + i); if (codlen (code) == 0) { continue; } - _fprintf (stdout, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code); + myfprintf (stdout, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code); } } @@ -391,9 +392,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++) { - _fprintf (stdout, "%02x", header[i]); + myfprintf (stdout, "%02x", header[i]); } - _fprintf (stdout, "\n"); + myfprintf (stdout, "\n"); } /* write crompressed file */ @@ -413,18 +414,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, _fprintf (stdout, "can't open file '%s' for reading\n", input)); + VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } - VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", input)); + VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input)); /* open output file */ fout = fopen (output, "wb"); if (fin == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for writing\n", output)); + VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for writing\n", output)); return 1; } - VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", output)); + VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output)); /* write header */ length = (header[3] << 8) + header[4]; @@ -499,10 +500,10 @@ code_t *read_header (char *filename) { /* open file */ fid = fopen (filename, "rb"); if (fid == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s'\n", filename)); + VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename)); return NULL; } - VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", filename)); + VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename)); /* read magic number */ nb = fread (buffer, 1, 6, fid); @@ -523,7 +524,7 @@ code_t *read_header (char *filename) { } fclose (fid); if (mode == 0) { - VERBOSE (ERROR, _fprintf (stdout, "incorrect file\n")); + VERBOSE (ERROR, myfprintf (stdout, "incorrect file\n")); return NULL; } @@ -552,14 +553,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, _fprintf (stdout, "incorrect code table length\n")); + VERBOSE (ERROR, myfprintf (stdout, "incorrect code table length\n")); return NULL; } /* allocate table */ table = (code_t *) calloc (NB_BYTES, sizeof (code_t)); if (table == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -605,25 +606,25 @@ int write_decompress (char *output, char *input, code_t *codes) /* open file for reading */ fin = fopen (input, "rb"); if (fin == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for reading\n", input)); + VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } - VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", input)); + VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input)); /* read magic number */ nb = fread (bufhea, 1, 6, fin); if (nb != 6) { - VERBOSE (ERROR, _fprintf (stdout, "can't read file\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't read file\n")); fclose (fin); return 1; } size = (bufhea[3] << 8) + bufhea[4]; - VERBOSE (DEBUG, _fprintf (stdout, "table size: %d\n", size)); + VERBOSE (DEBUG, myfprintf (stdout, "table size: %d\n", size)); rem = bufhea[5]; - VERBOSE (DEBUG, _fprintf (stdout, "remainder: %d\n", rem)); + VERBOSE (DEBUG, myfprintf (stdout, "remainder: %d\n", rem)); nb = fread (bufhea, 1, size, fin); if (nb != size) { - VERBOSE (ERROR, _fprintf (stdout, "can't read file\n")); + VERBOSE (ERROR, myfprintf (stdout, "can't read file\n")); fclose (fin); return 1; } @@ -631,10 +632,10 @@ int write_decompress (char *output, char *input, code_t *codes) /* open file for writing */ fout = fopen (output, "wb"); if (fout == NULL) { - VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for writing\n", output)); + VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for writing\n", output)); return 2; } - VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", output)); + VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output)); /* write file */ pt = bufout; @@ -708,7 +709,7 @@ int main (int argc, char *argv[]) while (argc-- > 1) { arg = *(++argv); if (arg[0] != '-') { - _fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + myfprintf (stderr, "%s: invalid option -- %s\n", progname, arg); usage (1); } c = arg[1]; @@ -731,11 +732,11 @@ int main (int argc, char *argv[]) case 'v': arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; if (arg == NULL) { - _fprintf (stderr, "%s: missing verbose level\n", progname); + myfprintf (stderr, "%s: missing verbose level\n", progname); usage (1); } - verbose = atoi (arg); - VERBOSE (INFO, _fprintf (stdout, "verbose: %d\n", verbose)); + verbose = myatoi (arg); + VERBOSE (INFO, myfprintf (stdout, "verbose: %d\n", verbose)); break; case 'h': default: @@ -743,7 +744,7 @@ int main (int argc, char *argv[]) } } if ((input == NULL) || (output == NULL)) { - _fprintf (stderr, "%s: missing file\n", progname); + myfprintf (stderr, "%s: missing file\n", progname); usage (1); } VERBOSE (DEBUG, PRINTF ("end processing arguments\n")); diff --git a/debug.h b/debug.h index 64ec161..a0d26f7 100644 --- a/debug.h +++ b/debug.h @@ -23,7 +23,7 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0) -#define PRINTF(args...) do { _fprintf (stdout, args); fflush (stdout); } while (0) +#define PRINTF(args...) do { myfprintf (stdout, args); fflush (stdout); } while (0) /* gobal variables */ diff --git a/fprintf.c b/fprintf.c index 47b19c7..405957e 100644 --- a/fprintf.c +++ b/fprintf.c @@ -26,7 +26,7 @@ inline unsigned int nextpow10 (unsigned int x) { /* simple fprintf function */ -size_t _fprintf (FILE *fid, const char *fmt, ...) +size_t myfprintf (FILE *fid, const char *fmt, ...) { char buffer[1024 + 1] = { 0 }; char *str = buffer; diff --git a/fprintf.h b/fprintf.h index 053620e..1820f1b 100644 --- a/fprintf.h +++ b/fprintf.h @@ -4,7 +4,7 @@ #include #include -size_t _fprintf (FILE *fid, const char *fmt, ...); +size_t myfprintf (FILE *fid, const char *fmt, ...); #endif /* __FPRINTF_H__ */ -- 2.30.2