2 Copyright (C) 2000-2007 MySQL AB
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19 /* file.cpp implements File Sources and Sinks
22 #include "runtime.hpp"
29 FileSource::FileSource(const char* fname
, Source
& source
)
31 file_
= fopen(fname
, "rb");
32 if (file_
) get(source
);
36 FileSource::~FileSource()
44 // return size of source from beginning or current position
45 word32
FileSource::size(bool use_current
)
47 long current
= ftell(file_
);
51 fseek(file_
, 0, SEEK_SET
);
55 fseek(file_
, 0, SEEK_END
);
56 long end
= ftell(file_
);
58 fseek(file_
, current
, SEEK_SET
);
64 word32
FileSource::size_left()
70 // fill file source from source
71 word32
FileSource::get(Source
& source
)
74 if (source
.size() < sz
)
77 size_t bytes
= fread(source
.buffer_
.get_buffer(), 1, sz
, file_
);
86 FileSink::FileSink(const char* fname
, Source
& source
)
88 file_
= fopen(fname
, "wb");
89 if (file_
) put(source
);
100 // fill source from file sink
101 void FileSink::put(Source
& source
)
103 fwrite(source
.get_buffer(), 1, source
.size(), file_
);
107 // swap with other and reset to beginning
108 void Source::reset(ByteBlock
& otherBlock
)
110 buffer_
.Swap(otherBlock
);