ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
df_file2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df_file2.cpp 1904 2014-04-17 19:34:22Z johnoel $
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 unsigned int num_bytes=sizeof(double);
00075   if (toffset < num_bytes)
00076   {
00077     my_off_t lpos = lseek(file_ptr,-((long int) 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(const int& _x)
00096 {
00097   int& x = (int&) _x;
00098   const unsigned int num_bytes=sizeof(int);
00099   if (toffset < num_bytes)
00100   {
00101      my_off_t lpos = lseek(file_ptr,-((long int) buff_size),SEEK_CUR);
00102     //cout << "In fread filepos = " << lpos << endl;
00103     read_cmpdif_stack_buffer(lpos);
00104     offset -= num_bytes;
00105     toffset = offset;
00106   }
00107   else
00108   {
00109     toffset-=num_bytes; //decrement the temporary offset count
00110   }
00111   memcpy(&x, buff+toffset, sizeof(int));
00112   offset=toffset;
00113 }
00114 
00119 void DF_FILE::fread(void* &x)
00120 {
00121   const unsigned int num_bytes=sizeof(void*);
00122   if (toffset < num_bytes)
00123   {
00124      my_off_t lpos = lseek(file_ptr,-((long int) buff_size),SEEK_CUR);
00125     //cout << "In fread filepos = " << lpos << endl;
00126     read_cmpdif_stack_buffer(lpos);
00127     offset -= num_bytes;
00128     toffset = offset;
00129   }
00130   else
00131   {
00132     toffset-=num_bytes; //decrement the temporary offset count
00133   }
00134   memcpy(&x, buff+toffset, sizeof(void*));
00135   offset=toffset;
00136 }
00137 
00142 void DF_FILE::fwrite( CGNU_DOUBLE x)
00143 {
00144   #ifdef NO_DERIVS
00145     if (gradient_structure::no_derivatives)
00146     {
00147       return;
00148     }
00149   #endif
00150   const unsigned int num_bytes=sizeof(double);
00151   toffset+=num_bytes; //increment the temporary offset count
00152   if (toffset>buff_end)
00153   {
00154     write_cmpdif_stack_buffer();
00155     toffset=num_bytes;
00156     offset=0;
00157   }
00158   memcpy(buff+offset, &x, sizeof(double));
00159   offset=toffset;
00160 }
00161 
00166 void DF_FILE::fwrite(const int& x)
00167 {
00168   #ifdef NO_DERIVS
00169     if (gradient_structure::no_derivatives)
00170     {
00171       return;
00172     }
00173   #endif
00174   const unsigned int num_bytes=sizeof(int);
00175   toffset+=num_bytes; //increment the temporary offset count
00176   if (toffset>buff_end)
00177   {
00178     write_cmpdif_stack_buffer();
00179     toffset=num_bytes;
00180     offset=0;
00181   }
00182   memcpy(buff+offset, &x, sizeof(int));
00183   offset=toffset;
00184 }
00185 
00190 void DF_FILE::fwrite(void * ptr)
00191 {
00192   #ifdef NO_DERIVS
00193     if (gradient_structure::no_derivatives)
00194     {
00195       return;
00196     }
00197   #endif
00198   const unsigned int num_bytes=sizeof(void*);
00199   toffset+=num_bytes; //increment the temporary offset count
00200   if (toffset>buff_end)
00201   {
00202     write_cmpdif_stack_buffer();
00203     toffset=num_bytes;
00204     offset=0;
00205   }
00206   memcpy(buff+offset, &ptr, sizeof(void*));
00207   offset=toffset;
00208 }