home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / istrusht.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  60 lines

  1. /***
  2. * istrusht.cpp - definitions for istream class operator>>(unsigned short) funct
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Definitions of operator>>(unsigned short) member function for istream
  8. *       class.
  9. *       [AT&T C++]
  10. *
  11. *******************************************************************************/
  12.  
  13. #include <cruntime.h>
  14. #include <internal.h>
  15. #include <stdlib.h>
  16. #include <limits.h>
  17. #include <errno.h>
  18. #include <iostream.h>
  19. #pragma hdrstop
  20.  
  21. /***
  22. *istream& istream::operator>>(unsigned short& n) - extract unsigned short
  23. *
  24. *Purpose:
  25. *       Extract unsigned short value from stream
  26. *       Valid range is SHRT_MIN to USHRT_MAX.
  27. *
  28. *Entry:
  29. *       n = value to update
  30. *
  31. *Exit:
  32. *       n updated, or ios::failbit and n=USHRT_MAX if error
  33. *
  34. *Exceptions:
  35. *       Stream error on entry or value out of range
  36. *
  37. *******************************************************************************/
  38. istream& istream::operator>>(unsigned short& n)
  39. {
  40. _WINSTATIC char ibuffer[MAXLONGSIZ];
  41.     unsigned long value;
  42.     char ** endptr = (char**)NULL;
  43.     if (ipfx(0))
  44.         {
  45.         value = strtoul(ibuffer, endptr, getint(ibuffer));
  46.  
  47.         if (((value>USHRT_MAX) && (value<=(ULONG_MAX-(-SHRT_MIN))))
  48.                 || ((value==ULONG_MAX) && (errno==ERANGE)))
  49.             {
  50.             n = USHRT_MAX;
  51.             state |= ios::failbit;
  52.             }
  53.         else
  54.             n = (unsigned short) value;
  55.  
  56.         isfx();
  57.         }
  58. return *this;
  59. }
  60.