Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
exception.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 * *************************************************************************************/
39 #include "./lib.h"
40 
41 static const void* s_vpMagicNumber = (const void*)"exception";
42 
43 static void vStrToBuf(const char *cpStr, char *cpBuf, size_t uiBufSize);
44 
57 void vExCtor(exception* spException){
58  if(!spException){
59  vExContext();
60  }
61  memset(spException, 0, sizeof(exception));
62  spException->vpValidate = s_vpMagicNumber;
63 }
64 
70 abool bExValidate(exception* spException){
71  if(spException && (spException->vpValidate == s_vpMagicNumber)){
72  return APG_TRUE;
73  }
74  return APG_FALSE;
75 }
76 
87 void vExThrow(exception* spEx, const char *cpMsg, unsigned int uiLine, const char *cpFile, const char *cpFunc) {
88  if(spEx && (spEx->vpValidate == s_vpMagicNumber)){
89  spEx->uiLine = uiLine;
90  vStrToBuf(cpMsg, spEx->caMsg, sizeof(spEx->caMsg));
91  vStrToBuf(cpFile, spEx->caFile, sizeof(spEx->caFile));
92  vStrToBuf(cpFunc, spEx->caFunc, sizeof(spEx->caFunc));
93  longjmp(spEx->aJmpBuf, 1);
94  }
95  vExContext();
96 }
97 
105 void vExRethrow(exception* spExFrom, exception* spExTo){
106  if(bExValidate(spExFrom) && bExValidate(spExTo)){
107  spExTo->uiLine = spExFrom->uiLine;
108  vStrToBuf(spExFrom->caMsg, spExTo->caMsg, sizeof(spExTo->caMsg));
109  vStrToBuf(spExFrom->caFile, spExTo->caFile, sizeof(spExTo->caFile));
110  vStrToBuf(spExFrom->caFunc, spExTo->caFunc, sizeof(spExTo->caFunc));
111  longjmp(spExTo->aJmpBuf, 1);
112  }
113  vExContext();
114  return;
115 }
116 
126 void vExContext(){
127  exit(BAD_CONTEXT);
128 }
129 
130 static void vStrToBuf(const char *cpStr, char *cpBuf, size_t uiBufSize) {
131  size_t ui = 0;
132  size_t uiLen = strlen(cpStr);
133  if (uiLen >= uiBufSize) {
134  uiLen = uiBufSize - 1;
135  }
136  for (; ui < uiLen; ui++) {
137  cpBuf[ui] = cpStr[ui];
138  }
139  cpBuf[ui] = 0;
140 }
141 
vExRethrow
void vExRethrow(exception *spExFrom, exception *spExTo)
Re-throw an exception from the "from" try/catch-block scope to the "to" try/catch-block scope.
Definition: exception.c:105
lib.h
This header "#include"s all publid lib headers and other standard headers needed by most objects.
exception::caFunc
char caFunc[64]
The source code function name where the error occurred. "__func__".
Definition: exception.h:54
exception::uiLine
unsigned int uiLine
The source code line number where the error occurred. "__LINE__".
Definition: exception.h:51
exception::aJmpBuf
jmp_buf aJmpBuf
A "long jump" context array. Long jumps to "catch" area on thrown exception.
Definition: exception.h:50
vExCtor
void vExCtor(exception *spException)
Initialize an exception structure.
Definition: exception.c:57
vExContext
void vExContext()
Handles bad context pointers.
Definition: exception.c:126
exception::caFile
char caFile[256]
The source code file name where the error occurred. "__FILE__".
Definition: exception.h:53
BAD_CONTEXT
#define BAD_CONTEXT
Application exit code when no exception context is available.
Definition: exception.h:42
exception
A structure to describe the type and location of a caught exception.
Definition: exception.h:47
bExValidate
abool bExValidate(exception *spException)
Test an exception structure for validity.
Definition: exception.c:70
exception::vpValidate
const void * vpValidate
Used by the memory object to validate the exception structure.
Definition: exception.h:48
APG_TRUE
#define APG_TRUE
Definition: apg.h:291
abool
uint8_t abool
abool is the APG bool type.
Definition: apg.h:140
exception::caMsg
char caMsg[256]
A the caller's error message.
Definition: exception.h:52
vExThrow
void vExThrow(exception *spEx, const char *cpMsg, unsigned int uiLine, const char *cpFile, const char *cpFunc)
Throws an exception.
Definition: exception.c:87
APG_FALSE
#define APG_FALSE
Definition: apg.h:292
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.