PLearn 0.1
pl_NSPR_io.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // pl_NSPR_io.cc
00004 // Copyright (C) 2005 Pascal Vincent
00005 //
00006 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 
00036  
00037 
00038 /* *******************************************************      
00039  * AUTHORS: Pascal Vincent
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00045 #include "pl_NSPR_io.h"
00046 #include <plearn/base/byte_order.h>   
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 // Functions to read from a file written in any representation
00052 
00053 void PR_Read_int(PRFileDesc *f, int* ptr, int n, bool is_file_bigendian)
00054 {
00055     PR_Read(f,ptr,sizeof(int)*n);
00056 #ifdef LITTLEENDIAN
00057     if(is_file_bigendian)
00058         endianswap(ptr,n);
00059 #endif
00060 #ifdef BIGENDIAN
00061     if(!is_file_bigendian)
00062         endianswap(ptr,n);
00063 #endif
00064 }
00065 
00066 void PR_Read_float(PRFileDesc *f, float* ptr, int n, bool is_file_bigendian)
00067 {
00068     PR_Read(f,ptr,sizeof(float)*n);
00069 #ifdef LITTLEENDIAN
00070     if(is_file_bigendian)
00071         endianswap(ptr,n);
00072 #endif
00073 #ifdef BIGENDIAN
00074     if(!is_file_bigendian)
00075         endianswap(ptr,n);
00076 #endif
00077 }
00078 
00079 void PR_Read_float(PRFileDesc *f, double* ptr, int n, bool is_file_bigendian)
00080 {
00081     float* fptr = new float[n];
00082     PR_Read_float(f,fptr,n,is_file_bigendian);
00083     for(int i=0; i<n; i++)
00084         ptr[i] = double(fptr[i]);
00085     delete[] fptr;
00086 }
00087 
00088 void PR_Read_double(PRFileDesc *f, double* ptr, int n, bool is_file_bigendian)
00089 {
00090     PR_Read(f,ptr,sizeof(double)*n);
00091 #ifdef LITTLEENDIAN
00092     if(is_file_bigendian)
00093         endianswap(ptr,n);
00094 #endif
00095 #ifdef BIGENDIAN
00096     if(!is_file_bigendian)
00097         endianswap(ptr,n);
00098 #endif
00099 }
00100 
00101 void PR_Read_double(PRFileDesc *f, float* ptr, int n, bool is_file_bigendian)
00102 {
00103     double* dptr = new double[n];
00104     PR_Read_double(f,dptr,n,is_file_bigendian);
00105     for(int i=0; i<n; i++)
00106         ptr[i] = float(dptr[i]);
00107     delete[] dptr;
00108 }
00109 
00110 void PR_Read_short(PRFileDesc *f, unsigned short* ptr, int n, bool is_file_bigendian)
00111 {
00112     PR_Read(f,ptr,sizeof(unsigned short)*n);
00113 #ifdef LITTLEENDIAN
00114     if(is_file_bigendian)
00115         endianswap(ptr,n);
00116 #endif
00117 #ifdef BIGENDIAN
00118     if(!is_file_bigendian)
00119         endianswap(ptr,n);
00120 #endif
00121 }
00122 
00123 
00124 // Functions to write a file in any representation
00125 
00126 void PR_Write_int(PRFileDesc *f, const int* ptr, int n, bool is_file_bigendian)
00127 {
00128 #ifdef LITTLEENDIAN
00129     if(is_file_bigendian)
00130     {
00131         endianswap(const_cast<int*>(ptr),n);
00132         PR_Write(f,ptr,sizeof(int)*n);
00133         endianswap(const_cast<int*>(ptr),n);
00134     }
00135     else
00136         PR_Write(f,ptr,sizeof(int)*n);
00137 #endif
00138 #ifdef BIGENDIAN
00139     if(is_file_bigendian)
00140         PR_Write(f,ptr,sizeof(int)*n);
00141     else
00142     {
00143         endianswap(const_cast<int*>(ptr),n);
00144         PR_Write(f,ptr,sizeof(int)*n);
00145         endianswap(const_cast<int*>(ptr),n);
00146     }
00147 #endif
00148 }
00149 
00150 void PR_Write_float(PRFileDesc *f, const float* ptr, int n, bool is_file_bigendian)
00151 {
00152 #ifdef LITTLEENDIAN
00153     if(is_file_bigendian)
00154     {
00155         endianswap(const_cast<float*>(ptr),n);
00156         PR_Write(f,ptr,sizeof(float)*n);
00157         endianswap(const_cast<float*>(ptr),n);
00158     }
00159     else
00160         PR_Write(f,ptr,sizeof(float)*n);
00161 #endif
00162 #ifdef BIGENDIAN
00163     if(is_file_bigendian)
00164         PR_Write(f,ptr,sizeof(float)*n);
00165     else
00166     {
00167         endianswap(const_cast<float*>(ptr),n);
00168         PR_Write(f,ptr,sizeof(float)*n);
00169         endianswap(const_cast<float*>(ptr),n);
00170     }
00171 #endif
00172 }
00173 
00174 void PR_Write_float(PRFileDesc *f, const double* ptr, int n, bool is_file_bigendian)
00175 {
00176     float* fptr = new float[n];
00177     for(int i=0; i<n; i++)
00178         fptr[i] = float(ptr[i]);
00179     PR_Write_float(f,fptr,n,is_file_bigendian);
00180     delete[] fptr;
00181 }
00182 
00183 void PR_Write_double(PRFileDesc *f, const double* ptr, int n, bool is_file_bigendian)
00184 {
00185 #ifdef LITTLEENDIAN
00186     if(is_file_bigendian)
00187     {
00188         endianswap(const_cast<double*>(ptr),n);
00189         PR_Write(f,ptr,sizeof(double)*n);
00190         endianswap(const_cast<double*>(ptr),n);
00191     }
00192     else
00193         PR_Write(f,ptr,sizeof(double)*n);
00194 #endif
00195 #ifdef BIGENDIAN
00196     if(is_file_bigendian)
00197         PR_Write(f,ptr,sizeof(double)*n);
00198     else
00199     {
00200         endianswap(const_cast<double*>(ptr),n);
00201         PR_Write(f,ptr,sizeof(double)*n);
00202         endianswap(const_cast<double*>(ptr),n);
00203     }
00204 #endif
00205 }
00206 
00207 void PR_Write_double(PRFileDesc *f, const float* ptr, int n, bool is_file_bigendian)
00208 {
00209     double* dptr = new double[n];
00210     for(int i=0; i<n; i++)
00211         dptr[i] = double(ptr[i]);
00212     PR_Write_double(f,dptr,n,is_file_bigendian);
00213     delete[] dptr;
00214 }
00215 
00216 
00217 } // end of namespace PLearn
00218 
00219 
00220 /*
00221   Local Variables:
00222   mode:c++
00223   c-basic-offset:4
00224   c-file-style:"stroustrup"
00225   c-file-offsets:((innamespace . 0)(inline-open . 0))
00226   indent-tabs-mode:nil
00227   fill-column:79
00228   End:
00229 */
00230 // 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