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
00026 #include <openrj/openrj.h>
00027
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032
00033
00034
00035
00036
00037
00038 static const char contents[] =
00039
00040 "%% Sample Open-RJ database - Cats and Dogs\n"
00041 "%% Created: 28th September 2004\n"
00042 "%% Updated: 29th September 2004\n"
00043 "Name: Barney\n"
00044 "Species: Dog\n"
00045 "Breed: Bijon \\\n"
00046 " Frieze\n"
00047 "%%\n"
00048 "Name: Elsa\n"
00049 "Species: Dog\n"
00050 "Breed: Mixed\n"
00051 "%%\n"
00052 "Name: Fluffy Kitten\n"
00053 "Species: Cat\n"
00054 "%%\n"
00055 "Name: Moet\n"
00056 "Species: Dog\n"
00057 "Breed: Boxer\n"
00058 "%%\n"
00059 "Name: Rebel\n"
00060 "Species: Dog\n"
00061 "Breed: German \\\n"
00062 " Shepherd\n"
00063 "%%\n"
00064 "Name: Sparky\n"
00065 "Species: Cat\n"
00066 "%%\n";
00067
00068
00069
00070 int main()
00071 {
00072 unsigned flags = ORJ_FLAG_ELIDEBLANKRECORDS;
00073 ORJDatabase const *db;
00074 ORJError error;
00075 ORJRC rc;
00076
00077
00078 rc = ORJ_CreateDatabaseFromMemory(contents, sizeof(contents), NULL, flags, &db, &error);
00079
00080 if(0 != rc)
00081 {
00082 printf("Error at line %ld, column %ld: %s\n", (long)error.invalidLine, (long)error.invalidColumn, ORJ_GetErrorStringA(rc));
00083
00084 return EXIT_FAILURE;
00085 }
00086 else
00087 {
00088 size_t iRecord;
00089
00090 printf( "Database has %ld lines in %ld fields in %ld records\n"
00091 , (long)db->numLines
00092 , (long)db->numFields
00093 , (long)db->numRecords);
00094
00095 printf("Enumerating all fields in the database:\n");
00096
00097 for(iRecord = 0; iRecord < db->numRecords; ++iRecord)
00098 {
00099 size_t iField;
00100 ORJRecordA const *record;
00101 ORJStringA const *comment;
00102
00103 record = &db->records[iRecord];
00104 comment = &record->comment;
00105
00106 if(0 != comment->len)
00107 {
00108 printf("record-#%ld; %.*s (%ld fields)\n", (long)iRecord, (int)comment->len, comment->ptr, (long)record->numFields);
00109 }
00110 else
00111 {
00112 printf("record-#%ld (%ld fields)\n", (long)iRecord, (long)record->numFields);
00113 }
00114
00115 for(iField = 0; iField < record->numFields; ++iField)
00116 {
00117 ORJFieldA const *field;
00118 ORJStringA const *name;
00119 ORJStringA const *value;
00120
00121 field = &record->fields[iField];
00122 name = &field->name;
00123 value = &field->value;
00124
00125 printf(" field-#%ld %.*s=%.*s\n", (long)iField, (int)field->name.len, field->name.ptr, (int)field->value.len, field->value.ptr);
00126 }
00127 }
00128
00129
00130 ORJ_FreeDatabase(db);
00131 }
00132
00133 return EXIT_SUCCESS;
00134 }
00135
00136
00137
00138