JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
Enumerations | Functions | Variables
JST_load.c File Reference
#include "JST_private.h"
#include "JST_list.h"
#include "JST_string.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "jstools.h"
Include dependency graph for JST_load.c:

Go to the source code of this file.

Enumerations

enum  TokenType {
  TOKEN_NONE, TOKEN_OPEN_OBJECT, TOKEN_COLON, TOKEN_CLOSE_OBJECT,
  TOKEN_COMMA, TOKEN_OPEN_ARRAY, TOKEN_CLOSE_ARRAY, TOKEN_TRUE,
  TOKEN_FALSE, TOKEN_INTEGER, TOKEN_DOUBLE, TOKEN_STRING,
  TOKEN_NULL
}
 

Functions

JST_Error JST_load_from_stream (FILE *stream, JST_Element *root, JST_SyntaxError *syntax_error)
 Reads the input stream, allocates the node of the corresponding tree, than frees the input buffer. More...
 
JST_Error JST_load_from_file (const char *path, JST_Element *root, JST_SyntaxError *syntax_error)
 Reads the input file, allocates the node of the corresponding tree, than frees the input buffer. More...
 

Variables

bool JST_DEBUG_SHOW_TOKENS = false
 
const JST_Array JST_Array_Zero = { .items = NULL, .count = 0U, .parent = NULL }
 Constant defined to initialize safely a variable of type JST_Array. More...
 
const JST_Object JST_Object_Zero = { .items = NULL, .count = 0U, .parent = NULL, .first = NULL }
 Constant defined to initialize safely a variable of type JST_Object. More...
 
const JST_Value JST_Value_Zero = { .object = JST_Object_Zero }
 Constant defined to initialize safely a variable of type JST_Value. More...
 
const JST_Element JST_Element_Zero = { .type = JST_NULL, .value = JST_Value_Zero }
 Constant defined to initialize safely a variable of type JST_Element. More...
 
const JST_ArrayItem JST_ArrayItem_Zero = { .parent = NULL, .element = JST_Element_Zero }
 
const JST_Pair JST_Pair_Zero = { .parent = NULL, .element = JST_Element_Zero, .name = NULL, .next = NULL }
 Constant defined to initialize safely a variable of type JST_Pair. More...
 
const JST_SyntaxError JST_SyntaxError_Zero = { .line = 0U, .pos = 0U }
 Constant defined to initialize safely a variable of type JST_SyntaxError. More...
 

Enumeration Type Documentation

◆ TokenType

enum TokenType
Enumerator
TOKEN_NONE 
TOKEN_OPEN_OBJECT 
TOKEN_COLON 
TOKEN_CLOSE_OBJECT 
TOKEN_COMMA 
TOKEN_OPEN_ARRAY 
TOKEN_CLOSE_ARRAY 
TOKEN_TRUE 
TOKEN_FALSE 
TOKEN_INTEGER 
TOKEN_DOUBLE 
TOKEN_STRING 
TOKEN_NULL 

Definition at line 24 of file JST_load.c.

Function Documentation

◆ JST_load_from_file()

JST_Error JST_load_from_file ( const char *  filepath,
JST_Element root,
JST_SyntaxError error 
)

Reads the input file, allocates the node of the corresponding tree, than frees the input buffer.

Parameters
filepaththe file path
rootthe root of the JSON tree
linethe last line number read, available in case of error
contextfew characters before the error, available in case of error
ditsancethe offset of the error, in context
Returns
an error status.

Definition at line 573 of file JST_load.c.

573  {
574  if(( path == NULL )) {
575  return JST_ERR_NULL_ARGUMENT;
576  }
577  FILE * stream = fopen( path, "rt" );
578  if( stream == NULL ) {
579  return JST_ERR_ERRNO;
580  }
581  JST_Error error = JST_load_from_stream( stream, root, syntax_error );
582  int myerrno = errno;
583  fclose( stream );
584  errno = myerrno;
585  return error;
586 }
JST_Error JST_load_from_stream(FILE *stream, JST_Element *root, JST_SyntaxError *syntax_error)
Reads the input stream, allocates the node of the corresponding tree, than frees the input buffer...
Definition: JST_load.c:533
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
Function misused.
Definition: jstools.h:110
Here is the call graph for this function:

◆ JST_load_from_stream()

JST_Error JST_load_from_stream ( FILE *  stream,
JST_Element root,
JST_SyntaxError error 
)

Reads the input stream, allocates the node of the corresponding tree, than frees the input buffer.

Parameters
streamthe character stream to read
rootthe root of the JSON tree
linethe last line number read, available in case of error
contextfew characters before the error, available in case of error
ditsancethe offset of the error, in context
Returns
an error status.

Definition at line 533 of file JST_load.c.

533  {
534  if( syntax_error ) {
535  *syntax_error = JST_SyntaxError_Zero;
536  }
537  if(( stream == NULL )||( root == NULL )) {
538  return JST_ERR_NULL_ARGUMENT;
539  }
540  *root = JST_Element_Zero;
541  Context context = Context_Zero;
542  Token token = Token_Zero;
543  context.stream = stream;
544  bool ok = true
545  && get_next_token( &context, &token )
546  && set_value( &context, root, NULL, &token )
547  && remaining_chars_are_only_whitespaces( &context );
548  if( ! ok ) {
549  if( syntax_error ) {
550  syntax_error->line = (unsigned)( 1 + context.line );
551  size_t pos = (( context.pos < context.limit )||(context.limit == 0 )) ? context.pos : ( context.limit - 1 );
552  while(( pos > 0 )
553  && (( context.pos - pos ) < ( sizeof( syntax_error->context ) / 2 ))
554  && ( context.buffer[pos] != '\r' )
555  && ( context.buffer[pos] != '\n' ))
556  {
557  --pos;
558  }
559  if(( context.buffer[pos] == '\r' )||( context.buffer[pos] == '\n' )) {
560  ++pos;
561  }
562  strncpy( syntax_error->context, context.buffer + pos, sizeof( syntax_error->context ) - 1 );
563  syntax_error->context[sizeof( syntax_error->context )-1] = '\0';
564  syntax_error->pos = (unsigned)( context.pos - pos );
565  if( syntax_error->pos > 0 ) {
566  --(syntax_error->pos);
567  }
568  }
569  }
570  return context.error;
571 }
const JST_Element JST_Element_Zero
Constant defined to initialize safely a variable of type JST_Element.
Definition: JST_load.c:19
char context[80]
Characters around the error.
Definition: jstools.h:141
const JST_SyntaxError JST_SyntaxError_Zero
Constant defined to initialize safely a variable of type JST_SyntaxError.
Definition: JST_load.c:22
unsigned line
Last line&#39;s index (from 1)
Definition: jstools.h:140
Function misused.
Definition: jstools.h:110
unsigned pos
Position of error in context.
Definition: jstools.h:142
Here is the caller graph for this function:

Variable Documentation

◆ JST_Array_Zero

const JST_Array JST_Array_Zero = { .items = NULL, .count = 0U, .parent = NULL }

Constant defined to initialize safely a variable of type JST_Array.

Definition at line 16 of file JST_load.c.

◆ JST_ArrayItem_Zero

const JST_ArrayItem JST_ArrayItem_Zero = { .parent = NULL, .element = JST_Element_Zero }

Definition at line 20 of file JST_load.c.

◆ JST_DEBUG_SHOW_TOKENS

bool JST_DEBUG_SHOW_TOKENS = false

Definition at line 14 of file JST_load.c.

◆ JST_Element_Zero

const JST_Element JST_Element_Zero = { .type = JST_NULL, .value = JST_Value_Zero }

Constant defined to initialize safely a variable of type JST_Element.

Definition at line 19 of file JST_load.c.

◆ JST_Object_Zero

const JST_Object JST_Object_Zero = { .items = NULL, .count = 0U, .parent = NULL, .first = NULL }

Constant defined to initialize safely a variable of type JST_Object.

Definition at line 17 of file JST_load.c.

◆ JST_Pair_Zero

const JST_Pair JST_Pair_Zero = { .parent = NULL, .element = JST_Element_Zero, .name = NULL, .next = NULL }

Constant defined to initialize safely a variable of type JST_Pair.

Definition at line 21 of file JST_load.c.

◆ JST_SyntaxError_Zero

const JST_SyntaxError JST_SyntaxError_Zero = { .line = 0U, .pos = 0U }

Constant defined to initialize safely a variable of type JST_SyntaxError.

Definition at line 22 of file JST_load.c.

◆ JST_Value_Zero

const JST_Value JST_Value_Zero = { .object = JST_Object_Zero }

Constant defined to initialize safely a variable of type JST_Value.

Definition at line 18 of file JST_load.c.