ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
df_file2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <fvar.hpp>
00012 #include <fcntl.h>
00013 
00014 #ifdef _MSC_VER
00015   #define lseek _lseek
00016   #define  read _read
00017   #define write _write
00018 #else
00019   #include <iostream>
00020   using namespace std;
00021   #include <sys/stat.h>
00022   #include <sys/types.h>
00023   #include <unistd.h>
00024 #endif
00025 
00026 #if defined(__TURBOC__)
00027 #pragma hdrstop
00028 #include <iostream.h>
00029 #include <iomanip.h>
00030 #include <sys\stat.h>
00031 #endif
00032 
00033 #ifdef __ZTC__
00034 #include <iostream.hpp>
00035 #define S_IREAD 0000400
00036 #define S_IWRITE 0000200
00037 #endif
00038 
00039 #ifdef __NDPX__
00040 #define O_RDONLY 0
00041 #define O_WRONLY 1
00042 #define O_RDWR 2
00043   extern "C"
00044   {
00045     int lseek(int, int, int);
00046     int open(const char*, int);
00047     int creat(const char*, int);
00048     int close(int);
00049     int write(int, char*, int);
00050     int read(int, char*, int);
00051   };
00052 #endif
00053 
00054 #ifdef __SUN__
00055 #include <iostream.h>
00056 #include <sys/stat.h>
00057 #include <sys/types.h>
00058 #ifndef _MSC_VER
00059 #include <unistd.h>
00060 #endif
00061 #endif
00062 
00063 #include <stdlib.h>
00064 #include <stdio.h>
00065 #include <string.h>
00066 
00071 void DF_FILE::fread(const double& _x)
00072 {
00073   double& x = (double&)_x;
00074   const size_t num_bytes = sizeof(double);
00075   if (toffset < num_bytes)
00076   {
00077     off_t lpos = lseek(file_ptr,-((off_t)buff_size),SEEK_CUR);
00078     //cout << "In fread filepos = " << lpos << endl;
00079     read_cmpdif_stack_buffer(lpos);
00080     offset -= num_bytes;
00081     toffset = offset;
00082   }
00083   else
00084   {
00085     toffset-=num_bytes; //decrement the temporary offset count
00086   }
00087   memcpy(&x, buff+toffset, sizeof(double));
00088   offset=toffset;
00089 }
00090 
00095 void DF_FILE::fread(void* &x)
00096 {
00097   const size_t num_bytes = sizeof(void*);
00098   if (toffset < num_bytes)
00099   {
00100      off_t lpos = lseek(file_ptr,-((off_t)buff_size),SEEK_CUR);
00101     //cout << "In fread filepos = " << lpos << endl;
00102     read_cmpdif_stack_buffer(lpos);
00103     offset -= num_bytes;
00104     toffset = offset;
00105   }
00106   else
00107   {
00108     toffset-=num_bytes; //decrement the temporary offset count
00109   }
00110   memcpy(&x, buff+toffset, sizeof(void*));
00111   offset=toffset;
00112 }
00113 
00118 void DF_FILE::fwrite(const double x)
00119 {
00120 #ifdef NO_DERIVS
00121   if (gradient_structure::no_derivatives)
00122   {
00123     return;
00124   }
00125 #endif
00126   const size_t num_bytes = sizeof(double);
00127   toffset+=num_bytes; //increment the temporary offset count
00128   if (toffset>buff_end)
00129   {
00130     write_cmpdif_stack_buffer();
00131     toffset=num_bytes;
00132     offset=0;
00133   }
00134   memcpy(buff+offset, &x, sizeof(double));
00135   offset=toffset;
00136 }
00137 
00142 void DF_FILE::fread(const int& _x)
00143 {
00144   int& x = (int&)_x;
00145   const size_t num_bytes = sizeof(int);
00146   if (toffset < num_bytes)
00147   {
00148     off_t lpos = lseek(file_ptr, -((off_t)buff_size), SEEK_CUR);
00149     read_cmpdif_stack_buffer(lpos);
00150     offset -= num_bytes;
00151     toffset = offset;
00152   }
00153   else
00154   {
00155     toffset-=num_bytes; //decrement the temporary offset count
00156   }
00157   memcpy(&x, buff+toffset, sizeof(int));
00158   offset=toffset;
00159 }
00160 
00165 void DF_FILE::fwrite(const int& x)
00166 {
00167   #ifdef NO_DERIVS
00168     if (gradient_structure::no_derivatives)
00169     {
00170       return;
00171     }
00172   #endif
00173   const size_t num_bytes = sizeof(int);
00174   toffset+=num_bytes; //increment the temporary offset count
00175   if (toffset>buff_end)
00176   {
00177     write_cmpdif_stack_buffer();
00178     toffset=num_bytes;
00179     offset=0;
00180   }
00181   memcpy(buff+offset, &x, sizeof(int));
00182   offset=toffset;
00183 }
00184 
00189 void DF_FILE::fwrite(void * ptr)
00190 {
00191   #ifdef NO_DERIVS
00192     if (gradient_structure::no_derivatives)
00193     {
00194       return;
00195     }
00196   #endif
00197   const size_t num_bytes = sizeof(void*);
00198   toffset+=num_bytes; //increment the temporary offset count
00199   if (toffset>buff_end)
00200   {
00201     write_cmpdif_stack_buffer();
00202     toffset=num_bytes;
00203     offset=0;
00204   }
00205   memcpy(buff+offset, &ptr, sizeof(void*));
00206   offset=toffset;
00207 }