2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
#include "dstr.h"
|
|
|
|
#include "text-lookup.h"
|
|
|
|
#include "lexer.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct text_leaf {
|
|
|
|
char *lookup, *value;
|
|
|
|
};
|
|
|
|
|
2013-12-07 09:22:56 -08:00
|
|
|
static inline void text_leaf_destroy(struct text_leaf *leaf)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2014-06-14 23:47:22 -07:00
|
|
|
if (leaf) {
|
|
|
|
bfree(leaf->lookup);
|
|
|
|
bfree(leaf->value);
|
|
|
|
bfree(leaf);
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct text_node {
|
|
|
|
struct dstr str;
|
2013-12-07 10:23:49 -08:00
|
|
|
struct text_node *first_subnode;
|
2013-09-30 19:37:13 -07:00
|
|
|
struct text_leaf *leaf;
|
2013-12-07 10:23:49 -08:00
|
|
|
|
|
|
|
struct text_node *next;
|
2013-09-30 19:37:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void text_node_destroy(struct text_node *node)
|
|
|
|
{
|
2013-12-07 10:23:49 -08:00
|
|
|
struct text_node *subnode;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
2013-12-07 10:23:49 -08:00
|
|
|
subnode = node->first_subnode;
|
|
|
|
while (subnode) {
|
|
|
|
struct text_node *destroy_node = subnode;
|
|
|
|
|
|
|
|
subnode = subnode->next;
|
|
|
|
text_node_destroy(destroy_node);
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
dstr_free(&node->str);
|
|
|
|
if (node->leaf)
|
2013-12-07 09:22:56 -08:00
|
|
|
text_leaf_destroy(node->leaf);
|
2013-09-30 19:37:13 -07:00
|
|
|
bfree(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct text_node *text_node_bychar(struct text_node *node, char ch)
|
|
|
|
{
|
2013-12-07 10:23:49 -08:00
|
|
|
struct text_node *subnode = node->first_subnode;
|
|
|
|
|
|
|
|
while (subnode) {
|
2014-08-05 13:38:24 -07:00
|
|
|
if (!dstr_is_empty(&subnode->str) &&
|
|
|
|
subnode->str.array[0] == ch)
|
2013-12-07 10:23:49 -08:00
|
|
|
return subnode;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-12-07 10:23:49 -08:00
|
|
|
subnode = subnode->next;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct text_node *text_node_byname(struct text_node *node,
|
|
|
|
const char *name)
|
|
|
|
{
|
2013-12-07 10:23:49 -08:00
|
|
|
struct text_node *subnode = node->first_subnode;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-12-07 10:23:49 -08:00
|
|
|
while (subnode) {
|
|
|
|
if (astrcmpi_n(subnode->str.array, name, subnode->str.len) == 0)
|
|
|
|
return subnode;
|
|
|
|
|
|
|
|
subnode = subnode->next;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct text_lookup {
|
|
|
|
struct dstr language;
|
|
|
|
struct text_node *top;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void lookup_createsubnode(const char *lookup_val,
|
|
|
|
struct text_leaf *leaf, struct text_node *node)
|
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct text_node *new = bzalloc(sizeof(struct text_node));
|
2013-09-30 19:37:13 -07:00
|
|
|
new->leaf = leaf;
|
2014-02-09 11:34:07 -08:00
|
|
|
new->next = node->first_subnode;
|
2013-09-30 19:37:13 -07:00
|
|
|
dstr_copy(&new->str, lookup_val);
|
|
|
|
|
2013-12-07 10:23:49 -08:00
|
|
|
node->first_subnode = new;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lookup_splitnode(const char *lookup_val, size_t len,
|
|
|
|
struct text_leaf *leaf, struct text_node *node)
|
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct text_node *split = bzalloc(sizeof(struct text_node));
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
dstr_copy(&split->str, node->str.array+len);
|
|
|
|
split->leaf = node->leaf;
|
2013-12-07 10:23:49 -08:00
|
|
|
split->first_subnode = node->first_subnode;
|
|
|
|
node->first_subnode = split;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
dstr_resize(&node->str, len);
|
|
|
|
|
|
|
|
if (lookup_val[len] != 0) {
|
|
|
|
node->leaf = NULL;
|
|
|
|
lookup_createsubnode(lookup_val+len, leaf, node);
|
|
|
|
} else {
|
|
|
|
node->leaf = leaf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 17:17:22 -08:00
|
|
|
static inline void lookup_replaceleaf(struct text_node *node,
|
|
|
|
struct text_leaf *leaf)
|
|
|
|
{
|
|
|
|
text_leaf_destroy(node->leaf);
|
|
|
|
node->leaf = leaf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lookup_addstring(const char *lookup_val, struct text_leaf *leaf,
|
2013-09-30 19:37:13 -07:00
|
|
|
struct text_node *node)
|
|
|
|
{
|
|
|
|
struct text_node *child;
|
|
|
|
|
2013-12-17 17:17:22 -08:00
|
|
|
/* value already exists, so replace */
|
|
|
|
if (!lookup_val || !*lookup_val) {
|
|
|
|
lookup_replaceleaf(node, leaf);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
child = text_node_bychar(node, *lookup_val);
|
|
|
|
if (child) {
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
for (len = 0; len < child->str.len; len++) {
|
|
|
|
char val1 = child->str.array[len],
|
|
|
|
val2 = lookup_val[len];
|
|
|
|
|
|
|
|
if (val1 >= 'A' && val1 <= 'Z')
|
|
|
|
val1 += 0x20;
|
|
|
|
if (val2 >= 'A' && val2 <= 'Z')
|
|
|
|
val2 += 0x20;
|
|
|
|
|
|
|
|
if (val1 != val2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-17 17:17:22 -08:00
|
|
|
if (len == child->str.len) {
|
|
|
|
lookup_addstring(lookup_val+len, leaf, child);
|
|
|
|
return;
|
|
|
|
} else {
|
2013-09-30 19:37:13 -07:00
|
|
|
lookup_splitnode(lookup_val, len, leaf, child);
|
2013-12-17 17:17:22 -08:00
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
} else {
|
2013-12-07 09:22:56 -08:00
|
|
|
lookup_createsubnode(lookup_val, leaf, node);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lookup_getstringtoken(struct lexer *lex, struct strref *token)
|
|
|
|
{
|
|
|
|
const char *temp = lex->offset;
|
|
|
|
bool was_backslash = false;
|
|
|
|
|
|
|
|
while (*temp != 0 && *temp != '\n') {
|
|
|
|
if (!was_backslash) {
|
|
|
|
if (*temp == '\\') {
|
|
|
|
was_backslash = true;
|
|
|
|
} else if (*temp == '"') {
|
2013-12-07 09:22:56 -08:00
|
|
|
temp++;
|
2013-09-30 19:37:13 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
was_backslash = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++temp;
|
|
|
|
}
|
|
|
|
|
2013-12-07 09:22:56 -08:00
|
|
|
token->len += (size_t)(temp - lex->offset);
|
|
|
|
|
|
|
|
if (*token->array == '"') {
|
|
|
|
token->array++;
|
|
|
|
token->len--;
|
|
|
|
|
2013-12-07 09:25:34 -08:00
|
|
|
if (*(temp-1) == '"')
|
|
|
|
token->len--;
|
|
|
|
}
|
2013-12-07 09:22:56 -08:00
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
lex->offset = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lookup_gettoken(struct lexer *lex, struct strref *str)
|
|
|
|
{
|
|
|
|
struct base_token temp;
|
|
|
|
|
|
|
|
base_token_clear(&temp);
|
|
|
|
strref_clear(str);
|
|
|
|
|
2013-12-15 23:05:27 -08:00
|
|
|
while (lexer_getbasetoken(lex, &temp, PARSE_WHITESPACE)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
char ch = *temp.text.array;
|
|
|
|
|
|
|
|
if (!str->array) {
|
|
|
|
/* comments are designated with a #, and end at LF */
|
|
|
|
if (ch == '#') {
|
|
|
|
while(ch != '\n' && ch != 0)
|
|
|
|
ch = *(++lex->offset);
|
2013-12-07 09:22:56 -08:00
|
|
|
} else if (temp.type == BASETOKEN_WHITESPACE) {
|
|
|
|
strref_copy(str, &temp.text);
|
|
|
|
break;
|
2013-09-30 19:37:13 -07:00
|
|
|
} else {
|
|
|
|
strref_copy(str, &temp.text);
|
2013-12-07 09:22:56 -08:00
|
|
|
if (ch == '"') {
|
|
|
|
lookup_getstringtoken(lex, str);
|
|
|
|
break;
|
|
|
|
} else if (ch == '=') {
|
|
|
|
break;
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-07 09:22:56 -08:00
|
|
|
if (temp.type == BASETOKEN_WHITESPACE ||
|
|
|
|
*temp.text.array == '=') {
|
2013-09-30 19:37:13 -07:00
|
|
|
lex->offset -= temp.text.len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-07 09:22:56 -08:00
|
|
|
if (ch == '#') {
|
2013-09-30 19:37:13 -07:00
|
|
|
lex->offset--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
str->len += temp.text.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (str->len != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool lookup_goto_nextline(struct lexer *p)
|
|
|
|
{
|
|
|
|
struct strref val;
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
strref_clear(&val);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!lookup_gettoken(p, &val)) {
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*val.array == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2013-12-07 09:22:56 -08:00
|
|
|
static char *convert_string(const char *str, size_t len)
|
|
|
|
{
|
|
|
|
struct dstr out;
|
|
|
|
out.array = bstrdup_n(str, len);
|
|
|
|
out.capacity = len+1;
|
|
|
|
out.len = len;
|
|
|
|
|
|
|
|
dstr_replace(&out, "\\n", "\n");
|
|
|
|
dstr_replace(&out, "\\t", "\t");
|
|
|
|
dstr_replace(&out, "\\r", "\r");
|
|
|
|
|
|
|
|
return out.array;
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
static void lookup_addfiledata(struct text_lookup *lookup,
|
|
|
|
const char *file_data)
|
|
|
|
{
|
|
|
|
struct lexer lex;
|
|
|
|
struct strref name, value;
|
|
|
|
|
2013-12-07 09:22:56 -08:00
|
|
|
lexer_init(&lex);
|
2013-09-30 19:37:13 -07:00
|
|
|
lexer_start(&lex, file_data);
|
|
|
|
strref_clear(&name);
|
|
|
|
strref_clear(&value);
|
|
|
|
|
|
|
|
while (lookup_gettoken(&lex, &name)) {
|
|
|
|
struct text_leaf *leaf;
|
|
|
|
bool got_eq = false;
|
|
|
|
|
|
|
|
if (*name.array == '\n')
|
|
|
|
continue;
|
|
|
|
getval:
|
|
|
|
if (!lookup_gettoken(&lex, &value))
|
|
|
|
break;
|
|
|
|
if (*value.array == '\n')
|
|
|
|
continue;
|
|
|
|
else if (!got_eq && *value.array == '=') {
|
|
|
|
got_eq = true;
|
|
|
|
goto getval;
|
|
|
|
}
|
|
|
|
|
|
|
|
leaf = bmalloc(sizeof(struct text_leaf));
|
|
|
|
leaf->lookup = bstrdup_n(name.array, name.len);
|
2013-12-07 09:22:56 -08:00
|
|
|
leaf->value = convert_string(value.array, value.len);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
lookup_addstring(leaf->lookup, leaf, lookup->top);
|
|
|
|
|
|
|
|
if (!lookup_goto_nextline(&lex))
|
|
|
|
break;
|
|
|
|
}
|
2013-12-07 09:22:56 -08:00
|
|
|
|
|
|
|
lexer_free(&lex);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool lookup_getstring(const char *lookup_val,
|
|
|
|
const char **out, struct text_node *node)
|
|
|
|
{
|
|
|
|
struct text_node *child;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
child = text_node_byname(node, lookup_val);
|
|
|
|
if (!child)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lookup_val += child->str.len;
|
|
|
|
ch = *lookup_val;
|
|
|
|
if (ch)
|
|
|
|
return lookup_getstring(lookup_val, out, child);
|
|
|
|
|
|
|
|
if (!child->leaf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*out = child->leaf->value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
lookup_t *text_lookup_create(const char *path)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct text_lookup *lookup = bzalloc(sizeof(struct text_lookup));
|
2013-12-17 12:55:09 -08:00
|
|
|
|
|
|
|
if (!text_lookup_add(lookup, path)) {
|
|
|
|
bfree(lookup);
|
|
|
|
lookup = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lookup;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
bool text_lookup_add(lookup_t *lookup, const char *path)
|
2013-12-17 12:55:09 -08:00
|
|
|
{
|
2013-09-30 19:37:13 -07:00
|
|
|
struct dstr file_str;
|
|
|
|
char *temp = NULL;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
file = os_fopen(path, "rb");
|
|
|
|
if (!file)
|
2013-12-17 12:55:09 -08:00
|
|
|
return false;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
os_fread_utf8(file, &temp);
|
|
|
|
dstr_init_move_array(&file_str, temp);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (!file_str.array)
|
2013-12-17 12:55:09 -08:00
|
|
|
return false;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-02-09 11:34:07 -08:00
|
|
|
if (!lookup->top)
|
|
|
|
lookup->top = bzalloc(sizeof(struct text_node));
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
dstr_replace(&file_str, "\r", " ");
|
|
|
|
lookup_addfiledata(lookup, file_str.array);
|
|
|
|
dstr_free(&file_str);
|
|
|
|
|
2013-12-17 12:55:09 -08:00
|
|
|
return true;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void text_lookup_destroy(lookup_t *lookup)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
if (lookup) {
|
|
|
|
dstr_free(&lookup->language);
|
|
|
|
text_node_destroy(lookup->top);
|
|
|
|
|
|
|
|
bfree(lookup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
bool text_lookup_getstr(lookup_t *lookup, const char *lookup_val,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char **out)
|
|
|
|
{
|
2014-07-24 00:27:15 -07:00
|
|
|
if (lookup)
|
|
|
|
return lookup_getstring(lookup_val, out, lookup->top);
|
|
|
|
return false;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|