Move construction/allocation code for JSON structures to json_value.[ch]

git-svn-id: https://warzone2100.svn.sourceforge.net/svnroot/warzone2100/trunk@7396 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2009-05-12 15:47:42 +00:00 committed by Git SVN Gateway
parent ef5a01eeb9
commit af40e39f77
3 changed files with 110 additions and 71 deletions

View File

@ -59,9 +59,8 @@ typedef struct __json_element
%union
{
char* sval;
int ival;
long int ival;
double fval;
bool bval;
uint8_t u8val;
uint32_t u32val;
json_value* jval;
@ -86,7 +85,6 @@ typedef struct __json_element
%token TTRUE
%token TFALSE
/* Boolean rules */
%type <bval> bool_rule
%type <jval> json_bool
/* NULL tokens */
@ -197,50 +195,26 @@ json_value: json_string
json_array: '[' ']'
{
$$ = malloc(sizeof(*$$));
$$ = createJsonArray(0);
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
$$->type = JSON_ARRAY;
$$->value.array.size = 0;
#ifdef DEBUG
$$->value.array.a = NULL;
#endif
}
| '[' json_array_elements ']'
{
size_t i;
size_t i, count;
json_element* cur;
$$ = malloc(sizeof(*$$));
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
$$->type = JSON_ARRAY;
// Determine the amount of elements
$$->value.array.size = 0;
count = 0;
for (cur = $2; cur != NULL; cur = cur->next)
{
$$->value.array.size += 1;
count += 1;
}
// Allocate memory for the elements
$$->value.array.a = malloc(sizeof($$->value.array.a[0]) * $$->value.array.size);
if ($$->value.array.a == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
$$ = createJsonArray(count);
if ($$ == NULL)
YYABORT;
}
// Copy all elements into the array and delete the originals
cur = $2;
@ -288,62 +262,37 @@ json_array_elements: json_value
json_number: INTEGER
{
$$ = malloc(sizeof(*$$));
$$ = createJsonInteger($1);
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
$$->type = JSON_NUMBER_INT;
$$->value.num_int = $1;
}
| FLOAT
{
$$ = malloc(sizeof(*$$));
$$ = createJsonFloat($1);
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
$$->type = JSON_NUMBER_FLOAT;
$$->value.num_float = $1;
}
;
json_bool: bool_rule
json_bool: TTRUE
{
$$ = malloc(sizeof(*$$));
$$ = createJsonBool(true);
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
$$->type = JSON_BOOL;
$$->value.boolean = $1;
}
;
bool_rule: TTRUE
{ $$ = true; }
| TFALSE
{ $$ = false; }
{
$$ = createJsonBool(false);
if ($$ == NULL)
YYABORT;
}
;
json_null: TNULL
{
$$ = malloc(sizeof(*$$));
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
$$->type = JSON_NULL;
}
@ -353,11 +302,7 @@ json_string: STRING
{
$$ = createJsonString($1);
if ($$ == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
YYABORT;
}
// Clean up our string parameter (as we own it)
free($1);

View File

@ -83,6 +83,19 @@ void freeJsonValue(json_value* v)
free(v);
}
static json_value* allocJsonVal(void)
{
json_value* const jsonVal = malloc(sizeof(*jsonVal));
if (jsonVal == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
return NULL;
}
return jsonVal;
}
json_value* createJsonString(const char* s)
{
json_value* v = malloc(sizeof(*v) + strlen(s) + 1);
@ -117,6 +130,82 @@ json_value_pair* createJsonValuePair(const char* name, json_value* value)
return p;
}
json_value* createJsonArray(size_t size)
{
json_value* const jsonArray = allocJsonVal();
if (jsonArray == NULL)
return NULL;
jsonArray->type = JSON_ARRAY;
jsonArray->value.array.size = size;
if (size == 0)
{
#ifdef DEBUG
jsonArray->value.array.a = NULL;
#endif
return jsonArray;
}
// Allocate memory for the elements
jsonArray->value.array.a = calloc(size, sizeof(*jsonArray->value.array.a[0]));
if (jsonArray->value.array.a == NULL)
{
debug(LOG_ERROR, "Out of memory!");
abort();
free(jsonArray);
return NULL;
}
return jsonArray;
}
json_value* createJsonInteger(long int val)
{
json_value* const jsonVal = allocJsonVal();
if (jsonVal == NULL)
return NULL;
jsonVal->type = JSON_NUMBER_INT;
jsonVal->value.num_int = val;
return jsonVal;
}
json_value* createJsonFloat(double val)
{
json_value* const jsonVal = allocJsonVal();
if (jsonVal == NULL)
return NULL;
jsonVal->type = JSON_NUMBER_INT;
jsonVal->value.num_float = val;
return jsonVal;
}
json_value* createJsonBool(bool val)
{
json_value* const jsonVal = allocJsonVal();
if (jsonVal == NULL)
return NULL;
jsonVal->type = JSON_BOOL;
jsonVal->value.boolean = val;
return jsonVal;
}
json_value* createJsonNull()
{
json_value* const jsonVal = allocJsonVal();
if (jsonVal == NULL)
return NULL;
jsonVal->type = JSON_NULL;
return jsonVal;
}
static char* serializeJsonString(char* out, const char* s)
{
char* dptr;

View File

@ -63,6 +63,11 @@ extern void freeJsonValue(json_value* v);
extern json_value* createJsonString(const char* s);
extern json_value_pair* createJsonValuePair(const char* name, json_value* value);
extern json_value* createJsonArray(size_t size);
extern json_value* createJsonInteger(long int val);
extern json_value* createJsonFloat(double val);
extern json_value* createJsonBool(bool val);
extern json_value* createJsonNull(void);
extern char* serializeJsonValuePair(const json_value_pair* p);
extern char* serializeJsonValue(const json_value* v);