summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc117
1 files changed, 67 insertions, 50 deletions
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;
}