tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
sdf.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2013 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_SDF_H
21 #define TENSOR_SDF_H
22 
23 #include <fstream>
24 #include <string>
25 #include <vector>
26 #include <tensor/tensor.h>
27 
28 namespace sdf {
29 
30  using namespace tensor;
31 
32  bool file_exists(const std::string &filename);
33  bool delete_file(const std::string &filename);
34  bool rename_file(const std::string &orig, const std::string &dest,
35  bool overwrite = true);
36  bool isdir(const std::string &filename);
37  bool make_directory(const std::string &dirname, int mode = 0777);
38 
39  class DataFile {
40  public:
41  enum file_tags {
42  TAG_RTENSOR = 0,
43  TAG_CTENSOR = 1,
44  TAG_RTENSOR_VECTOR = 2,
45  TAG_CTENSOR_VECTOR = 3
46  };
47 
48  enum endianness {
49  BIG_ENDIAN_FILE = 0,
50  LITTLE_ENDIAN_FILE = 1
51  };
52 
53  enum flags {
54  SDF_SHARED = 1, /* Append with exclusive access */
55  SDF_OVERWRITE = 0, /* Overwrite original file */
56  SDF_PARANOID = 4 /* Overwrite only when all operations have finished */
57  };
58 
59  protected:
60  static const size_t var_name_size = 64;
61  const char *_suffix;
62  std::string _actual_filename;
63  std::string _filename;
64  std::string _lock_filename;
65  int _flags;
66  int _lock;
67  bool _open;
68  static const enum endianness endian;
69 
70  DataFile(const std::string &a_filename, int flags = SDF_SHARED);
71  ~DataFile();
72  const char *tag_to_name(size_t tag);
73  void close();
74  bool is_open() { return _open; }
75  bool is_locked() { return _lock; }
76  const std::string &actual_filename() { return _actual_filename; }
77 
78  };
79 
80 
81  class OutDataFile : public DataFile {
82 
83  public:
84 
85  OutDataFile(const std::string &a_filename, int flags = SDF_SHARED);
86  ~OutDataFile();
87 
88  void dump(const int r, const std::string &name = "");
89  void dump(const size_t r, const std::string &name = "");
90  void dump(const double r, const std::string &name = "");
91  void dump(const cdouble r, const std::string &name = "");
92  void dump(const RTensor &t, const std::string &name = "");
93  void dump(const CTensor &t, const std::string &name = "");
94  void dump(const std::vector<RTensor> &t, const std::string &name = "");
95  void dump(const std::vector<CTensor> &t, const std::string &name = "");
96 
97  void close();
98 
99  private:
100 
101  std::ofstream _stream;
102 
103  void write_raw(const char *data, size_t n);
104  void write_raw(const int *data, size_t n);
105  void write_raw(const long *data, size_t n);
106  void write_raw(const size_t *data, size_t n);
107  void write_raw(const double *data, size_t n);
108  void write_raw(const cdouble *data, size_t n);
109 
110  template<typename t> void write_raw(t v) {
111  write_raw(&v, 1);
112  }
113 
114  template<class Vector> void dump_vector(const Vector &v);
115 
116  void write_header();
117  void write_variable_name(const std::string &name);
118  void write_tag(const std::string &name, size_t tag);
119  };
120 
121  class InDataFile : public DataFile {
122 
123  public:
124 
125  InDataFile(const std::string &a_filename, int flags = SDF_SHARED);
126 
127  void load(int *r, const std::string &name = "");
128  void load(size_t *r, const std::string &name = "");
129  void load(double *r, const std::string &name = "");
130  void load(cdouble *r, const std::string &name = "");
131  void load(RTensor *t, const std::string &name = "");
132  void load(CTensor *t, const std::string &name = "");
133  void load(std::vector<RTensor> *m, const std::string &name = "");
134  void load(std::vector<CTensor> *m, const std::string &name = "");
135 
136  void close();
137 
138  private:
139 
140  std::ifstream _stream;
141 
142  void read_raw(char *data, size_t n);
143  void read_raw(int *data, size_t n);
144  void read_raw(size_t *data, size_t n);
145  void read_raw(long *data, size_t n);
146  void read_raw(double *data, size_t n);
147  void read_raw(cdouble *data, size_t n);
148 
149  template<typename t> void read_raw(t &v) {
150  read_raw(&v, 1);
151  }
152 
153  template<class Vector> const Vector load_vector();
154 
155  size_t read_tag_code();
156  std::string read_variable_name();
157 
158  void read_header();
159  void read_tag(const std::string &record_name, size_t tag);
160  };
161 
162 } // namespace sdf
163 
164 #endif /* !__MPS_IO_H */
Real Tensor with elements of type "double".
Definition: tensor.h:349
Complex Tensor with elements of type "cdouble".
Definition: tensor.h:435