SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
io
BinaryStream.h
Go to the documentation of this file.
1
/*
2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 3 of the License, or
5
* (at your option) any later version.
6
*
7
* Written (W) 2009 Soeren Sonnenburg
8
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#ifndef __BINARYSTREAM_H__
12
#define __BINARYSTREAM_H__
13
14
#include <
shogun/io/SGIO.h
>
15
#include <
shogun/base/SGObject.h
>
16
#include <
shogun/lib/memory.h
>
17
18
#include <stdio.h>
19
20
namespace
shogun
21
{
27
template
<
class
T>
class
CBinaryStream
:
public
CSGObject
28
{
29
public
:
32
CBinaryStream
() :
CSGObject
()
33
{
34
rw
=NULL;
35
m_fname
=NULL;
36
fd
= NULL;
37
length
= 0;
38
39
set_generic<T>();
40
}
41
49
CBinaryStream
(
const
char
* fname,
const
char
* flag=
"r"
)
50
:
CSGObject
()
51
{
52
/* open_stream(bs.m_fname, bs.rw); */
53
SG_NOTIMPLEMENTED
54
set_generic<T>();
55
}
56
57
62
CBinaryStream
(
const
CBinaryStream
&bs)
63
{
64
open_stream
(bs.
m_fname
, bs.
rw
);
65
ASSERT
(
length
==bs.
length
)
66
set_generic<T>();
67
}
68
69
71
virtual
~CBinaryStream
()
72
{
73
close_stream
();
74
}
75
81
void
open_stream
(
const
char
* fname,
const
char
* flag=
"r"
)
82
{
83
rw
=get_strdup(flag);
84
m_fname
=get_strdup(fname);
85
86
fd
= fopen(fname, flag);
87
if
(!
fd
)
88
SG_ERROR
(
"Error opening file '%s'\n"
,
m_fname
)
89
90
struct
stat sb;
91
if
(stat(fname, &sb) == -1)
92
SG_ERROR
(
"Error determining file size of '%s'\n"
,
m_fname
)
93
94
length
= sb.st_size;
95
SG_DEBUG
(
"Opened file '%s' of size %ld byte\n"
, fname,
length
)
96
}
97
99
void
close_stream
()
100
{
101
SG_FREE(
rw
);
102
SG_FREE(
m_fname
);
103
if
(
fd
)
104
fclose(
fd
);
105
106
rw
=NULL;
107
m_fname
=NULL;
108
fd
= NULL;
109
length
= 0;
110
}
111
116
uint64_t
get_length
()
117
{
118
return
length
/
sizeof
(T);
119
}
120
125
uint64_t
get_size
()
126
{
127
return
length
;
128
}
129
141
char
*
get_line
(uint64_t& len, uint64_t& offs)
142
{
143
return
NULL;
144
}
145
150
int32_t
get_num_lines
()
151
{
152
return
0;
153
}
154
161
void
pre_buffer
(T* buffer,
long
index,
long
num)
const
162
{
163
ASSERT
(index>=0)
164
ASSERT
(num>=0)
165
166
if
(num==0)
167
return
;
168
169
if
(fseek(
fd
, ((
long
)
sizeof
(T))*((
long
) index), SEEK_SET) != 0)
170
SG_ERROR
(
"Error seeking to %ld (file '%s')\n"
,
sizeof
(T)*((int64_t) index),
m_fname
)
171
172
if
( fread(buffer,
sizeof
(T), num,
fd
) != num)
173
SG_ERROR
(
"Error calling fread (file '%s')\n"
,
m_fname
)
174
}
175
180
inline
T
read_next
()
const
181
{
182
T ptr;
183
if
( fread(&ptr,
sizeof
(T), 1,
fd
) != 1)
184
{
185
fprintf(stderr,
"Error calling fread (file '%s')\n"
,
m_fname
);
186
exit(1);
187
}
188
return
ptr;
189
}
190
196
inline
T
operator[]
(int32_t index)
const
197
{
198
199
if
(fseek(
fd
, ((
long
)
sizeof
(T))*((
long
) index), SEEK_SET) != 0)
200
SG_ERROR
(
"Error seeking to %ld (file '%s')\n"
,
sizeof
(T)*((int64_t) index),
m_fname
)
201
202
T ptr;
203
204
if
( fread(&ptr,
sizeof
(T), 1,
fd
) != 1)
205
SG_ERROR
(
"Error calling fread (file '%s')\n"
,
m_fname
)
206
207
return
ptr;
208
}
209
211
virtual
const
char
*
get_name
()
const
{
return
"BinaryStream"
; }
212
213
protected
:
215
FILE*
fd
;
217
uint64_t
length
;
219
char
*
rw
;
221
char
*
m_fname
;
222
};
223
}
224
#endif // BINARY_STREAM
SHOGUN
Machine Learning Toolbox - Documentation