1 | //keyval.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | struct roar_keyval * roar_keyval_lookup (struct roar_keyval * kv, const char * key, ssize_t len, int casesens) { |
---|
39 | int (*sc)(const char *s1, const char *s2) = strcasecmp; |
---|
40 | ssize_t i; |
---|
41 | |
---|
42 | if ( kv == NULL || key == NULL ) { |
---|
43 | roar_err_set(ROAR_ERROR_FAULT); |
---|
44 | return NULL; |
---|
45 | } |
---|
46 | |
---|
47 | if ( casesens ) |
---|
48 | sc = strcmp; |
---|
49 | |
---|
50 | for (i = 0; len != -1 ? (i < len) : kv[i].key != NULL; i++) { |
---|
51 | if ( !sc(key, kv[i].key) ) |
---|
52 | return &(kv[i]); |
---|
53 | } |
---|
54 | |
---|
55 | roar_err_set(ROAR_ERROR_NOENT); |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | |
---|
59 | static inline int is_in (const char c, const char * delm) { |
---|
60 | for (; *delm != 0; delm++) |
---|
61 | if ( *delm == c ) |
---|
62 | return 1; |
---|
63 | |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | ssize_t roar_keyval_split (struct roar_keyval ** kv, char * str, const char * fdel, const char * kdel, int quotes) { |
---|
68 | struct roar_keyval * kvs; |
---|
69 | int pos = -1; |
---|
70 | size_t len = 0; |
---|
71 | char * sp; |
---|
72 | char quote = 0; |
---|
73 | int last_was_seg = 0; |
---|
74 | |
---|
75 | if ( kv == NULL || str == NULL ) { |
---|
76 | roar_err_set(ROAR_ERROR_FAULT); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | // we currently do not support quotes |
---|
81 | if ( quotes ) { |
---|
82 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
83 | return -1; |
---|
84 | } |
---|
85 | |
---|
86 | if ( fdel == NULL ) |
---|
87 | fdel = " \t,"; |
---|
88 | |
---|
89 | if ( kdel == NULL ) |
---|
90 | kdel = "=:"; |
---|
91 | |
---|
92 | // count num of segements: |
---|
93 | for (sp = str; *sp != 0; sp++) { |
---|
94 | if ( quote ) { |
---|
95 | if ( *sp == quote ) |
---|
96 | quote = 0; |
---|
97 | } else { |
---|
98 | if ( last_was_seg ) { |
---|
99 | last_was_seg = !is_in(*sp, fdel); |
---|
100 | } else { |
---|
101 | if ( !is_in(*sp, fdel) ) { |
---|
102 | last_was_seg = 1; |
---|
103 | len++; |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | kvs = roar_mm_malloc(sizeof(struct roar_keyval)*(len+1)); |
---|
110 | |
---|
111 | if ( kvs == NULL ) |
---|
112 | return -1; |
---|
113 | |
---|
114 | *kv = kvs; |
---|
115 | |
---|
116 | // End of Array Mark: |
---|
117 | kvs[len].key = NULL; |
---|
118 | kvs[len].value = NULL; |
---|
119 | |
---|
120 | // do the acctual filling: |
---|
121 | last_was_seg = 0; |
---|
122 | |
---|
123 | for (sp = str; *sp != 0; sp++) { |
---|
124 | if ( last_was_seg ) { |
---|
125 | if ( is_in(*sp, fdel) ) { |
---|
126 | last_was_seg = 0; |
---|
127 | *sp = 0; |
---|
128 | } else { |
---|
129 | last_was_seg = 1; |
---|
130 | if ( is_in(*sp, kdel) ) { |
---|
131 | *sp = 0; |
---|
132 | kvs[pos].value = sp+1; |
---|
133 | } |
---|
134 | } |
---|
135 | } else { |
---|
136 | if ( !is_in(*sp, fdel) ) { |
---|
137 | last_was_seg = 1; |
---|
138 | pos++; |
---|
139 | kvs[pos].key = sp; |
---|
140 | kvs[pos].value = NULL; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | return len; |
---|
146 | } |
---|
147 | |
---|
148 | //ll |
---|