ADMB Documentation  11.1.2397
 All Classes Files Functions Variables Typedefs Friends Defines
gst_read.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: gst_read.cpp 2338 2014-09-13 09:51:05Z 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 #endif
00039 
00040 #ifdef _MSC_VER
00041   #define lseek _lseek
00042   #define read _read
00043   #define write _write
00044   #define open _open
00045   #define close _close
00046 #else
00047   #include <iostream>
00048   using namespace std;
00049   #include <fcntl.h>
00050   #include <sys/stat.h>
00051   #include <sys/types.h>
00052   #include <unistd.h>
00053 #endif
00054 
00055 #if defined(__NDPX__ )
00056   extern "C" {
00057     int lseek(int, int, int);
00058     int read(int, char*, int);
00059   };
00060 #endif
00061 
00062 #include <math.h>
00063 
00068 int grad_stack::read_grad_stack_buffer(off_t& lpos)
00069   {
00070     // check to see if we are past the beginning of this file
00071     if (lpos < 0)
00072     {
00073       lpos = lseek(gradient_structure::
00074         GRAD_STACK1->_GRADFILE_PTR,0L,SEEK_SET);
00075       // get the previous file
00076       if (gradient_structure::GRAD_STACK1->decrement_current_gradfile_ptr()
00077                         < 0)
00078       {
00079         // there is no more file to read ... but perhaps the beginning of
00080         // the buffer has not been used
00081         if (ptr_first==true_ptr_first)
00082         {
00083           // break condition
00084           return 0;
00085         }
00086         else
00087         {
00088           ptr_first=true_ptr_first;
00089           length=true_length;
00090           return 1;
00091         }
00092       }
00093       else
00094       {
00095         // get the end of file for the first file
00096         end_pos = end_pos1;
00097         // repostion the first file to end of pointer
00098         lseek(_GRADFILE_PTR,end_pos,SEEK_SET);
00099       }
00100       // now back up the file one buffer size
00101       lpos = lseek(_GRADFILE_PTR,
00102          -((off_t)(sizeof(grad_stack_entry)*length)),SEEK_CUR);
00103       if (lpos == -1L)
00104       {
00105         cerr << "Error positioning temporary gradient file "
00106              << gradient_structure::GRAD_STACK1->get_gradfile_name()
00107              << " after open.\n";
00108         ad_exit(1);
00109       }
00110     }
00111     int nread = read(_GRADFILE_PTR,
00112         (char*)ptr_first,((off_t)(sizeof(grad_stack_entry)*length)));
00113     ptr = ptr_first + length-1;
00114 
00115     if (nread == -1 )
00116     {
00117       perror("IO error trying to read temporary gradient file\n");
00118     }
00119     if (nread <((int)(sizeof(grad_stack_entry)*length)) )
00120     {
00121      perror("End of file encountered trying to read temporary gradient file\n");
00122       cout << "Read " << nread << "bytes from temp. grad. file\n";
00123     }
00124     lpos = lseek(_GRADFILE_PTR,-((off_t)(sizeof(grad_stack_entry)*length)),
00125                                                            SEEK_CUR);
00126     // no break condition
00127     return 1;
00128   }