cr_map.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007-2008 1&1 Internet AG
00005  *
00006  * This file is part of SIP-router, a free SIP server.
00007  *
00008  * SIP-router 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  * SIP-router 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 
00030 #include <stdlib.h>
00031 #include "cr_map.h"
00032 #include "../../mem/shm_mem.h"
00033 #include "../../ut.h"
00034 
00035 
00036 
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 
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 
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 }