ADMB Documentation  11.1.1897
 All Classes Files Functions Variables Typedefs Friends Defines
gst_read.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: gst_read.cpp 1863 2014-04-08 00:13:37Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 #include <sys/stat.h>
00014 #include <fcntl.h>
00015 #include <string.h>
00016 
00017 #ifdef __TURBOC__
00018   #pragma hdrstop
00019   #include <iostream.h>
00020 #endif
00021 
00022 #ifdef __ZTC__
00023   #include <iostream.hpp>
00024 #endif
00025 
00026 #if defined (__WAT32__)
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #ifdef __SUN__
00033   #include <iostream.h>
00034   #include <fcntl.h>
00035   #include <sys/stat.h>
00036   #include <sys/types.h>
00037   #include <unistd.h>
00038   #ifdef _MSC_VER
00039     #define lseek _lseek
00040     #define  read _read
00041     #define write _write
00042     #define open _open
00043     #define close _close
00044   #endif
00045 #endif
00046 
00047 #ifdef __GNU__
00048   #if (__GNUC__ >3)
00049      #include <iostream>
00050      using namespace std;
00051   #else
00052     #include <iostream.h>
00053   #endif
00054   #include <fcntl.h>
00055   #include <sys/stat.h>
00056   #include <sys/types.h>
00057   #include <unistd.h>
00058 #endif
00059 
00060 #if defined(__NDPX__ )
00061   extern "C" {
00062     int lseek(int, int, int);
00063     int read(int, char*, int);
00064   };
00065 #endif
00066 
00067 #include <math.h>
00068 
00073 #     if defined(__GNU__)
00074   int  grad_stack::read_grad_stack_buffer(my_off_t& lpos)
00075 #     else
00076   int  grad_stack::read_grad_stack_buffer(long int& lpos)
00077 #     endif
00078   {
00079     // check to see if we are past the beginning of this file
00080     if (lpos < 0)
00081     {
00082       lpos = lseek(gradient_structure::
00083         GRAD_STACK1->_GRADFILE_PTR,0L,SEEK_SET);
00084       // get the previous file
00085       if (gradient_structure::GRAD_STACK1->decrement_current_gradfile_ptr()
00086                         < 0)
00087       {
00088         // there is no more file to read ... but perhaps the beginning of
00089         // the buffer has not been used
00090         if (ptr_first==true_ptr_first)
00091         {
00092           // break condition
00093           return 0;
00094         }
00095         else
00096         {
00097           ptr_first=true_ptr_first;
00098           length=true_length;
00099           return 1;
00100         }
00101       }
00102       else
00103       {
00104         // get the end of file for the first file
00105         end_pos = end_pos1;
00106         // repostion the first file to end of pointer
00107         lseek(_GRADFILE_PTR,end_pos,SEEK_SET);
00108       }
00109       // now back up the file one buffer size
00110       lpos = lseek(_GRADFILE_PTR,
00111          -((long int)(sizeof(grad_stack_entry)*length)),SEEK_CUR);
00112       if (lpos == -1L)
00113       {
00114         cerr << "Error positioning temporary gradient file "
00115              << gradient_structure::GRAD_STACK1->get_gradfile_name()
00116              << " after open.\n";
00117         ad_exit(1);
00118       }
00119     }
00120     #if !defined( __NDPX__) && !defined( __SUN__)
00121       int nread = read(_GRADFILE_PTR,ptr_first,
00122         ((long int)(sizeof(grad_stack_entry)*length)) );
00123     #else
00124       int nread = read(_GRADFILE_PTR,
00125         (char*)ptr_first,((long int)(sizeof(grad_stack_entry)*length)) );
00126     #endif
00127     ptr = ptr_first + length-1;
00128 
00129     if (nread == -1 )
00130     {
00131       perror("IO error trying to read temporary gradient file\n");
00132     }
00133     if (nread <((long int)(sizeof(grad_stack_entry)*length)) )
00134     {
00135      perror("End of file encountered trying to read temporary gradient file\n");
00136       cout << "Read " << nread << "bytes from temp. grad. file\n";
00137     }
00138     lpos = lseek(_GRADFILE_PTR,-((long int)(sizeof(grad_stack_entry)*length)),
00139                                                            SEEK_CUR);
00140     // no break condition
00141     return 1;
00142   }
00143