increase test coverage
[compress.git] / code.c
1 #include "debug.h"
2 #include "fdprintf.h"
3
4 #include "code.h"
5
6 /* constants */
7
8 #define NB_LEAFS ((NB_BYTES + 1) * NB_BYTES / 2)
9
10 /* concatenate code */
11
12 int codcat (char *dst, int n, char *src)
13 {
14 while ((*dst != 0) && (n > 0)) {
15 n--;
16 dst++;
17 }
18
19 return (n > 0) ? codcpy (dst, n, src) : -1;
20 }
21
22 /* compare 2 codes */
23
24 int codcmp (char *cod1, char *cod2)
25 {
26 int i = -1;
27
28 do {
29 i++;
30 if (cod1[i] != cod2[i]) {
31 return (cod1[i] < cod2[i]) ? -1 : 1;
32 }
33 } while (cod1[i] != 0);
34
35 return 0;
36 }
37
38 /* copy code */
39
40 int codcpy (char *dst, int n, char *src)
41 {
42 int i;
43
44 for (i = 0; i < n; i++) {
45 dst[i] = src[i];
46 if (src[i]== 0) {
47 return i;
48 }
49 }
50 VERBOSE (ERROR, PRINTERR ("Buffer too short\n"));
51
52 return -1;
53 }
54
55 /* code length */
56
57 int codlen (char *code)
58 {
59 int i = 0;
60
61 while (code[i] != 0) {
62 i++;
63 }
64
65 return i;
66 }
67
68 /* get leaf(s) */
69
70 leaf_t *getleaf (int n)
71 {
72 static leaf_t leafs[NB_LEAFS] = {0};
73 static int pos = 0;
74 leaf_t *pt = (void *)0;
75
76 if (pos + n < NB_LEAFS) {
77 pt = leafs + pos;
78 pos += n;
79 } else {
80 VERBOSE (ERROR, PRINTERR ("No more leaf avaliable\n"));
81 }
82
83 return pt;
84 }
85
86 /* vim: set ts=4 sw=4 et: */