Open-RJ C/C++ User's Journal Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

example_c_4.c

C example program for the Open-RJ core library. Demonstrates handling of parsing errors.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_4.c
00003  *
00004  * Purpose:     C example program for the Open-RJ core library. Demonstrates
00005  *              handling of parsing errors.
00006  *
00007  * Created:     12th May 2006
00008  * Updated:     28th May 2006
00009  *
00010  * www:         http://www.openrj.org/
00011  *
00012  * License:     Copyright (c) 2006, Synesis Software Pty Ltd.
00013  *              All rights reserved.
00014  *
00015  *              (Licensed under the Synesis Software Open License)
00016  *
00017  *              This source code is placed into the public domain 2006
00018  *              by Synesis Software Pty Ltd. There are no restrictions
00019  *              whatsoever to your use of the software.
00020  *
00021  * ////////////////////////////////////////////////////////////////////////// */
00022 
00023 
00024 /* Open-RJ Header Files */
00025 #include <openrj/openrj.h>
00026 
00027 /* Standard C Library Files */
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 /* /////////////////////////////////////////////////////////////////////////////
00033  * Globals
00034  */
00035 
00036 /* Sample database */
00037 static const char   contents[] =
00038 
00039     "%% Sample Open-RJ database - Cats and Dogs\n"
00040     "%% Created:   28th September 2004\n"
00041     "%% Updated:   29th September 2004\n"
00042     "Name:      Barney\n"
00043     "Species:   Dog\n"
00044     "Breed:     Bijon \\\n"
00045     "           Frieze\n"
00046     "%%\n"
00047     "Name:      Elsa\n"
00048     "Species:   Dog\n"
00049     "Breed:     Mixed\n"
00050     "%%\n"
00051     "Name:      Fluffy Kitten\n"
00052     "Species:   Cat\n"
00053     "%%\n"
00054     "Name:      Moet\n"
00055     "Species:   Dog\n"
00056     "Breed:     Boxer\n"
00057     "%%\n"
00058     "Name:      Rebel\n"
00059     "Species:   Dog\n"
00060     "Breed:     German \\\n"
00061     "           Shepherd"       /* The error is here (the \n has been removed) */
00062     "%%\n"
00063     "Name:      Sparky\n"
00064     "Species:   Cat\n"
00065     "%%\n";
00066 
00067 /* ////////////////////////////////////////////////////////////////////////// */
00068 
00069 int main()
00070 {
00071     unsigned            flags       =   0; /* Could try ORJ_FLAG_ORDERFIELDS and/or ORJ_FLAG_ELIDEBLANKRECORDS */
00072     ORJDatabase const   *db;
00073     ORJError            error;
00074     ORJRC               rc;
00075 
00076     /* 1. Create a database instance on memory, so use memory_database */
00077     rc = ORJ_CreateDatabaseFromMemory(contents, sizeof(contents), NULL, flags, &db, &error);
00078 
00079     if(0 != rc)
00080     {
00081         char    message[1001];
00082 
00083         fprintf(stdout, "Display the error manually:\n");
00084         fprintf(stderr, "  Error at line %ld, column %ld: %s\n", (long)error.invalidLine, (long)error.invalidColumn, ORJ_GetErrorStringA(rc));
00085 
00086         fprintf(stdout, "Display the error using ORJ_FormatErrorA():\n");
00087         ORJ_FormatErrorA(&message[0], sizeof(message) / sizeof(0[message]), rc, &error, "  Error code %d means: %E\n", (int)rc);
00088         fprintf(stderr, message);
00089 
00090         return EXIT_FAILURE;
00091     }
00092     else
00093     {
00094         /* Finally, free the database */
00095         ORJ_FreeDatabase(db);
00096     }
00097 
00098     return EXIT_SUCCESS;
00099 }
00100 
00101 /* /////////////////////////////////////////////////////////////////////////////
00102  * end of file
00103  */

Open-RJ Library documentation © Synesis Software Pty Ltd, 2004-2005