/src/pupnp/upnp/src/genlib/util/strintmap.c
Line | Count | Source |
1 | | /******************************************************************************* |
2 | | * |
3 | | * Copyright (c) 2000-2003 Intel Corporation |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions are met: |
8 | | * |
9 | | * - Redistributions of source code must retain the above copyright notice, |
10 | | * this list of conditions and the following disclaimer. |
11 | | * - Redistributions in binary form must reproduce the above copyright notice, |
12 | | * this list of conditions and the following disclaimer in the documentation |
13 | | * and/or other materials provided with the distribution. |
14 | | * - Neither name of Intel Corporation nor the names of its contributors |
15 | | * may be used to endorse or promote products derived from this software |
16 | | * without specific prior written permission. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR |
22 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
25 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
26 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
27 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
28 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | * |
30 | | ******************************************************************************/ |
31 | | |
32 | | /************************************************************************ |
33 | | * Purpose: This file contains string to integer and integer to string |
34 | | * conversion functions |
35 | | ************************************************************************/ |
36 | | |
37 | | #include "config.h" /* IWYU pragma: keep */ |
38 | | |
39 | | #include "membuffer.h" |
40 | | #include "strintmap.h" |
41 | | #include <stddef.h> |
42 | | |
43 | | /************************************************************************ |
44 | | * Function : map_str_to_int |
45 | | * |
46 | | * Parameters : |
47 | | * IN const char* name ; string containing the name to be matched |
48 | | * IN size_t name_len ; size of the string to be matched |
49 | | * IN str_int_entry* table ; table of entries that need to be |
50 | | * matched. |
51 | | * IN int num_entries ; number of entries in the table that need |
52 | | * to be searched. |
53 | | * IN int case_sensitive ; whether the case should be case |
54 | | * sensitive or not |
55 | | * |
56 | | * Description : Match the given name with names from the entries in the |
57 | | * table. Returns the index of the table when the entry is found. |
58 | | * |
59 | | * Return : int ; |
60 | | * index - On Success |
61 | | * -1 - On failure |
62 | | * |
63 | | * Note : |
64 | | ************************************************************************/ |
65 | | int map_str_to_int(const char *name, |
66 | | size_t name_len, |
67 | | str_int_entry *table, |
68 | | int num_entries, |
69 | | int case_sensitive) |
70 | 42.1k | { |
71 | 42.1k | int top, mid, bot; |
72 | 42.1k | int cmp; |
73 | 42.1k | memptr name_ptr; |
74 | | |
75 | 42.1k | name_ptr.buf = (char *)name; |
76 | 42.1k | name_ptr.length = name_len; |
77 | | |
78 | 42.1k | top = 0; |
79 | 42.1k | bot = num_entries - 1; |
80 | | |
81 | 192k | while (top <= bot) { |
82 | 185k | mid = (top + bot) / 2; |
83 | 185k | if (case_sensitive) { |
84 | | /*cmp = strcmp( name, table[mid].name ); */ |
85 | 14.6k | cmp = memptr_cmp(&name_ptr, table[mid].name); |
86 | 170k | } else { |
87 | | /*cmp = strcasecmp( name, table[mid].name ); */ |
88 | 170k | cmp = memptr_cmp_nocase(&name_ptr, table[mid].name); |
89 | 170k | } |
90 | | |
91 | 185k | if (cmp > 0) { |
92 | 93.2k | top = mid + 1; /* look below mid */ |
93 | 93.2k | } else if (cmp < 0) { |
94 | 56.9k | bot = mid - 1; /* look above mid */ |
95 | 56.9k | } else /* cmp == 0 */ |
96 | 35.4k | { |
97 | 35.4k | return mid; /* match; return table index */ |
98 | 35.4k | } |
99 | 185k | } |
100 | | |
101 | 6.64k | return -1; /* header name not found */ |
102 | 42.1k | } |
103 | | |
104 | | /************************************************************************ |
105 | | * Function : map_int_to_str |
106 | | * |
107 | | * Parameters : |
108 | | * IN int id ; ID to be matched |
109 | | * IN str_int_entry* table ; table of entries that need to be |
110 | | * matched. |
111 | | * IN int num_entries ; number of entries in the table that need |
112 | | * to be searched. |
113 | | * |
114 | | * Description : Returns the index from the table where the id matches |
115 | | * the entry from the table. |
116 | | * |
117 | | * Return : int ; |
118 | | * |
119 | | * Note : |
120 | | ************************************************************************/ |
121 | | int map_int_to_str(int id, str_int_entry *table, int num_entries) |
122 | 0 | { |
123 | 0 | int i; |
124 | |
|
125 | 0 | for (i = 0; i < num_entries; i++) { |
126 | 0 | if (table[i].id == id) { |
127 | 0 | return i; |
128 | 0 | } |
129 | 0 | } |
130 | 0 | return -1; |
131 | 0 | } |