Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.6 KiB

  1. /***
  2. * istrgint.cpp - definitions for istream class core integer routines
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Definitions of member function getint() for istream class.
  8. * [AT&T C++]
  9. *
  10. *Revision History:
  11. * 09-26-91 KRS Created. Split off from istream.cxx for granularity.
  12. * 01-06-92 KRS Remove buflen argument.
  13. * 05-24-94 GJF Copy no more than MAXLONGSIZ characters, counting the
  14. * '\0', into the buffer. Also, moved definition of
  15. * MAXLONGSIZ to istream.h.
  16. * 06-14-95 CFW Comment cleanup.
  17. *
  18. *******************************************************************************/
  19. #include <cruntime.h>
  20. #include <internal.h>
  21. #include <ctype.h>
  22. #include <iostream.h>
  23. #pragma hdrstop
  24. /***
  25. *int istream::getint(char * buffer) - get an int
  26. *
  27. *Purpose:
  28. * Get an int from stream.
  29. *
  30. *Entry:
  31. * char * buffer = area for number to be copied.
  32. *
  33. *Exit:
  34. * Returns base for conversion: (0, 2, 8, or 16).
  35. * If successful, buffer[] contains the number, followed by \0.
  36. *
  37. *Exceptions:
  38. * Sets ios::failbit on error forming number.
  39. * Sets ios::badbit on error after failbit
  40. * Sets ios::eofbit if at EOF at return
  41. *
  42. *******************************************************************************/
  43. int istream::getint(char * buffer) // returns length
  44. {
  45. int base, i;
  46. int c;
  47. int fDigit = 0;
  48. int bindex = 1;
  49. if (x_flags & ios::dec)
  50. base = 10;
  51. else if (x_flags & ios::hex)
  52. base = 16;
  53. else if (x_flags & ios::oct)
  54. base = 8;
  55. else
  56. base = 0;
  57. if (ipfx(0))
  58. {
  59. c=bp->sgetc();
  60. for (i = 0; i<MAXLONGSIZ-1; buffer[i] = (char)c,c=bp->snextc(),i++)
  61. {
  62. if (c==EOF)
  63. {
  64. state |= ios::eofbit;
  65. break;
  66. }
  67. if (!i)
  68. {
  69. if ((c=='-') || (c=='+'))
  70. {
  71. bindex++;
  72. continue;
  73. }
  74. }
  75. if ((i==bindex) && (buffer[i-1]=='0'))
  76. {
  77. if (((c=='x') || (c=='X')) && ((base==0) || (base==16)))
  78. {
  79. base = 16; // simplifies matters
  80. fDigit = 0;
  81. continue;
  82. }
  83. else if (base==0)
  84. {
  85. base = 8;
  86. }
  87. }
  88. // now simply look for a digit and set fDigit if found else break
  89. if (base==16)
  90. {
  91. if (!isxdigit(c))
  92. break;
  93. }
  94. else if ((!isdigit(c)) || ((base==8) && (c>'7')))
  95. break;
  96. fDigit++;
  97. }
  98. if (!fDigit)
  99. {
  100. state |= ios::failbit;
  101. while (i--)
  102. {
  103. if(bp->sputbackc(buffer[i])==EOF)
  104. {
  105. state |= ios::badbit;
  106. break;
  107. }
  108. else
  109. state &= ~(ios::eofbit);
  110. }
  111. i=0;
  112. }
  113. // buffer contains a valid number or '\0'
  114. buffer[i] = '\0';
  115. isfx();
  116. }
  117. if (i==MAXLONGSIZ)
  118. {
  119. state |= ios::failbit;
  120. }
  121. return base;
  122. }