Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
Tools.c
Go to the documentation of this file.
1 /*******************************************************************************
2  APG Version 6.3
3  Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved
4 
5  author: Lowell D. Thomas
6  email: lowell@coasttocoastresearch.com
7  website: http://www.coasttocoastresearch.com
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see
21  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
22  or write to the Free Software Foundation, Inc.,
23  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
25 
26 #include "Apg.h"
27 #include "Private.h"
32 // default alert(error) handler
33 void vDefaultAlertHandler(unsigned int uiLine, const char* cpFile){
34  exit(EXIT_FAILURE);
35 }
36 
46 int apg_stricmp(const char* cpLhs, const char* cpRhs)
47 {
48  apg_uint uiLhsEmpty = APG_FALSE;
49  apg_uint uiRhsEmpty = APG_FALSE;
50 
51  // handle any empty strings
52  if(cpLhs == NULL || *cpLhs == 0){uiLhsEmpty = APG_TRUE;}
53  if(cpRhs == NULL || *cpRhs == 0){uiRhsEmpty = APG_TRUE;}
54  if(uiLhsEmpty && uiRhsEmpty){return 0;}
55  if(uiLhsEmpty){return -1;}
56  if(uiRhsEmpty){return 1;}
57 
58  // compare the strings
59  while(APG_TRUE)
60  {
61  char cLhs = *cpLhs;
62  char cRhs = *cpRhs;
63  if(cLhs >= 65 && cLhs <= 90){cLhs += 32;}
64  if(cRhs >= 65 && cRhs <= 90){cRhs += 32;}
65  if(cLhs < cRhs){return -1;}
66  if(cLhs > cRhs){return 1;}
67  if(cLhs == 0){return 0;}
68  cpLhs++;
69  cpRhs++;
70  }
71  return 0; // never reached - just to keep the parser from complaining about no return function
72 }
73 
79 void vCharToAChar(apg_achar* acpAChars, const char* cpChars, apg_uint uiLen){
80  if(acpAChars && cpChars && uiLen){
81  apg_achar* acpACharsEnd = acpAChars + uiLen;
82  for(; acpAChars < acpACharsEnd; acpAChars++, cpChars++)
83  *acpAChars = (apg_achar)*cpChars;
84  }
85 }
86 
87 static char g_caTrans[17] = {'0','1', '2','3','4','5','6','7','8','9','A','B','C','D','E','F','X'};
88 static apg_uint uiAddDigit(char* cpAdd, apg_achar tChar, apg_uint uiSize, apg_uint uiShift, apg_uint* uipLeadingNonZero){
89  apg_uint uiRet = 0;
90 
91  // NOTE: this weird masking of bits is to avoid compiler warning about integer constants being to big on 32-bit machines.
92  unsigned long int uiMaskGen = 0xF;
93  unsigned long int uiSizeof = uiSize * 8;
94  unsigned long int uiShiftBits = uiSizeof - uiShift;
95  unsigned long int uiMask = uiMaskGen << uiShiftBits;
96  char cHex = (tChar & uiMask) >> uiShiftBits;
97  if(*uipLeadingNonZero){
98  cpAdd[0] = g_caTrans[(unsigned int)cHex];
99  uiRet = 1;
100  } else if(cHex > 0){
101  cpAdd[0] = g_caTrans[(unsigned int)cHex];
102  *uipLeadingNonZero = APG_TRUE;
103  uiRet = 1;
104  }
105  return uiRet;
106 }
107 static apg_uint uiAddHex(char* cpAdd, apg_achar tChar, apg_uint uiSize){
108  apg_uint uiRet = 0;
109  apg_uint uiError = APG_FALSE;
110  apg_uint uiLeadingNonZero = APG_FALSE;
111  cpAdd[uiRet++] = '\\';
112  cpAdd[uiRet++] = 'x';
113  switch(uiSize){
114  case 1:
115  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 4, &uiLeadingNonZero);
116  break;
117  case 2:
118  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 4, &uiLeadingNonZero);
119  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 8, &uiLeadingNonZero);
120  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 12, &uiLeadingNonZero);
121  break;
122  case 4:
123  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 4, &uiLeadingNonZero);
124  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 8, &uiLeadingNonZero);
125  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 12, &uiLeadingNonZero);
126  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 16, &uiLeadingNonZero);
127  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 20, &uiLeadingNonZero);
128  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 24, &uiLeadingNonZero);
129  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 28, &uiLeadingNonZero);
130  break;
131  case 8:
132  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 4, &uiLeadingNonZero);
133  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 8, &uiLeadingNonZero);
134  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 12, &uiLeadingNonZero);
135  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 16, &uiLeadingNonZero);
136  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 20, &uiLeadingNonZero);
137  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 24, &uiLeadingNonZero);
138  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 28, &uiLeadingNonZero);
139  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 32, &uiLeadingNonZero);
140  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 36, &uiLeadingNonZero);
141  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 40, &uiLeadingNonZero);
142  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 44, &uiLeadingNonZero);
143  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 48, &uiLeadingNonZero);
144  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 52, &uiLeadingNonZero);
145  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 56, &uiLeadingNonZero);
146  uiRet += uiAddDigit(&cpAdd[uiRet], tChar, uiSize, 60, &uiLeadingNonZero);
147  break;
148  default:
149  uiError = APG_TRUE;
150  break;
151  }
152  if(!uiError){
153  char cHex = tChar & 0x0F;
154  cpAdd[uiRet++] = g_caTrans[(unsigned int)cHex];
155  }
156  return uiRet;
157 }
158 
165 apg_uint uiACharToStringSize(const apg_achar* acpAChars, apg_uint uiALen){
166  char caCharBuf[20];
167  apg_uint i;
168  apg_achar acChar;
169  apg_uint uiStrLen;
170  apg_uint uiSize = sizeof(apg_achar);
171  uiStrLen = 0;
172  for(i = 0; i < uiALen; i++){
173  acChar = acpAChars[i];
174  if(acChar < (apg_achar)20 || acChar > (apg_achar)127){
175  uiStrLen += uiAddHex(&caCharBuf[0], acChar, uiSize);
176  } else{
177  uiStrLen++;
178  }
179  }
180  return uiStrLen + 1;
181 }
182 
197 apg_uint uiACharToString(char* cpChars, apg_uint uiCLen, const apg_achar* acpAChars, apg_uint uiALen){
198  char caCharBuf[20];
199  apg_uint i = 0;
200  apg_achar acChar;
201  char* cpNextChar;
202  const apg_achar* cpBufferEnd;
203  apg_uint uiCharLen, uiStrLen;
204  apg_uint uiCLenMax;
205  apg_uint uiSize = sizeof(apg_achar);
206  cpNextChar = cpChars;
207  if(acpAChars && uiALen && cpChars && uiCLen){
208  cpBufferEnd = acpAChars + uiALen;
209  uiCLenMax = uiCLen - 1;
210  uiStrLen = 0;
211  for(i = 0; i < uiALen && uiStrLen < uiCLenMax; i++){
212  acChar = acpAChars[i];
213  if(acChar < (apg_achar)20 || acChar > (apg_achar)127){
214  uiCharLen = uiAddHex(&caCharBuf[0], acChar, uiSize);
215  if(uiStrLen + uiCharLen > uiCLenMax){break;}
216  } else{
217  uiCharLen = 1;
218  caCharBuf[0] = (char)acChar;
219  }
220  memcpy((void*)cpNextChar, (void*)&caCharBuf[0], uiCharLen * sizeof(char));
221  uiStrLen += uiCharLen;
222  cpNextChar += uiCharLen;
223  }
224  }
225  *cpNextChar = 0;
226  return i;
227 }
228 
230 void* vpStrBufInit(void* vpBuf, apg_uint uiBufLen){
231  void* vpRet = NULL;
232  PRINTBUF* spBuf = NULL;
233  apg_uint uiOverhead = (apg_uint)sizeof(PRINTBUF) + 1;
234  if(vpBuf && uiBufLen > uiOverhead){
235  memset(vpBuf, 0, (sizeof(char) * uiBufLen));
236  spBuf = (PRINTBUF*)vpBuf;
237  spBuf->cpBuffer = (char*)vpBuf + sizeof(PRINTBUF);
238  spBuf->uiBufLen = uiBufLen - uiOverhead;
239  spBuf->uiBufCount = 0;
240  spBuf->uiBufIsTruncated = APG_FALSE;
241  spBuf->vpValidate = vpBuf;
242  vpRet = vpBuf;
243  }
244  return vpRet;
245 }
246 apg_uint uiStrBufCat(void* vpBuf, char* cpString){
247  PRINTBUF* spBuf = (PRINTBUF*)vpBuf;
248  apg_uint uiRet = 0;
249  apg_uint uiStrLen;
250  apg_uint i;
251  if(spBuf && (spBuf->vpValidate == vpBuf) && cpString && cpString[0]){
252  uiStrLen = strlen(cpString);
253  for(i = 0; (i < uiStrLen) && (spBuf->uiBufCount < spBuf->uiBufLen); i++){
254  spBuf->cpBuffer[spBuf->uiBufCount] = cpString[i];
255  spBuf->uiBufCount++;
256  uiRet++;
257  }
258  spBuf->cpBuffer[spBuf->uiBufCount] = 0;
259  spBuf->uiBufIsTruncated = (apg_uint)(uiRet < uiStrLen);
260  }
261  return uiRet;
262 }
263 char* cpStrBufString(void* vpBuf){
264  PRINTBUF* spBuf = (PRINTBUF*)vpBuf;
265  char* cpRet = NULL;
266  if(spBuf && (spBuf->vpValidate == vpBuf)){
267  cpRet = spBuf->cpBuffer;
268  }
269  return cpRet;
270 }
271 apg_uint uiStrBufCount(void* vpBuf){
272  PRINTBUF* spBuf = (PRINTBUF*)vpBuf;
273  apg_uint uiRet = 0;
274  if(spBuf && (spBuf->vpValidate == vpBuf)){
275  uiRet = spBuf->uiBufCount;
276  }
277  return uiRet;
278 }
279 apg_uint uiStrBufSize(void* vpBuf){
280  PRINTBUF* spBuf = (PRINTBUF*)vpBuf;
281  apg_uint uiRet = 0;
282  if(spBuf && (spBuf->vpValidate == vpBuf)){
283  uiRet = spBuf->uiBufLen;
284  }
285  return uiRet;
286 }
287 apg_uint uiStrBufIsTruncated(void* vpBuf){
288  PRINTBUF* spBuf = (PRINTBUF*)vpBuf;
289  apg_uint uiRet = APG_FALSE;
290  if(spBuf && (spBuf->vpValidate == vpBuf)){
291  uiRet = spBuf->uiBufIsTruncated;
292  }
293  return uiRet;
294 }
295 
296 char* cpApgVersion(){return "6.3";}
297 char* cpApgAuthor(){return "Lowell D. Thomas";}
298 char* cpApgDescription(){return "APG - an ABNF Parser Generator";}
299 char* cpApgCopyright(){return "Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved";}
300 char* cpApgEmail(){return "lowell@coasttocoastresearch.com";}
301 char* cpApgWebsite(){return "http://www.coasttocoastresearch.com";}
302 char* cpApgGPLNotice(){
303  static char* apg_cpApgGPLNotice =
304  " This program is free software: you can redistribute it and/or modify\n"
305  " it under the terms of the GNU General Public License as published by\n"
306  " the Free Software Foundation, either version 2 of the License, or\n"
307  " (at your option) any later version.\n"
308  "\n"
309  " This program is distributed in the hope that it will be useful,\n"
310  " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
311  " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
312  " GNU General Public License for more details.\n"
313  "\n"
314  " You should have received a copy of the GNU General Public License\n"
315  " along with this program. If not, see\n"
316  " <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
317  " or write to the Free Software Foundation, Inc.,\n"
318  " 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n";
319 
320  return apg_cpApgGPLNotice;
321 }
apg_stricmp
int apg_stricmp(const char *cpLhs, const char *cpRhs)
Definition: Tools.c:46
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
uiACharToString
apg_uint uiACharToString(char *cpChars, apg_uint uiCLen, const apg_achar *acpAChars, apg_uint uiALen)
Definition: Tools.c:197
vCharToAChar
void vCharToAChar(apg_achar *acpAChars, const char *cpChars, apg_uint uiLen)
Definition: Tools.c:79
Apg.h
Required header file for all APG-generated parsers. Contains important configuration macros and decla...
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
uiACharToStringSize
apg_uint uiACharToStringSize(const apg_achar *acpAChars, apg_uint uiALen)
Definition: Tools.c:165
vDefaultAlertHandler
void vDefaultAlertHandler(unsigned int uiLine, const char *cpFile)
Definition: Tools.c:33
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/licenses.html or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.