Gems3k  3.1
GEMS3K standalone solver for geochemical equilibria
 All Classes Files Functions Variables Enumerations Enumerator
/Users/kulik/DevGEMS/trunk/standalone/GEMS3K/gdatastream.h
Go to the documentation of this file.
00001 //-------------------------------------------------------------------
00002 // $Id: gdatastream.h 725 2012-10-02 15:43:37Z kulik $
00006 //
00010 //
00011 // Copyright (c) 1996-2012 A.Rysin, S.Dmytriyeva
00012 // <GEMS Development Team, mailto:gems2.support@psi.ch>
00013 //
00014 // This file is part of the GEMS3K code for thermodynamic modelling
00015 // by Gibbs energy minimization <http://gems.web.psi.ch/GEMS3K/>
00016 //
00017 // GEMS3K is free software: you can redistribute it and/or modify
00018 // it under the terms of the GNU Lesser General Public License as
00019 // published by the Free Software Foundation, either version 3 of
00020 // the License, or (at your option) any later version.
00021 
00022 // GEMS3K is distributed in the hope that it will be useful,
00023 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 // GNU Lesser General Public License for more details.
00026 
00027 // You should have received a copy of the GNU General Public License
00028 // along with GEMS3K code. If not, see <http://www.gnu.org/licenses/>.
00029 //-------------------------------------------------------------------
00030 
00031 #ifndef _gemdatastream_h_
00032 #define _gemdatastream_h_
00033 
00034 #include <fstream>
00035 #include "verror.h"
00036 
00037 class GemDataStream                             // data stream class
00038 {
00039 
00040     ios::openmode mod;
00041     gstring Path;
00042 
00043     int swap;
00044     int byteorder;
00045     fstream ff;
00046 
00047 public:
00048 //    GemDataStream( fstream& ff  );
00049     GemDataStream( ) {    setByteOrder(LittleEndian); };
00050     GemDataStream( gstring& aPath, ios::openmode aMod  );
00051     virtual ~GemDataStream();
00052 
00053     const gstring& GetPath() const
00054     {
00055         return Path;
00056     }
00057     
00058 //    bool       atEnd() const;
00059 //    bool       eof() const;
00060 
00061     enum ByteOrder { BigEndian, LittleEndian };
00062     int  byteOrder() const { return byteorder; };
00063     void setByteOrder( int );
00064 
00065     filebuf* rdbuf() { return ff.rdbuf(); }
00066     streamsize gcount() { return ff.gcount(); }
00067     istream& getline(char* s, streamsize n, char delim) { return ff.getline(s, n, delim); }
00068     void close() { ff.close(); }
00069     void put(char ch) { ff.put(ch); }
00070     istream& get(char& ch) { return ff.get(ch); }
00071     void sync() { ff.sync(); }
00072     bool good() { return ff.good(); }
00073     void clear() { ff.clear(); }
00074     void flush() { ff.flush(); }
00075     size_t tellg() { return ff.tellg(); }
00076     void open(const char* filename, ios::openmode mode) { ff.open(filename, mode); }
00077     ostream& seekp(size_t pos, ios_base::seekdir dir) { return ff.seekp(pos, dir); }
00078     istream& seekg(size_t pos, ios_base::seekdir dir) { return ff.seekg(pos, dir); }
00079 
00080     GemDataStream &operator>>( char &i );
00081     GemDataStream &operator>>( unsigned char &i ) { return operator>>((char&)i); }
00082     GemDataStream &operator>>( signed char &i ) { return operator>>((char&)i); }
00083     GemDataStream &operator>>( short &i );
00084     GemDataStream &operator>>( unsigned short &i ) { return operator>>((short&)i); }
00085     GemDataStream &operator>>( int &i );
00086     GemDataStream &operator>>( unsigned int &i ) { return operator>>((int&)i); }
00087     GemDataStream &operator>>( long &i );
00088     GemDataStream &operator>>( unsigned long &i ) { return operator>>((long&)i); }
00089     GemDataStream &operator>>( float &f );
00090     GemDataStream &operator>>( double &f );
00091 //    GemDataStream &operator>>( char *&str );
00092 
00093     GemDataStream &operator<<( char i );
00094     GemDataStream &operator<<( unsigned char i ) { return operator<<((char) i); }
00095     GemDataStream &operator<<( signed char i ) { return operator<<((char) i); }
00096     GemDataStream &operator<<( short i );
00097     GemDataStream &operator<<( unsigned short i ) { return operator<<((short) i); }
00098     GemDataStream &operator<<( int i );
00099     GemDataStream &operator<<( unsigned int i ) { return operator<<((int) i); }
00100     GemDataStream &operator<<( long i );
00101     GemDataStream &operator<<( unsigned long i ) { return operator<<((long) i); }
00102     GemDataStream &operator<<( float f );
00103     GemDataStream &operator<<( double f );
00104 //    GemDataStream &operator<<( const char *str );
00105 
00106     void readArray( char* arr, int size );
00107     void readArray( short* arr, int size );
00108     void readArray( int* arr, int size );
00109     void readArray( long* arr, int size );
00110     void readArray( float* arr, int size );
00111     void readArray( double* arr, int size );
00112 
00113     void writeArray( char* arr, int size );
00114     void writeArray( short* arr, int size );
00115     void writeArray( int* arr, int size );
00116     void writeArray( long* arr, int size );
00117     void writeArray( float* arr, int size );
00118     void writeArray( double* arr, int size );
00119 
00120 #ifndef IPMGEMPLUGIN
00121 
00122     template <class T> void writeArray( T* arr, int size );
00123     template <class T> void readArray( T* arr, int size );
00124 #endif
00125 };
00126 
00127 #ifndef IPMGEMPLUGIN
00128 
00129 template <class T>
00130 void GemDataStream::writeArray( T* arr, int size )
00131 {
00132   if( !arr )
00133     return;
00134   for(int ii=0; ii<size; ii++)
00135    *this << arr[ii];
00136 }
00137 
00138 template <class T>
00139 void GemDataStream::readArray( T* arr, int size )
00140 {
00141   if( !arr )
00142     return;
00143   for(int ii=0; ii<size; ii++)
00144    *this >> arr[ii];
00145 }
00146 
00147 #endif
00148 #endif