| | @@ -1396,10 +1396,11 @@ |
| 1396 | 1396 | /* end file parser/JSON_parser.c */ |
| 1397 | 1397 | /* begin file ./cson.c */ |
| 1398 | 1398 | #include <assert.h> |
| 1399 | 1399 | #include <stdlib.h> /* malloc()/free() */ |
| 1400 | 1400 | #include <string.h> |
| 1401 | +#include <errno.h> |
| 1401 | 1402 | |
| 1402 | 1403 | #ifdef _MSC_VER |
| 1403 | 1404 | # if _MSC_VER >= 1400 /* Visual Studio 2005 and up */ |
| 1404 | 1405 | # pragma warning( push ) |
| 1405 | 1406 | # pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */ |
| | @@ -4950,10 +4951,81 @@ |
| 4950 | 4951 | } |
| 4951 | 4952 | else continue; |
| 4952 | 4953 | } |
| 4953 | 4954 | return 0; |
| 4954 | 4955 | } |
| 4956 | + |
| 4957 | +static cson_value * cson_guess_arg_type(char const *arg){ |
| 4958 | + char * end = NULL; |
| 4959 | + if(('0'<=*arg) && ('9'>=*arg)){ |
| 4960 | + goto do_string; |
| 4961 | + } |
| 4962 | + { |
| 4963 | + long const val = strtol(arg, &end, 10); |
| 4964 | + if(!*end){ |
| 4965 | + return cson_value_new_integer( (cson_int_t)val); |
| 4966 | + } |
| 4967 | + } |
| 4968 | + { |
| 4969 | + double const val = strtod(arg, &end); |
| 4970 | + if(!*end){ |
| 4971 | + return cson_value_new_double(val); |
| 4972 | + } |
| 4973 | + } |
| 4974 | + |
| 4975 | + |
| 4976 | + do_string: |
| 4977 | + return cson_value_new_string(arg, strlen(arg)); |
| 4978 | +} |
| 4979 | + |
| 4980 | + |
| 4981 | +int cson_parse_argv_flags( int argc, char const * const * argv, |
| 4982 | + cson_object ** tgt, unsigned int * count ){ |
| 4983 | + cson_object * o = NULL; |
| 4984 | + int rc = 0; |
| 4985 | + int i = 0; |
| 4986 | + if(argc<1 || !argc || !tgt) return cson_rc.ArgError; |
| 4987 | + o = *tgt ? *tgt : cson_new_object(); |
| 4988 | + if(count) *count = 0; |
| 4989 | + for( i = 0; i < argc; ++i ){ |
| 4990 | + char const * arg = argv[i]; |
| 4991 | + char const * key = arg; |
| 4992 | + char const * pos; |
| 4993 | + cson_string * k = NULL; |
| 4994 | + cson_value * v = NULL; |
| 4995 | + if('-' != *arg) continue; |
| 4996 | + while('-'==*key) ++key; |
| 4997 | + if(!*key) continue; |
| 4998 | + pos = key; |
| 4999 | + while( *pos && ('=' != *pos)) ++pos; |
| 5000 | + k = cson_new_string(key, pos-key); |
| 5001 | + if(!k){ |
| 5002 | + rc = cson_rc.AllocError; |
| 5003 | + break; |
| 5004 | + } |
| 5005 | + if(!*pos){ /** --key */ |
| 5006 | + v = cson_value_true(); |
| 5007 | + }else{ /** --key=...*/ |
| 5008 | + assert('=' == *pos); |
| 5009 | + ++pos /*skip '='*/; |
| 5010 | + v = *pos |
| 5011 | + ? cson_guess_arg_type(pos) |
| 5012 | + : cson_value_null(); |
| 5013 | + } |
| 5014 | + if(0 != (rc=cson_object_set_s(o, k, v))){ |
| 5015 | + cson_free_string(k); |
| 5016 | + cson_value_free(v); |
| 5017 | + break; |
| 5018 | + } |
| 5019 | + else if(count) ++*count; |
| 5020 | + } |
| 5021 | + if(o != *tgt){ |
| 5022 | + if(rc) cson_free_object(o); |
| 5023 | + else *tgt = o; |
| 5024 | + } |
| 5025 | + return rc; |
| 5026 | +} |
| 4955 | 5027 | |
| 4956 | 5028 | #if defined(__cplusplus) |
| 4957 | 5029 | } /*extern "C"*/ |
| 4958 | 5030 | #endif |
| 4959 | 5031 | |
| 4960 | 5032 | |