basespec.c.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. basespec.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='b64dump.c.html' title=' b64dump.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='baudrate.c.html' title=' baudrate.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * uint64_t basespec (char const * string, unsigned base, unsigned size);
  24. *
  25. * number.h
  26. *
  27. * convert a character string to an equivalent unsigned integer and
  28. * return the result; terminate the program on failure;
  29. *
  30. * the base argument is the number base to be used for conversion;
  31. * base 0 permits the number base to be determined by the string
  32. * string prefix; 0b, 0d or 0x for binary, decimal or hex;
  33. *
  34. * this implementation accepts a minus sign in order to negate any
  35. * number in any base;
  36. *
  37. * the size argument is the maximum number of bytes permitted in the
  38. * result;
  39. *
  40. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  41. * Copyright (c) 2001-2006 by Charles Maier Associates;
  42. * Licensed under the Internet Software Consortium License;
  43. *
  44. *--------------------------------------------------------------------*/
  45. #ifndef BASESPEC_SOURCE
  46. #define BASESPEC_SOURCE
  47. #include &lt;stdlib.h&gt;
  48. #include &lt;ctype.h&gt;
  49. #include &quot;../tools/number.h&quot;
  50. #include &quot;../tools/error.h&quot;
  51. uint64_t basespec (char const * string, unsigned base, unsigned size)
  52. {
  53. char const * number = string;
  54. unsigned radix = RADIX_DEC;
  55. signed scale = 1;
  56. uint64_t limit = 0;
  57. uint64_t value = 0;
  58. unsigned digit = 0;
  59. limit = ~limit;
  60. if (size &lt; sizeof (limit))
  61. {
  62. limit &lt;&lt;= size &lt;&lt; 3;
  63. limit = ~limit;
  64. }
  65. if (base)
  66. {
  67. radix = base;
  68. }
  69. if (* number == '=')
  70. {
  71. number++;
  72. }
  73. else if (* number == '+')
  74. {
  75. number++;
  76. }
  77. else if (* number == '-')
  78. {
  79. number++;
  80. scale = -1;
  81. }
  82. if (*number == '0')
  83. {
  84. number++;
  85. if ((*number == 'b') || (*number == 'B'))
  86. {
  87. radix = RADIX_BIN;
  88. number++;
  89. }
  90. else if ((*number == 'd') || (*number == 'D'))
  91. {
  92. radix = RADIX_DEC;
  93. number++;
  94. }
  95. else if ((*number == 'x') || (*number == 'X'))
  96. {
  97. radix = RADIX_HEX;
  98. number++;
  99. }
  100. }
  101. if ((base) &amp;&amp; (base != radix))
  102. {
  103. error (1, EINVAL, &quot;%s is not base %d notation&quot;, string, base);
  104. }
  105. while ((digit = todigit (*number)) &lt; radix)
  106. {
  107. value *= radix;
  108. value += digit;
  109. if (value &gt; limit)
  110. {
  111. error (1, ERANGE, &quot;%s exceeds %d bits&quot;, string, (size &lt;&lt; 3));
  112. }
  113. number++;
  114. }
  115. #ifdef WIN32
  116. while (isspace (*number))
  117. {
  118. number++;
  119. }
  120. #endif
  121. if (*number)
  122. {
  123. error (1, EINVAL, &quot;%s is not base %d notation&quot;, string, radix);
  124. }
  125. return (scale * value);
  126. }
  127. #endif
  128. </pre>
  129. <div class='footerlink'>
  130. [<a href='b64dump.c.html' title=' b64dump.c '>PREV</a>]
  131. [<a href='toolkit.html' title=' Index '>HOME</a>]
  132. [<a href='baudrate.c.html' title=' baudrate.c '>NEXT</a>]
  133. </div>
  134. </body>
  135. </html>