1 #include "../include/dictionary.hpp"
2 #include "../include/util.hpp"
31 throw exception(
"invalid argument: nm (=%p)", nm);
33 if (
likely(path != NULL) )
126 throw exception(
"invalid argument: nm (=%p)", nm);
128 u32 len = strlen(nm);
129 if (len > strlen(
m_name)) {
201 i32 retval = stat(path, &inf);
203 throw exception(
"file '%s' does not exist", path);
207 "failed to stat path '%s' (errno %d - %s)",
214 throw exception(
"'%s' is not a regular file", path);
217 throw exception(
"file '%s' is not readable", path);
219 i32 sz = inf.st_size;
228 fd = open(path, O_RDONLY);
230 while (
unlikely(fd < 0 && errno == EINTR) );
234 "failed to open file '%s' (errno %d - %s)",
241 void *mmap_base = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
242 if (
unlikely(mmap_base == MAP_FAILED) ) {
245 "failed to memory map file '%s' (errno %d - %s)",
259 offset = cur =
static_cast<i8*
> (mmap_base);
262 while (
likely(bytes-- > 0) )
264 if (
likely(cur != offset) ) {
265 word =
new string(
"%.*s", cur - offset, offset);
287 munmap(mmap_base, sz);
292 munmap(mmap_base, sz);
295 #if CSDBG_DBG_LEVEL & CSDBG_DBGL_INFO
298 "file '%s' (%d word%s) loaded on dictionary %s",
301 (cnt != 1) ?
"s" :
"",
327 string *word =
at(i);
virtual dictionary * clone() const
Object virtual copy constructor.
virtual string & trim(i32=0)
Remove leading and/or trailing whitespace characters.
dictionary(const i8 *, const i8 *=NULL, bool=false)
Object constructor.
virtual string * at(u32) const
Lightweight, templated, doubly-linked list (using XOR linking)
A named collection of words (for syntax highlighters)
virtual bool mode() const
Get the lookup mode.
char i8
8-bit signed integer
struct stat fileinfo_t
File metadata.
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
static void dbg_warn(const i8 *,...)
Print a warning debug message on the standard error stream.
i8 * m_name
Dictionary name.
virtual const i8 * name() const
Get the dictionary name.
static bool is_readable(const fileinfo_t &)
Check if the process has read access to a file.
virtual i32 cmp(const string &, bool=false) const
Compare to another string.
virtual ~dictionary()
Object destructor.
virtual dictionary & set_name(const i8 *)
Set the name.
virtual dictionary & operator=(const dictionary &)
Assignment operator.
virtual chain & add(string *)
virtual dictionary & load_file(const i8 *)
Load words from a dictionary file.
Lightweight string buffer class (for ISO-8859-1 text)
virtual bool match(const string &, bool=false) const
Match against a POSIX extended regular expression.
unsigned int u32
32-bit unsigned integer
virtual chain & operator=(const chain &)
Assignment operator.
int i32
32-bit signed integer
static bool is_regular(const fileinfo_t &)
Check if a file is a regular one.
virtual u32 length() const
Get the character count.
This class is a throwable with a textual description of an error.
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
static void dbg_info(const i8 *,...)
Print an informational debug message on the standard error stream.
virtual dictionary & set_mode(bool)
Set the lookup mode.
virtual const string * lookup(const string &, bool=false) const
Dictionary lookup.
#define __D_ASSERT(x)
Assertion macro.