home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / NString 1.0 beta / Sources / NString_IO.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-20  |  3.3 KB  |  147 lines  |  [TEXT/KAHL]

  1.  
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <limits.h>
  6.  
  7. #include "NString.h"
  8.  
  9. //_________________________________________________________________________________
  10.  
  11. ostream& operator<<(ostream& theStream, const NString& theString)
  12. {
  13.     return (theStream << theString.sb->str);
  14. }
  15.  
  16. //_________________________________________________________________________________
  17.  
  18. istream& operator>>(istream& theStream, NString& theString)
  19. {
  20.     if (! theStream)                                                // if the stream is not in good state, abort
  21.         return theStream;
  22.         
  23.     char c;
  24.  
  25.     theString = "";
  26.     
  27.     while (theStream.get(c))                                // eat whitespace and NUL chars
  28.         if ((! isspace(c)) && (c != '\0'))
  29.         {
  30.             theStream.putback(c);
  31.             break;
  32.         }
  33.     
  34.     while ((theStream.peek() != EOF) && theStream.get(c))
  35.         if (isspace(c) || (c == '\0'))
  36.         {
  37.             theStream.putback(c);                            // a NUL or a whitespace character: put it back and abort
  38.             break;
  39.         }
  40.         else
  41.             theString += c;                                        // a plain, good character: add it to the string
  42.             
  43.     return theStream;
  44. }
  45.  
  46. //_________________________________________________________________________________
  47.  
  48. istream& get (istream& theStream, NString& theString, 
  49.                     const unsigned long int maxLength, const char terminator)
  50. {
  51.     if (! theStream)                                                // if the stream is not in good state, abort
  52.         return theStream;
  53.         
  54.     char c;
  55.     const unsigned long int maxlen = (maxLength ? maxLength : ULONG_MAX);
  56.  
  57.     theString = "";
  58.     
  59.     while (theStream.get(c))                                // eat NUL chars
  60.         if (c != '\0')
  61.         {
  62.             theStream.putback(c);
  63.             break;
  64.         }
  65.     
  66.     while ((theStream.peek() != EOF) && (theString.sb->len < maxlen) && theStream.get(c))
  67.         if ((c == '\0') || (c == terminator))
  68.         {
  69.             theStream.putback(c);                            // a NUL character: put it back and abort
  70.             break;
  71.         }
  72.         else
  73.             theString += c;                                        // a plain, good character: add it to the string
  74.             
  75.     return theStream;
  76. }
  77.  
  78. //_________________________________________________________________________________
  79.  
  80. NString& NString::operator<< (const unsigned long int n)
  81. {
  82.     char text[50];
  83.  
  84.     sprintf(text, "%lu", n);
  85.     return (*this += text);
  86. }
  87.  
  88. //_________________________________________________________________________________
  89.  
  90. NString& NString::operator<< (const signed long int n)
  91. {
  92.     char text[50];
  93.  
  94.     sprintf(text, "%ld", n);
  95.     return (*this += text);
  96. }
  97.  
  98. //_________________________________________________________________________________
  99.  
  100. NString& NString::operator<< (const unsigned short int n)
  101. {
  102.     char text[50];
  103.  
  104.     sprintf(text, "%hu", n);
  105.     return (*this += text);
  106. }
  107.  
  108. //_________________________________________________________________________________
  109.  
  110. NString& NString::operator<< (const signed short int n)
  111. {
  112.     char text[50];
  113.  
  114.     sprintf(text, "%hd", n);
  115.     return (*this += text);
  116. }
  117.  
  118. //_________________________________________________________________________________
  119.  
  120. NString& NString::operator<< (const double n)
  121. {
  122.     char text[50];
  123.  
  124.     sprintf(text, "%.12lf", n);
  125.     return (*this += text);
  126. }
  127.  
  128. //_________________________________________________________________________________
  129.  
  130. NString& NString::operator<< (const float n)
  131. {
  132.     char text[50];
  133.  
  134.     sprintf(text, "%.6f", n);
  135.     return (*this += text);
  136. }
  137.  
  138. //_________________________________________________________________________________
  139.  
  140. NString& NString::operator<< (const void *p)
  141. {
  142.     char text[50];
  143.  
  144.     sprintf(text, "%p", p);
  145.     return (*this += text);
  146. }
  147.