fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 218 lines 4.2 kB view raw
1/***************************************************************************** 2 * libini * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/libini/value.c * 7 * Created: 2001-08-24 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2001-2010 Hampa Hug <hampa@hampa.ch> * 9 *****************************************************************************/ 10 11/***************************************************************************** 12 * This program is free software. You can redistribute it and / or modify it * 13 * under the terms of the GNU General Public License version 2 as published * 14 * by the Free Software Foundation. * 15 * * 16 * This program is distributed in the hope that it will be useful, but * 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 * Public License for more details. * 20 *****************************************************************************/ 21 22 23#include <config.h> 24 25#include <stdlib.h> 26#include <string.h> 27 28#include <libini/libini.h> 29 30 31void ini_val_init (ini_val_t *val, const char *name) 32{ 33 val->next = NULL; 34 val->name = NULL; 35 val->type = INI_VAL_NONE; 36 37 if (name != NULL) { 38 val->name = strdup (name); 39 } 40} 41 42void ini_val_free (ini_val_t *val) 43{ 44 if (val != NULL) { 45 ini_val_set_none (val); 46 free (val->name); 47 } 48} 49 50ini_val_t *ini_val_new (const char *name) 51{ 52 ini_val_t *val; 53 54 val = malloc (sizeof (ini_val_t)); 55 56 if (val == NULL) { 57 return (NULL); 58 } 59 60 ini_val_init (val, name); 61 62 return (val); 63} 64 65void ini_val_del (ini_val_t *val) 66{ 67 ini_val_t *tmp; 68 69 while (val != NULL) { 70 tmp = val; 71 val = val->next; 72 73 ini_val_free (tmp); 74 75 free (tmp); 76 } 77} 78 79void ini_val_set_none (ini_val_t *val) 80{ 81 if (val->type == INI_VAL_STR) { 82 free (val->val.str); 83 } 84 85 val->type = INI_VAL_NONE; 86} 87 88void ini_val_copy (ini_val_t *dst, const ini_val_t *src) 89{ 90 switch (src->type) { 91 case INI_VAL_INT: 92 ini_val_set_uint32 (dst, src->val.u32); 93 break; 94 95 case INI_VAL_STR: 96 ini_val_set_str (dst, src->val.str); 97 break; 98 99 default: 100 ini_val_set_none (dst); 101 break; 102 } 103} 104 105void ini_val_set_uint32 (ini_val_t *val, unsigned long v) 106{ 107 ini_val_set_none (val); 108 109 val->type = INI_VAL_INT; 110 val->val.u32 = v; 111} 112 113void ini_val_set_sint32 (ini_val_t *val, long v) 114{ 115 unsigned long t; 116 117 ini_val_set_none (val); 118 119 if (v < 0) { 120 t = -v; 121 t = (~t + 1) & 0xffffffff; 122 } 123 else { 124 t = v; 125 } 126 127 val->type = INI_VAL_INT; 128 val->val.u32 = t; 129} 130 131void ini_val_set_bool (ini_val_t *val, int v) 132{ 133 ini_val_set_none (val); 134 135 val->type = INI_VAL_INT; 136 val->val.u32 = (v != 0); 137} 138 139void ini_val_set_str (ini_val_t *val, const char *v) 140{ 141 ini_val_set_none (val); 142 143 val->type = INI_VAL_STR; 144 val->val.str = strdup (v); 145} 146 147 148int ini_val_get_uint32 (const ini_val_t *val, unsigned long *v) 149{ 150 if (val->type == INI_VAL_INT) { 151 *v = val->val.u32; 152 return (0); 153 } 154 155 return (1); 156} 157 158int ini_val_get_sint32 (const ini_val_t *val, long *v) 159{ 160 if (val->type == INI_VAL_INT) { 161 if (val->val.u32 & 0x80000000) { 162 *v = -(long) ((~val->val.u32 + 1) & 0x7fffffff); 163 } 164 else { 165 *v = (long) val->val.u32; 166 } 167 168 return (0); 169 } 170 171 return (1); 172} 173 174int ini_val_get_uint16 (const ini_val_t *val, unsigned *v) 175{ 176 if (val->type == INI_VAL_INT) { 177 *v = val->val.u32 & 0xffff; 178 return (0); 179 } 180 181 return (1); 182} 183 184int ini_val_get_sint16 (const ini_val_t *val, int *v) 185{ 186 long tmp; 187 188 if (ini_val_get_sint32 (val, &tmp)) { 189 return (1); 190 } 191 192 if ((tmp < -0x7fff) || (tmp > 0x7fff)) { 193 return (1); 194 } 195 196 *v = tmp; 197 198 return (0); 199} 200 201int ini_val_get_bool (const ini_val_t *val, int *v) 202{ 203 if (val->type == INI_VAL_INT) { 204 *v = (val->val.u32 != 0); 205 return (0); 206 } 207 208 return (1); 209} 210 211const char *ini_val_get_str (const ini_val_t *val) 212{ 213 if (val->type != INI_VAL_STR) { 214 return (NULL); 215 } 216 217 return (val->val.str); 218}