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

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