#define _CRT_SECURE_NO_WARNINGS
#include "json.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
//#include <memory.h>
enum LEX_VALUE
{
LEX_MORE = 0,
LEX_INVALID_CHARACTER,
LEX_TRUE,
LEX_FALSE,
LEX_NULL,
LEX_BEGIN_OBJECT,
LEX_END_OBJECT,
LEX_BEGIN_ARRAY,
LEX_END_ARRAY,
LEX_NAME_SEPARATOR,
LEX_VALUE_SEPARATOR,
LEX_STRING,
LEX_NUMBER,
LEX_ERROR,
LEX_MEMORY
};
/* rc_string part */
#define RSTRING_INCSTEP 5
#define RSTRING_DEFAULT 8
struct rui_cstring
{
char* text; /*<! char c-string */
size_t length; /*<! put in place to avoid strlen() calls */
size_t max; /*<! usable memory allocated to text minus the space for the nul character */
};
typedef struct rui_cstring rcstring;
//返回结果,自定义
enum rui_string_error_codes
{
RS_MEMORY,
RS_OK = 1,
RS_UNKNOWN
};
typedef enum rui_string_error_codes rstring_code;
//创建一个rcstring
rcstring* rcs_create ( size_t length )
{
rcstring* rcs;
rcs = malloc( sizeof( rcstring ) ); /* allocates memory for a struct rcstring */
if ( rcs == NULL )
{
return NULL;
}
rcs->max = length;
rcs->length = 0;
rcs->text = malloc( ( rcs->max + 1 ) * sizeof( char ) );
if ( rcs->text == NULL )
{
free( rcs );
return NULL;
}
rcs->text[0] = '\0';
return rcs;
}
void rcs_free ( rcstring** rcs )
{
assert( rcs != NULL );
if ( *rcs != NULL )
{
if ( ( *rcs )->text != NULL )
{
free( ( *rcs )->text );
( *rcs )->text = NULL;
}
free( *rcs );
*rcs = NULL;
}
}
//重新分配内存
rstring_code rcs_resize ( rcstring* rcs, size_t length )
{
char* temp;
assert( rcs != NULL );
temp = realloc( rcs->text, sizeof( char ) * ( length + 1 ) ); /* length plus '\0' */
if ( temp == NULL )
{
free( rcs );
return RS_MEMORY;
}
rcs->text = temp;
rcs->max = length;
rcs->text[rcs->max] = '\0';
return RS_OK;
}
//将pos内容续接到pre里
rstring_code rcs_catcs ( rcstring* pre, const char* pos, const size_t length )
{
assert( pre != NULL );
assert( pos != NULL );
if ( pre->max < pre->length + length )
{
if ( rcs_resize( pre, pre->length + length + RSTRING_INCSTEP ) != RS_OK )
{
return RS_MEMORY;
}
}
strncpy( pre->text + pre->length, pos, length );
pre->text[pre->length + length] = '\0';
pre->length += length;
return RS_OK;
}
//将字符续接到pre
rstring_code rcs_catc ( rcstring* pre, const char c )
{
assert( pre != NULL );
if ( pre->max <= pre->length )
{
if ( rcs_resize( pre, pre->max + RSTRING_INCSTEP ) != RS_OK )
{
return RS_MEMORY;
}
}
pre->text[pre->length] = c;
pre->length++;
pre->text[pre->length] = '\0';
return RS_OK;
}
//返回字符串原先的内存
char* rcs_unwrap ( rcstring* rcs )
{
char* out;
assert( rcs != NULL );
if ( rcs->text == NULL )
{
out = NULL;
}
else
{
out = realloc( rcs->text, sizeof( char ) * ( strlen( rcs->text ) + 1 ) );
}
free( rcs );
return out;
}
//返回字符串内容的长度
size_t rcs_length ( rcstring* rcs )
{
/*TODO account for UTF8 */
assert( rcs != NULL );
return rcs->length;
}
/* end of rc_string part */
//新建一个type类型的json节点
json_t* json_new_value ( const enum json_value_type type )
{
json_t* new_object;
/* allocate memory to the new object */
new_object = malloc( sizeof( json_t ) );
if ( new_object == NULL )
{
return NULL;
}
/* initialize members */
new_object->text = NULL;
new_object->parent = NULL;
new_object->child = NULL;
new_object->child_end = NULL;
new_object->previous = NULL;
new_object->next = NULL;
new_object->type = type;
return new_object;
}
//新建一个字符串类型的json
json_t* json_new_string ( const char* text )
{
json_t* new_object;
size_t length;
assert( text != NULL );
/* allocate memory for the new object */
new_object = malloc( sizeof( json_t ) );
if ( new_object == NULL )
{
return NULL;
}
/* initialize members */
length = strlen( text ) + 1;
new_object->text = malloc( length * sizeof( char ) );
if ( new_object->text == NULL )
{
free( new_object );
return NULL;
}
strncpy( new_object->text, text, length );
new_object->parent = NULL;
new_object->child = NULL;
new_object->child_end = NULL;
new_object->previous = NULL;
new_object->next = NULL;
new_object->type = JSON_STRING;
new_object->flg = 0;
return new_object;
}
//新建一个数字类型json节点
json_t* json_new_number ( const char* text )
{
json_t* new_object;
size_t length;
assert( text != NULL );
/* allocate memory for the new object */
new_object = malloc( sizeof( json_t ) );
if ( new_object == NULL )
{
return NULL;
}
/* initialize members */
length = strlen( text ) + 1;
new_object->text = malloc( length * sizeof( char ) );
if ( new_object->text == NULL )
{
free( new_object );
return NULL;
}
strncpy( new_object->text, text, length );
new_object->parent = NULL;
new_object->child = NULL;
new_object->child_end = NULL;
new_object->previous = NULL;
new_object->next = NULL;
new_object->type = JSON_NUMBER;
return new_object;
}
json_t* json_new_object ( void )
{
return json_new_value( JSON_OBJECT );
}
json_t* json_new_array ( void )
{
return json_new_value( JSON_ARRAY );
}
json_t* json_new_null ( void )
{
return json_new_value( JSON_NULL );
}
json_t* json_new_true ( void )
{
return json_new_value( JSON_TRUE );
}
json_t* json_new_false ( void )
{
return json_new_value( JSON_FALSE );
}
//释放json节点
void json_free_value ( json_t** value )
{
assert( value != NULL );
assert( ( *value ) != NULL );
/* free each and every child node */
if ( ( *value )->child != NULL )
{
json_t* i, * j;
i = ( *value )->child_end;
while ( i != NULL )
{
j = i->previous;
json_free_value( &i ); /*TODO replace recursive solution with an iterative one */
i = j;
}
}
/* fixing sibling linked list connections */
if ( ( *value )->previous && ( *value )->next )
{
( *value )->previous->next = ( *value )->next;
( *value )->next->previous = ( *value )->previous;
}
else
{
if ( ( *value )->previous )
{
( *value )->previous->next = NULL;
}
if ( ( *value )->next )
{
( *value )->next->previous = NULL;
}
}
/*fixing parent node connections */
if ( ( *value )->parent )
{
/* fix the tree connection to the first node in the children's list */
if ( ( *value )->parent->child == ( *value ) )
{
if ( ( *value )->next )
{
( *value )->parent->child = ( *value )->next; /* the parent node always points to the first node in the children linked list */
}
else
{
( *value )->parent->child = NULL;
}
}
/* fix the tree connection to the last node in the children's list */
if ( ( *value )->parent->child_end == ( *value ) )
{
if ( ( *value )->previous )
{
( *value )->parent->child_end = ( *value )->previous; /* the parent node always points to the last node in the children linked list */
}
else
{
( *value )->parent->child_end = NULL;
}
}
}
/*finally, freeing the memory allocated for this value */
if ( ( *value )->text != NULL )
{
free( ( *value )->text );
评论12
最新资源