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