diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/dict.sample | 6 | ||||
-rw-r--r-- | src/main.cc | 117 | ||||
-rw-r--r-- | src/test.cc | 16 |
4 files changed, 85 insertions, 56 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index c8a43f6..fb784dd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,3 +13,5 @@ LDADD = ../lib/libPriorityLists.la testTas_LDADD = $(LDADD) Huffman_LDADD = $(LDADD) ../lib/libHuffman.la + +EXTRA_DIST = dict.sample
\ No newline at end of file diff --git a/src/dict.sample b/src/dict.sample new file mode 100644 index 0000000..6a46bb0 --- /dev/null +++ b/src/dict.sample @@ -0,0 +1,6 @@ +MOT1 : 2000 +MOT2 : 1234 +MOT3 : 15987 +MOT4 : 1203 +MOT5 : 192837 +MOT6 : 12987 diff --git a/src/main.cc b/src/main.cc index 9cc96d5..16e9985 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,6 +8,7 @@ #include "BinHeap.h" #include "PLList.h" #include "Huffman.h" +#include "numbers.h" void exception(int e, char *msg) { @@ -18,8 +19,8 @@ void exception(int e, char *msg) PriorityList *newlist(int method) { switch (method) { - case 0: - return new BinHeap; + case 0: + return new BinHeap; break; case 1: return new BHeap; @@ -33,13 +34,13 @@ PriorityList *newlist(int method) default: cerr << _("Unknow priority list type: ") << method << endl; exit(-1); - } + } } void Count(FILE * strm, PriorityList * P) { - int tab[256], i, valid; - char *t; + int tab[256], i; + char t[2]; if (!strm) { cerr << _("Error opening file (") << strerror(errno) << ")\n"; @@ -56,7 +57,6 @@ void Count(FILE * strm, PriorityList * P) for (i = 0; i < 256; i++) { if (tab[i]) { - t = (char *) malloc(2); t[0] = i; t[1] = 0; HInsert(P, tab[i], t); @@ -64,18 +64,20 @@ void Count(FILE * strm, PriorityList * P) } } -void ReadDic(FILE * strm, PriorityList * P) { +void ReadDic(FILE * strm, PriorityList * P) +{ char t[1024], *f, *word, *p; + int valid, freq; if (!strm) { cerr << _("Error opening file (") << strerror(errno) << ")\n"; exit(-1); } - - while(gets(t)) { + + while (fgets(t, 1024, strm)) { if (!(f = strchr(t, ':'))) { cerr << _("Bad dictionnary structure. See doc/README.en (missing : separator)") << endl; - exit (-1); + exit(-1); } *(f++) = '\0'; word = t; @@ -88,19 +90,25 @@ void ReadDic(FILE * strm, PriorityList * P) { } if (!strlen(word)) { cerr << _("Bad dictionnary structure. See doc/README.en (missing word)") << endl; - exit (-1); + exit(-1); } while (*f == ' ') { f++; } p = f + strlen(f) - 1; - while (*p == ' ') { + while ((*p == ' ') || (*p == '\n')) { *(p--) = '\0'; } if (!strlen(f)) { cerr << _("Bad dictionnary structure. See doc/README.en (missing frequency)") << endl; - exit (-1); + exit(-1); + } + freq = char_to_number(f, &valid); + if (!valid) { + cerr << _("Error: \"") << f << _("\" is not a valid number.") << endl; + exit(-1); } + HInsert(P, freq, word); } } @@ -129,68 +137,77 @@ int main(int argc, char **argv) { PriorityList *P; HTree *H; - FILE * f; int method = -1, readm = -1; char *filename = NULL; while (--argc) { argv++; - if (*argv[0] == '-') { + if ((*argv)[0] == '-') { if (strlen(*argv) != 2) { cerr << _("Unknow option: ") << *argv << endl; Usage(); } - switch (*argv[1]) { - case 'h': + switch ((*argv)[1]) { + case 'h': + Usage(); + break; + case 'i': + if (readm != -1) { + cerr << _("-i and -f options are exclusive") << endl; + Usage(); + } + readm = 1; + filename = *(++argv); + argc--; + break; + case 'f': + if (readm != -1) { + cerr << _("-i and -f options are exclusive") << endl; Usage(); - break; - case 'i': - if (readm != -1) { - cerr << _("-i and -f options are exclusive") << endl; - Usage(); - } - readm = 1; - filename = *(++argv); - break; - case 'f': - if (readm != -1) { - cerr << _("-i and -f options are exclusive") << endl; - Usage(); - } - readm = 2; - filename = *(++argv); - break; + } + readm = 2; + filename = *(++argv); + argc--; + break; } } else { - if ((strlen(*argv) != 1) || ((*argv[0] < '0' || *argv[0] > '3'))) { + if ((strlen(*argv) != 1) || (((*argv)[0] < '0' || (*argv)[0] > '3'))) { cerr << _("Unknow priority list type: ") << *argv << endl; Usage(); } if (method != -1) { cerr << _("Extra command: ") << *argv << endl; } - method = *argv[0] - '0'; + method = (*argv)[0] - '0'; } } - + + if (method == -1) + method = 0; P = newlist(method); - + switch (readm) { - case -1: - Count(stdin, P); - break; - case 1: - Count(fopen(filename, "r"), P); - break; - case 2: - ReadDic(fopen(filename, "r"), P); - break; - default: - cerr << _("Internal error.") << endl; - exit(-1); + case -1: + Count(stdin, P); + break; + case 1: + if (!filename) + cerr << _("-i needs a filename") << endl; + Count(fopen(filename, "r"), P); + break; + case 2: + if (!filename) + cerr << _("-f needs a filename") << endl; + ReadDic(fopen(filename, "r"), P); + break; + default: + cerr << _("Internal error.") << endl; + exit(-1); } H = Coder(P); + delete P; + H->Trace(cout); return 0; } diff --git a/src/test.cc b/src/test.cc index fec7131..c5be9b8 100644 --- a/src/test.cc +++ b/src/test.cc @@ -20,7 +20,7 @@ void exception(int e, char *msg) PriorityList *newlist(void) { - return new BHeap; + return new FHeap; } void DoCombTest(int number) @@ -33,7 +33,9 @@ void DoCombTest(int number) cerr << _("Creation of a priority list and adding ") << number << _(" random entrie(s)..."); T = newlist(); for (i = 1; i <= number; i++) { - T->Insert(rand() % P_INFINITY, NULL); +// T->Insert(rand() % P_INFINITY, NULL); +// T->Insert(rand() % 100, NULL); + T->Insert(1, NULL); } cerr << _("Ok.\nDeleting the list..."); oK = P_INFINITY; @@ -73,7 +75,7 @@ void FullTest(void) cerr << _("Size of a BinHeap cell : ") << sizeof(binheap_t) << endl; DoCombTest(0); - DoCombTest(10); + DoCombTest(30); DoCombTest(70); DoCombTest(1000); // DoCombTest(10000); @@ -89,10 +91,12 @@ void FullTest(void) for (i = 0; i < 30; i++) { fprintf(stderr, "%i ", N[i]); C1 = T->Insert(N[i], NULL); - if (N[i] == 30) C3 = C1; + if (N[i] == 30) + C3 = C1; } - - if (!C3) exit(-1); + + if (!C3) + exit(-1); fprintf(stderr, "59 54 -10\n"); C1 = T->Insert(59, NULL); |