From: Laurent Mazet Date: Mon, 14 Apr 2025 21:41:27 +0000 (+0200) Subject: full adaptation to c99 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=e8078542c9d21d573682492f07bc2100bc7faa36;p=compress.git full adaptation to c99 --- diff --git a/code.c b/code.c index b8c8e78..ac8b754 100644 --- a/code.c +++ b/code.c @@ -37,9 +37,7 @@ int codcmp (char *cod1, char *cod2) int codcpy (char *dst, int n, char *src) { - int i; - - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { dst[i] = src[i]; if (src[i]== 0) { return i; diff --git a/compress.c b/compress.c index d8842b4..7189fa7 100644 --- a/compress.c +++ b/compress.c @@ -82,10 +82,8 @@ int *create_table (char *filename) void print_occ_table (int *table) { - int i; - PRINTOUT ("Occurence table\n"); - for (i = 0; i < NB_BYTES; i++) { + for (int i = 0; i < NB_BYTES; i++) { if (table[i]) { PRINTOUT ("0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); } @@ -98,19 +96,18 @@ leaf_t **init_forest (int *table) { static leaf_t *leafs[NB_BYTES + 1] = {0}; int nb_leafs = 0; - int i, l; VERBOSE (DEBUG, PRINTOUT ("start initiliazing forest\n")); /* count number of leafs */ - for (i = 0; i < NB_BYTES; i++) { + for (int i = 0; i < NB_BYTES; i++) { if (table[i] > 0) { nb_leafs++; } } /* initialize leafs */ - for (i = 0, l = 0; i < NB_BYTES; i++) { + for (int i = 0, l = 0; i < NB_BYTES; i++) { if (table[i] > 0) { leafs[l] = getleaf (1); if (leafs[l] == NULL) { @@ -135,8 +132,6 @@ leaf_t *create_tree (leaf_t **leafs) leaf_t *branch = NULL; int nb_leafs = 0; int last = -1; - int ante; - int i, j; VERBOSE (DEBUG, PRINTOUT ("start creating tree\n")); @@ -146,11 +141,11 @@ leaf_t *create_tree (leaf_t **leafs) } /* create tree */ - for (j = 0; j < nb_leafs - 1; j++) { + for (int j = 0; j < nb_leafs - 1; j++) { /* look for leatest occurence */ last = -1; - for (i = 0; i < nb_leafs; i++) { + for (int i = 0; i < nb_leafs; i++) { if (leafs[i] == NULL) { continue; } @@ -160,8 +155,8 @@ leaf_t *create_tree (leaf_t **leafs) } /* look for ante leatest occurence */ - ante = -1; - for (i = 0; i < nb_leafs; i++) { + int ante = -1; + for (int i = 0; i < nb_leafs; i++) { if ((i == last) || (leafs[i] == NULL)) { continue; } @@ -202,7 +197,9 @@ void explore_tree (code_t *table, leaf_t *root, char *code, int index) if ((root->left == NULL) && (root->right == NULL)) { codcpy ((char *)(table + (int)(root->c)), sizeof (code_t), code); } - else { + else if ((root->left == NULL) || (root->right == NULL)) { + VERBOSE (WARNING, PRINTERR ("incoherent tree\n")); + } else { codcpy (code + index, sizeof (code_t), "1"); explore_tree (table, root->left, code, index + 1); codcpy (code + index, sizeof (code_t), "0"); @@ -231,12 +228,9 @@ code_t *create_code (leaf_t *root) void print_code_table (code_t *codes) { - char *code; - int i; - PRINTOUT ("Code table\n"); - for (i = 0; i < NB_BYTES; i++) { - code = (char *)(codes + i); + for (int i = 0; i < NB_BYTES; i++) { + char *code = (char *)(codes + i); if (codlen (code) == 0) { continue; } @@ -250,23 +244,21 @@ byte_t *encode_header_table (code_t *codes, int *occ) { static byte_t buffer[NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6] = {0}; char bits[(NB_BYTES - 1) + 8 + 1] = {0}; - char *code; byte_t *header = buffer; - int i, j, length, mode; - int nb = 0; - int size = 0; VERBOSE (DEBUG, PRINTOUT ("start encoding header and code table\n")); /* mode 1 or 2 */ - for (i = 0; i < NB_BYTES; i++) { - code = (char *)(codes + i); + int nb = 0; + int size = 0; + for (int i = 0; i < NB_BYTES; i++) { + char *code = (char *)(codes + i); if (codlen (code) > 0) { nb++; size += codlen (code) * occ[i]; } } - mode = (NB_BYTES < 2 * nb + 1) ? 1 : 2; + int mode = (NB_BYTES < 2 * nb + 1) ? 1 : 2; VERBOSE (DEBUG, PRINTOUT ("nb chars: %d\n", nb)); VERBOSE (DEBUG, PRINTOUT ("mode: %d\n", mode)); VERBOSE (DEBUG, PRINTOUT ("size: %d\n", size)); @@ -279,15 +271,15 @@ byte_t *encode_header_table (code_t *codes, int *occ) /* size */ switch (mode) { case 1: - for (i = 0; i < NB_BYTES; i++) { - code = (char *)(codes + i); + for (int i = 0; i < NB_BYTES; i++) { + char *code = (char *)(codes + i); *(header++) = (byte_t) codlen (code); } break; case 2: *(header++) = (byte_t)(nb - 1); - for (i = 0; i < NB_BYTES; i++) { - code = (char *)(codes + i); + for (int i = 0; i < NB_BYTES; i++) { + char *code = (char *)(codes + i); if (codlen (code) > 0) { *(header++) = (byte_t) i; *(header++) = (byte_t) codlen (code); @@ -297,12 +289,12 @@ byte_t *encode_header_table (code_t *codes, int *occ) } /* bits */ - for (i = 0; i < NB_BYTES; i++) { - code = (char *)(codes + i); + for (int i = 0; i < NB_BYTES; i++) { + char *code = (char *)(codes + i); if (codlen (code) > 0) { codcat (bits, sizeof (code_t), code); while (codlen (bits) > (8 - 1)) { - for (j = 0; j < 8; j++) { + for (int j = 0; j < 8; j++) { *header <<= 1; if (bits[j] == '1') { (*header)++; @@ -314,20 +306,20 @@ byte_t *encode_header_table (code_t *codes, int *occ) } } if (codlen (bits) > 0) { - for (j = 0; j < (int)codlen (bits); j++) { + for (int j = 0; j < (int)codlen (bits); j++) { *header <<= 1; if (bits[j] == '1') { (*header)++; } } - for (j = (int)codlen (bits); j < 8; j++) { + for (int j = (int)codlen (bits); j < 8; j++) { *header <<= 1; } header++; } /* length */ - length = (int)(header - buffer - 6); + int length = (int)(header - buffer - 6); VERBOSE (DEBUG, PRINTOUT ("lengh: %d %02x %02x\n", length, length >> 8, length & 0xff)); buffer[3] = (byte_t)(length >> 8); buffer[4] = (byte_t)(length & 0xff); @@ -343,11 +335,9 @@ byte_t *encode_header_table (code_t *codes, int *occ) void print_header (byte_t *header) { - int length, i; - - length = (header[3] << 8) + header[4]; + int length = (header[3] << 8) + header[4]; VERBOSE (DEBUG, PRINTOUT ("lengh: %d\n", length)); - for (i = 0; i < length + 6; i++) { + for (int i = 0; i < length + 6; i++) { PRINTOUT ("%02x", header[i]); } PRINTOUT ("\n"); @@ -360,15 +350,11 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) byte_t bufin[BUFFER_SIZE] = {0}; byte_t bufout[BUFFER_SIZE] = {0}; char bits[(NB_BYTES - 1) + 8 + 1] = {0}; - int fin, fout; - int length = 0; - int i, j, nbread, nbwrite; - byte_t *pt; VERBOSE (DEBUG, PRINTOUT ("start writting compressed file\n")); /* open input file */ - fin = open (input, O_RDONLY|O_RAW); + int fin = open (input, O_RDONLY|O_RAW); if (fin == -1) { VERBOSE (ERROR, PRINTERR ("can't open file '%s' for reading\n", input)); return 1; @@ -376,7 +362,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", input)); /* open output file */ - fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700); + int fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700); if (fout == -1) { VERBOSE (ERROR, PRINTERR ("can't open file '%s' for writing\n", output)); close (fin); @@ -385,9 +371,9 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", output)); /* write header */ - length = (header[3] << 8) + header[4]; + int length = (header[3] << 8) + header[4]; VERBOSE (DEBUG, PRINTOUT ("lengh: %d\n", length)); - nbwrite = write (fout, header, length + 6); + int nbwrite = write (fout, header, length + 6); if (nbwrite != length + 6) { VERBOSE (ERROR, PRINTERR ("can't write %d bytes in file '%s'\n", length + 6 - nbwrite, output)); close (fout); @@ -397,13 +383,14 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) /* write file */ - pt = bufout; + byte_t *pt = bufout; + int nbread; while ((nbread = read (fin, bufin, BUFFER_SIZE)) > 0) { VERBOSE (DEBUG, PRINTOUT ("nbread: %d\n", nbread)); - for (i = 0; i < nbread; i++) { + for (int i = 0; i < nbread; i++) { codcat (bits, sizeof (code_t), (char *)(codes + bufin[i])); while (codlen (bits) > (8 - 1)) { - for (j = 0; j < 8; j++) { + for (int j = 0; j < 8; j++) { *pt <<= 1; if (bits[j] == '1') { (*pt)++; @@ -427,13 +414,13 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) } VERBOSE (DEBUG, PRINTOUT ("lastest bits : %d\n", codlen (bits))); if (codlen (bits) > 0) { - for (j = 0; j < (int)codlen (bits); j++) { + for (int j = 0; j < (int)codlen (bits); j++) { *pt <<= 1; if (bits[j] == '1') { (*pt)++; } } - for (j = (int)codlen (bits); j < 8; j++) { + for (int j = (int)codlen (bits); j < 8; j++) { *pt <<= 1; } pt++; @@ -463,17 +450,12 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) code_t *read_header (char *filename) { static code_t table[NB_BYTES] = {0}; byte_t buffer[NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6] = {0}; - byte_t *codes = NULL; - byte_t cur; int lengths[NB_BYTES] = {0}; - int fid; - int mode = 0; - int i, j, l, nb, size; VERBOSE (DEBUG, PRINTOUT ("start reading header\n")); /* open file */ - fid = open (filename, O_RDONLY|O_RAW); + int fid = open (filename, O_RDONLY|O_RAW); if (fid == -1) { VERBOSE (ERROR, PRINTERR ("can't open file '%s'\n", filename)); return NULL; @@ -481,7 +463,9 @@ code_t *read_header (char *filename) { VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", filename)); /* read magic number */ - nb = read (fid, buffer, 6); + int mode = 0; + int nb = read (fid, buffer, 6); + int size = 0; VERBOSE (DEBUG, PRINTOUT ("nb, buffer: %d 0x%02x 0x%02x\n", nb, buffer[0], buffer[1])); if ((nb == 6) && (buffer[0] == 'M') && (buffer[2] == 'Z')) { mode = (buffer[1] == '1') ? 1 : (buffer[1] == '2') ? 2 : 0; @@ -504,26 +488,27 @@ code_t *read_header (char *filename) { } /* analyse header */ - codes = buffer; + byte_t *codes = buffer; switch (mode) { case 1: - for (i = 0; i < NB_BYTES; i++) { + for (int i = 0; i < NB_BYTES; i++) { lengths[i] = *(codes++); } break; case 2: nb = *(codes++) + 1; VERBOSE (DEBUG, PRINTOUT ("nb codes: %d\n", nb)); - for (i = 0; i < nb; i++) { - j = *(codes++); + for (int i = 0; i < nb; i++) { + int j = *(codes++); lengths[j] = *(codes++); } break; } - VERBOSE (DEBUG, for (i = 0; i < NB_BYTES; i++) if (lengths[i]) PRINTOUT ("%d: %d\n", i, lengths[i])); + VERBOSE (DEBUG, for (int i = 0; i < NB_BYTES; i++) if (lengths[i]) PRINTOUT ("%d: %d\n", i, lengths[i])); /* check lengths */ - for (i = 0, l = 0; i < NB_BYTES; i++) { + int l = 0; + for (int i = 0; i < NB_BYTES; i++) { l += lengths[i]; } if (((mode == 1) && (size - 256 != (l + 7) / 8)) || @@ -533,9 +518,9 @@ code_t *read_header (char *filename) { } /* decode code */ - cur = *(codes++); + byte_t cur = *(codes++); l = 8; - for (i = 0; i < NB_BYTES; i++) { + for (int i = 0; i < NB_BYTES; i++) { if (lengths[i] == 0) { continue; } @@ -563,16 +548,11 @@ int write_decompress (char *output, char *input, code_t *codes) byte_t bufout[BUFFER_SIZE] = {0}; byte_t bufhea[MAX(NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6, BUFFER_SIZE)] = {0}; char bits[(NB_BYTES - 1) + 1] = {0}; - int fin, fout; - int i, j, k, nb, size, nbwrite, rem; - int is_found; - int l = 0; - byte_t *pt; VERBOSE (DEBUG, PRINTOUT ("start writing decompressed file\n")); /* open file for reading */ - fin = open (input, O_RDONLY|O_RAW); + int fin = open (input, O_RDONLY|O_RAW); if (fin == -1) { VERBOSE (ERROR, PRINTERR ("can't open file '%s' for reading\n", input)); return 1; @@ -580,15 +560,15 @@ int write_decompress (char *output, char *input, code_t *codes) VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", input)); /* read magic number */ - nb = read (fin, bufhea, 6); + int nb = read (fin, bufhea, 6); if (nb != 6) { VERBOSE (ERROR, PRINTERR ("can't read file\n")); close (fin); return 1; } - size = (bufhea[3] << 8) + bufhea[4]; + int size = (bufhea[3] << 8) + bufhea[4]; VERBOSE (DEBUG, PRINTOUT ("table size: %d\n", size)); - rem = bufhea[5]; + int rem = bufhea[5]; VERBOSE (DEBUG, PRINTOUT ("remainder: %d\n", rem)); nb = read (fin, bufhea, size); if (nb != size) { @@ -598,7 +578,7 @@ int write_decompress (char *output, char *input, code_t *codes) } /* open file for writing */ - fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700); + int fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700); if (fout == -1) { VERBOSE (ERROR, PRINTERR ("can't open file '%s' for writing\n", output)); close (fin); @@ -607,19 +587,21 @@ int write_decompress (char *output, char *input, code_t *codes) VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", output)); /* write file */ - pt = bufout; + int l = 0; + int nbwrite = 0; + byte_t *pt = bufout; while ((nb = read (fin, bufin, BUFFER_SIZE)) > 0) { VERBOSE (DEBUG, PRINTOUT ("nbread: %d\n", nb)); - for (i = 0; i < nb; i++) { - for (j = 0; j < 8; j++) { + for (int i = 0; i < nb; i++) { + for (int j = 0; j < 8; j++) { codcat (bits, sizeof (bits), ((bufin[i] & 0x80) == 0) ? "0" : "1"); bufin[i] <<= 1; l++; VERBOSE (DEBUG, PRINTOUT ("bits: %d - %s\n", codlen (bits), bits)); /* look for correct code */ - is_found = 0; - for (k = 0; (k < NB_BYTES) && (!is_found); k++) { + int is_found = 0; + for (int k = 0; (k < NB_BYTES) && (!is_found); k++) { if (codcmp ((char *)(codes + k), bits) == 0) { is_found = 1; VERBOSE (DEBUG, PRINTOUT ("found: %d\n", k)); diff --git a/fdprintf.c b/fdprintf.c index 46f27ac..21c5c81 100644 --- a/fdprintf.c +++ b/fdprintf.c @@ -46,11 +46,10 @@ char *itoa (char *str, unsigned u, unsigned int sz) double tenpower(int n) { double t = 1.0; - int i; - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { t *= 10; } - for (i = 0; i > n; i--) { + for (int i = 0; i > n; i--) { t /= 10; } return t; @@ -88,7 +87,7 @@ int fdprintf (int fd, const char *fmt, ...) char *s; double f = 0.0; int d = 0; - unsigned int u; + unsigned long long int u; char c = *fmt++; /* copy standard char */ @@ -97,7 +96,7 @@ int fdprintf (int fd, const char *fmt, ...) } else { int t = 0; char w = '0'; - int i, sz = 0; + int sz = 0; void *p = NULL; /* stamp */ @@ -177,7 +176,8 @@ int fdprintf (int fd, const char *fmt, ...) } else { u = (uintptr_t)p; } - for (i = sz, t = 1; i > 0; i--) { + t = 1; + for (int i = sz; i > 0; i--) { char x = (char)((u >> (i * 4 - 4)) & 0xf); if ((t == 1) && (x == 0)) { *str++ = w; diff --git a/getcomments.pl b/getcomments.pl deleted file mode 100755 index 47edf67..0000000 --- a/getcomments.pl +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/perl - -use strict; - -# default value -my $format = "%"; -my $pattern = ""; - -# help message -sub usage() { - - print <) { - my $cmt; - - # process c++ comments - ($cmt, $_) = m{//\s*(.*?)\s*$()} if (m{//} && !m{/\*.*//}); - - # process standard c comments - ($cmt, $_) = m{^.*?/\*\s*(.*?)\s*\*/(.*)}s if (m{/\*.*\*/}s); - - push(@comments, $cmt) if ($cmt); - - # empty buffer if no comment is present - undef($_) if (!m{/[/*]}); - } - - # close file - close (IN); - - # display comment blocks - foreach my $block (@comments) { - if (($block) = ($block =~ /$pattern(.*)/s)) { - ($_ = $format) =~ s/%/$block/gs; - print "$_\n"; - } - } -}