fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 269 lines 4.9 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/lib/msg.c * 7 * Created: 2005-12-08 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2005-2020 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 <stdio.h> 24#include <stdlib.h> 25#include <string.h> 26 27#include <lib/msg.h> 28#include <lib/string.h> 29 30 31int msg_is_message (const char *msg, const char *val) 32{ 33 while (*msg != 0) { 34 if (strcmp (msg, val) == 0) { 35 return (1); 36 } 37 38 while ((*msg != 0) && (*msg != '.')) { 39 msg += 1; 40 } 41 42 if (*msg == '.') { 43 msg += 1; 44 } 45 } 46 47 return (0); 48} 49 50int msg_is_prefix (const char *pre, const char *val) 51{ 52 while ((*pre != 0) && (*pre == *val)) { 53 pre += 1; 54 val += 1; 55 } 56 57 if ((*pre != 0) || (*val != '.')) { 58 return (0); 59 } 60 61 return (1); 62} 63 64int msg_get_ulng (const char *str, unsigned long *val) 65{ 66 unsigned long tmp; 67 char *end; 68 69 tmp = strtoul (str, &end, 0); 70 71 if ((end == str) || (*end != 0)) { 72 return (1); 73 } 74 75 *val = tmp; 76 77 return (0); 78} 79 80int msg_get_slng (const char *str, long *val) 81{ 82 unsigned long tmp; 83 char *end; 84 85 tmp = strtol (str, &end, 0); 86 87 if ((end == str) || (*end != 0)) { 88 return (1); 89 } 90 91 *val = tmp; 92 93 return (0); 94} 95 96int msg_get_uint (const char *str, unsigned *val) 97{ 98 unsigned long tmp; 99 100 if (msg_get_ulng (str, &tmp)) { 101 return (1); 102 } 103 104 *val = tmp; 105 106 if (*val != tmp) { 107 return (1); 108 } 109 110 return (0); 111} 112 113int msg_get_sint (const char *str, int *val) 114{ 115 long tmp; 116 117 if (msg_get_slng (str, &tmp)) { 118 return (1); 119 } 120 121 *val = tmp; 122 123 if (*val != tmp) { 124 return (1); 125 } 126 127 return (0); 128} 129 130int msg_get_double (const char *str, double *val) 131{ 132 double tmp; 133 char *end; 134 135 tmp = strtod (str, &end); 136 137 if ((end == str) || (*end != 0)) { 138 return (1); 139 } 140 141 *val = tmp; 142 143 return (0); 144} 145 146int msg_get_bool (const char *str, int *val) 147{ 148 unsigned long tmp; 149 150 if ((str == NULL) || (*str == 0)) { 151 *val = 0; 152 return (0); 153 } 154 155 if (strcmp (str, "true") == 0) { 156 *val = 1; 157 return (0); 158 } 159 else if (strcmp (str, "false") == 0) { 160 *val = 0; 161 return (0); 162 } 163 164 if (msg_get_ulng (str, &tmp)) { 165 return (1); 166 } 167 168 *val = (tmp != 0); 169 170 return (0); 171} 172 173int msg_get_prefix_ulng (const char **str, unsigned long *val, const char *sep, const char *trim) 174{ 175 int r; 176 char *pre1, *pre2; 177 const char *tmp; 178 179 tmp = *str; 180 181 pre1 = str_extract_alloc (tmp, sep, &tmp); 182 pre2 = str_trim (pre1, " \t", " \t"); 183 184 r = msg_get_ulng (pre2, val); 185 186 free (pre1); 187 188 if (r) { 189 return (1); 190 } 191 192 *str = str_ltrim (tmp, trim); 193 194 return (0); 195} 196 197int msg_get_prefix_uint (const char **str, unsigned *val, const char *sep, const char *trim) 198{ 199 unsigned long tmp; 200 201 if (msg_get_prefix_ulng (str, &tmp, sep, trim)) { 202 return (1); 203 } 204 205 *val = tmp; 206 207 return (0); 208} 209 210int msg_get_prefix_slng (const char **str, long *val, const char *sep, const char *trim) 211{ 212 int r; 213 char *pre1, *pre2; 214 const char *tmp; 215 216 tmp = *str; 217 218 pre1 = str_extract_alloc (tmp, sep, &tmp); 219 pre2 = str_trim (pre1, " \t", " \t"); 220 221 r = msg_get_slng (pre2, val); 222 223 free (pre1); 224 225 if (r) { 226 return (1); 227 } 228 229 *str = str_ltrim (tmp, trim); 230 231 return (0); 232} 233 234int msg_get_prefix_sint (const char **str, int *val, const char *sep, const char *trim) 235{ 236 long tmp; 237 238 if (msg_get_prefix_slng (str, &tmp, sep, trim)) { 239 return (1); 240 } 241 242 *val = tmp; 243 244 return (0); 245} 246 247int msg_get_prefix_bool (const char **str, int *val, const char *sep, const char *trim) 248{ 249 int r; 250 char *pre1, *pre2; 251 const char *tmp; 252 253 tmp = *str; 254 255 pre1 = str_extract_alloc (tmp, sep, &tmp); 256 pre2 = str_trim (pre1, " \t", " \t"); 257 258 r = msg_get_bool (pre2, val); 259 260 free (pre1); 261 262 if (r) { 263 return (1); 264 } 265 266 *str = str_ltrim (tmp, trim); 267 268 return (0); 269}