Merge branch 'master' of https://secure.softndesign.org/git/compress
[compress.git] / code.c
... / ...
CommitLineData
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
12int 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
24int codcmp (char *cod1, char *cod2)
25{
26 while ((*cod1 != 0) || (*cod2 != 0)) {
27 int test = *cod1++ - *cod2++;
28 if (test != 0) {
29 return (test < 0) ? -1 : 1;
30 }
31 }
32
33 return 0;
34}
35
36/* copy code */
37
38int codcpy (char *dst, int n, char *src)
39{
40 int i;
41
42 for (i = 0; i < n; i++) {
43 dst[i] = src[i];
44 if (src[i]== 0) {
45 return i;
46 }
47 }
48 VERBOSE (ERROR, PRINTERR ("Buffer too short\n"));
49
50 return -1;
51}
52
53/* code length */
54
55int codlen (char *code)
56{
57 int i = 0;
58
59 while (code[i] != 0) {
60 i++;
61 }
62
63 return i;
64}
65
66/* get leaf(s) */
67
68leaf_t *getleaf (int n)
69{
70 static leaf_t leafs[NB_LEAFS] = {0};
71 static int pos = 0;
72 leaf_t *pt = (void *)0;
73
74 if (pos + n < NB_LEAFS) {
75 pt = leafs + pos;
76 pos += n;
77 } else {
78 VERBOSE (ERROR, PRINTERR ("No more leaf avaliable\n"));
79 }
80
81 return pt;
82}
83
84/* vim: set ts=4 sw=4 et: */