PLearn 0.1
pl_io_deprecated.cc
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.cc 9184 2008-06-26 21:07:47Z chrish $
00041  * AUTHORS: Pascal Vincent
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00047 #include "pl_io_deprecated.h"
00048 #include <plearn/base/stringutils.h>
00049 #include "pl_streambuf.h"
00050 
00051 namespace PLearn {
00052 using namespace std;
00053 
00054 
00055 void writeHeader(ostream& out, const string& classname, int version)
00056 { out << "<" << classname << ":" << version << ">\n"; }
00057 
00058 void writeFooter(ostream& out, const string& classname)
00059 { out << "</" << classname << ">\n"; }
00060 
00061 int readHeader(PStream& in, const string& classname)
00062 {
00063     string header;
00064 //#if defined(_MINGW_) || defined(WIN32)
00065 //  in.tellg();   // Don't remove this line under MinGW, it apparently does nothing
00066 //                // but if it's not there, it won't work (Hint: Microsoft conspiracy)
00067 //#endif
00068     in >> header;
00069     int headerlen = (int)header.length();
00070     in.get(); // consume newline
00071     int classnamelen = (int)classname.length();
00072     if (   headerlen<classnamelen+2 
00073            || header[0]!='<' || header.substr(1,classnamelen)!=classname
00074            || (header[1+classnamelen]!='>' && header[1+classnamelen]!=':') )
00075         PLERROR("In Object::readHeader WRONG HEADER: %s (SHOULD BE {%s:version>)",header.c_str(),classname.c_str());
00076     if (header[1+classnamelen]==':')
00077         return toint(header.substr(2+classnamelen, headerlen-classnamelen-2));
00078     else return 0;
00079 }
00080 
00081 void readFooter(PStream& in, const string& classname)
00082 {
00083     string footer;
00084     in >> footer;
00085     string correctfooter = string("</")+classname+">";
00086     if(footer != correctfooter)
00087         PLERROR("In Object::readFooter WRONG FOOTER: %s (SHOULD BE %s)",footer.c_str(),correctfooter.c_str());
00088     in.get(); // consume newline
00089 }
00090 
00091 
00092 void writeFieldName(ostream& out, const string& fieldname)
00093 { out << fieldname << ": "; }
00094 
00095 bool readFieldName(istream& in, const string& fieldname, bool force)
00096 { 
00097     pl_streambuf* buffer = dynamic_cast<pl_streambuf*>(in.rdbuf());
00098     pl_streammarker fence(buffer);
00099     string word;
00100     in >> word;
00101     if(word != fieldname+":")
00102     {
00103         if (force)
00104             PLERROR("In readFieldName read %s while expected fieldname was %s",word.c_str(),fieldname.c_str());
00105         else {
00106             // back-track to before trying to read the field name
00107             //NOTE: FIX_ME
00108             // seekmark is done on 'buffer'... which is NOT in's buffer...
00109             // so the pl_streambuf and the pl_streammarker are useless.
00110             // It would be an error anyways to set in's buffer to 'buffer':
00111             // 'buffer' is local to this function and seekmark is done just before
00112             // return.  suggestion: don't use this function...
00113             //                        -xsm
00114             buffer->seekmark(fence);
00115             return false;
00116         }
00117     }
00118     in.get(); // consume following white space
00119     return true;
00120 
00121 }
00122 
00123 
00124 
00125 
00126 // Functions to write a file in any representation
00127 
00128 void fwrite_int(FILE *f, const int* ptr, int n, bool is_file_bigendian)
00129 {
00130 #ifdef LITTLEENDIAN
00131     if(is_file_bigendian)
00132     {
00133         reverse_int(ptr,n);
00134         fwrite(ptr,sizeof(int),n,f);
00135         reverse_int(ptr,n);
00136     }
00137     else
00138         fwrite(ptr,sizeof(int),n,f);
00139 #endif
00140 #ifdef BIGENDIAN
00141     if(is_file_bigendian)
00142         fwrite(ptr,sizeof(int),n,f);
00143     else
00144     {
00145         reverse_int(ptr,n);
00146         fwrite(ptr,sizeof(int),n,f);
00147         reverse_int(ptr,n);
00148     }
00149 #endif
00150 }
00151 
00152 void fwrite_float(FILE *f, const float* ptr, int n, bool is_file_bigendian)
00153 {
00154 #ifdef LITTLEENDIAN
00155     if(is_file_bigendian)
00156     {
00157         reverse_float(ptr,n);
00158         fwrite(ptr,sizeof(float),n,f);
00159         reverse_float(ptr,n);
00160     }
00161     else
00162         fwrite(ptr,sizeof(float),n,f);
00163 #endif
00164 #ifdef BIGENDIAN
00165     if(is_file_bigendian)
00166         fwrite(ptr,sizeof(float),n,f);
00167     else
00168     {
00169         reverse_float(ptr,n);
00170         fwrite(ptr,sizeof(float),n,f);
00171         reverse_float(ptr,n);
00172     }
00173 #endif
00174 }
00175 
00176 void fwrite_float(FILE *f, const double* ptr, int n, bool is_file_bigendian)
00177 {
00178     float* fptr = new float[n];
00179     for(int i=0; i<n; i++)
00180         fptr[i] = float(ptr[i]);
00181     fwrite_float(f,fptr,n,is_file_bigendian);
00182     delete[] fptr;
00183 }
00184 
00185 void fwrite_double(FILE *f, const double* ptr, int n, bool is_file_bigendian)
00186 {
00187 #ifdef LITTLEENDIAN
00188     if(is_file_bigendian)
00189     {
00190         reverse_double(ptr,n);
00191         fwrite(ptr,sizeof(double),n,f);
00192         reverse_double(ptr,n);
00193     }
00194     else
00195         fwrite(ptr,sizeof(double),n,f);
00196 #endif
00197 #ifdef BIGENDIAN
00198     if(is_file_bigendian)
00199         fwrite(ptr,sizeof(double),n,f);
00200     else
00201     {
00202         reverse_double(ptr,n);
00203         fwrite(ptr,sizeof(double),n,f);
00204         reverse_double(ptr,n);
00205     }
00206 #endif
00207 }
00208 
00209 void fwrite_double(FILE *f, const float* ptr, int n, bool is_file_bigendian)
00210 {
00211     double* dptr = new double[n];
00212     for(int i=0; i<n; i++)
00213         dptr[i] = double(ptr[i]);
00214     fwrite_double(f,dptr,n,is_file_bigendian);
00215     delete[] dptr;
00216 }
00217 
00218 // Functions to read from a file written in any representation
00219 
00220 void fread_int(FILE *f, int* ptr, int n, bool is_file_bigendian)
00221 {
00222     fread(ptr,sizeof(int),n,f);
00223 #ifdef LITTLEENDIAN
00224     if(is_file_bigendian)
00225         reverse_int(ptr,n);
00226 #endif
00227 #ifdef BIGENDIAN
00228     if(!is_file_bigendian)
00229         reverse_int(ptr,n);
00230 #endif
00231 }
00232 
00233 void fread_float(FILE *f, float* ptr, int n, bool is_file_bigendian)
00234 {
00235     fread(ptr,sizeof(float),n,f);
00236 #ifdef LITTLEENDIAN
00237     if(is_file_bigendian)
00238         reverse_float(ptr,n);
00239 #endif
00240 #ifdef BIGENDIAN
00241     if(!is_file_bigendian)
00242         reverse_float(ptr,n);
00243 #endif
00244 }
00245 
00246 void fread_float(FILE *f, double* ptr, int n, bool is_file_bigendian)
00247 {
00248     float* fptr = new float[n];
00249     fread_float(f,fptr,n,is_file_bigendian);
00250     for(int i=0; i<n; i++)
00251         ptr[i] = double(fptr[i]);
00252     delete[] fptr;
00253 }
00254 
00255 void fread_double(FILE *f, double* ptr, int n, bool is_file_bigendian)
00256 {
00257     fread(ptr,sizeof(double),n,f);
00258 #ifdef LITTLEENDIAN
00259     if(is_file_bigendian)
00260         reverse_double(ptr,n);
00261 #endif
00262 #ifdef BIGENDIAN
00263     if(!is_file_bigendian)
00264         reverse_double(ptr,n);
00265 #endif
00266 }
00267 
00268 void fread_double(FILE *f, float* ptr, int n, bool is_file_bigendian)
00269 {
00270     double* dptr = new double[n];
00271     fread_double(f,dptr,n,is_file_bigendian);
00272     for(int i=0; i<n; i++)
00273         ptr[i] = float(dptr[i]);
00274     delete[] dptr;
00275 }
00276 
00277 void fread_short(FILE *f, unsigned short* ptr, int n, bool is_file_bigendian)
00278 {
00279     fread(ptr,sizeof(unsigned short),n,f);
00280 #ifdef LITTLEENDIAN
00281     if(is_file_bigendian)
00282         reverse_ushort(ptr,n);
00283 #endif
00284 #ifdef BIGENDIAN
00285     if(!is_file_bigendian)
00286         reverse_ushort(ptr,n);
00287 #endif
00288 }
00289 
00290 void write_int(ostream& out, const int* ptr, int n, bool is_file_bigendian)
00291 {
00292 #ifdef LITTLEENDIAN
00293     if(is_file_bigendian)
00294     {
00295         reverse_int(ptr,n);
00296         out.write((char*)ptr,n*sizeof(int));
00297         reverse_int(ptr,n);
00298     }
00299     else
00300         out.write((char*)ptr,n*sizeof(int));
00301 #endif
00302 #ifdef BIGENDIAN
00303     if(is_file_bigendian)
00304         out.write((char*)ptr,n*sizeof(int));
00305     else
00306     {
00307         reverse_int(ptr,n);
00308         out.write((char*)ptr,n*sizeof(int));
00309         reverse_int(ptr,n);
00310     }
00311 #endif
00312 }
00313 
00314 void write_short(ostream& out, const short* ptr, int n, bool is_file_bigendian)
00315 {
00316 #ifdef LITTLEENDIAN
00317     if(is_file_bigendian)
00318     {
00319         reverse_short(ptr,n);
00320         out.write((char*)ptr,n*sizeof(short));
00321         reverse_short(ptr,n);
00322     }
00323     else
00324         out.write((char*)ptr,n*sizeof(short));
00325 #endif
00326 #ifdef BIGENDIAN
00327     if(is_file_bigendian)
00328         out.write((char*)ptr,n*sizeof(short));
00329     else
00330     {
00331         reverse_short(ptr,n);
00332         out.write((char*)ptr,n*sizeof(short));
00333         reverse_short(ptr,n);
00334     }
00335 #endif
00336 }
00337 
00338 void write_double(ostream& out, const double* ptr, int n, bool is_file_bigendian)
00339 {
00340 #ifdef LITTLEENDIAN
00341     if(is_file_bigendian)
00342     {
00343         reverse_double(ptr,n);
00344         out.write((char*)ptr,n*sizeof(double));
00345         reverse_double(ptr,n);
00346     }
00347     else
00348         out.write((char*)ptr,n*sizeof(double));
00349 #endif
00350 #ifdef BIGENDIAN
00351     if(is_file_bigendian)
00352         out.write((char*)ptr,n*sizeof(double));
00353     else
00354     {
00355         reverse_double(ptr,n);
00356         out.write((char*)ptr,n*sizeof(double));
00357         reverse_double(ptr,n);
00358     }
00359 #endif
00360 }
00361 
00362 
00363 void write_float(ostream& out, const float* ptr, int n, bool is_file_bigendian)
00364 {
00365 #ifdef LITTLEENDIAN
00366     if(is_file_bigendian)
00367     {
00368         reverse_float(ptr,n);
00369         out.write((char*)ptr,n*sizeof(float));
00370         reverse_float(ptr,n);
00371     }
00372     else
00373         out.write((char*)ptr,n*sizeof(float));
00374 #endif
00375 #ifdef BIGENDIAN
00376     if(is_file_bigendian)
00377         out.write((char*)ptr,n*sizeof(float));
00378     else
00379     {
00380         reverse_float(ptr,n);
00381         out.write((char*)ptr,n*sizeof(float));
00382         reverse_float(ptr,n);
00383     }
00384 #endif
00385 }
00386 
00387 
00388 // Functions to read from a file written in any representation
00389 
00390 void read_int(istream& in, int* ptr, int n, bool is_file_bigendian)
00391 {
00392     in.read((char *)ptr,n*sizeof(int));
00393 #ifdef LITTLEENDIAN
00394     if(is_file_bigendian)
00395         reverse_int(ptr,n);
00396 #endif
00397 #ifdef BIGENDIAN
00398     if(!is_file_bigendian)
00399         reverse_int(ptr,n);
00400 #endif
00401 }
00402 
00403 void read_short(istream& in, short* ptr, int n, bool is_file_bigendian)
00404 {
00405     in.read((char *)ptr,n*sizeof(short));
00406 #ifdef LITTLEENDIAN
00407     if(is_file_bigendian)
00408         reverse_short(ptr,n);
00409 #endif
00410 #ifdef BIGENDIAN
00411     if(!is_file_bigendian)
00412         reverse_short(ptr,n);
00413 #endif
00414 }
00415 
00416 void read_float(istream& in, float* ptr, int n, bool is_file_bigendian)
00417 {
00418     in.read((char *)ptr,n*sizeof(float));
00419 #ifdef LITTLEENDIAN
00420     if(is_file_bigendian)
00421         reverse_float(ptr,n);
00422 #endif
00423 #ifdef BIGENDIAN
00424     if(!is_file_bigendian)
00425         reverse_float(ptr,n);
00426 #endif
00427 }
00428 
00429 void read_double(istream& in, double* ptr, int n, bool is_file_bigendian)
00430 {
00431     in.read((char *)ptr,n*sizeof(double));
00432 #ifdef LITTLEENDIAN
00433     if(is_file_bigendian)
00434         reverse_double(ptr,n);
00435 #endif
00436 #ifdef BIGENDIAN
00437     if(!is_file_bigendian)
00438         reverse_double(ptr,n);
00439 #endif
00440 }
00441 
00442 
00443 
00444 } // end of namespace PLearn
00445 
00446 
00447 /*
00448   Local Variables:
00449   mode:c++
00450   c-basic-offset:4
00451   c-file-style:"stroustrup"
00452   c-file-offsets:((innamespace . 0)(inline-open . 0))
00453   indent-tabs-mode:nil
00454   fill-column:79
00455   End:
00456 */
00457 // 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