-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
IOBuffer.cpp
218 lines (194 loc) · 4.29 KB
/
IOBuffer.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights
embodied in the content of this file are licensed under the BSD
(revised) open source license.
Copyright (c) 2011 Berlin Institute of Technology and Max-Planck-Society.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Shogun adjustments (w) 2011 Shashwat Lal Das
*/
#include <string.h>
#include <fcntl.h>
#include <shogun/io/IOBuffer.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/v_array.h>
using namespace shogun;
CIOBuffer::CIOBuffer()
{
init();
}
CIOBuffer::CIOBuffer(int fd)
{
init();
working_file = fd;
}
CIOBuffer::~CIOBuffer()
{
}
void CIOBuffer::init()
{
size_t s = 1 << 16;
space.reserve(s);
endloaded = space.begin;
working_file=-1;
}
void CIOBuffer::use_file(int fd)
{
working_file = fd;
}
int CIOBuffer::open_file(const char* name, char flag)
{
int ret=1;
switch(flag)
{
case 'r':
working_file = open(name, O_RDONLY|O_LARGEFILE);
break;
case 'w':
working_file = open(name, O_CREAT|O_TRUNC|O_WRONLY, 0666);
break;
default:
SG_ERROR("Unknown file operation. Something other than 'r'/'w' specified.\n")
ret = 0;
}
return ret;
}
void CIOBuffer::reset_file()
{
lseek(working_file, 0, SEEK_SET);
endloaded = space.begin;
space.end = space.begin;
}
void CIOBuffer::set(char *p)
{
space.end = p;
}
ssize_t CIOBuffer::read_file(void* buf, size_t nbytes)
{
return read(working_file, buf, nbytes);
}
size_t CIOBuffer::fill()
{
if (space.end_array - endloaded == 0)
{
size_t offset = endloaded - space.begin;
space.reserve(2 * (space.end_array - space.begin));
endloaded = space.begin+offset;
}
ssize_t num_read = read_file(endloaded, space.end_array - endloaded);
if (num_read >= 0)
{
endloaded = endloaded+num_read;
return num_read;
}
else
return 0;
}
ssize_t CIOBuffer::write_file(const void* buf, size_t nbytes)
{
return write(working_file, buf, nbytes);
}
void CIOBuffer::flush()
{
if (working_file>=0)
{
if (write_file(space.begin, space.index()) != (int) space.index())
SG_ERROR("Error, failed to write example!\n")
}
space.end = space.begin;
fsync(working_file);
}
bool CIOBuffer::close_file()
{
if (working_file < 0)
return false;
else
{
int r = close(working_file);
if (r < 0)
SG_ERROR("Error closing the file!\n")
return true;
}
}
ssize_t CIOBuffer::readto(char* &pointer, char terminal)
{
//Return a pointer to the bytes before the terminal. Must be less
//than the buffer size.
pointer = space.end;
while (pointer != endloaded && *pointer != terminal)
pointer++;
if (pointer != endloaded)
{
size_t n = pointer - space.end;
space.end = pointer+1;
pointer -= n;
return n;
}
else
{
if (endloaded == space.end_array)
{
size_t left = endloaded - space.end;
memmove(space.begin, space.end, left);
space.end = space.begin;
endloaded = space.begin+left;
pointer = endloaded;
}
if (fill() > 0)// more bytes are read.
return readto(pointer,terminal);
else //no more bytes to read, return nothing.
return 0;
}
}
void CIOBuffer::buf_write(char* &pointer, int n)
{
if (space.end + n <= space.end_array)
{
pointer = space.end;
space.end += n;
}
else // Time to dump the file
{
if (space.end != space.begin)
flush();
else // Array is short, so increase size.
{
space.reserve(2 * (space.end_array - space.begin));
endloaded = space.begin;
}
buf_write(pointer,n);
}
}
unsigned int CIOBuffer::buf_read(char* &pointer, int n)
{
// Return a pointer to the next n bytes.
// n must be smaller than the maximum size.
if (space.end + n <= endloaded)
{
pointer = space.end;
space.end += n;
return n;
}
else // out of bytes, so refill.
{
if (space.end != space.begin) //There exists room to shift.
{
// Out of buffer so swap to beginning.
int left = endloaded - space.end;
memmove(space.begin, space.end, left);
space.end = space.begin;
endloaded = space.begin+left;
}
if (fill() > 0)
return buf_read(pointer,n);// more bytes are read.
else
{
// No more bytes to read, return all that we have left.
pointer = space.end;
space.end = endloaded;
return endloaded - pointer;
}
}
}