uintspec.c.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. uintspec.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='typename.c.html' title=' typename.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='UnwantedMessage.c.html' title=' UnwantedMessage.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * uint64_t uintspec (char const * string, uint64_t minimum, uint64_t maximum);
  24. *
  25. * number.h
  26. *
  27. * convert a numeric string to an unsigned integer; confirm that
  28. * the result does not exceed the specified range; report errors
  29. * and terminate the program on error;
  30. *
  31. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  32. * Copyright (c) 2001-2006 by Charles Maier Associates;
  33. * Licensed under the Internet Software Consortium License;
  34. *
  35. *--------------------------------------------------------------------*/
  36. #ifndef UINTSPEC_SOURCE
  37. #define UINTSPEC_SOURCE
  38. #include &lt;stdlib.h&gt;
  39. #include &lt;inttypes.h&gt;
  40. #include &lt;ctype.h&gt;
  41. #include &lt;errno.h&gt;
  42. #include &quot;../tools/number.h&quot;
  43. #include &quot;../tools/error.h&quot;
  44. #include &quot;../tools/types.h&quot;
  45. uint64_t uintspec (char const * string, uint64_t minimum, uint64_t maximum)
  46. {
  47. char const * number = string;
  48. unsigned radix = RADIX_DEC;
  49. uint64_t value = 0;
  50. unsigned digit;
  51. if (*number == '0')
  52. {
  53. number++;
  54. if ((*number == 'b') || (*number == 'B'))
  55. {
  56. radix = RADIX_BIN;
  57. number++;
  58. }
  59. else if ((*number == 'x') || (*number == 'X'))
  60. {
  61. radix = RADIX_HEX;
  62. number++;
  63. }
  64. }
  65. while ((digit = todigit (*number)) &lt; radix)
  66. {
  67. value *= radix;
  68. value += digit;
  69. number++;
  70. }
  71. if (*number)
  72. {
  73. error (1, EINVAL, &quot;Have '%s' but want base %d integer&quot;, string, radix);
  74. }
  75. if ((value &lt; minimum) || (value &gt; maximum))
  76. {
  77. error (1, ERANGE, &quot;Have %s but want %&quot; PRId64 &quot; thru %&quot; PRId64, string, minimum, maximum);
  78. }
  79. return (value);
  80. }
  81. #endif
  82. </pre>
  83. <div class='footerlink'>
  84. [<a href='typename.c.html' title=' typename.c '>PREV</a>]
  85. [<a href='toolkit.html' title=' Index '>HOME</a>]
  86. [<a href='UnwantedMessage.c.html' title=' UnwantedMessage.c '>NEXT</a>]
  87. </div>
  88. </body>
  89. </html>