Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
main.c
Go to the documentation of this file.
1 /* *************************************************************************************
2  Copyright (c) 2021, Lowell D. Thomas
3  All rights reserved.
4 
5  This file is part of APG Version 7.0.
6  APG Version 7.0 may be used under the terms of the BSD 2-Clause License.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 * *************************************************************************************/
85 #include "../../utilities/utilities.h"
86 
87 static char* s_cpDescription =
88  "Illustrate the construction and use of the message log object.";
89 
90 static char* s_cppCases[] = {
91  "Display application information.",
92  "Illustrate the use of the message log object.",
93 };
94 static long int s_iCaseCount = (long int)(sizeof(s_cppCases) / sizeof(s_cppCases[0]));
95 
96 static int iHelp(void){
97  long int i = 0;
99  printf("description: %s\n", s_cpDescription);
100  printf(" usage: ex-api arg\n");
101  printf(" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
102  printf(" execute case number n\n");
103  printf(" arg = anthing else\n");
104  printf(" print this help screen\n");
105  printf("\n");
106  for(; i < s_iCaseCount; i++){
107  printf("case %ld %s\n", (i + 1), s_cppCases[i]);
108  }
109  return EXIT_SUCCESS;
110 }
111 
112 static int iApp() {
113  // print the current working directory
115  printf("\n");
116 
117  // display the current APG sizes and macros
118  vUtilApgInfo();
119  return EXIT_SUCCESS;
120 }
121 
122 static int iMsgs() {
123  int iReturn = EXIT_SUCCESS;
124  static void* vpMem = NULL;
125  static void* vpMsgs = NULL;
126  const char* cpMsg;
127  exception e;
128  XCTOR(e);
129  if(e.try){
130  // try block
131  vpMem = vpMemCtor(&e);
132  vpMsgs = vpMsgsCtor(&e);
133 
134  // display the information header
135  char* cpHeader =
136  "This example case uses the message logging object to log, display and clear a few messages.\n";
137  printf("\n%s", cpHeader);
138 
139  // log a few messages
140  vMsgsLog(vpMsgs, "bad character here");
141  vMsgsLog(vpMsgs, "bad format there");
142  vMsgsLog(vpMsgs, "wrong thing to do here");
143  vMsgsLog(vpMsgs, "too many errors to continue");
144  printf("\nDisplay the %d logged messages with the iterator.\n", (int)uiMsgsCount(vpMsgs));
145  cpMsg = cpMsgsFirst(vpMsgs);
146  while(cpMsg){
147  printf("%s\n", cpMsg);
148  cpMsg = cpMsgsNext(vpMsgs);
149  }
150 
151  printf("\nDisplay the %d logged messages with vUtilPrintMsgs().\n", (int)uiMsgsCount(vpMsgs));
152  vUtilPrintMsgs(vpMsgs);
153 
154  printf("\nClear the message log and start again.\n");
155  vMsgsClear(vpMsgs);
156  vMsgsLog(vpMsgs, "bad start with the new app");
157  vMsgsLog(vpMsgs, "errors abound");
158  vMsgsLog(vpMsgs, "time to quit");
159  vUtilPrintMsgs(vpMsgs);
160 
161  }else{
162  // catch block - display the exception location and message
164  iReturn = EXIT_FAILURE;
165  }
166 
167  // clean up resources
168  vMsgsDtor(vpMsgs);
169  vMemDtor(vpMem);
170  return iReturn;
171 }
172 
180 int main(int argc, char **argv) {
181  long int iCase = 0;
182  if(argc > 1){
183  iCase = atol(argv[1]);
184  }
185  if((iCase > 0) && (iCase <= s_iCaseCount)){
186  printf("%s\n", s_cppCases[iCase -1]);
187  }
188  switch(iCase){
189  case 1:
190  return iApp();
191  case 2:
192  return iMsgs();
193  default:
194  return iHelp();
195  }
196 }
197 
vMsgsLog
void vMsgsLog(void *vpCtx, const char *cpMsg)
Logs a message.
Definition: msglog.c:141
XCTOR
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
Definition: exception.h:77
vMemDtor
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
Definition: memory.c:141
exception::try
abool try
True for the try block, false for the catch block.
Definition: exception.h:49
uiMsgsCount
aint uiMsgsCount(void *vpCtx)
Get the number of logged messages.
Definition: msglog.c:213
vUtilPrintMsgs
void vUtilPrintMsgs(void *vpMsgs)
Display the list of messages in a message object to stdout.
Definition: utilities.c:747
vpMsgsCtor
void * vpMsgsCtor(exception *spEx)
The Message Log constructor.
Definition: msglog.c:68
main
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
Definition: main.c:61
vMsgsClear
void vMsgsClear(void *vpCtx)
Clears the object of all messages.
Definition: msglog.c:124
exception
A structure to describe the type and location of a caught exception.
Definition: exception.h:47
vMsgsDtor
void vMsgsDtor(void *vpCtx)
The object destructor.
Definition: msglog.c:92
cpMsgsFirst
const char * cpMsgsFirst(void *vpCtx)
Get a pointer to the first logged message, if any.
Definition: msglog.c:164
cpMsgsNext
const char * cpMsgsNext(void *vpCtx)
Get a pointer to the next logged message, if any.
Definition: msglog.c:185
vpMemCtor
void * vpMemCtor(exception *spException)
Construct a memory component.
Definition: memory.c:121
vUtilApgInfo
void vUtilApgInfo(void)
Display the current state of apg.h.
Definition: utilities.c:60
vUtilPrintException
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
Definition: utilities.c:415
vUtilCurrentWorkingDirectory
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
Definition: utilities.c:191
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.