diff options
author | sgeerken <devnull@localhost> | 2012-07-04 12:35:50 +0200 |
---|---|---|
committer | sgeerken <devnull@localhost> | 2012-07-04 12:35:50 +0200 |
commit | 2f713f4545458cf1e0719dd0d520f00373851da3 (patch) | |
tree | 2fd73894dba65668a732d593ccf13462cd27622f /dw/hyphenator.cc | |
parent | 121d1bba2dd7411d9e9bce30772ab5c5e58b9c2b (diff) |
Hyphenator: better file error handling
Diffstat (limited to 'dw/hyphenator.cc')
-rw-r--r-- | dw/hyphenator.cc | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/dw/hyphenator.cc b/dw/hyphenator.cc index 0e400f60..861a1330 100644 --- a/dw/hyphenator.cc +++ b/dw/hyphenator.cc @@ -25,26 +25,30 @@ HashTable <TypedPair <TypedPointer <core::Platform>, ConstString>, Hyphenator::Hyphenator (core::Platform *platform, const char *filename) { this->platform = platform; - tree = new HashTable <Integer, Collection <Integer> > (true, true); + tree = NULL; // As long we are not sure whether a pattern file can be read. FILE *file = fopen (filename, "r"); - // TODO Error handling - while (!feof (file)) { - char buf[LEN + 1]; - char *s = fgets (buf, LEN, file); - if (s) { - int l = strlen (s); - if (s[l - 1] == '\n') - s[l - 1] = 0; - insertPattern (s); + if (file) { + tree = new HashTable <Integer, Collection <Integer> > (true, true); + while (!feof (file)) { + char buf[LEN + 1]; + char *s = fgets (buf, LEN, file); + if (s) { + // TODO Better exit with an error, when the line is too long. + int l = strlen (s); + if (s[l - 1] == '\n') + s[l - 1] = 0; + insertPattern (s); + } } + fclose (file); } - fclose (file); } Hyphenator::~Hyphenator () { - delete tree; + if (tree) + delete tree; } Hyphenator *Hyphenator::getHyphenator (core::Platform *platform, @@ -132,7 +136,8 @@ bool Hyphenator::isHyphenationCandidate (const char *word) */ int *Hyphenator::hyphenateWord(const char *word, int *numBreaks) { - if (!isHyphenationCandidate (word)) { + // tree == NULL means that there is no pattern file. + if (tree == NULL || !isHyphenationCandidate (word)) { *numBreaks = 0; return NULL; } |