2 * Copyright 2008 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "key_value.h"
22 static KeyValue *key_values = NULL;
23 static unsigned int number_of_key_values = 0;
25 void set_key_values(KeyValue * const new_key_values,
26 const unsigned int new_number_of_key_values) {
27 key_values = new_key_values;
28 number_of_key_values = new_number_of_key_values;
31 /* Compare two key members of KeyValue structures. */
32 static int key_value_compare_keys(const void *a, const void *b) {
33 return (int)((KeyValue*)a)->key - (int)((KeyValue*)b)->key;
36 /* Search an array of key value pairs for the item with the specified value. */
37 KeyValue* find_item_by_value(const char * const value) {
39 for (i = 0; i < number_of_key_values; i++) {
40 if (strcmp(key_values[i].value, value) == 0) {
41 return &key_values[i];
47 /* Sort an array of key value pairs by key. */
48 void sort_items_by_key(void) {
49 qsort(key_values, number_of_key_values, sizeof(*key_values),
50 key_value_compare_keys);