2 * Parse a simple file with messages, e.g used for USSD messages
4 * (C) 2010 by Holger Hans Peter Freyther
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <osmocore/msgfile.h>
25 #include <osmocore/talloc.h>
27 #include <sys/types.h>
33 static struct msg_entry *alloc_entry(struct msg_entries *entries,
34 const char *mcc, const char *mnc,
35 const char *option, const char *text)
37 struct msg_entry *entry = talloc_zero(entries, struct msg_entry);
41 entry->mcc = talloc_strdup(entry, mcc);
42 entry->mnc = talloc_strdup(entry, mnc);
43 entry->option = talloc_strdup(entry, option);
44 entry->text = talloc_strdup(entry, text);
46 llist_add_tail(&entry->list, &entries->entry);
50 static struct msg_entries *alloc_entries(void *ctx)
52 struct msg_entries *entries;
54 entries = talloc_zero(ctx, struct msg_entries);
58 INIT_LLIST_HEAD(&entries->entry);
63 * split a line like 'foo:Text'.
65 static void handle_line(struct msg_entries *entries, char *line)
68 const int len = strlen(line);
73 /* Skip comments from the file */
77 for (i = 0; i < len; ++i) {
78 if (line[i] == '\n' || line[i] == '\r')
80 else if (line[i] == ':' && last_item < 3) {
83 items[last_item++] = &line[i + 1];
88 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
95 struct msg_entries *msg_entry_parse(void *ctx, const char *filename)
97 struct msg_entries *entries;
102 file = fopen(filename, "r");
106 entries = alloc_entries(ctx);
114 while (getline(&line, &n, file) != -1) {
115 handle_line(entries, line);