File: parser_callbacks.c

package info (click to toggle)
cif-api 0.4.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,472 kB
  • sloc: ansic: 19,411; lex: 246; makefile: 201; sql: 164; yacc: 115; sh: 37
file content (56 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (3)
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
/**
 * basic_parsing.c
 *
 * Copyright 2014, 2015 John C. Bollinger
 *
 *
 * This file is part of the CIF API.
 *
 * The CIF API is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The CIF API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the CIF API.  If not, see <http://www.gnu.org/licenses/>.
 */
int assign_loop_category(cif_loop_tp *loop, void *context) {
    UChar **names;
    UChar **next;
    UChar *dot_location;
    const UChar unicode_dot = 0x2E;

    /* We can rely on at least one data name */
    cif_loop_get_names(loop, &names);
    /*
     * Assumes the name contains a decimal point (Unicode code point U+002E), and
     * takes the category as everything preceding it.  Ignores case sensitivity considerations.
     */
    dot_location = u_strchr(names[0] + 1, unicode_dot);
    *dot_location = 0;
    cif_loop_set_category(loop, names[0]);
    /* Clean up */
    for (next = names; *next != NULL; next += 1) {
        free(*next);
    }
    free(names);
}

void parse_with_categories(FILE *in) {
    cif_tp *cif = NULL;
    struct cif_parse_opts_s *opts = NULL;
    cif_handler_tp handler = { NULL, NULL, NULL, NULL, NULL, NULL, assign_loop_category, NULL, NULL, NULL, NULL };

    cif_parse_options_create(&opts);
    opts->handler = &handler;
    cif_parse(in, opts, &cif);
    free(opts);
    /* the parsed results are available via 'cif' */
    /* ... */
    cif_destroy(cif);
}