summaryrefslogtreecommitdiffstats
path: root/src/storage/tag_utility.cpp
blob: 232a07dcf4a9e7755ef912b040cc6f9b6b6d2b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "novel_types.h"
#include "phrase_index.h"
#include "phrase_large_table3.h"
#include "tag_utility.h"

namespace pinyin{

/* internal taglib structure */
struct tag_entry{
    int m_line_type;
    char * m_line_tag;
    int m_num_of_values;
    char ** m_required_tags;
    /* char ** m_optional_tags; */
    /* int m_optional_count = 0; */
    char ** m_ignored_tags;
};

tag_entry tag_entry_copy(int line_type, const char * line_tag,
                         int num_of_values,
                         char * required_tags[],
                         char * ignored_tags[]){
    tag_entry entry;
    entry.m_line_type = line_type;
    entry.m_line_tag = g_strdup( line_tag );
    entry.m_num_of_values = num_of_values;
    entry.m_required_tags = g_strdupv( required_tags );
    entry.m_ignored_tags = g_strdupv( ignored_tags );
    return entry;
}

tag_entry tag_entry_clone(tag_entry * entry){
    return tag_entry_copy(entry->m_line_type, entry->m_line_tag,
                          entry->m_num_of_values,
                          entry->m_required_tags, entry->m_ignored_tags);
}

void tag_entry_reclaim(tag_entry * entry){
    g_free( entry->m_line_tag );
    g_strfreev( entry->m_required_tags );
    g_strfreev(entry->m_ignored_tags);
}

static bool taglib_free_tag_array(GArray * tag_array){
    for ( size_t i = 0; i < tag_array->len; ++i) {
        tag_entry * entry = &g_array_index(tag_array, tag_entry, i);
        tag_entry_reclaim(entry);
    }
    g_array_free(tag_array, TRUE);
    return true;
}

/* special unichar to be handled in split_line. */
static gunichar backslash = 0;
static gunichar quote = 0;

static gboolean split_line_init(){
    backslash = g_utf8_get_char("\\");
    quote = g_utf8_get_char("\"");
    return TRUE;
}

/* Pointer Array of Array of tag_entry */
static GPtrArray * g_tagutils_stack = NULL;

bool taglib_init(){
    assert( g_tagutils_stack == NULL);
    g_tagutils_stack = g_ptr_array_new();
    GArray * tag_array = g_array_new(TRUE, TRUE, sizeof(tag_entry));
    g_ptr_array_add(g_tagutils_stack, tag_array);

    /* init split_line. */
    split_line_init();
    return true;
}

bool taglib_add_tag(int line_type, const char * line_tag, int num_of_values,
                    const char * required_tags, const char * ignored_tags){
    GArray * tag_array = (GArray *) g_ptr_array_index(g_tagutils_stack,
                                     g_tagutils_stack->len - 1);

    /* some duplicate tagname or line_type check here. */
    for ( size_t i = 0; i < tag_array->len; ++i) {
        tag_entry * entry = &g_array_index(tag_array, tag_entry, i);
        if ( entry->m_line_type == line_type ||
             strcmp( entry->m_line_tag, line_tag ) == 0 )
            return false;
    }

    char ** required = g_strsplit_set(required_tags, ",:", -1);
    char ** ignored = g_strsplit_set(ignored_tags, ",:", -1);

    tag_entry entry = tag_entry_copy(line_type, line_tag, num_of_values,
                                     required, ignored);
    g_array_append_val(tag_array, entry);

    g_strfreev(required);
    g_strfreev(ignored);
    return true;
}

static void ptr_array_entry_free(gpointer data, gpointer user_data){
    g_free(data);
}

static gboolean hash_table_key_value_free(gpointer key, gpointer value,
                                          gpointer user_data){
    g_free(key);
    g_free(value);
    return TRUE;
}

/* split the line into tokens. */
static gchar ** split_line(const gchar * line){
    /* array for tokens. */
    GArray * tokens = g_array_new(TRUE, TRUE, sizeof(gchar *));

    for ( const gchar * cur = line; *cur; cur = g_utf8_next_char(cur) ){
        gunichar unichar = g_utf8_get_char(cur);
        const gchar * begin = cur;
        gchar * token = NULL;

        if ( g_unichar_isspace (unichar) ) {
            continue;
        }else if ( unichar == quote ) {
            /* handles "\"". */
            /* skip the first '"'. */
            begin = cur = g_utf8_next_char(cur);
            while (*cur) {
                unichar = g_utf8_get_char(cur);
                if ( unichar == backslash ) {
                    cur = g_utf8_next_char(cur);
                    g_return_val_if_fail(*cur, NULL);
                } else if ( unichar == quote ){
                    break;
                }
                cur = g_utf8_next_char(cur);
            }
            gchar * tmp = g_strndup( begin, cur - begin);
            /* TODO: switch to own strdup_escape implementation
               for \"->" transforming. */
            token = g_strdup_printf("%s", tmp);
            g_free(tmp);
        } else {
            /* handles other tokens. */
            while(*cur) {
                unichar = g_utf8_get_char(cur);
                if ( g_unichar_isgraph(unichar) ) {
                    /* next unichar */
                    cur = g_utf8_next_char(cur);
                } else {
                    /* space and other characters handles. */
                    break;
                }
            }
            token = g_strndup( begin, cur - begin );
        }

        g_array_append_val(tokens, token);
        if ( !*cur )
            break;
    }

    return (gchar **)g_array_free(tokens, FALSE);
}

bool taglib_read(const char * input_line, int & line_type, GPtrArray * values,
                 GHashTable * required){
    /* reset values and required. */
    g_ptr_array_foreach(values, ptr_array_entry_free, NULL);
    g_ptr_array_set_size(values, 0);
    g_hash_table_foreach_steal(required, hash_table_key_value_free, NULL);

    /* use own version of split_line
       instead of g_strsplit_set for special token.*/
    char ** tokens = split_line(input_line);
    int num_of_tokens = g_strv_length(tokens);

    char * line_tag = tokens[0];
    GArray * tag_array = (GArray *) g_ptr_array_index(g_tagutils_stack, g_tagutils_stack->len - 1);

    tag_entry * cur_entry = NULL;
    /* find line type. */
    for ( size_t i = 0; i < tag_array->len; ++i) {
        tag_entry * entry = &g_array_index(tag_array, tag_entry, i);
        if ( strcmp( entry->m_line_tag, line_tag ) == 0 ) {
            cur_entry = entry;
            break;
        }
    }

    if ( !cur_entry )
        return false;

    line_type = cur_entry->m_line_type;

    for ( int i = 1; i < cur_entry->m_num_of_values + 1; ++i) {
        g_return_val_if_fail(i < num_of_tokens, false);
        char * value = g_strdup( tokens[i] );
        g_ptr_array_add(values, value);
    }

    int ignored_len = g_strv_length( cur_entry->m_ignored_tags );
    int required_len = g_strv_length( cur_entry->m_required_tags);

    for ( int i = cur_entry->m_num_of_values + 1; i < num_of_tokens; ++i){
        g_return_val_if_fail(i < num_of_tokens, false);
        const char * tmp = tokens[i];

        /* check ignored tags. */
        bool tag_ignored = false;
        for ( int m = 0; m < ignored_len; ++m) {
            if ( strcmp(tmp, cur_entry->m_ignored_tags[m]) == 0) {
                tag_ignored = true;
                break;
            }
        }

        if ( tag_ignored ) {
            ++i;
            continue;
        }

        /* check required tags. */
        bool tag_required = false;
        for ( int m = 0; m < required_len; ++m) {
            if ( strcmp(tmp, cur_entry->m_required_tags[m]) == 0) {
                tag_required = true;
                break;
            }
        }

        /* warning on the un-expected tags. */
        if ( !tag_required ) {
            g_warning("un-expected tags:%s.\n", tmp);
            ++i;
            continue;
        }

        char * key = g_strdup(tokens[i]);
        ++i;
        g_return_val_if_fail(i < num_of_tokens, false);
        char * value = g_strdup(tokens[i]);
        g_hash_table_insert(required, key, value);
    }

    /* check for all required tags. */
    for ( int i = 0; i < required_len; ++i) {
        const char * required_tag_str = cur_entry->m_required_tags[i];
        gboolean result = g_hash_table_lookup_extended(required, required_tag_str, NULL, NULL);
        if ( !result ) {
            g_warning("missed required tags: %s.\n", required_tag_str);
            g_strfreev(tokens);
            return false;
        }
    }

    g_strfreev(tokens);
    return true;
}

bool taglib_remove_tag(int line_type){
    /* Note: duplicate entry check is in taglib_add_tag. */
    GArray * tag_array = (GArray *) g_ptr_array_index(g_tagutils_stack, g_tagutils_stack->len - 1);
    for ( size_t i = 0; i < tag_array->len; ++i) {
        tag_entry * entry = &g_array_index(tag_array, tag_entry, i);
        if (entry->m_line_type != line_type)
            continue;
        tag_entry_reclaim(entry);
        g_array_remove_index(tag_array, i);
        return true;
    }
    return false;
}

bool taglib_push_state(){
    assert(g_tagutils_stack->len >= 1);
    GArray * next_tag_array = g_array_new(TRUE, TRUE, sizeof(tag_entry));
    GArray * prev_tag_array = (GArray *) g_ptr_array_index(g_tagutils_stack, g_tagutils_stack->len - 1);
    for ( size_t i = 0; i < prev_tag_array->len; ++i) {
        tag_entry * entry = &g_array_index(prev_tag_array, tag_entry, i);
        tag_entry new_entry = tag_entry_clone(entry);
        g_array_append_val(next_tag_array, new_entry);
    }
    g_ptr_array_add(g_tagutils_stack, next_tag_array);
    return true;
}

bool taglib_pop_state(){
    assert(g_tagutils_stack->len > 1);
    GArray * tag_array = (GArray *) g_ptr_array_index(g_tagutils_stack, g_tagutils_stack->len - 1);
    g_ptr_array_remove_index(g_tagutils_stack, g_tagutils_stack->len - 1);
    taglib_free_tag_array(tag_array);
    return true;
}

bool taglib_fini(){
    for ( size_t i = 0; i < g_tagutils_stack->len; ++i){
        GArray * tag_array = (GArray *) g_ptr_array_index(g_tagutils_stack, i);
        taglib_free_tag_array(tag_array);
    }
    g_ptr_array_free(g_tagutils_stack, TRUE);
    g_tagutils_stack = NULL;
    return true;
}

#if 0

static phrase_token_t taglib_special_string_to_token(const char * string){
    struct token_pair{
        phrase_token_t token;
        const char * string;
    };

    static const token_pair tokens [] = {
        {sentence_start, "<start>"},
        {0, NULL}
    };

    const token_pair * pair = tokens;
    while (pair->string) {
        if ( strcmp(string, pair->string ) == 0 )
            return pair->token;
        pair++;
    }

    fprintf(stderr, "error: unknown token:%s.\n", string);
    return 0;
}

phrase_token_t taglib_string_to_token(PhraseLargeTable3 * phrase_table,
                                      FacadePhraseIndex * phrase_index,
                                      const char * string){
    phrase_token_t token = null_token;
    if ( string[0] == '<' ) {
        return taglib_special_string_to_token(string);
    }

    glong phrase_len = g_utf8_strlen(string, -1);
    ucs4_t * phrase = g_utf8_to_ucs4(string, -1, NULL, NULL, NULL);

    PhraseTokens tokens;
    memset(tokens, 0, sizeof(PhraseTokens));
    phrase_index->prepare_tokens(tokens);
    int result = phrase_table->search(phrase_len, phrase, tokens);
    int num = get_first_token(tokens, token);
    phrase_index->destroy_tokens(tokens);

    if ( !(result & SEARCH_OK) )
        fprintf(stderr, "error: unknown token:%s.\n", string);

    g_free(phrase);
    return token;
}

#endif

static const char * taglib_special_token_to_string(phrase_token_t token){
    struct token_pair{
        phrase_token_t token;
        const char * string;
    };

    static const token_pair tokens [] = {
        {sentence_start, "<start>"},
        {0, NULL}
    };

    const token_pair * pair = tokens;
    while (pair->token) {
        if ( token == pair->token )
            return pair->string;
        pair++;
    }

    fprintf(stderr, "error: unknown token:%d.\n", token);
    return NULL;
}

char * taglib_token_to_string(FacadePhraseIndex * phrase_index,
                              phrase_token_t token) {
    PhraseItem item;
    ucs4_t buffer[MAX_PHRASE_LENGTH];

    gchar * phrase;
    /* deal with the special phrase index, for "<start>..." */
    if ( PHRASE_INDEX_LIBRARY_INDEX(token) == 0 ) {
        return g_strdup(taglib_special_token_to_string(token));
    }

    int result = phrase_index->get_phrase_item(token, item);
    if (result != ERROR_OK) {
        fprintf(stderr, "error: unknown token:%d.\n", token);
        return NULL;
    }

    item.get_phrase_string(buffer);
    guint8 length = item.get_phrase_length();
    phrase = g_ucs4_to_utf8(buffer, length, NULL, NULL, NULL);
    return phrase;
}

bool taglib_validate_token_with_string(FacadePhraseIndex * phrase_index,
                                       phrase_token_t token,
                                       const char * string){
    bool result = false;

    char * str = taglib_token_to_string(phrase_index, token);
    result = (0 == strcmp(str, string));
    g_free(str);

    return result;
}


};