root/trunk/whisperlib/common/io/file/file_input_stream.cc

Revision 7, 3.2 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #include <sys/types.h>
33 #include "common/io/file/file_input_stream.h"
34 #include "common/base/log.h"
35
36 namespace io {
37
38 string FileInputStream::ReadFileOrDie(const char* filename) {
39   string s;
40   CHECK(TryReadFile(filename, &s));
41   return s;
42 }
43 bool FileInputStream::TryReadFile(const char* filename, string* content) {
44   io::File* const infile = io::File::TryOpenFile(filename);
45   if ( infile == NULL ) {
46     return false;
47   }
48   io::FileInputStream fis(infile);
49   int64 size = fis.Readable();
50   if ( size > kMaxInt32 ) {
51     return false;
52   }
53   string s;
54   return size == fis.ReadString(content, static_cast<int32>(size));
55 }
56
57 FileInputStream::FileInputStream(File* file,
58                                  common::ByteOrder byte_order)
59   : InputStream(byte_order, NULL, NULL),
60     file_(file) {
61 }
62 FileInputStream::~FileInputStream() {
63   delete file_;
64 }
65
66 int32 FileInputStream::Read(void* buffer, int32 len) {
67   return file_->Read(buffer, len);
68 }
69
70 int64 FileInputStream::Skip(int64 len) {
71   return file_->SetPosition(len, File::FILE_CUR);
72 }
73
74 int64 FileInputStream::Readable() const {
75   return file_->Size() - file_->Position();
76 }
77
78 bool FileInputStream::IsEos() const {
79   return file_->Size() <= file_->Position();
80 }
81
82 void FileInputStream::MarkerSet() {
83   read_mark_positions_.push_back(file_->Position());
84 }
85
86 void FileInputStream::MarkerRestore() {
87   CHECK(!read_mark_positions_.empty());
88   const int64 mark_position = read_mark_positions_.back();
89   read_mark_positions_.pop_back();
90   file_->SetPosition(mark_position, File::FILE_SET);
91 }
92
93 void FileInputStream::MarkerClear() {
94   CHECK(!read_mark_positions_.empty());
95   read_mark_positions_.pop_back();
96 }
97 }
Note: See TracBrowser for help on using the browser.