Exemple #1
0
bool us_testutil_start( int32_t US_UNUSED( sys_allocator_size ), int32_t session_allocator_size, int argc, const char **argv )
{
    bool r = true;
    (void)argc;
    (void)argv;
#if US_ENABLE_NETWORK
    r = us_platform_init_sockets();
    if ( !r )
    {
        return r;
    }
#endif
    /*
      Initialize the system and the session allocators
    */
    us_testutil_sys_allocator = 0;
    us_testutil_session_allocator = 0;
#if US_ENABLE_MALLOC
    /*
      With malloc if we have it
    */
    us_testutil_sys_allocator = us_malloc_allocator_init( &us_testutil_sys_allocator_impl );
#else
    (void)sys_allocator_size;
    /*
      or with statically allocated bss segment data if we don't have malloc
    */
    us_testutil_sys_allocator = us_simple_allocator_init( &us_testutil_sys_allocator_impl,
                                                          &us_testutil_session_sys_buffer,
                                                          ( int32_t )( US_TESTUTIL_BUFFER_SIZE_IN_WORDS * sizeof( int32_t ) ) );
#endif
    (void)session_allocator_size;
    us_testutil_session_allocator
        = us_simple_allocator_init( &us_testutil_session_allocator_impl,
                                    &us_testutil_session_sys_buffer,
                                    ( int32_t )( US_TESTUTIL_BUFFER_SIZE_IN_WORDS * sizeof( int32_t ) ) );
    /*
      check for allocation failure
    */
    if ( !us_testutil_sys_allocator || !us_testutil_session_allocator )
    {
        r = false;
    }
/*
  Initialize any printing mechanism if enabled
*/
#if US_ENABLE_PRINTING
#if US_ENABLE_STDIO
    /*
      Using stdio.h FILE stdout if available
    */
    us_testutil_printer_stdout = us_print_file_init( &us_testutil_printer_stdout_impl, stdout );
    us_testutil_printer_stderr = us_print_file_init( &us_testutil_printer_stderr_impl, stderr );
#else
    /*
      Using statically allocated character buffer if not
    */
    us_testutil_printer_stdout
        = us_printraw_init( &us_testutil_printer_stdout_impl, us_testutil_printbuffer_stdout, US_TESTUTIL_PRINTBUFFER_SIZE );
    us_testutil_printer_stderr
        = us_printraw_init( &us_testutil_printer_stderr_impl, us_testutil_printbuffer_stderr, US_TESTUTIL_PRINTBUFFER_SIZE );
#endif
    /*
      report any failure of the printer
    */
    if ( !us_testutil_printer_stdout || !us_testutil_printer_stderr )
    {
        r = false;
    }
    us_stdout = us_testutil_printer_stdout;
    us_stderr = us_testutil_printer_stderr;
#endif
    return r;
}
int main ( int argc, char* argv[] )
{
    int count = 0, result = 0;
    FILE* input;
    us_malloc_allocator_t malloc_allocator;
    us_allocator_t *allocator;
    us_json_config_t config;
    us_json_parser_t jc = NULL;
    allocator = us_malloc_allocator_init ( &malloc_allocator );
    us_json_config_init ( &config );
    config.depth                  = 20;
    config.callback               = &print;
    config.allow_comments         = 1;
    config.handle_floats_manually = 0;
    /* Important! Set locale before parser is created.*/
#if 0
    if ( argc >= 2 )
    {
        if ( !setlocale ( LC_ALL, argv[1] ) )
        {
            fprintf ( stderr, "Failed to set locale to '%s'\n", argv[1] );
        }
    }
    else
    {
        fprintf ( stderr, "No locale provided, C locale is used\n" );
    }
#endif
    jc = us_json_parser_create ( allocator, &config );
    if ( argc > 1 )
    {
        input = fopen ( argv[1], "rt" );
    }
    else
    {
        input = stdin;
    }
    for ( ; input ; ++count )
    {
        int next_char = fgetc ( input );
        if ( next_char <= 0 )
        {
            break;
        }
        if ( !us_json_parser_char ( jc, next_char ) )
        {
            fprintf ( stderr, "JSON_parser_char: syntax error, byte %d\n", count );
            result = 1;
            goto done;
        }
    }
    if ( !us_json_parser_done ( jc ) )
    {
        fprintf ( stderr, "JSON_parser_end: syntax error\n" );
        result = 1;
        goto done;
    }
done:
    us_json_parser_destroy ( jc );
    if ( input != stdin )
        fclose ( input );
    return result;
}