blob: 051f5b75cbb442509b7aef34ccf3d75f882b2c5b [file] [log] [blame]
Holger Hans Peter Freytherc87f2662010-10-07 00:00:15 +08001/*
2 * Parse a simple file with messages, e.g used for USSD messages
3 *
4 * (C) 2010 by Holger Hans Peter Freyther
5 * (C) 2010 by On-Waves
6 * All Rights Reserved
7 *
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.
12 *
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.
17 *
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.
21 *
22 */
23
24#include <osmocore/msgfile.h>
25#include <osmocore/talloc.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31#include <string.h>
32
33static struct msg_entry *alloc_entry(struct msg_entries *entries,
34 const char *mcc, const char *mnc,
35 const char *option, const char *text)
36{
37 struct msg_entry *entry = talloc_zero(entries, struct msg_entry);
38 if (!entry)
39 return NULL;
40
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);
45
46 llist_add_tail(&entry->list, &entries->entry);
47 return entry;
48}
49
50static struct msg_entries *alloc_entries(void *ctx)
51{
52 struct msg_entries *entries;
53
54 entries = talloc_zero(ctx, struct msg_entries);
55 if (!entries)
56 return NULL;
57
58 INIT_LLIST_HEAD(&entries->entry);
59 return entries;
60}
61
62/*
63 * split a line like 'foo:Text'.
64 */
65static void handle_line(struct msg_entries *entries, char *line)
66{
67 int i;
68 const int len = strlen(line);
69
70 char *items[3];
71 int last_item = 0;
72
73 for (i = 0; i < len; ++i) {
74 if (line[i] == '\n' || line[i] == '\r')
75 line[i] = '\0';
76 else if (line[i] == ':' && last_item < 3) {
77 line[i] = '\0';
78
79 items[last_item++] = &line[i + 1];
80 }
81 }
82
83 if (last_item == 3) {
84 alloc_entry(entries, &line[0] , items[0], items[1], items[2]);
85 return;
86 }
87
88 /* nothing found */
89}
90
91struct msg_entries *msg_entry_parse(void *ctx, const char *filename)
92{
93 struct msg_entries *entries;
94 size_t n;
95 char *line;
96 FILE *file;
97
98 file = fopen(filename, "r");
99 if (!file)
100 return NULL;
101
102 entries = alloc_entries(ctx);
103 if (!entries) {
104 fclose(file);
105 return NULL;
106 }
107
108 n = 2342;
109 line = NULL;
110 while (getline(&line, &n, file) != -1) {
111 handle_line(entries, line);
112 free(line);
113 line = NULL;
114 }
115
116 fclose(file);
117 return entries;
118}