source: MondoRescue/branches/2.2.9/mindi-busybox/libbb/compare_string_array.c@ 2729

Last change on this file since 2729 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 2.0 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]2/*
[2725]3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]4 */
5
6#include "libbb.h"
7
[1765]8/* returns the array index of the string */
9/* (index of first match is returned, or -1) */
[2725]10int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
[821]11{
12 int i;
13
14 for (i = 0; string_array[i] != 0; i++) {
15 if (strcmp(string_array[i], key) == 0) {
16 return i;
17 }
18 }
[1765]19 return -1;
[821]20}
21
[2725]22int FAST_FUNC index_in_strings(const char *strings, const char *key)
[1765]23{
24 int idx = 0;
25
[2725]26 while (*strings) {
[1765]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
36/* returns the array index of the string, even if it matches only a beginning */
37/* (index of first match is returned, or -1) */
38#ifdef UNUSED
[2725]39int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
[1765]40{
41 int i;
42 int len = strlen(key);
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 }
48 }
49 }
50 return -1;
51}
52#endif
53
[2725]54int FAST_FUNC index_in_substrings(const char *strings, const char *key)
[1765]55{
[2725]56 int matched_idx = -1;
57 const int len = strlen(key);
[1765]58
59 if (len) {
60 int idx = 0;
[2725]61 while (*strings) {
[1765]62 if (strncmp(strings, key, len) == 0) {
[2725]63 if (strings[len] == '\0')
64 return idx; /* exact match */
65 if (matched_idx >= 0)
66 return -1; /* ambiguous match */
67 matched_idx = idx;
[1765]68 }
69 strings += strlen(strings) + 1; /* skip NUL */
70 idx++;
71 }
72 }
[2725]73 return matched_idx;
[1765]74}
[2725]75
76const char* FAST_FUNC nth_string(const char *strings, int n)
77{
78 while (n) {
79 n--;
80 strings += strlen(strings) + 1;
81 }
82 return strings;
83}
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. */
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
Note: See TracBrowser for help on using the repository browser.