"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 7 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 8 | /* returns the array index of the string */ |
| 9 | /* (index of first match is returned, or -1) */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 10 | int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 11 | { |
"Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 12 | int i; |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 13 | |
| 14 | for (i = 0; string_array[i] != 0; i++) { |
| 15 | if (strcmp(string_array[i], key) == 0) { |
"Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 16 | return i; |
Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 17 | } |
| 18 | } |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 19 | return -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 22 | int FAST_FUNC index_in_strings(const char *strings, const char *key) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 23 | { |
| 24 | int idx = 0; |
| 25 | |
Denis Vlasenko | d12fcc2 | 2008-05-31 07:34:14 +0000 | [diff] [blame] | 26 | while (*strings) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 27 | if (strcmp(strings, key) == 0) { |
| 28 | return idx; |
| 29 | } |
| 30 | strings += strlen(strings) + 1; /* skip NUL */ |
| 31 | idx++; |
| 32 | } |
| 33 | return -1; |
| 34 | } |
| 35 | |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 36 | /* returns the array index of the string, even if it matches only a beginning */ |
| 37 | /* (index of first match is returned, or -1) */ |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 38 | #ifdef UNUSED |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 39 | int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key) |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 40 | { |
| 41 | int i; |
| 42 | int len = strlen(key); |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 43 | if (len) { |
| 44 | for (i = 0; string_array[i] != 0; i++) { |
| 45 | if (strncmp(string_array[i], key, len) == 0) { |
| 46 | return i; |
| 47 | } |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | return -1; |
| 51 | } |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 54 | int FAST_FUNC index_in_substrings(const char *strings, const char *key) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 55 | { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 56 | int matched_idx = -1; |
| 57 | const int len = strlen(key); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 58 | |
| 59 | if (len) { |
| 60 | int idx = 0; |
Denis Vlasenko | d12fcc2 | 2008-05-31 07:34:14 +0000 | [diff] [blame] | 61 | while (*strings) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 62 | if (strncmp(strings, key, len) == 0) { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 63 | if (strings[len] == '\0') |
| 64 | return idx; /* exact match */ |
| 65 | if (matched_idx >= 0) |
| 66 | return -1; /* ambiguous match */ |
| 67 | matched_idx = idx; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 68 | } |
| 69 | strings += strlen(strings) + 1; /* skip NUL */ |
| 70 | idx++; |
| 71 | } |
| 72 | } |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 73 | return matched_idx; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 74 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 76 | const char* FAST_FUNC nth_string(const char *strings, int n) |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 77 | { |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 78 | while (n) { |
| 79 | n--; |
| 80 | strings += strlen(strings) + 1; |
| 81 | } |
| 82 | return strings; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 83 | } |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 84 | |
| 85 | #ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */ |
| 86 | /* Returns 0 for no, 1 for yes or a negative value on error. */ |
| 87 | smallint FAST_FUNC yesno(const char *str) |
| 88 | { |
| 89 | static const char no_yes[] ALIGN1 = |
| 90 | "0\0" "off\0" "no\0" |
| 91 | "1\0" "on\0" "yes\0"; |
| 92 | int ret = index_in_substrings(no_yes, str); |
| 93 | return ret / 3; |
| 94 | } |
| 95 | #endif |