ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2f10.cpp
Go to the documentation of this file.
1 
5 #include <df1b2fun.h>
6 #include <cassert>
7 
8 #ifdef _MSC_VER
9  #ifdef _M_X64
10  typedef __int64 ssize_t;
11  #else
12  typedef int ssize_t;
13  #endif
14 #else
15  #include <unistd.h>
16 #endif
17 
23 {
24  bptr=buffer;
25  eof_flag=0;
26  lseek(fp, 0L, SEEK_SET);
27  written_flag=0;
28  end_saved=0;
29 }
30 
35 {
36  bufsize=0;
37  buffer=0;
38  true_buffer=0;
39  buffend=0;
40  bptr=0;
41  fp=-1;
42 }
46 test_smartlist::test_smartlist(const size_t _bufsize,const adstring& _filename):
47  recend(NULL),
48  sbptr(NULL)
49 {
50  allocate(_bufsize,_filename);
51 }
52 
57 void test_smartlist::allocate(const size_t _bufsize,const adstring& _filename)
58 {
59  //cerr << "need to modify test_smartlist class for multibyte char" << endl;
60 #ifdef DEBUG
61  assert(sizeof(char) == 1);
62 #endif
63 
64  end_saved=0;
65  eof_flag=0;
66  noreadflag=0;
67  written_flag=0;
68  direction=0;
69  bufsize=_bufsize;
70  filename=_filename;
71  //AD_ALLOCATE(true_buffer,char,bufsize+2*sizeof(double),df1b2_gradlist)
72  if ((true_buffer=new char[(bufsize+2)*sizeof(double) ])==0)
73  {
74  cerr << "Allocation error in df1b2_gradlist" << endl;
75  ad_exit(1);
76  }
77  doubleptr=(double*)true_buffer;
78  true_buffend=true_buffer+bufsize+2*sizeof(double)-1;
79  buffer=true_buffer+sizeof(double);
80  *(double*)(true_buffer)=5678.9;
81  *(double*)(true_buffer+bufsize+sizeof(double))=9876.5;
82  //buffend=true_buffer+bufsize-1+sizeof(double);
84  bptr=buffer;
85 #ifdef _MSC_VER
86  fp=open((char*)(filename), O_RDWR | O_CREAT | O_TRUNC |
87  O_BINARY, S_IREAD | S_IWRITE);
88 #else
89  fp=open((char*)(filename), O_RDWR | O_CREAT | O_TRUNC |
90  O_BINARY, S_IRUSR | S_IWUSR);
91 #endif
92  if (fp == -1)
93  {
94  cerr << "Error trying to open file " << filename
95  << " in class test_smartlist " << endl;
96  ad_exit(1);
97  }
98 
99  /*off_t pos=*/lseek(fp,0L,SEEK_CUR);
100 }
101 
106 void test_smartlist::write(const size_t n)
107 {
108 #if defined(__MINGW64__) || (defined(_MSC_VER) && defined(_WIN64))
109  #ifdef DEBUG
110  assert(n <= UINT_MAX);
111  #endif
112  ssize_t nw = ::write(fp,buffer,(unsigned int)n);
113 #else
114  ssize_t nw = ::write(fp,buffer,n);
115 #endif
116  if (nw <= -1 || n != (size_t)nw)
117  {
118  cerr << "Error writing to file " << filename << endl;
119  ad_exit(1);
120  }
121 }
122 
128 {
129  bptr=buffer;
130  if (written_flag)
131  {
132  lseek(fp,0L,SEEK_SET);
133  // get the record size
134  unsigned int nbytes = 0;
135 #ifdef OPT_LIB
136  ::read(fp,&nbytes,sizeof(unsigned int));
137 #else
138  ssize_t ret = ::read(fp,&nbytes,sizeof(unsigned int));
139  assert(ret != -1);
140 #endif
141  if (nbytes > bufsize)
142  {
143  cerr << "Error -- record size in file seems to be larger than"
144  " the buffer it was created from " << endl
145  << " buffer size is " << bufsize << " record size is supposedly "
146  << nbytes << endl;
147  }
148 #ifdef OPT_LIB
149  ::read(fp,buffer,nbytes);
150 #else
151  // now read the record into the buffer
152  ret = ::read(fp,buffer,nbytes);
153  assert(ret != -1);
154 #endif
155 
156  // skip over file postion entry in file
157  // so we are ready to read second record
158 #ifdef OPT_LIB
159  lseek(fp, (off_t)sizeof(off_t), SEEK_CUR);
160 #else
161  ret = lseek(fp, (off_t)sizeof(off_t), SEEK_CUR);
162  assert(ret >= 0);
163 #endif
164  }
165 }
166 
172 {
173  end_saved=0;
174  bptr=buffer;
175  //int nbytes=0;
176  written_flag=0;
177  lseek(fp,0L,SEEK_SET);
178  set_forward();
179 }
180 
185 void test_smartlist::check_buffer_size(const size_t nsize)
186 {
187  if ( bptr+nsize-1 > buffend)
188  {
190  {
191  read_buffer();
192  }
193  else
194  {
195  if (nsize>bufsize)
196  {
197  cout << "Need to increase buffsize in list" << endl;
198  ad_exit(1);
199  }
200  write_buffer();
201  }
202  }
203 }
204 
210 {
211  if (written_flag)
212  {
213  if (end_saved)
214  {
215  read_buffer();
216  set_recend();
217  }
218  }
219 }
220 
226 {
227  if (written_flag)
228  {
229  if (!end_saved)
230  {
231  write_buffer();
232  end_saved=1;
233  }
234  }
235 }
236 
242 {
243  int _nbytes=adptr_diff(bptr,buffer);
244  if (_nbytes > 0)
245  {
246  const unsigned int nbytes = (unsigned int)_nbytes;
247 
248  written_flag=1;
249  // get the current file position
250  off_t pos=lseek(fp,0L,SEEK_CUR);
251 
252  // write the size of the next record into the file
253 #if defined(OPT_LIB) && !defined(_MSC_VER)
254  ::write(fp,&nbytes,sizeof(int));
255 #else
256  ssize_t ret = ::write(fp,&nbytes,sizeof(int));
257  assert(ret != -1);
258 #endif
259 
260  // write the record into the file
261  ssize_t nw=::write(fp,buffer,nbytes);
262  if (nw <= -1 || (unsigned int)nw != nbytes)
263  {
264  cerr << "Error writing to file " << filename << endl;
265  ad_exit(1);
266  }
267  // reset the pointer to the beginning of the buffer
268  bptr=buffer;
269 
270  // now write the previous file position into the file so we can back up
271  // when we want to.
272 #if defined(OPT_LIB) && !defined(_MSC_VER)
273  ::write(fp,&pos,sizeof(off_t));
274 #else
275  ret = ::write(fp,&pos,sizeof(off_t));
276  assert(ret != -1);
277 #endif
278 
279  //cout << lseek(fp,0L,SEEK_CUR) << endl;
280  }
281 }
282 
288 {
289  if (!written_flag)
290  {
291  if (direction ==-1)
292  eof_flag=-1;
293  else
294  eof_flag=1;
295  }
296  else
297  {
298  off_t pos = sizeof(off_t);
299  if (direction ==-1) // we are going backwards
300  {
301  // offset of the begining of the record is at the end
302  // of the record
303  lseek(fp,-pos,SEEK_CUR);
304 #ifdef OPT_LIB
305  ::read(fp, &pos, sizeof(off_t));
306 #else
307  ssize_t ret = read(fp,&pos,sizeof(off_t));
308  assert(ret != -1);
309 #endif
310  // back up to the beginning of the record (plus record size)
311  lseek(fp,pos,SEEK_SET);
312  //*(off_t*)(bptr)=lseek(fp,pos,SEEK_SET);
313  }
314  // get the record size
315  unsigned int nbytes = 0;
316 #ifdef OPT_LIB
317  ::read(fp,&nbytes,sizeof(unsigned int));
318 #else
319  ssize_t result = ::read(fp,&nbytes,sizeof(unsigned int));
320  assert(result != -1);
321 #endif
322  if (nbytes <= 0 || nbytes > bufsize)
323  {
324  cerr << "Error -- record size in file seems to be larger than"
325  " the buffer it was created from " << endl
326  << " buffer size is " << bufsize << " record size is supposedly "
327  << nbytes << endl;
328  ad_exit(1);
329  }
330  // now read the record into the buffer
331  ssize_t nr = ::read(fp,buffer,nbytes);
332  if (nr <= -1 || (size_t)nr != nbytes)
333  {
334  cerr << "Error reading -- should be " << nbytes << " got " << nr << endl;
335  ad_exit(1);
336  }
337  // reset the pointer to the beginning of the buffer
338  bptr=buffer;
339  recend=bptr+nbytes-1;
340  if (direction ==-1) // we are going backwards
341  {
342  // backup the file pointer again
343 #ifdef OPT_LIB
344  lseek(fp,pos,SEEK_SET);
345 #else
346  off_t ret = lseek(fp,pos,SEEK_SET);
347  assert(ret >= 0);
348 #endif
349  // *(off_t*)(bptr)=lseek(fp,pos,SEEK_SET);
350  }
351  else // we are going forward
352  {
353  // skip over file postion entry in file
354 #ifdef OPT_LIB
355  lseek(fp, pos, SEEK_CUR);
356 #else
357  off_t ret = lseek(fp, pos, SEEK_CUR);
358  assert(ret >= 0);
359 #endif
360  }
361  }
362 }
363 
367 void memcpy(test_smartlist& dest, void* source, const size_t nsize)
368 {
369 #ifdef DEBUG
370  //Trying to write outside list buffer
371  assert(dest.bptr + nsize - 1 <= dest.buffend);
372 #endif
373 
374  memcpy(dest.bptr, source, nsize);
375  dest.bptr += nsize;
376 }
377 
381 void memcpy(void* dest, test_smartlist& source, const size_t nsize)
382 {
383 #ifdef DEBUG
384  //Trying to write outside list buffer
385  assert(source.bptr + nsize - 1 <= source.buffend);
386 #endif
387 
388  memcpy(dest, source.bptr, nsize);
389  source.bptr += nsize;
390 }
391 
396 void test_smartlist::operator-=(const int n)
397 {
398  if (bptr-n<buffer)
399  {
400  if (bptr != buffer)
401  {
402  cerr << " Sanity error in test_smartlist::operator -= (int)" << endl;
403  ad_exit(1);
404  }
405  else
406  {
407  // get previous record from the file
408  read_buffer();
409  bptr=recend-n+1;
410  }
411  }
412  else
413  {
414  bptr-=n;
415  }
416 }
417 
422 void test_smartlist::operator+=(const int nsize)
423 {
424  if ( bptr+nsize-1 > buffend)
425  {
427  {
428  read_buffer();
429  }
430  else
431  {
432  if ((unsigned int)nsize>bufsize)
433  {
434  cout << "Need to increase buffsize in list" << endl;
435  ad_exit(1);
436  }
437  write_buffer();
438  }
439  }
440  else
441  {
442  bptr+=nsize;
443  }
444 }
449 {
450  end_saved=0;
451  eof_flag=0;
452  noreadflag=0;
453  written_flag=0;
454  direction=0;
455  bufsize=0;
456 
457  if (true_buffer)
458  {
459  delete [] true_buffer;
460  true_buffer=0;
461  }
462  true_buffend=0;
463  buffer=0;
464  buffend=0;
465  bptr=0;
466  off_t pos=lseek(fp,0L,SEEK_END);
467  int on1=-1;
468  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-fsize"))>-1)
469  {
471  {
472  *ad_comm::global_logfile << "size of file " << filename
473  << " = " << pos << endl;
474  }
475  }
476  close(fp);
477  fp=0;
478 #if defined (_MSC_VER)
479  remove(filename);
480 #else
481  unlink(filename);
482 #endif
483 }
char * bptr
Definition: df1b2fun.h:543
void read_buffer(void)
Description not yet available.
Definition: df1b2f10.cpp:287
void read(const test_smartlist &, void *, int nsize)
void check_buffer_size(const size_t)
Description not yet available.
Definition: df1b2f10.cpp:185
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
char * true_buffer
Definition: df1b2fun.h:538
static ofstream * global_logfile
Definition: fvar.hpp:8858
void write_buffer(void)
Description not yet available.
Definition: df1b2f10.cpp:241
static char ** argv
Definition: fvar.hpp:8866
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
void save_end(void)
Description not yet available.
Definition: df1b2f10.cpp:225
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize(void)
Description not yet available.
Definition: df1b2f10.cpp:171
void allocate(const size_t bufsize, const adstring &filename)
Description not yet available.
Definition: df1b2f10.cpp:57
char * buffer
Definition: df1b2fun.h:541
char * true_buffend
Definition: df1b2fun.h:539
void reset(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: df1b2f10.cpp:22
char * buffend
Definition: df1b2fun.h:542
prnstream & endl(prnstream &)
#define O_BINARY
Definition: fvar.hpp:163
adstring filename
Definition: df1b2fun.h:546
static int argc
Definition: fvar.hpp:8863
double * doubleptr
Definition: df1b2fun.h:537
void write(const size_t n)
Description not yet available.
Definition: df1b2f10.cpp:106
test_smartlist(void)
Default constructor.
Definition: df1b2f10.cpp:34
char * recend
Definition: df1b2fun.h:540
Description not yet available.
Definition: df1b2fun.h:527
static int get_passnumber(void)
Definition: df1b2fun.h:296
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
void operator+=(const int)
Description not yet available.
Definition: df1b2f10.cpp:422
void set_recend()
Definition: df1b2fun.h:553
size_t bufsize
Definition: df1b2fun.h:545
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
void operator-=(const int)
Description not yet available.
Definition: df1b2f10.cpp:396
Description not yet available.
void set_forward(void)
Definition: df1b2fun.h:565
~test_smartlist()
Destructor.
Definition: df1b2f10.cpp:448
int written_flag
Definition: df1b2fun.h:531
void restore_end(void)
Description not yet available.
Definition: df1b2f10.cpp:209
void rewind(void)
Description not yet available.
Definition: df1b2f10.cpp:127