ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2f13.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  end_saved=0;
25  bptr=buffer;
26  eof_flag=0;
27  /*off_t pos=*/lseek(fp,0L,SEEK_CUR);
28  endof_file_ptr=lseek(fp,0L,SEEK_SET);
29  written_flag=0;
30 }
31 
36 {
37  nentries=0;
38  bufsize=0;
39  buffer=0;
40  true_buffer=0;
41  buffend=0;
42  bptr=0;
43  fp=-1;
44 }
45 
49 fixed_smartlist::fixed_smartlist(const size_t _bufsize,
50  const adstring& _filename):
51  endof_file_ptr(-1),
52  recend(NULL),
53  sbptr(NULL)
54 {
55  allocate(_bufsize,_filename);
56 }
57 void fixed_smartlist::allocate(const size_t _bufsize,const adstring& _filename)
58 {
59  nentries=_bufsize/sizeof(fixed_list_entry);
60  end_saved=0;
61  eof_flag=0;
62  noreadflag=0;
63  written_flag=0;
64  direction=0;
65  bufsize=_bufsize;
66  filename=_filename;
68  memset(true_buffer, 0, sizeof(fixed_list_entry) * (nentries + 2));
69  doubleptr=(double*)true_buffer;
73  bptr=buffer;
74  true_buffer->numbytes=5678;
75  //int(true_buffer->pf)=1234;
76  true_buffend->numbytes=9999;
77  //int(true_buffend->pf)=6666;
78 #ifdef _MSC_VER
79  fp=open((char*)(filename), O_RDWR | O_CREAT | O_TRUNC |
80  O_BINARY, S_IREAD | S_IWRITE);
81 #else
82  fp=open((char*)(filename), O_RDWR | O_CREAT | O_TRUNC |
83  O_BINARY, S_IRUSR | S_IWUSR);
84 #endif
85  if (fp < 0)
86  {
87  cerr << "Error trying to open file " << filename
88  << " in class fixed_smartlist " << endl;
89  ad_exit(1);
90  }
91  else
92  {
93  /*off_t pos=*/lseek(fp, 0L, SEEK_CUR);
94  }
95 }
96 
100 void fixed_smartlist::write(const size_t n)
101 {
102 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
103  #ifndef OPT_LIB
104  assert(n <= UINT_MAX);
105  #endif
106  ssize_t nw = ::write(fp,buffer,(unsigned int)n);
107 #else
108  ssize_t nw = ::write(fp, buffer, n);
109 #endif
110  if (nw <= -1 || (size_t)nw != n)
111  {
112  cerr << "Error writing to file " << filename << endl;
113  ad_exit(1);
114  }
115 }
116 
121 {
122  bptr=buffer;
123  eof_flag=0;
124  if (written_flag)
125  {
126  lseek(fp,0L,SEEK_SET);
127  // get the record size
128  unsigned int nbytes=0;
129  ssize_t ret = ::read(fp,&nbytes,sizeof(unsigned int));
130  assert(ret != -1);
131  if (nbytes > bufsize)
132  {
133  cerr << "Error -- record size in file seems to be larger than"
134  " the buffer it was created from " << endl
135  << " buffer size is " << bufsize << " record size is supposedly "
136  << nbytes << endl;
137  ad_exit(1);
138  }
139  // now read the record into the buffer
140  ret = ::read(fp,buffer,nbytes);
141  assert(ret != -1);
142 
143  // skip over file postion entry in file
144  // so we are ready to read second record
145 #ifdef OPT_LIB
146  lseek(fp, (off_t)sizeof(off_t), SEEK_CUR);
147 #else
148  ret = lseek(fp, (off_t)sizeof(off_t), SEEK_CUR);
149  assert(ret >= 0);
150 #endif
151  }
152 }
153 
159 {
160  end_saved=0;
161  eof_flag=0;
162  bptr=buffer;
163  //int nbytes=0;
164  written_flag=0;
165  lseek(fp,0L,SEEK_SET);
166  set_forward();
167 }
168 
173 void fixed_smartlist::check_buffer_size(const size_t nsize)
174 {
175  if ( bptr+nsize-1 > buffend)
176  {
178  {
179  read_buffer();
180  }
181  else
182  {
183  if (nsize>bufsize)
184  {
185  cout << "Need to increase buffsize in list" << endl;
186  ad_exit(1);
187  }
188  write_buffer();
189  }
190  }
191 }
196 {
197  if (written_flag)
198  {
199  if (end_saved)
200  {
201 #ifdef OPT_LIB
202  lseek(fp, 0L, SEEK_END);
203  lseek(fp, endof_file_ptr, SEEK_SET);
204 #else
205  off_t ret = lseek(fp, 0L, SEEK_END);
206  assert(ret >= 0);
207  ret = lseek(fp, endof_file_ptr, SEEK_SET);
208  assert(ret >= 0);
209 #endif
210  read_buffer();
211  set_recend();
212  }
213  }
214 }
215 
221 {
222  if (written_flag)
223  {
224  if (!end_saved)
225  {
227  end_saved=1;
228  }
229  }
230 }
231 
237 {
238  int _nbytes=adptr_diff(bptr,buffer);
239  if (_nbytes > 0)
240  {
241  const unsigned int nbytes = (unsigned int)_nbytes;
242 
243  written_flag=1;
244  // get the current file position
245  off_t pos=lseek(fp,0L,SEEK_CUR);
246 
247  // write the size of the next record into the file
248 #if defined(OPT_LIB) && !defined(_MSC_VER)
249  ::write(fp, &nbytes, sizeof(unsigned int));
250 #else
251  ssize_t ret = ::write(fp, &nbytes, sizeof(unsigned int));
252  assert(ret != -1);
253 #endif
254 
255  // write the record into the file
256  ssize_t nw = ::write(fp,buffer,nbytes);
257 #ifndef OPT_LIB
258  assert(nw != -1);
259 #endif
260  if ((size_t)nw < nbytes)
261  {
262  cerr << "Error writing to file " << filename << endl;
263  ad_exit(1);
264  }
265  // reset the pointer to the beginning of the buffer
266  bptr=buffer;
267 
268  // now write the previous file position into the file so we can back up
269  // when we want to.
270 #if defined(OPT_LIB) && !defined(_MSC_VER)
271  ::write(fp,&pos,sizeof(off_t));
272 #else
273  ret = ::write(fp,&pos,sizeof(off_t));
274  assert(ret != -1);
275 #endif
276 
277  endof_file_ptr=lseek(fp,0L,SEEK_CUR);
278  //cout << lseek(fp,0L,SEEK_CUR) << endl;
279  }
280 }
281 
287 {
288  int _nbytes=adptr_diff(bptr+1,buffer);
289  if (_nbytes > 0)
290  {
291  unsigned int nbytes = (unsigned int)_nbytes;
292  if (nbytes > bufsize)
293  {
294  cerr << "n bytes > bufsize in "
295  "fixed_smartlist::write_buffer(void) this can't happen!" << endl;
296  }
297  written_flag=1;
298  // get the current file position
299  off_t pos=lseek(fp,0L,SEEK_CUR);
300 
301  // write the size of the next record into the file
302  ssize_t ret = ::write(fp,&nbytes,sizeof(unsigned int));
303  assert(ret != -1);
304 
305  // write the record into the file
306  ssize_t nw=::write(fp,buffer,nbytes);
307 #ifndef OPT_LIB
308  assert(nw != -1);
309 #endif
310  if ((size_t)nw < nbytes)
311  {
312  cerr << "Error writing to file " << filename << endl;
313  ad_exit(1);
314  }
315  // reset the pointer to the beginning of the buffer
316  bptr=buffer;
317 
318  // now write the previous file position into the file so we can back up
319  // when we want to.
320  ret = ::write(fp,&pos,sizeof(off_t));
321  assert(ret != -1);
322 
323  endof_file_ptr=lseek(fp,0L,SEEK_CUR);
324  //cout << lseek(fp,0L,SEEK_CUR) << endl;
325  }
326 }
327 
333 {
334  if (!written_flag)
335  {
336  if (direction ==-1)
337  eof_flag=-1;
338  else
339  eof_flag=1;
340  }
341  else
342  {
343  off_t pos = 0;
344  if (direction ==-1) // we are going backwards
345  {
346  off_t ipos=lseek(fp,0L,SEEK_CUR);
347  if (ipos ==0)
348  {
349  eof_flag=-1;
350  return;
351  }
352  // offset of the begining of the record is at the end
353  // of the record
354  lseek(fp,-((off_t)sizeof(off_t)),SEEK_CUR);
355  ssize_t ret = read(fp,&pos,sizeof(off_t));
356  assert(ret != -1);
357  // back up to the beginning of the record (plus record size)
358  lseek(fp,pos,SEEK_SET);
359  //*(off_t*)(bptr)=lseek(fp,pos,SEEK_SET);
360  }
361  // get the record size
362  unsigned int nbytes = 0;
363  ssize_t result = ::read(fp,&nbytes,sizeof(unsigned int));
364  assert(result != -1);
365  if (nbytes > bufsize)
366  {
367  cerr << "Error -- record size in file seems to be larger than"
368  " the buffer it was created from " << endl
369  << " buffer size is " << bufsize << " record size is supposedly "
370  << nbytes << endl;
371  ad_exit(1);
372  }
373  // now read the record into the buffer
374  ssize_t nr = ::read(fp,buffer,nbytes);
375 #ifndef OPT_LIB
376  assert(nr != -1);
377 #endif
378  if ((size_t)nr != nbytes)
379  {
380  cerr << "Error reading -- should be " << nbytes << " got " << nr << endl;
381  ad_exit(1);
382  }
383 
384  // reset the pointer to the beginning of the buffer
385  bptr=buffer;
386  int ns=nbytes/(int)sizeof(fixed_list_entry);
387  recend=bptr+ns-1;
388  //cout << "Number of bytes read " << nr
389  // << " recend value = " << recend << endl;
390  if (direction ==-1) // we are going backwards
391  {
392  // backup the file pointer again
393 #ifdef OPT_LIB
394  lseek(fp,pos,SEEK_SET);
395 #else
396  off_t ret = lseek(fp, pos, SEEK_SET);
397  assert(ret >= 0);
398 #endif
399  // *(off_t*)(bptr)=lseek(fp,pos,SEEK_SET);
400  }
401  else // we are going forward
402  {
403  // skip over file postion entry in file
404 #ifdef OPT_LIB
405  lseek(fp,(off_t)sizeof(off_t),SEEK_CUR);
406 #else
407  off_t ret = lseek(fp, (off_t)sizeof(off_t), SEEK_CUR);
408  assert(ret >= 0);
409 #endif
410  }
411  }
412 }
413 
418 void memcpy(const fixed_smartlist& _list, void* p, const size_t nsize)
419 {
421  if (list.bptr+nsize-1 > list.buffend)
422  {
423  cerr << " Trying to write outside list buffer" << endl;
424  ad_exit(1);
425  }
426  memcpy(list.bptr,p,nsize);
427  list.bptr+=nsize;
428 }
429 
434 void memcpy(void* p, const fixed_smartlist& _list, const size_t nsize)
435 {
437  if (list.bptr+nsize-1 > list.buffend)
438  {
439  cerr << " Trying to write outside list buffer" << endl;
440  ad_exit(1);
441  }
442  memcpy(p,list.bptr,nsize);
443  list.bptr+=nsize;
444 }
445 
447 {
448  if (bptr-n<buffer)
449  {
450  if (bptr != buffer)
451  {
452  cerr << " Sanity error in fixed_smartlist::operator -= (int)" << endl;
453  ad_exit(1);
454  }
455  else
456  {
457  // get previous record from the file
458  read_buffer();
459  bptr=recend-n+1;
460  }
461  }
462  else
463  {
464  bptr-=n;
465  }
466 }
468 {
469  if (bptr-1<buffer)
470  {
471  if (bptr != buffer)
472  {
473  cerr << " Sanity error in fixed_smartlist::operator -= (int)" << endl;
474  ad_exit(1);
475  }
476  else
477  {
478  // get previous record from the file
479  read_buffer();
480  //bptr=recend+1;
481  bptr=recend;
482  }
483  }
484  else
485  {
486  bptr--;
487  }
488 }
489 
491 {
492  if ( bptr+nsize-1 > buffend)
493  {
495  {
496  read_buffer();
497  }
498  else
499  {
500  if ((unsigned int)nsize>bufsize)
501  {
502  cout << "Need to increase buffsize in list" << endl;
503  ad_exit(1);
504  }
505  write_buffer();
506  }
507  }
508  else
509  {
510  bptr+=nsize;
511  }
512 }
513 
519 {
520  if ( bptr==buffend)
521  {
523  {
524  read_buffer();
525  }
526  else
527  {
528  write_buffer();
529  }
530  }
531  else
532  {
533  bptr++;
534  }
535 }
536 
541 {
542  //rewind the file
543  off_t pos=lseek(fp,0L,SEEK_SET);
544  char buffer[50000];
545  int offset=0;
546  fixed_list_entry * b= (fixed_list_entry*) &(buffer[0]);
547  cout << b << endl;
548  ssize_t nw = 0;
549  do
550  {
551  unsigned int nbytes=0;
552  nw = ::read(fp,&nbytes,sizeof(unsigned int));
553  if (nw != -1)
554  {
555  nw = ::read(fp, buffer + offset, (size_t)nbytes);
556  if (nw != -1)
557  {
558  offset+=nbytes;
559  nw = ::read(fp, &pos, sizeof(off_t));
560  }
561  }
562  }
563  while(nw);
564 }
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
fixed_list_entry * recend
Definition: df1b2fun.h:623
off_t endof_file_ptr
Definition: df1b2fun.h:613
fixed_smartlist()
Default constructor.
Definition: df1b2f13.cpp:35
void rewind(void)
Rewind buffer.
Definition: df1b2f13.cpp:120
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
void write(const size_t)
Definition: df1b2f13.cpp:100
void allocate(const size_t bufsize, const adstring &filename)
Definition: df1b2f13.cpp:57
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
void restore_end(void)
Restore end.
Definition: df1b2f13.cpp:195
void reset(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: df1b2f13.cpp:22
void operator--(void)
Definition: df1b2f13.cpp:467
exitptr ad_exit
Definition: gradstrc.cpp:53
#define AD_ALLOCATE(ptr, type, n, classname)
Definition: df1b2fun.h:179
Description not yet available.
Definition: df1b2fun.h:745
size_t bufsize
Definition: df1b2fun.h:628
void operator+=(int)
Definition: df1b2f13.cpp:490
void initialize(void)
Description not yet available.
Definition: df1b2f13.cpp:158
double * doubleptr
Definition: df1b2fun.h:620
Description not yet available.
Definition: df1b2fun.h:602
prnstream & endl(prnstream &)
void operator++(void)
Description not yet available.
Definition: df1b2f13.cpp:518
#define O_BINARY
Definition: fvar.hpp:163
fixed_list_entry * buffer
Definition: df1b2fun.h:624
fixed_list_entry * true_buffer
Definition: df1b2fun.h:621
void read_buffer(void)
Description not yet available.
Definition: df1b2f13.cpp:332
void read_file(void)
Definition: df1b2f13.cpp:540
void set_forward(void)
Definition: df1b2fun.h:644
static int get_passnumber(void)
Definition: df1b2fun.h:296
void write_buffer(void)
Description not yet available.
Definition: df1b2f13.cpp:286
fixed_list_entry * buffend
Definition: df1b2fun.h:625
void check_buffer_size(const size_t)
Description not yet available.
Definition: df1b2f13.cpp:173
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
void write_buffer_one_less(void)
Description not yet available.
Definition: df1b2f13.cpp:236
Description not yet available.
Definition: df1b2fun.h:591
Description not yet available.
fixed_list_entry * true_buffend
Definition: df1b2fun.h:622
fixed_list_entry * bptr
Definition: df1b2fun.h:626
void save_end(void)
Description not yet available.
Definition: df1b2f13.cpp:220
void operator-=(int)
Definition: df1b2f13.cpp:446
void set_recend()
Definition: df1b2fun.h:632
size_t nentries
Definition: df1b2fun.h:611
int numbytes
Definition: df1b2fun.h:594
adstring filename
Definition: df1b2fun.h:629