blob: 00e1bc4768db765524d73079f79a19302af82f49 [file] [log] [blame]
Heather Lee Wilsonbdd62c52013-12-28 15:12:39 -08001/*
2 * Copyright 2008 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <stddef.h>
17#include <stdlib.h>
18#include <string.h>
19
20typedef struct KeyValue {
21 unsigned int key;
22 const char* value;
23} KeyValue;
24
25static KeyValue *key_values = NULL;
26static unsigned int number_of_key_values = 0;
27
28void set_key_values(KeyValue * const new_key_values,
29 const unsigned int new_number_of_key_values) {
30 key_values = new_key_values;
31 number_of_key_values = new_number_of_key_values;
32}
33
34// Compare two key members of KeyValue structures.
35int key_value_compare_keys(const void *a, const void *b) {
36 return (int)((KeyValue*)a)->key - (int)((KeyValue*)b)->key;
37}
38
39// Search an array of key value pairs for the item with the specified value.
40KeyValue* find_item_by_value(const char * const value) {
41 unsigned int i;
42 for (i = 0; i < number_of_key_values; i++) {
43 if (strcmp(key_values[i].value, value) == 0) {
44 return &key_values[i];
45 }
46 }
47 return NULL;
48}
49
50// Sort an array of key value pairs by key.
51void sort_items_by_key() {
52 qsort(key_values, number_of_key_values, sizeof(*key_values),
53 key_value_compare_keys);
54}