ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df_file2.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <fvar.hpp>
12 #include <fcntl.h>
13 
14 #ifndef _MSC_VER
15  #include <iostream>
16  #include <sys/stat.h>
17  #include <sys/types.h>
18  #include <unistd.h>
19 #endif
20 
21 #if defined(__TURBOC__)
22 #pragma hdrstop
23 #include <iostream.h>
24 #include <iomanip.h>
25 #include <sys\stat.h>
26 #endif
27 
28 #ifdef __ZTC__
29 #include <iostream.hpp>
30 #define S_IREAD 0000400
31 #define S_IWRITE 0000200
32 #endif
33 
34 #ifdef __NDPX__
35 #define O_RDONLY 0
36 #define O_WRONLY 1
37 #define O_RDWR 2
38  extern "C"
39  {
40  int LSEEK(int, int, int);
41  int open(const char*, int);
42  int creat(const char*, int);
43  int close(int);
44  int write(int, char*, int);
45  int read(int, char*, int);
46  };
47 #endif
48 
49 #ifdef __SUN__
50 #include <iostream.h>
51 #include <sys/stat.h>
52 #include <sys/types.h>
53 #ifndef _MSC_VER
54 #include <unistd.h>
55 #endif
56 #endif
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <string.h>
61 
66 void DF_FILE::fread(const double& _x)
67 {
68  double& x = (double&)_x;
69  constexpr size_t num_bytes = sizeof(double);
70  if (toffset < static_cast<OFF_T>(num_bytes))
71  {
72  OFF_T lpos = LSEEK(file_ptr, -static_cast<OFF_T>(buff_size), SEEK_CUR);
73  //cout << "In fread filepos = " << lpos << endl;
75  offset -= num_bytes;
76  toffset = offset;
77  }
78  else
79  {
80  //decrement the temporary offset count
81  toffset -= num_bytes;
82  }
83  memcpy(&x, buff + toffset, num_bytes);
84  offset = toffset;
85 }
86 
91 void DF_FILE::fread(void* &x)
92 {
93  constexpr size_t num_bytes = sizeof(void*);
94  if (toffset < static_cast<OFF_T>(num_bytes))
95  {
96  OFF_T lpos = LSEEK(file_ptr, -static_cast<OFF_T>(buff_size), SEEK_CUR);
97  //cout << "In fread filepos = " << lpos << endl;
99  offset -= num_bytes;
100  toffset = offset;
101  }
102  else
103  {
104  toffset-=num_bytes; //decrement the temporary offset count
105  }
106  memcpy(&x, buff + toffset, num_bytes);
107  offset=toffset;
108 }
109 
114 void DF_FILE::fwrite(const double x)
115 {
116 #ifdef NO_DERIVS
118  {
119  return;
120  }
121 #endif
122  constexpr size_t num_bytes = sizeof(double);
123  toffset += num_bytes; //increment the temporary offset count
124  if (toffset > buff_end)
125  {
127  toffset=num_bytes;
128  offset=0;
129  }
130  memcpy(buff + offset, &x, num_bytes);
131  offset=toffset;
132 }
138 void DF_FILE::fread(const int& _x)
139 {
140  int& x = (int&)_x;
141  constexpr size_t num_bytes = sizeof(int);
142  if (toffset < static_cast<OFF_T>(num_bytes))
143  {
144  OFF_T lpos = LSEEK(file_ptr, -static_cast<OFF_T>(buff_size), SEEK_CUR);
146  offset -= num_bytes;
147  toffset = offset;
148  }
149  else
150  {
151  //decrement the temporary offset count
152  toffset -= static_cast<OFF_T>(num_bytes);
153  }
154  memcpy(&x, buff + toffset, num_bytes);
155  offset = toffset;
156 }
162 void DF_FILE::fwrite(const int& x)
163 {
164  #ifdef NO_DERIVS
166  {
167  return;
168  }
169  #endif
170  constexpr size_t num_bytes = sizeof(int);
171  toffset+=num_bytes; //increment the temporary offset count
172  if (toffset>buff_end)
173  {
175  toffset=num_bytes;
176  offset=0;
177  }
178  memcpy(buff+offset, &x, num_bytes);
179  offset=toffset;
180 }
181 
186 void DF_FILE::fwrite(void * ptr)
187 {
188  #ifdef NO_DERIVS
190  {
191  return;
192  }
193  #endif
194  constexpr size_t num_bytes = sizeof(void*);
195  toffset+=num_bytes; //increment the temporary offset count
196  if (toffset>buff_end)
197  {
199  toffset=num_bytes;
200  offset=0;
201  }
202  memcpy(buff+offset, &ptr, num_bytes);
203  offset=toffset;
204 }
void read(const test_smartlist &, void *, int nsize)
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
#define x
size_t buff_size
#define LSEEK
Definition: fvar.hpp:75
void write_cmpdif_stack_buffer()
Description not yet available.
Definition: df_file.cpp:350
void read_cmpdif_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: df_file.cpp:328
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
#define OFF_T
Definition: fvar.hpp:74
void write(const test_smartlist &, void *, int nsize)
void fwrite(const void *s, const size_t num_bytes)
Reads num_bytes from s and writes to buffer.
Definition: df_file.cpp:296
void fread(void *s, const size_t num_bytes)
Reads num_bytes from buffer and copies to s.
Definition: df_file.cpp:270