Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
86 #include "../../utilities/utilities.h"
90 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
91 strcpy(cpBuffer, cpBase);
92 strcat(cpBuffer, cpDivider);
93 strcat(cpBuffer, cpName);
97 static char s_caBuf[PATH_MAX];
99 static char* s_cpDescription =
100 "Illustrate the construction and use of the data formatting utility object.";
102 static char* s_cppCases[] = {
103 "Display application information.",
104 "Display 8-bit bytes, illustrating indentation and limits.",
105 "Display a mix of ASCII and non-ASCII bytes in all formats.",
106 "Display Unicode data in the Unicode format.",
108 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
110 static int iHelp(
void){
113 printf(
"description: %s\n", s_cpDescription);
114 printf(
" usage: ex-api arg\n");
115 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
116 printf(
" execute case number n\n");
117 printf(
" arg = anthing else\n");
118 printf(
" print this help screen\n");
120 for(; i < s_iCaseCount; i++){
121 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
136 static int iLimits() {
137 int iReturn = EXIT_SUCCESS;
138 static void* vpMem = NULL;
139 static void* vpFmt = NULL;
140 aint uiBufSize = 1024;
141 uint8_t ucaData[1024];
143 const char* cpDataFile = cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"display-data");
154 "This example case uses the utilities format object to do hexdump-style display\n"
155 "illustrating offsets, limits and indentation.\n\n";
156 printf(
"\n%s", cpHeader);
159 uiDataLen = uiBufSize;
161 if(uiDataLen > uiBufSize){
162 XTHROW(&e,
"data buffer too small for file");
167 printf(
"File %s data as 8-bit bytes.\n", cpDataFile);
169 printf(
"%s", cpLine);
175 printf(
"\nFile %s data as 8-bit bytes. Display only 10 bytes from offset 4 .\n", cpDataFile);
177 printf(
"%s", cpLine);
184 printf(
"\nFile %s data as 8-bit bytes, indented 4 spaces.\n", cpDataFile);
186 printf(
"%s", cpLine);
193 iReturn = EXIT_FAILURE;
202 static int iAscii() {
203 int iReturn = EXIT_SUCCESS;
204 static void* vpMem = NULL;
205 static void* vpFmt = NULL;
206 aint uiBufSize = 1024;
207 uint8_t ucaData[1024];
209 const char* cpDataFile = cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"display-data");
220 "This example case uses the utilities format object to do a hexdump-style display\n"
221 "of a mix of printing ASCII characters and non-printing bytes in several formats.\n"
222 "Data from both files and arrays are used.\n";
223 printf(
"\n%s", cpHeader);
226 uiDataLen = uiBufSize;
228 if(uiDataLen > uiBufSize){
229 XTHROW(&e,
"data buffer too small for file");
234 printf(
"\nData as 8-bit bytes from file %s.\n", cpDataFile);
236 printf(
"%s", cpLine);
242 printf(
"\nData as 8-bit bytes from an array.\n");
244 printf(
"%s", cpLine);
250 char* cpEndian =
bIsBigEndian() ?
"big endian" :
"little endian";
251 printf(
"\nData as 16-bit, %s words.\n", cpEndian);
253 printf(
"%s", cpLine);
259 printf(
"\nData as ASCII characters. When non-printing decimal digit is displayed.\n");
261 printf(
"%s", cpLine);
267 printf(
"\nData in canonical display of both bytes and characters when possible.\n");
269 printf(
"%s", cpLine);
276 iReturn = EXIT_FAILURE;
285 static int iUnicode() {
286 int iReturn = EXIT_SUCCESS;
287 static void* vpMem = NULL;
288 static void* vpFmt = NULL;
289 aint uiBufSize = 1024;
290 uint8_t ucaData[1024];
315 "This example case uses the utilities format object to do a hexdump-style\n"
316 "display of Unicode code points.\n";
317 printf(
"\n%s", cpHeader);
320 uiDataLen = uiBufSize;
322 vUtilFileRead(vpMem, cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"unicode-data-be"), ucaData, &uiDataLen);
324 vUtilFileRead(vpMem, cpMakeFileName(s_caBuf, SOURCE_DIR,
"/../input/",
"unicode-data-le"), ucaData, &uiDataLen);
326 if(uiDataLen > uiBufSize){
327 XTHROW(&e,
"data buffer too small for file");
331 printf(
"\nDisplay Unicode format.\n");
332 uip32 = (uint32_t*)ucaData;
333 uiLen32 = uiDataLen / 4;
336 printf(
"%s", cpLine);
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]);
#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 vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool try
True for the try block, false for the catch block.
#define XTHROW(ctx, msg)
Exception throw macro.
uint_fast32_t aint
The APG parser's unsigned integer type.
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 * vpMemCtor(exception *spException)
Construct a memory component.
void vUtilApgInfo(void)
Display the current state of apg.h.
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.