home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / libhoward / part05 / ma2ul.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  3KB  |  109 lines

  1. /*
  2.  * ma2ul - convert Ada-syntax integer literal to long unsigned, handle errors
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  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, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27.  
  28. MODVER ("@(#)$Header: ma2ul.c,v 1.3 89/08/12 09:56:17 howard Exp $");
  29.  
  30. #include <errno.h>
  31. #include <limits.h>
  32. #include <string.h>
  33. #include <howard/a2.h>
  34. #include <howard/malf.h>
  35. #include <howard/registers.i>
  36.  
  37. PUBLIC ulongT ma2ul (str, lim, synok, fn, end)
  38.    bStrT  str;   /* Input string.*/
  39.    bStrT  lim;   /* Don't pass this.*/
  40.    boolT  synok; /* Accept non-fatal syntax errors.*/
  41.    bStrT  fn;    /* Field name, for error messages.*/
  42. R2 bStrT *end;   /* End pointer stored here.*/
  43.  
  44. /* Function:
  45.  *    
  46.  * Algorithm:
  47.  *    Call a2ul() then switch on return code.
  48.  * Returns:
  49.  *    
  50.  * Notes:
  51.  *    
  52.  */
  53. {
  54.    bStrT  ep; /* a2ul() stores pointer to end of string here.*/
  55.    ulongT u;  /* a2ul() stores its result here.*/
  56. R3 int    p1; /* Precision for printing str (up to syntax error).*/
  57. R4 int    p2; /* Precision for printing str (after syntax error).*/
  58. R1 int    s;  /* Return code.*/
  59.  
  60. s = a2ul (str, lim, synok, &u, &ep);
  61. switch (s)
  62.    {
  63.    case SUCCESS:
  64.       break;
  65.    case EDOM:
  66.       p1 = ep - str;
  67.       p2 = ((NULBSTR == lim) ? strlen (str) : lim - str) - p1;
  68.       malf1 ("%s [%.*s|%.*s]: syntax error", fn, p1, str, p2, ep);
  69.       break;
  70.    case ERANGE:
  71.       p1 = ((NULBSTR == lim) ? INT_MAX : lim - str);
  72.       malf1 ("%s [%.*s] not in range [0, %lu]", fn, p1, str, ULONG_MAX);
  73.       break;
  74.    case EINVAL:
  75.       if (NULBSTR == str) malf1 ("ma2ul: null string argument");
  76.       /* Falls through.*/
  77.    default:
  78.       malf1 ("ma2ul: impossible error %d", s);
  79.    }
  80. if (((bStrT *) NULL) != end) *end = ep;
  81. return (u);
  82. }
  83.  
  84. #ifdef TEST
  85. #include <howard/usage.h>
  86.  
  87. MAINVER ("@(#)$Header: ma2ul.c,v 1.3 89/08/12 09:56:17 howard Exp $");
  88. USAGE ("integer-numeric-literal [limit]");
  89.  
  90. PUBLIC int main (argc, argv)
  91. int    argc; /* Number of arguments.*/
  92. bStrT *argv; /* Points to array of argument strings.*/
  93. {
  94. bStrT end; /* Points to end of string.*/
  95. ulongT u; /* Returned by ma2ul().*/
  96.  
  97. if (argc < 2) usage();
  98. u = ma2ul (argv[1], (2 == argc) ? NULBSTR : &argv[1][atoi (argv[2])], FALSE,
  99.           S("integer-numeric-literal"), &end);
  100. PRINTF ("8#%lo#\t10#%lu#\t16#%lX#\t%s\n", u, u, u, end);
  101. mfflush (stdout, "Standard Output");
  102. exit (SUCCESS);
  103.  
  104. #ifdef lint
  105. return (SUCCESS);
  106. #endif
  107. }
  108. #endif
  109.