[3973] | 1 | //keyval.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
[3973] | 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 | |
---|
[3975] | 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 ( casesens ) |
---|
| 43 | sc = strcmp; |
---|
| 44 | |
---|
| 45 | for (i = 0; len != -1 ? (i < len) : kv[i].key != NULL; i++) { |
---|
| 46 | if ( !sc(key, kv[i].key) ) |
---|
| 47 | return &(kv[i]); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | return NULL; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | static inline int is_in (const char c, const char * delm) { |
---|
| 54 | for (; *delm != 0; delm++) |
---|
| 55 | if ( *delm == c ) |
---|
| 56 | return 1; |
---|
| 57 | |
---|
| 58 | return 0; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | ssize_t roar_keyval_split (struct roar_keyval ** kv, char * str, const char * fdel, const char * kdel, int quotes) { |
---|
| 62 | struct roar_keyval * kvs; |
---|
| 63 | int pos = -1; |
---|
| 64 | size_t len = 0; |
---|
| 65 | char * sp; |
---|
| 66 | char quote = 0; |
---|
| 67 | int last_was_seg = 0; |
---|
| 68 | |
---|
| 69 | if ( kv == NULL || str == NULL ) |
---|
| 70 | return -1; |
---|
| 71 | |
---|
| 72 | // we currently do not support quotes |
---|
| 73 | if ( quotes ) |
---|
| 74 | return -1; |
---|
| 75 | |
---|
| 76 | if ( fdel == NULL ) |
---|
| 77 | fdel = " \t,"; |
---|
| 78 | |
---|
| 79 | if ( kdel == NULL ) |
---|
| 80 | kdel = "=:"; |
---|
| 81 | |
---|
| 82 | // count num of segements: |
---|
| 83 | for (sp = str; *sp != 0; sp++) { |
---|
| 84 | if ( quote ) { |
---|
| 85 | if ( *sp == quote ) |
---|
| 86 | quote = 0; |
---|
| 87 | } else { |
---|
| 88 | if ( last_was_seg ) { |
---|
| 89 | last_was_seg = !is_in(*sp, fdel); |
---|
| 90 | } else { |
---|
| 91 | if ( !is_in(*sp, fdel) ) { |
---|
| 92 | last_was_seg = 1; |
---|
| 93 | len++; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | kvs = roar_mm_malloc(sizeof(struct roar_keyval)*(len+1)); |
---|
| 100 | |
---|
| 101 | if ( kvs == NULL ) |
---|
| 102 | return -1; |
---|
| 103 | |
---|
| 104 | *kv = kvs; |
---|
| 105 | |
---|
| 106 | // End of Array Mark: |
---|
| 107 | kvs[len].key = NULL; |
---|
| 108 | kvs[len].value = NULL; |
---|
| 109 | |
---|
| 110 | // do the acctual filling: |
---|
| 111 | last_was_seg = 0; |
---|
| 112 | |
---|
| 113 | for (sp = str; *sp != 0; sp++) { |
---|
| 114 | if ( last_was_seg ) { |
---|
| 115 | if ( is_in(*sp, fdel) ) { |
---|
| 116 | last_was_seg = 0; |
---|
| 117 | *sp = 0; |
---|
| 118 | } else { |
---|
| 119 | last_was_seg = 1; |
---|
| 120 | if ( is_in(*sp, kdel) ) { |
---|
| 121 | *sp = 0; |
---|
| 122 | kvs[pos].value = sp+1; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } else { |
---|
| 126 | if ( !is_in(*sp, fdel) ) { |
---|
| 127 | last_was_seg = 1; |
---|
| 128 | pos++; |
---|
| 129 | kvs[pos].key = sp; |
---|
| 130 | kvs[pos].value = NULL; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | return len; |
---|
| 136 | } |
---|
| 137 | |
---|
[3973] | 138 | //ll |
---|