00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <openrj/openrj.h>
00026
00027
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031
00032
00033
00034
00035
00036
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\n"
00062 "%%\n"
00063 "Name: Sparky\n"
00064 "Species: Cat\n"
00065 "%%\n";
00066
00067
00068
00069 int main()
00070 {
00071 unsigned flags = ORJ_FLAG_ELIDEBLANKRECORDS | ORJ_FLAG_IGNORECASEONLOOKUP;
00072 ORJDatabase const *db;
00073 ORJError error;
00074 ORJRC rc;
00075
00076
00077 rc = ORJ_CreateDatabaseFromMemory(contents, sizeof(contents), NULL, flags, &db, &error);
00078
00079 if(0 != rc)
00080 {
00081 printf("Error at line %ld, column %ld: %s\n", (long)error.invalidLine, (long)error.invalidColumn, ORJ_GetErrorStringA(rc));
00082
00083 return EXIT_FAILURE;
00084 }
00085 else
00086 {
00087 size_t iRecord;
00088
00089 printf( "Database has %ld lines in %ld fields in %ld records\n"
00090 , (long)ORJ_Database_GetNumLinesA(db)
00091 , (long)ORJ_Database_GetNumFieldsA(db)
00092 , (long)ORJ_Database_GetNumRecordsA(db));
00093
00094 printf("Enumerating all records in the database, and displaying the \"Breed\" fields:\n");
00095
00096 for(iRecord = 0; iRecord < ORJ_Database_GetNumRecordsA(db); ++iRecord)
00097 {
00098 ORJRecordA const *record;
00099 ORJFieldA const *breed;
00100
00101 ORJ_Database_GetRecordA(db, iRecord, &record);
00102
00103 breed = ORJ_Record_FindFieldByNameA(record, "BREED", NULL);
00104
00105 if(NULL == breed)
00106 {
00107 printf("record does not contain a field called \"Breed\"\n");
00108 }
00109 else
00110 {
00111 ORJFieldA const *name = ORJ_Record_FindFieldByNameA(record, "NAME", NULL);
00112 ORJFieldA const *species = ORJ_Record_FindFieldByNameA(record, "SPECIES", NULL);
00113 ORJStringA const *comment;
00114
00115 ORJ_Record_GetCommentA(record, &comment);
00116
00117 printf("record-#%ld; %.*s (%ld fields)\n", (long)iRecord, (int)comment->len, comment->ptr, (long)ORJ_Record_GetNumFieldsA(record));
00118 printf(" %.*s=%.*s\n", (int)name->name.len, name->name.ptr, (int)name->value.len, name->value.ptr);
00119 printf(" %.*s=%.*s\n", (int)species->name.len, species->name.ptr, (int)species->value.len, species->value.ptr);
00120 printf(" %.*s=%.*s\n", (int)breed->name.len, breed->name.ptr, (int)breed->value.len, breed->value.ptr);
00121 }
00122 }
00123
00124
00125 ORJ_FreeDatabase(db);
00126 }
00127
00128 return EXIT_SUCCESS;
00129 }
00130
00131
00132
00133