home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume8
/
libhoward
/
part05
/
ma2ul.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-01
|
3KB
|
109 lines
/*
* ma2ul - convert Ada-syntax integer literal to long unsigned, handle errors
*/
#ifndef lint
static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
#endif lint
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 1,
* as published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <howard/port.h>
#include <howard/version.h>
MODVER ("@(#)$Header: ma2ul.c,v 1.3 89/08/12 09:56:17 howard Exp $");
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <howard/a2.h>
#include <howard/malf.h>
#include <howard/registers.i>
PUBLIC ulongT ma2ul (str, lim, synok, fn, end)
bStrT str; /* Input string.*/
bStrT lim; /* Don't pass this.*/
boolT synok; /* Accept non-fatal syntax errors.*/
bStrT fn; /* Field name, for error messages.*/
R2 bStrT *end; /* End pointer stored here.*/
/* Function:
*
* Algorithm:
* Call a2ul() then switch on return code.
* Returns:
*
* Notes:
*
*/
{
bStrT ep; /* a2ul() stores pointer to end of string here.*/
ulongT u; /* a2ul() stores its result here.*/
R3 int p1; /* Precision for printing str (up to syntax error).*/
R4 int p2; /* Precision for printing str (after syntax error).*/
R1 int s; /* Return code.*/
s = a2ul (str, lim, synok, &u, &ep);
switch (s)
{
case SUCCESS:
break;
case EDOM:
p1 = ep - str;
p2 = ((NULBSTR == lim) ? strlen (str) : lim - str) - p1;
malf1 ("%s [%.*s|%.*s]: syntax error", fn, p1, str, p2, ep);
break;
case ERANGE:
p1 = ((NULBSTR == lim) ? INT_MAX : lim - str);
malf1 ("%s [%.*s] not in range [0, %lu]", fn, p1, str, ULONG_MAX);
break;
case EINVAL:
if (NULBSTR == str) malf1 ("ma2ul: null string argument");
/* Falls through.*/
default:
malf1 ("ma2ul: impossible error %d", s);
}
if (((bStrT *) NULL) != end) *end = ep;
return (u);
}
#ifdef TEST
#include <howard/usage.h>
MAINVER ("@(#)$Header: ma2ul.c,v 1.3 89/08/12 09:56:17 howard Exp $");
USAGE ("integer-numeric-literal [limit]");
PUBLIC int main (argc, argv)
int argc; /* Number of arguments.*/
bStrT *argv; /* Points to array of argument strings.*/
{
bStrT end; /* Points to end of string.*/
ulongT u; /* Returned by ma2ul().*/
if (argc < 2) usage();
u = ma2ul (argv[1], (2 == argc) ? NULBSTR : &argv[1][atoi (argv[2])], FALSE,
S("integer-numeric-literal"), &end);
PRINTF ("8#%lo#\t10#%lu#\t16#%lX#\t%s\n", u, u, u, end);
mfflush (stdout, "Standard Output");
exit (SUCCESS);
#ifdef lint
return (SUCCESS);
#endif
}
#endif