00001 /* 00002 * $Id: cr_map.c 5189 2008-11-12 15:53:01Z henningw $ 00003 * 00004 * Copyright (C) 2007-2008 1&1 Internet AG 00005 * 00006 * This file is part of Kamailio, a free SIP server. 00007 * 00008 * Kamailio is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version 00012 * 00013 * Kamailio is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 /** 00024 * \file cr_map.c 00025 * \brief Contains the functions to map domain and carrier names to ids. 00026 * \ingroup carrierroute 00027 * - Module; \ref carrierroute 00028 */ 00029 00030 #include <stdlib.h> 00031 #include "cr_map.h" 00032 #include "../../mem/shm_mem.h" 00033 #include "../../ut.h" 00034 00035 00036 00037 /** 00038 * Searches for the ID of a name 00039 * 00040 * @param map the mapping list to search in 00041 * @param size the size of the list 00042 * @param name the name, we are looking for 00043 * 00044 * @return values: on succcess the id for this name, -1 on failure 00045 */ 00046 int map_name2id(struct name_map_t * map, int size, const str * name) { 00047 int i; 00048 00049 if ((!name) || (name->len <= 0)) { 00050 return -1; 00051 } 00052 00053 for (i=0; i<size; i++) { 00054 if (str_strcmp(&map[i].name, name) == 0) return map[i].id; 00055 } 00056 return -1; 00057 } 00058 00059 00060 /** 00061 * Searches for the name of an ID 00062 * 00063 * @param map the mapping list to search in 00064 * @param size the size of the list 00065 * @param id the id, we are looking for 00066 * 00067 * @return values: on succcess the name for this id, NULL on failure 00068 */ 00069 str * map_id2name(struct name_map_t * map, int size, int id) { 00070 struct name_map_t key; 00071 struct name_map_t * tmp; 00072 00073 key.id = id; 00074 tmp = bsearch(&key, map, size, sizeof(struct name_map_t), compare_name_map); 00075 if (tmp == NULL) return NULL; 00076 return &tmp->name; 00077 } 00078 00079 00080 /** 00081 * Compares the IDs of two name_map_t structures. 00082 * 00083 * @return -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2 00084 */ 00085 int compare_name_map(const void *v1, const void *v2) { 00086 if (((struct name_map_t *)v1)->id < ((struct name_map_t *)v2)->id) return -1; 00087 else if (((struct name_map_t *)v1)->id > ((struct name_map_t *)v2)->id) return 1; 00088 else return 0; 00089 }
1.5.6