Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
79 #include "../../xml/xml.h"
83 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
84 strcpy(cpBuffer, cpBase);
85 strcat(cpBuffer, cpDivider);
86 strcat(cpBuffer, cpName);
89 static char caBuf[PATH_MAX];
91 static char* s_cpDescription =
92 "Illustrate using the XML parser.";
94 static char* s_cppCases[] = {
95 "Display application information.",
96 "Illustrate parsing a simple XML file with no XML declaration or DTD.",
97 "Illustrate displaying the Processing Instructions and comments found in an XML document.",
98 "Illustrate displaying implicit and explicit XML declaration and parsing UTF-16 encoded files.",
99 "Illustrate parsing the DTD and applying entities and default attributes.",
101 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
103 static int iHelp(
void){
106 printf(
"description: %s\n", s_cpDescription);
107 printf(
" usage: ex-api arg\n");
108 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
109 printf(
" execute case number n\n");
110 printf(
" arg = anthing else\n");
111 printf(
" print this help screen\n");
113 for(; i < s_iCaseCount; i++){
114 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
129 static int iSimple() {
130 int iReturn = EXIT_SUCCESS;
131 static void* vpMem = NULL;
132 static void* vpXml = NULL;
133 uint8_t ucaBuf[1024];
134 aint uiBufSize = 1024;
143 const char* cpInput = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"simple.xml");
147 "This example case illustrates parsing a simple XML file.\n"
148 "The file has no XML declaration or DTD.\n"
149 "Only the tag names, attributes and tagged content are captured and displayed.\n";
150 printf(
"\n%s", cpHeader);
152 printf(
"\nGet the XML file and use default call back functions for display of captured items.\n");
153 printf(
"XML data from file %s\n", cpInput);
156 if(uiSize >= uiBufSize){
157 XTHROW(&e,
"buffer size too small for file");
160 printf(
"%s\n", (
char*)ucaBuf);
170 iReturn = EXIT_FAILURE;
179 static int iComment() {
180 int iReturn = EXIT_SUCCESS;
181 static void* vpMem = NULL;
182 static void* vpXml = NULL;
183 uint8_t ucaBuf[1024];
184 aint uiBufSize = 1024;
193 const char* cpInput = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"comment.xml");
197 "This example case illustrates displaying the Processing Instructions and comments\n"
198 "optionally found in an XML document.\n";
199 printf(
"\n%s", cpHeader);
201 printf(
"\nGet the XML file and use default call back functions for display PIs and comments.\n");
202 printf(
"XML data from file %s\n", cpInput);
205 if(uiSize >= uiBufSize){
206 XTHROW(&e,
"buffer size too small for file");
209 printf(
"%s\n", (
char*)ucaBuf);
219 iReturn = EXIT_FAILURE;
228 static int iXmlDecl() {
229 int iReturn = EXIT_SUCCESS;
230 static void* vpMem = NULL;
231 static void* vpXml = NULL;
232 static void* vpFmt = NULL;
234 uint8_t ucaBuf[1024];
235 aint uiBufSize = 1024;
247 "This example case illustrates the XML declaration.\n"
248 "A display of the XML declaration is compared between\n"
249 "XML files with and without XML declarations.\n"
250 "Furthermore, the file with the declaration is UTF-16 encoded.";
251 printf(
"\n%s", cpHeader);
253 const char* cpSimple = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"simple.xml");
255 printf(
"\nGet the XML files and use default call back functions for display of the XML declaration.\n");
256 printf(
"XML data from file %s\n", cpSimple);
259 if(uiSize >= uiBufSize){
260 XTHROW(&e,
"buffer size too small for file");
263 printf(
"%s\n", (
char*)ucaBuf);
269 const char* cpDecl16le = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"xml-decl-16le.xml");
271 printf(
"XML data from file %s\n", cpDecl16le);
274 if(uiSize >= uiBufSize){
275 XTHROW(&e,
"buffer size too small for file");
280 printf(
"%s", cpLine);
292 iReturn = EXIT_FAILURE;
302 int iReturn = EXIT_SUCCESS;
303 static void* vpMem = NULL;
304 static void* vpXml = NULL;
305 uint8_t ucaBuf[1024];
306 aint uiBufSize = 1024;
315 const char* cpInput = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"dtd-entity-attr.xml");
319 "This example case illustrates parsing an XML file with a Document Type Declaration (DTD).\n"
320 "The DTD will define entities and default attributes.\n"
321 "These will be reflected in the parse of the input file.\n";
322 printf(
"\n%s", cpHeader);
324 printf(
"\nGet the XML file and use default call back functions for display of captured items.\n");
325 printf(
"XML data from file %s\n", cpInput);
328 if(uiSize >= uiBufSize){
329 XTHROW(&e,
"buffer size too small for file");
332 printf(
"%s\n", (
char*)ucaBuf);
343 iReturn = EXIT_FAILURE;
359 int main(
int argc,
char **argv) {
362 iCase = atol(argv[1]);
364 if((iCase > 0) && (iCase <= s_iCaseCount)){
365 printf(
"%s\n", s_cppCases[iCase -1]);
void vXmlParse(void *vpCtx)
Parse the XML data from vXmlGetFile or vXmlGetArray.
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
void vUtilFileRead(void *vpMem, const char *cpFileName, uint8_t *ucpData, aint *uipLen)
Read a file into the caller's data area.
void * vpXmlCtor(exception *spEx)
The XML Parser constructor.
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool try
True for the try block, false for the catch block.
#define DEFAULT_CALLBACK
Indicator for a pre-defined, default callback function.
void vXmlSetCommentCallback(void *vpCtx, pfnCommentCallback pfnCallback, void *vpUserData)
Set the user's callback function for comments.
#define XTHROW(ctx, msg)
Exception throw macro.
uint_fast32_t aint
The APG parser's unsigned integer type.
void vXmlGetFile(void *vpCtx, const char *cpFileName)
Gets the XML byte stream from a file.
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
A structure to describe the type and location of a caught exception.
void vXmlSetStartTagCallback(void *vpCtx, pfnStartTagCallback pfnCallback, void *vpUserData)
Set the user's callback function for the start tags (<name attr="10">).
void vXmlSetDTDCallback(void *vpCtx, pfnDTDCallback pfnCallback, void *vpUserData)
Set the user's callback function for the Processing Instruction tags(<?target instructions?...
void * vpMemCtor(exception *spException)
Construct a memory component.
void vXmlSetEndTagCallback(void *vpCtx, pfnEndTagCallback pfnCallback, void *vpUserData)
Set the user's callback function for the end tags (</name>).
void vXmlSetXmlDeclCallback(void *vpCtx, pfnXmlDeclCallback pfnCallback, void *vpUserData)
Set the user's callback function for the XML declaration.
void vUtilApgInfo(void)
Display the current state of apg.h.
void vXmlSetPICallback(void *vpCtx, pfnPICallback pfnCallback, void *vpUserData)
Set the user's callback function for the Processing Instruction tags(<?target instructions?...
void vXmlDtor(void *vpCtx)
The XML Parser component destructor.
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.