1 //---------------------------------------------------------------------- 2 // This software is part of the OpenBeOS distribution and is covered 3 // by the OpenBeOS license. 4 // 5 // Copyright (c) 2003 Waldemar Kornewald, Waldemar.Kornewald@web.de 6 //--------------------------------------------------------------------- 7 8 #include <KPPPDefs.h> 9 #include <driver_settings.h> 10 11 #include "settings_tools.h" 12 13 #include <cstring> 14 #include <malloc.h> 15 16 17 static void copy_parameter(const driver_parameter *from, driver_parameter *to); 18 static void free_driver_parameter(driver_parameter *p); 19 20 21 driver_settings* 22 dup_driver_settings(const driver_settings *dup) 23 { 24 if(!dup) 25 return NULL; // we got a NULL pointer, so return nothing 26 27 driver_settings *ret = (driver_settings*) malloc(sizeof(driver_settings)); 28 29 ret->parameter_count = dup->parameter_count; 30 31 if(ret->parameter_count > 0) 32 ret->parameters = 33 (driver_parameter*) malloc(ret->parameter_count * sizeof(driver_parameter)); 34 else 35 ret->parameters = NULL; 36 37 for(int32 index = 0; index < ret->parameter_count; index++) 38 copy_parameter(&dup->parameters[index], &ret->parameters[index]); 39 40 return ret; 41 } 42 43 44 static 45 void 46 copy_parameter(const driver_parameter *from, driver_parameter *to) 47 { 48 to->name = strdup(from->name); 49 to->value_count = from->value_count; 50 51 if(to->value_count > 0) 52 to->values = (char**) malloc(to->value_count * sizeof(char*)); 53 else 54 to->values = NULL; 55 56 for(int32 index = 0; index < to->value_count; index++) 57 to->values[index] = strdup(from->values[index]); 58 59 to->parameter_count = from->parameter_count; 60 61 if(to->parameter_count > 0) 62 to->parameters = 63 (driver_parameter*) malloc(to->parameter_count * sizeof(driver_parameter)); 64 else 65 to->parameters = NULL; 66 67 for(int32 index = 0; index < to->parameter_count; index++) 68 copy_parameter(&from->parameters[index], &to->parameters[index]); 69 } 70 71 72 void 73 free_driver_settings(driver_settings *settings) 74 { 75 if(!settings) 76 return; 77 78 for(int32 index = 0; index < settings->parameter_count; index++) 79 free_driver_parameter(&settings->parameters[index]); 80 81 free(settings->parameters); 82 free(settings); 83 } 84 85 86 static 87 void 88 free_driver_parameter(driver_parameter *p) 89 { 90 free(p->name); 91 92 for(int32 index = 0; index < p->value_count; index++) 93 free(p->values[index]); 94 95 free(p->values); 96 97 for(int32 index = 0; index < p->parameter_count; index++) 98 free_driver_parameter(&p->parameters[index]); 99 100 free(p->parameters); 101 } 102 103 104 bool 105 equal_driver_settings(const driver_settings *lhs, const driver_settings *rhs) 106 { 107 if(!lhs && !rhs) 108 return true; 109 else if(!lhs || !rhs) 110 return false; 111 112 if(lhs->parameter_count != rhs->parameter_count) 113 return false; 114 115 for(int32 index = 0; index < lhs->parameter_count; index++) { 116 if(!equal_driver_parameters(&lhs->parameters[index], &rhs->parameters[index])) 117 return false; 118 } 119 120 return true; 121 } 122 123 124 bool 125 equal_driver_parameters(const driver_parameter *lhs, const driver_parameter *rhs) 126 { 127 if(!lhs && !rhs) 128 return true; 129 else if(!lhs || !rhs) 130 return false; 131 132 if(lhs->name && rhs->name) { 133 if(strcmp(lhs->name, rhs->name)) 134 return false; 135 } else if(lhs->name != rhs->name) 136 return false; 137 138 if(lhs->value_count != rhs->value_count 139 || lhs->parameter_count != rhs->parameter_count) 140 return false; 141 142 for(int32 index = 0; index < lhs->value_count; index++) { 143 if(strcmp(lhs->values[index], rhs->values[index])) 144 return false; 145 } 146 147 for(int32 index = 0; index < lhs->parameter_count; index++) { 148 if(!equal_driver_parameters(&lhs->parameters[index], &rhs->parameters[index])) 149 return false; 150 } 151 152 return true; 153 } 154 155 156 ppp_side 157 get_side_string_value(const char *sideString, ppp_side unknownValue) 158 { 159 if(!sideString) 160 return unknownValue; 161 162 if(!strcasecmp(sideString, "local")) 163 return PPP_LOCAL_SIDE; 164 if(!strcasecmp(sideString, "peer")) 165 return PPP_PEER_SIDE; 166 if(!strcasecmp(sideString, "none") 167 || !strcasecmp(sideString, "no")) 168 return PPP_NO_SIDE; 169 if(!strcasecmp(sideString, "both")) 170 return PPP_BOTH_SIDES; 171 172 // no correct value has been found => return default value 173 return unknownValue; 174 } 175 176 177 bool 178 get_boolean_value(const char *string, bool unknownValue) 179 { 180 if(!string) 181 return unknownValue; 182 183 if (!strcmp(string, "1") 184 || !strcasecmp(string, "true") 185 || !strcasecmp(string, "yes") 186 || !strcasecmp(string, "on") 187 || !strcasecmp(string, "enable") 188 || !strcasecmp(string, "enabled")) 189 return true; 190 191 if (!strcmp(string, "0") 192 || !strcasecmp(string, "false") 193 || !strcasecmp(string, "no") 194 || !strcasecmp(string, "off") 195 || !strcasecmp(string, "disable") 196 || !strcasecmp(string, "disabled")) 197 return false; 198 199 // no correct value has been found => return default value 200 return unknownValue; 201 } 202 203 204 const char* 205 get_settings_value(const char *name, const driver_settings *settings) 206 { 207 if(!name || !settings) 208 return NULL; 209 210 for(int32 index = 0; index < settings->parameter_count; index++) 211 if(!strcasecmp(settings->parameters[index].name, name) 212 && settings->parameters[index].value_count > 0 213 && settings->parameters[index].values) 214 return settings->parameters[index].values[0]; 215 216 return NULL; 217 } 218