STK++ 1.0
STK_Binary.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  stkpp::STKernel::Base
00027  * Purpose:  Implement the fundamental type Binary.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00036 #include "../include/STK_Binary.h"
00037 #include "../include/STK_String_Util.h"
00038 
00039 namespace STK
00040 {
00041 
00042 /*  Overloading of the ostream << for the type Binary.
00043  **/
00044 ostream& operator << (ostream& os, const Binary& output)
00045 { if (Arithmetic<Binary>::isNA(output))
00046     return (os <<  STRING_NA);
00047   else
00048     return (os << static_cast<int> (output));
00049 }
00050 
00051 /*  Overloading of the istream >> for the type Binary.
00052  **/
00053 istream& operator >> (istream& is, Proxy<Binary>& input)
00054 {
00055   // get current file position
00056   std::ios::pos_type pos = is.tellg();
00057   // try to read a discrete value
00058   int buff;
00059   // failed to read a discrete value
00060   if ((is >> buff).fail())
00061   {
00062     // clear failbit state
00063     is.clear(is.rdstate() & ~std::ios::failbit);
00064     // clear eofbit state if necessary and rewind position
00065     if (is.eof())
00066     {
00067       is.seekg(pos);
00068       is.clear(is.rdstate() & ~std::ios::eofbit);
00069     }
00070     // in all case input is a NA object
00071     input = Arithmetic<Binary>::NA();
00072     // get current position in the stream
00073     pos = is.tellg();
00074 
00075     // Create a String buffer
00076     Char Lbuff[STRING_NA_SIZE+1];
00077 
00078     // try to read a NA String
00079     is.getline(Lbuff, STRING_NA_SIZE+1);
00080 
00081     // if we don't get a NA String, rewind stream
00082     if (!(STRING_NA.compare(Lbuff) == 0))
00083     { is.seekg(pos); }
00084   }
00085   else
00086     input = static_cast<Binary>(buff);
00087   return is;
00088 }
00089 
00090 
00091 
00092 } // namespace STK