GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
binary_parser.hpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 
24 #ifndef GRAPHLAB_BINARY_PARSER_HPP
25 #define GRAPHLAB_BINARY_PARSER_HPP
26 
27 #include <iostream>
28 #include <fstream>
29 
30 namespace graphlab {
31 
32  /**
33  * \ingroup util_internal
34  * A thin wrapper around ifstream to provide simplicity of reading
35  * of binary data.
36  * \see binary_output_stream
37  */
38  class binary_input_stream : public std::ifstream {
39  typedef std::ifstream base_type;
40  using base_type::bad;
41  public:
42  binary_input_stream(const char* fname) :
43  base_type(fname, std::ios::binary | std::ios::in) {
44  assert(bad() == false);
45  }
46 
47  /**
48  * Read an arbitrary type.
49  */
50  template<typename T> T read() {
51  T t;
52  base_type::read(reinterpret_cast<char*>(&t), sizeof(T));
53  if(bad()) {
54  std::cout << "Error reading file!" << std::endl;
55  assert(false);
56  }
57  return t;
58  }
59  /**
60  * Read an arbitrary type.
61  */
62  template<typename T> void read(T& ret) {
63  base_type::read(reinterpret_cast<char*>(&ret), sizeof(T));
64  if(bad()) {
65  std::cout << "Error reading file!" << std::endl;
66  assert(false);
67  }
68  }
69 
70  /**
71  * Read an arbitrary type.
72  */
73  template<typename T> void read_vector(std::vector<T>& ret) {
74  if(ret.empty()) return;
75  base_type::read(reinterpret_cast<char*>(&(ret[0])),
76  sizeof(T) * ret.size());
77  if(bad()) {
78  std::cout << "Error reading file!" << std::endl;
79  assert(false);
80  }
81  }
82  };
83 
84 
85 
86  /**
87  * \ingroup util_internal
88  * A thin wrapper around ifstream to provide simplicity of writing
89  * of binary data.
90  * \see binary_input_stream
91  */
92  class binary_output_stream : public std::ofstream {
93  typedef std::ofstream base_type;
94  using std::ofstream::bad;
95  public:
96  binary_output_stream(const char* fname) :
97  std::ofstream(fname, std::ios::binary | std::ios::out) {
98  assert(bad() == false);
99  }
100 
101  //! Write the arbitrary data type to file
102  template<typename T> void write(T t) {
103  base_type::write(reinterpret_cast<char*>(&t), sizeof(T));
104  if(bad()) {
105  std::cout << "Error writing file!" << std::endl;
106  assert(false);
107  }
108  }
109  }; // end of binary_output_stream
110 
111 
112 
113 }
114 
115 
116 
117 
118 #endif
119 
120 
121 
122