pv_svar.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2006 voice-system.ro
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 
00029 #include <assert.h>
00030 #include <ctype.h>
00031 #include <string.h>
00032 #include <stdlib.h>
00033 
00034 #include "../../dprint.h"
00035 #include "../../mem/mem.h"
00036 
00037 #include "pv_svar.h"
00038 
00039 static script_var_t *script_vars = 0;
00040 
00041 script_var_t* add_var(str *name)
00042 {
00043         script_var_t *it;
00044 
00045         if(name==0 || name->s==0 || name->len<=0)
00046                 return 0;
00047 
00048         for(it=script_vars; it; it=it->next)
00049         {
00050                 if(it->name.len==name->len
00051                                 && strncmp(name->s, it->name.s, name->len)==0)
00052                         return it;
00053         }
00054         it = (script_var_t*)pkg_malloc(sizeof(script_var_t));
00055         if(it==0)
00056         {
00057                 LM_ERR("out of pkg mem\n");
00058                 return 0;
00059         }
00060         memset(it, 0, sizeof(script_var_t));
00061         it->name.s = (char*)pkg_malloc((name->len+1)*sizeof(char));
00062 
00063         if(it->name.s==0)
00064         {
00065                 LM_ERR("out of pkg mem!\n");
00066                 return 0;
00067         }
00068         it->name.len = name->len;
00069         strncpy(it->name.s, name->s, name->len);
00070         it->name.s[it->name.len] = '\0';
00071 
00072         it->next = script_vars;
00073 
00074         script_vars = it;
00075 
00076         return it;
00077 }
00078 
00079 script_var_t* set_var_value(script_var_t* var, int_str *value, int flags)
00080 {
00081         if(var==0)
00082                 return 0;
00083         if(value==NULL)
00084         {
00085                 if(var->v.flags&VAR_VAL_STR)
00086                 {
00087                         pkg_free(var->v.value.s.s);
00088                         var->v.flags &= ~VAR_VAL_STR;
00089                 }
00090                 memset(&var->v.value, 0, sizeof(int_str));
00091 
00092                 return var;
00093         }
00094 
00095         if(flags&VAR_VAL_STR)
00096         {
00097                 if(var->v.flags&VAR_VAL_STR)
00098                 { /* old and new value is str */
00099                         if(value->s.len>var->v.value.s.len)
00100                         { /* not enough space to copy */
00101                                 pkg_free(var->v.value.s.s);
00102                                 memset(&var->v.value, 0, sizeof(int_str));
00103                                 var->v.value.s.s =
00104                                         (char*)pkg_malloc((value->s.len+1)*sizeof(char));
00105                                 if(var->v.value.s.s==0)
00106                                 {
00107                                         LM_ERR("out of pkg mem\n");
00108                                         goto error;
00109                                 }
00110                         }
00111                 } else {
00112                         memset(&var->v.value, 0, sizeof(int_str));
00113                         var->v.value.s.s =
00114                                         (char*)pkg_malloc((value->s.len+1)*sizeof(char));
00115                         if(var->v.value.s.s==0)
00116                         {
00117                                 LM_ERR("out of pkg mem!\n");
00118                                 goto error;
00119                         }
00120                         var->v.flags |= VAR_VAL_STR;
00121                 }
00122                 strncpy(var->v.value.s.s, value->s.s, value->s.len);
00123                 var->v.value.s.len = value->s.len;
00124                 var->v.value.s.s[value->s.len] = '\0';
00125         } else {
00126                 if(var->v.flags&VAR_VAL_STR)
00127                 {
00128                         pkg_free(var->v.value.s.s);
00129                         var->v.flags &= ~VAR_VAL_STR;
00130                         memset(&var->v.value, 0, sizeof(int_str));
00131                 }
00132                 var->v.value.n = value->n;
00133         }
00134 
00135         return var;
00136 error:
00137         /* set the var to init value */
00138         memset(&var->v.value, 0, sizeof(int_str));
00139         var->v.flags &= ~VAR_VAL_STR;
00140         return NULL;
00141 }
00142 
00143 script_var_t* get_var_by_name(str *name)
00144 {
00145         script_var_t *it;
00146 
00147         if(name==0 || name->s==0 || name->len<=0)
00148                 return 0;
00149 
00150         for(it=script_vars; it; it=it->next)
00151         {
00152                 if(it->name.len==name->len
00153                                 && strncmp(name->s, it->name.s, name->len)==0)
00154                         return it;
00155         }
00156         return 0;
00157 }
00158 
00159 void reset_vars(void)
00160 {
00161         script_var_t *it;
00162         for(it=script_vars; it; it=it->next)
00163         {
00164                 if(it->v.flags&VAR_VAL_STR)
00165                 {
00166                         pkg_free(it->v.value.s.s);
00167                         it->v.flags &= ~VAR_VAL_STR;
00168                 }
00169                 memset(&it->v.value, 0, sizeof(int_str));
00170         }
00171 }
00172 
00173 void destroy_vars_list(script_var_t *svl)
00174 {
00175         script_var_t *it;
00176         script_var_t *it0;
00177 
00178         it = svl;
00179         while(it)
00180         {
00181                 it0 = it;
00182                 it = it->next;
00183                 pkg_free(it0->name.s);
00184                 if(it0->v.flags&VAR_VAL_STR)
00185                         pkg_free(it0->v.value.s.s);
00186                 pkg_free(it0);
00187         }
00188 
00189         svl = 0;
00190 }
00191 
00192 void destroy_vars(void)
00193 {
00194         destroy_vars_list(script_vars);
00195 }