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.

104 lines
2.1 KiB

  1. /***
  2. * istrgdbl.cpp - definitions for istream class core double routine
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Definitions of member function for istream getdouble().
  8. * [AT&T C++]
  9. *
  10. *Revision History:
  11. * 09-26-91 KRS Created. Split off from istream.cxx for granularity.
  12. * 06-14-95 CFW Comment cleanup.
  13. *
  14. *******************************************************************************/
  15. #include <cruntime.h>
  16. #include <internal.h>
  17. #include <ctype.h>
  18. #include <iostream.h>
  19. #pragma hdrstop
  20. /***
  21. *int istream::getdouble(char * buffer, int buflen) - get a double
  22. *
  23. *Purpose:
  24. * Get a double from stream.
  25. *
  26. *Entry:
  27. * char * buffer = area for number to be copied.
  28. * int buflen = max. length of buffer
  29. *
  30. *Exit:
  31. * Returns 0 if fatal error
  32. * Otherwise, returns length of buffer filled.
  33. * Sets ios::failbit on error forming number.
  34. * If successful, buffer[] contains the number, followed by \0.
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39. int istream::getdouble(char * buffer, int buflen) // returns length
  40. {
  41. int c;
  42. int i = 0;
  43. int fDigit = 0; // true if legal digit encountered
  44. int fDecimal=0; // true if '.' encountered or no longer valid
  45. int fExp=0; // true if 'E' or 'e' encounted
  46. if (ipfx(0))
  47. {
  48. c=bp->sgetc();
  49. for (; i<buflen; buffer[i] = (char)c,c=bp->snextc(),i++)
  50. {
  51. if (c==EOF)
  52. {
  53. state |= ios::eofbit;
  54. break;
  55. }
  56. if ((!i) || (fExp==1))
  57. {
  58. if ((c=='-') || (c=='+'))
  59. {
  60. continue;
  61. }
  62. }
  63. if ((c=='.') && (!fExp) && (!fDecimal))
  64. {
  65. fDecimal++;
  66. continue;
  67. }
  68. if (((c=='E') || (c=='e')) && (!fExp))
  69. {
  70. fDecimal++; // can't allow decimal now
  71. fExp++;
  72. continue;
  73. }
  74. if (!isdigit(c))
  75. break;
  76. if (fExp)
  77. fExp++;
  78. else
  79. fDigit++;
  80. }
  81. if (fExp==1) // E or e with no number after it
  82. {
  83. if (bp->sputbackc(buffer[i])!=EOF)
  84. {
  85. i--;
  86. state &= ~(ios::eofbit);
  87. }
  88. else
  89. {
  90. state |= ios::failbit;
  91. }
  92. }
  93. if ((!fDigit) || (i==buflen))
  94. state |= ios::failbit;
  95. // buffer contains a valid number or '\0'
  96. buffer[i] = '\0';
  97. isfx();
  98. }
  99. return i;
  100. }