mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / extra / yassl / taocrypt / src / file.cpp
blob0498038a04ba93ef5f7a86947b40a5ce227ab344
1 /*
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,
16 MA 02110-1301 USA.
19 /* file.cpp implements File Sources and Sinks
22 #include "runtime.hpp"
23 #include "file.hpp"
26 namespace TaoCrypt {
29 FileSource::FileSource(const char* fname, Source& source)
31 file_ = fopen(fname, "rb");
32 if (file_) get(source);
36 FileSource::~FileSource()
38 if (file_)
39 fclose(file_);
44 // return size of source from beginning or current position
45 word32 FileSource::size(bool use_current)
47 long current = ftell(file_);
48 long begin = current;
50 if (!use_current) {
51 fseek(file_, 0, SEEK_SET);
52 begin = ftell(file_);
55 fseek(file_, 0, SEEK_END);
56 long end = ftell(file_);
58 fseek(file_, current, SEEK_SET);
60 return end - begin;
64 word32 FileSource::size_left()
66 return size(true);
70 // fill file source from source
71 word32 FileSource::get(Source& source)
73 word32 sz(size());
74 if (source.size() < sz)
75 source.grow(sz);
77 size_t bytes = fread(source.buffer_.get_buffer(), 1, sz, file_);
79 if (bytes == 1)
80 return sz;
81 else
82 return 0;
86 FileSink::FileSink(const char* fname, Source& source)
88 file_ = fopen(fname, "wb");
89 if (file_) put(source);
93 FileSink::~FileSink()
95 if (file_)
96 fclose(file_);
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);
111 current_ = 0;
115 } // namespace