file.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <gsl/gsl>
23 
24 #include <fstream>
25 
26 #include <file.h>
27 #include <exception.h>
28 
30 file::read_text(const filename_type &filename) const
31 {
32  expects(!filename.empty());
33 
34  if (auto && handle = std::fstream(filename, std::ios_base::in))
35  return text_data(std::istreambuf_iterator<char>(handle),
36  std::istreambuf_iterator<char>());
37 
38  throw invalid_file(filename);
39 }
40 
42 file::read_binary(const filename_type &filename) const
43 {
44  expects(!filename.empty());
45 
46  if (auto && handle = std::fstream(filename, std::ios_base::in | std::ios_base::binary))
47  return binary_data(std::istreambuf_iterator<char>(handle),
48  std::istreambuf_iterator<char>());
49 
50  throw invalid_file(filename);
51 }
52 
53 void
54 file::write_text(const filename_type &filename, const text_data &data) const
55 {
56  expects(!filename.empty());
57  expects(!data.empty());
58 
59  if (auto && handle = std::fstream(filename, std::ios_base::out | std::ios_base::binary))
60  {
61  std::copy(data.begin(), data.end(), std::ostreambuf_iterator<char>(handle));
62  return;
63  }
64 
65  throw invalid_file(filename);
66 }
67 
68 void
69 file::write_binary(const filename_type &filename, const binary_data &data) const
70 {
71  expects(!filename.empty());
72  expects(!data.empty());
73 
74  if (auto && handle = std::fstream(filename, std::ios_base::out | std::ios_base::binary))
75  {
76  std::copy(data.begin(), data.end(), std::ostreambuf_iterator<char>(handle));
77  return;
78  }
79 
80  throw invalid_file(filename);
81 }
expects(rbx)
int64_t unsigned long void * data
#define invalid_file(a)
Definition: exception.h:223
virtual void write_binary(const filename_type &filename, const binary_data &data) const
Definition: file.cpp:69
std::string text_data
Definition: file.h:39
virtual text_data read_text(const filename_type &filename) const
Definition: file.cpp:30
std::string filename_type
Definition: file.h:41
virtual void write_text(const filename_type &filename, const text_data &data) const
Definition: file.cpp:54
virtual binary_data read_binary(const filename_type &filename) const
Definition: file.cpp:42
std::vector< char > binary_data
Definition: file.h:40