forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 3
/
io.cc
46 lines (43 loc) · 1.2 KB
/
io.cc
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
/*
Copyright (c) 2007 Yahoo! Inc. All rights reserved. The copyrights
embodied in the content of this file are licensed under the BSD
(revised) open source license
*/
#include "io.h"
unsigned int buf_read(io_buf &i, char* &pointer, int n)
{//return a pointer to the next n bytes. n must be smaller than 2^24.
if (i.space.end + n <= i.space.end_array)
{
pointer = i.space.end;
i.space.end += n;
return n;
}
else if (n > (i.space.end_array - i.space.begin)) // the file is ending or ended.
{
pointer = i.space.end;
int ret = i.space.end_array - i.space.end;
i.space.end = i.space.end_array;
return ret;
}
else // Change locations and refill buffer.
{
int left = i.space.end_array - i.space.end;
memmove(i.space.begin, i.space.end, left);
i.fill(left);
i.space.end = i.space.begin;
return buf_read(i,pointer,n);
}
}
void buf_write(io_buf &o, char* &pointer, int n)
{//return a pointer to the next n bytes to write into.
if (o.space.end + n <= o.space.end_array)
{
pointer = o.space.end;
o.space.end += n;
}
else // Time to dump the file
{
o.flush();
buf_write (o, pointer,n);
}
}