blob: 4b10cc138ab216556e12b93eecaa09a013e46f35 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathb9638752002-12-02 00:01:36 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathb9638752002-12-02 00:01:36 +00004 */
5
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00006#include "libbb.h"
Glenn L McGrathb9638752002-12-02 00:01:36 +00007
Denis Vlasenko5af906e2006-11-05 18:05:09 +00008/* returns the array index of the string */
9/* (index of first match is returned, or -1) */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000010int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
Glenn L McGrathb9638752002-12-02 00:01:36 +000011{
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000012 int i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000013
14 for (i = 0; string_array[i] != 0; i++) {
15 if (strcmp(string_array[i], key) == 0) {
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000016 return i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000017 }
18 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000019 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000020}
21
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000022int FAST_FUNC index_in_strings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000023{
24 int idx = 0;
25
Denis Vlasenkod12fcc22008-05-31 07:34:14 +000026 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +000027 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 Vlasenko5af906e2006-11-05 18:05:09 +000036/* returns the array index of the string, even if it matches only a beginning */
37/* (index of first match is returned, or -1) */
Denis Vlasenko990d0f62007-07-24 15:54:42 +000038#ifdef UNUSED
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000039int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
Denis Vlasenko5af906e2006-11-05 18:05:09 +000040{
41 int i;
42 int len = strlen(key);
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000043 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 Vlasenko5af906e2006-11-05 18:05:09 +000048 }
49 }
50 return -1;
51}
Denis Vlasenko990d0f62007-07-24 15:54:42 +000052#endif
53
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000054int FAST_FUNC index_in_substrings(const char *strings, const char *key)
Denis Vlasenko990d0f62007-07-24 15:54:42 +000055{
Denys Vlasenko8a659f62010-04-03 00:52:16 +020056 int matched_idx = -1;
57 const int len = strlen(key);
Denis Vlasenko990d0f62007-07-24 15:54:42 +000058
59 if (len) {
60 int idx = 0;
Denis Vlasenkod12fcc22008-05-31 07:34:14 +000061 while (*strings) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +000062 if (strncmp(strings, key, len) == 0) {
Denys Vlasenko8a659f62010-04-03 00:52:16 +020063 if (strings[len] == '\0')
64 return idx; /* exact match */
65 if (matched_idx >= 0)
66 return -1; /* ambiguous match */
67 matched_idx = idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000068 }
69 strings += strlen(strings) + 1; /* skip NUL */
70 idx++;
71 }
72 }
Denys Vlasenko8a659f62010-04-03 00:52:16 +020073 return matched_idx;
Denis Vlasenko990d0f62007-07-24 15:54:42 +000074}
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000075
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000076const char* FAST_FUNC nth_string(const char *strings, int n)
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000077{
Denis Vlasenko6b404432008-01-07 16:13:14 +000078 while (n) {
79 n--;
80 strings += strlen(strings) + 1;
81 }
82 return strings;
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000083}
Denys Vlasenko8a659f62010-04-03 00:52:16 +020084
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. */
87smallint 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