File: mbl_file_data_wrapper.txx

package info (click to toggle)
vxl 1.17.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 153,280 kB
  • ctags: 105,123
  • sloc: cpp: 747,420; ansic: 209,130; fortran: 34,230; lisp: 14,915; sh: 6,187; python: 5,856; makefile: 340; perl: 294; xml: 160
file content (199 lines) | stat: -rw-r--r-- 4,244 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// This is mul/mbl/mbl_file_data_wrapper.txx
#ifndef mbl_file_data_wrapper_txx_
#define mbl_file_data_wrapper_txx_
//:
// \file

#include "mbl_file_data_wrapper.h"

#include <vcl_ios.h>
#include <vcl_iostream.h>
#include <vcl_cstdlib.h>

// constructor
template<class T>
mbl_file_data_wrapper<T>::mbl_file_data_wrapper(vcl_string path)
: bfs_(path)
{
  path_ = path;
  if (!bfs_)
  {
    vcl_cerr<<"ERROR: mbl_file_data_wrapper::constructor\n"
            <<"file stream failed\n";
    vcl_abort();
  }
  
  calc_data_size();
  
  if ( size() > 0 )
    reset();
}

//: Count number of items in file
template<class T>
void mbl_file_data_wrapper<T>::calc_data_size()
{
  unsigned long count=0;

  short version;
  if (!bfs_)
  {
     vcl_cerr<<"ERROR: mbl_file_data_wrapper::calc_data_size()\n"
             <<"file stream failed\n";
     vcl_abort();
  }


  //vcl_cout<<"count= "<<count<<vcl_endl;
  vsl_b_read(bfs_,version);
  T d;
  bool is_last;
  vsl_b_read(bfs_,is_last);
  while (!is_last)
  {
    vsl_b_read(bfs_,d);
    count++;
    //vcl_cout<<"count= "<<count<<vcl_endl;
    vsl_b_read(bfs_,is_last);
#if 0
    vcl_cout << " ***Results1 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
             << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl
             << " ***Results2 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
             << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl;
#endif
  }

  size_=count;
}


//: Default destructor
template<class T>
mbl_file_data_wrapper<T>::~mbl_file_data_wrapper()
{
}

//: return number of items in file
template<class T>
unsigned long mbl_file_data_wrapper<T>::size() const
{
  return size_;
}


//: Reset
template<class T>
void mbl_file_data_wrapper<T>::reset()
{
  bfs_.is().seekg(vsl_b_ostream::header_length, vcl_ios_beg);
#if 0
  vcl_cout << " ***Results3 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
           << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl
           << " ***Results4 " << bfs_.is().eof() << ' ' << bfs_.is().bad()
           << ' ' << bfs_.is().fail() << ' ' << bfs_.is().good() << vcl_endl;
#endif

  if (!bfs_)
  {
    vcl_cerr<<"ERROR: mbl_file_data_wrapper::reset()\n"
            <<"file stream failed\n";
    vcl_abort();
  }

  short version;
  vsl_b_read(bfs_,version);
  bool is_last;
  vsl_b_read(bfs_,is_last);
  if (is_last)
  {
    vcl_cerr<<"ERROR: mbl_file_data_wrapper::reset()\n"
            <<"appears to be no data in file\n";
    vcl_abort();
  }
  else
   vsl_b_read(bfs_,d_);

  index_=0;
}

//: Return current object
template<class T>
const T& mbl_file_data_wrapper<T>::current()
{
  // return current value
  return d_;
}

//: Read in next object
template<class T>
bool mbl_file_data_wrapper<T>::next()
{
  if (!bfs_)
  {
    vcl_cerr<<"ERROR: mbl_file_data_wrapper::next()\n"
            <<"file stream failed\n";
    vcl_abort();
  }

  bool is_last;
  vsl_b_read(bfs_,is_last);
  if (is_last)
  {
    reset();
    //vcl_cout<<"WARNING: Reached end of file, so wrapper has been reset!\n";
    return false;
  }
  else
  {
    vsl_b_read(bfs_,d_);
    index_++;
    return true;
  }
}

//: Return current index
//  First example has index 0
template<class T>
unsigned long mbl_file_data_wrapper<T>::index() const
{
  return index_;
}


#if 0

// an acceptable implementation is defined in mbl_data_wrapper base class!
// maybe just use that??

//: Move to element n
//  First example has index 0
template<class T>
void mbl_file_data_wrapper<T>::set_index(unsigned long n)
{
  // unsupported
  vcl_cout<<"mbl_file_data_wrapper<T>::set_index unsupported\n";
}

#endif


//: Create copy on heap and return base pointer
template<class T>
mbl_data_wrapper< T >* mbl_file_data_wrapper<T>::clone() const
{
  return new mbl_file_data_wrapper<T>(*this);
}

template <class T>
bool mbl_file_data_wrapper<T>::is_class(vcl_string const& s) const
{
  return s==is_a(); // no ref to parent's is_class() since that is pure virtual
}


#define MBL_FILE_DATA_WRAPPER_INSTANTIATE(T) \
VCL_DEFINE_SPECIALIZATION vcl_string mbl_file_data_wrapper<T >::is_a() const \
{ return vcl_string("mbl_file_data_wrapper<" #T ">"); } \
template class mbl_file_data_wrapper<T >

#endif // mbl_file_data_wrapper_txx_