PLearn 0.1
pl_io_deprecated.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999,2000 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037  
00038 
00039 /* *******************************************************      
00040  * $Id: pl_io_deprecated.h 8232 2007-11-07 20:52:17Z nouiz $
00041  * AUTHORS: Pascal Vincent
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00045 
00048 #ifndef pl_io_deprecated_INC
00049 #define pl_io_deprecated_INC
00050 
00051 #include <iostream>                   
00052 //#include "pl_io.h"
00053 #include <plearn/base/plerror.h>
00054 //#include <cstdio>
00055 #include <plearn/base/byte_order.h>   
00056 #include <plearn/io/PStream.h>
00057 
00058 namespace PLearn {
00059 using namespace std;
00060 
00061 
00062 // *************************************************************************
00063 // *************************************************************************
00064 // *********** ALL THAT FOLLOWS IS DEPRECATED: DO NOT USE!!!! **************
00065 // *************************************************************************
00066 // *************************************************************************
00067 
00070 inline void reverse_uint(const unsigned int* ptr, int n) { endianswap((unsigned int*) ptr,n); }
00071 inline void reverse_int(const int* ptr, int n) { endianswap((int*)ptr,n); }
00072 inline void reverse_float(const float* ptr, int n) { endianswap((float*)ptr,n); }
00073 inline void reverse_double(const double* ptr, int n) { endianswap((double*)ptr,n); }
00074 inline void reverse_ushort(const unsigned short* ptr, int n) { endianswap((unsigned short*)ptr,n); }
00075 inline void reverse_short(const short* ptr, int n) { endianswap((short*)ptr,n); }
00076 
00079 void  fwrite_int(FILE *f, const int* ptr, int n, bool is_file_bigendian=true);
00080 void  fwrite_float(FILE *f, const float* ptr, int n, bool is_file_bigendian=true);
00081 void  fwrite_float(FILE *f, const double* ptr, int n, bool is_file_bigendian=true); 
00082 void  fwrite_double(FILE *f, const double* ptr, int n, bool is_file_bigendian=true);
00083 void  fwrite_double(FILE *f, const float* ptr, int n, bool is_file_bigendian=true); 
00084 
00087 void  fread_int(FILE *f, int* ptr, int n, bool is_file_bigendian=true);
00088 void  fread_float(FILE *f, float* ptr, int n, bool is_file_bigendian=true);
00089 void  fread_float(FILE *f, double* ptr, int n, bool is_file_bigendian=true); 
00090 void  fread_double(FILE *f, double* ptr, int n, bool is_file_bigendian=true);
00091 void  fread_double(FILE *f, float* ptr, int n, bool is_file_bigendian=true); 
00092 void  fread_short(FILE *f, unsigned short* ptr, int n, bool is_file_bigendian=true);
00093 
00098 inline int fread_int(FILE *f, bool is_file_bigendian=true) 
00099 { int res; fread_int(f,&res,1,is_file_bigendian); return res; }
00100 inline float fread_float(FILE *f, bool is_file_bigendian=true)
00101 { float res; fread_float(f,&res,1,is_file_bigendian); return res; }
00102 inline double fread_double(FILE *f, bool is_file_bigendian=true)
00103 { double res; fread_double(f,&res,1,is_file_bigendian); return res; }
00104 
00107 inline void fwrite_int(FILE *f, int value, bool is_file_bigendian=true)
00108 { fwrite_int(f, &value, 1, is_file_bigendian); }
00109 inline void fwrite_float(FILE *f, float value, bool is_file_bigendian=true)
00110 { fwrite_float(f, &value, 1, is_file_bigendian); }
00111 inline void fwrite_double(FILE *f, double value, bool is_file_bigendian=true)
00112 { fwrite_double(f, &value, 1, is_file_bigendian); }
00113 
00116 void write_int(ostream& out, const int* ptr, int n, bool is_file_bigendian);
00117 void write_short(ostream& out, const short* ptr, int n, bool is_file_bigendian);
00118 void write_double(ostream& out, const double* ptr, int n, bool is_file_bigendian);
00119 void write_float(ostream& out, const float* ptr, int n, bool is_file_bigendian);
00120 inline void write_uint(ostream& out, const unsigned int* ptr, int n, bool is_file_bigendian)
00121 { write_int(out,(int*)ptr,n,is_file_bigendian); }
00122 inline void write_ushort(ostream& out, const unsigned short* ptr, int n, bool is_file_bigendian)
00123 { write_short(out,(short*)ptr,n,is_file_bigendian); }
00124 void write_bool(ostream& out, const bool* ptr, int n, bool is_file_bigendian);
00125 
00128 inline void write_int(ostream& out, int value, bool is_file_bigendian=true)
00129 { write_int(out, &value, 1, is_file_bigendian); }
00130 inline void write_short(ostream& out, short value, bool is_file_bigendian=true)
00131 { write_short(out, &value, 1, is_file_bigendian); }
00132 inline void write_float(ostream& out, float value, bool is_file_bigendian=true)
00133 { write_float(out, &value, 1, is_file_bigendian); }
00134 inline void write_double(ostream& out, double value, bool is_file_bigendian=true)
00135 { write_double(out, &value, 1, is_file_bigendian); }
00136 inline void write_uint(ostream& out, unsigned int value, bool is_file_bigendian=true)
00137 { write_uint(out, &value, 1, is_file_bigendian); }
00138 inline void write_ushort(ostream& out, unsigned short value, bool is_file_bigendian=true)
00139 { write_ushort(out, &value, 1, is_file_bigendian); }
00140 inline void write_sbyte(ostream& out, signed char x) { out.put(x); }
00141 inline void write_ubyte(ostream& out, unsigned char x) { out.put(x); }
00142 
00145 void read_int(istream& in, int* ptr, int n, bool is_file_bigendian);
00146 void read_short(istream& in, short* ptr, int n, bool is_file_bigendian);
00147 void read_float(istream& in, float* ptr, int n, bool is_file_bigendian);
00148 void read_double(istream& in, double* ptr, int n, bool is_file_bigendian);
00149 inline void read_uint(istream& in, unsigned int* ptr, int n, bool is_file_bigendian)
00150 { read_int(in,(int*)ptr,n,is_file_bigendian); }
00151 inline void read_ushort(istream& in, unsigned short* ptr, int n, bool is_file_bigendian)
00152 { read_short(in,(short*)ptr,n,is_file_bigendian); }
00153 void read_bool(istream& in, bool* ptr, int n, bool is_file_bigendian);
00154 
00159 inline int read_int(istream& in, bool is_file_bigendian=true) 
00160 { int res; read_int(in,&res,1,is_file_bigendian); return res; }
00161 inline short read_short(istream& in, bool is_file_bigendian=true) 
00162 { short res; read_short(in,&res,1,is_file_bigendian); return res; }
00163 inline float read_float(istream& in, bool is_file_bigendian=true)
00164 { float res; read_float(in,&res,1,is_file_bigendian); return res; }
00165 inline double read_double(istream& in, bool is_file_bigendian=true)
00166 { double res; read_double(in,&res,1,is_file_bigendian); return res; }
00167 inline unsigned int read_uint(istream& in, bool is_file_bigendian=true) 
00168 { unsigned int res; read_uint(in,&res,1,is_file_bigendian); return res; }
00169 inline unsigned short read_ushort(istream& in, bool is_file_bigendian=true) 
00170 { unsigned short res; read_ushort(in,&res,1,is_file_bigendian); return res; }
00171 inline signed char read_sbyte(istream& in)
00172 { 
00173     char res;
00174     in.read(&res,1);
00175     return (signed char) res;
00176 }
00177 inline unsigned char read_ubyte(istream& in)
00178 { 
00179     char res;
00180     in.read(&res,1);
00181     return (unsigned char) res;
00182 }
00183 
00184 // ***************************************
00185 // *** DEPRECATED SERIALIZATION SYSTEM ***
00186 // ***************************************
00187 
00190 void writeHeader(ostream& out, const string& classname, int version=0); 
00191 void writeFooter(ostream& out, const string& classname); 
00192 int readHeader(PStream& in, const string& classname);   
00193 void readFooter(PStream& in, const string& classname);   
00194 
00195 
00197 inline void writeNewline(ostream& out)
00198 { out << '\n'; }
00199 
00201 inline void readNewline(istream& in) 
00202 { if(in.get()!='\n') PLERROR("In readNewline: character read is not a newline char"); }
00203 
00204 
00210 void writeFieldName(ostream& out, const string& fieldname); 
00211 
00212 
00213 bool readFieldName(istream& in, const string& fieldname, bool force=false);   
00214 
00216 template<class T>
00217 void writeField(ostream& out, const string& fieldname, const T& x)
00218 { 
00219 // Norman: This gives problems on VSNet when T is a Array<VMFieldStat> or VMFieldStat.
00220 //         Because it is deprecated, well, I have decided to wipe it out! :)
00221 #ifndef WIN32
00222     writeFieldName(out,fieldname); write(out,x); out << '\n'; 
00223 #endif
00224 }
00225 
00226 template<class T>
00227 void readField(istream& in, const string& fieldname, T& x)
00228 { 
00229 // Norman: This gives problems on VSNet when T is a Array<VMFieldStat> or VMFieldStat.
00230 //         Because it is deprecated, well, I have decided to wipe it out! :)
00231 #ifndef WIN32
00232     readFieldName(in,fieldname,true); 
00233     read(in,x); 
00234     if(!isspace(in.get())) 
00235         in.unget(); 
00236 #endif
00237 }
00238 
00240 template<class T>
00241 void binwriteField(ostream& out, const string& fieldname, const T& x)
00242 { writeFieldName(out,fieldname); binwrite(out,x); out << '\n'; }
00243 
00244 template<class T>
00245 void binreadField(istream& in, const string& fieldname, T& x)
00246 { readFieldName(in,fieldname,true); binread(in,x); in.get(); }
00247 
00248 template<class T>
00249 void binwriteField_double(ostream& out, const string& fieldname, const T& x)
00250 { writeFieldName(out,fieldname); binwrite_double(out,x); out << '\n'; }
00251 
00252 template<class T>
00253 void binreadField_double(istream& in, const string& fieldname, T& x)
00254 { readFieldName(in,fieldname,true); binread_double(in,x); in.get(); }
00255 
00257 template<class T>
00258 void readField(istream& in, const string& fieldname, T& x, T default_value)
00259 { 
00260     if (readFieldName(in,fieldname)) 
00261     { 
00262         read(in,x); 
00263         if(!isspace(in.get())) 
00264             in.unget(); 
00265     } 
00266     else x=default_value; 
00267 }
00268 
00269 
00270 
00271 
00272 
00273 } // end of namespace PLearn
00274 
00275 
00276 #endif
00277 
00278 
00279 /*
00280   Local Variables:
00281   mode:c++
00282   c-basic-offset:4
00283   c-file-style:"stroustrup"
00284   c-file-offsets:((innamespace . 0)(inline-open . 0))
00285   indent-tabs-mode:nil
00286   fill-column:79
00287   End:
00288 */
00289 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines