Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 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) |
| 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 | |
| 50 | static 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 | */ |
| 65 | static 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 | |
Holger Hans Peter Freyther | 131bc80 | 2010-10-26 09:31:16 +0200 | [diff] [blame] | 73 | /* Skip comments from the file */ |
| 74 | if (line[0] == '#') |
| 75 | return; |
| 76 | |
Holger Hans Peter Freyther | c87f266 | 2010-10-07 00:00:15 +0800 | [diff] [blame] | 77 | for (i = 0; i < len; ++i) { |
| 78 | if (line[i] == '\n' || line[i] == '\r') |
| 79 | line[i] = '\0'; |
| 80 | else if (line[i] == ':' && last_item < 3) { |
| 81 | line[i] = '\0'; |
| 82 | |
| 83 | items[last_item++] = &line[i + 1]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (last_item == 3) { |
| 88 | alloc_entry(entries, &line[0] , items[0], items[1], items[2]); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | /* nothing found */ |
| 93 | } |
| 94 | |
| 95 | struct msg_entries *msg_entry_parse(void *ctx, const char *filename) |
| 96 | { |
| 97 | struct msg_entries *entries; |
| 98 | size_t n; |
| 99 | char *line; |
| 100 | FILE *file; |
| 101 | |
| 102 | file = fopen(filename, "r"); |
| 103 | if (!file) |
| 104 | return NULL; |
| 105 | |
| 106 | entries = alloc_entries(ctx); |
| 107 | if (!entries) { |
| 108 | fclose(file); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | n = 2342; |
| 113 | line = NULL; |
| 114 | while (getline(&line, &n, file) != -1) { |
| 115 | handle_line(entries, line); |
| 116 | free(line); |
| 117 | line = NULL; |
| 118 | } |
| 119 | |
| 120 | fclose(file); |
| 121 | return entries; |
| 122 | } |