ADMB Documentation  11.1.1927
 All Classes Files Functions Variables Typedefs Friends Defines
gst_read.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: gst_read.cpp 1913 2014-04-18 00:18:43Z 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(long int& lpos)
00069 int grad_stack::read_grad_stack_buffer(my_off_t& lpos)
00070   {
00071     // check to see if we are past the beginning of this file
00072     if (lpos < 0)
00073     {
00074       lpos = lseek(gradient_structure::
00075         GRAD_STACK1->_GRADFILE_PTR,0L,SEEK_SET);
00076       // get the previous file
00077       if (gradient_structure::GRAD_STACK1->decrement_current_gradfile_ptr()
00078                         < 0)
00079       {
00080         // there is no more file to read ... but perhaps the beginning of
00081         // the buffer has not been used
00082         if (ptr_first==true_ptr_first)
00083         {
00084           // break condition
00085           return 0;
00086         }
00087         else
00088         {
00089           ptr_first=true_ptr_first;
00090           length=true_length;
00091           return 1;
00092         }
00093       }
00094       else
00095       {
00096         // get the end of file for the first file
00097         end_pos = end_pos1;
00098         // repostion the first file to end of pointer
00099         lseek(_GRADFILE_PTR,end_pos,SEEK_SET);
00100       }
00101       // now back up the file one buffer size
00102       lpos = lseek(_GRADFILE_PTR,
00103          -((long int)(sizeof(grad_stack_entry)*length)),SEEK_CUR);
00104       if (lpos == -1L)
00105       {
00106         cerr << "Error positioning temporary gradient file "
00107              << gradient_structure::GRAD_STACK1->get_gradfile_name()
00108              << " after open.\n";
00109         ad_exit(1);
00110       }
00111     }
00112     #if !defined( __NDPX__) && !defined( __SUN__)
00113       int nread = read(_GRADFILE_PTR,ptr_first,
00114         ((long int)(sizeof(grad_stack_entry)*length)) );
00115     #else
00116       int nread = read(_GRADFILE_PTR,
00117         (char*)ptr_first,((long int)(sizeof(grad_stack_entry)*length)) );
00118     #endif
00119     ptr = ptr_first + length-1;
00120 
00121     if (nread == -1 )
00122     {
00123       perror("IO error trying to read temporary gradient file\n");
00124     }
00125     if (nread <((long int)(sizeof(grad_stack_entry)*length)) )
00126     {
00127      perror("End of file encountered trying to read temporary gradient file\n");
00128       cout << "Read " << nread << "bytes from temp. grad. file\n";
00129     }
00130     lpos = lseek(_GRADFILE_PTR,-((long int)(sizeof(grad_stack_entry)*length)),
00131                                                            SEEK_CUR);
00132     // no break condition
00133     return 1;
00134   }