MADNESS  version 0.9
textfsar.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_WORLD_TEXTFSAR_H__INCLUDED
37 #define MADNESS_WORLD_TEXTFSAR_H__INCLUDED
38 
41 
42 #include <fstream>
43 #include <cstring>
44 #include <madness/world/archive.h>
45 
46 namespace madness {
47  namespace archive {
48 
51  mutable std::ofstream os;
52 
53  public:
54  TextFstreamOutputArchive(const char* filename = 0,
55  std::ios_base::openmode mode=std::ios_base::binary | std::ios_base::out | std::ios_base::trunc)
56  {
57  if (filename)
58  open(filename, mode);
59  }
60 
61  template <class T>
62  void store_start_tag() const {
63  char tag[256];
64  unsigned char cookie = archive_typeinfo<T>::cookie;
65  sprintf(tag,"<t%d>", cookie);
66  os << tag << std::endl;
67  MAD_ARCHIVE_DEBUG(std::cout << "textarchive: tag = " << tag << std::endl);
68  }
69 
70  template <class T>
71  void store_end_tag() const {
72  char tag[256];
73  unsigned char cookie = archive_typeinfo<T>::cookie;
74  sprintf(tag,"</t%d>",cookie);
75  os << tag << std::endl;
76  }
77 
78  template <class T>
80  store(const T* t, long n) const {
81  for (long i=0; i<n; ++i)
82  os << t[i] << std::endl;
83  }
84 
85  void store(const char* t, long /*n*/) const;
86 
87  void store(const unsigned char* t, long n) const {
88  for (long i=0; i<n; ++i)
89  os << (unsigned int) t[i] << std::endl;
90  }
91 
92  void open(const char* filename, std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc);
93 
94  void close();
95 
96  void flush() {
97  if (os.is_open())
98  os.flush();
99  }
100 
102  close();
103  }
104  }; // class TextFstreamOutputArchive
105 
106 
109  private:
110  mutable std::ifstream is;
111 
112  // Eat EOL after each entry to enable char-by-char read of strings
113  void eat_eol() const;
114 
115  public:
116  TextFstreamInputArchive(const char* filename = 0,
117  std::ios_base::openmode mode = std::ios_base::in)
118  {
119  if (filename)
120  open(filename, mode);
121  }
122 
123  template <class T>
124  void check_start_tag(bool end=false) const {
125  char tag[256], ftag[256];
126  is.getline(ftag,256);
127  unsigned char cookie = archive_typeinfo<T>::cookie;
128  if (end)
129  sprintf(tag,"</t%d>",cookie);
130  else
131  sprintf(tag,"<t%d>",cookie);
132 
133  if (strcmp(tag,ftag) != 0) {
134  std::cout << "TextFstreamInputArchive: type mismatch: expected=" << tag
135  << " "
136  << archive_type_names[cookie]
137  << " "
138  << " got=" << ftag << std::endl;
139  MADNESS_EXCEPTION("TextFstreamInputArchive: check_tag: types do not match/corrupt file", 1);
140  }
141  }
142 
143  template <class T>
144  inline void check_end_tag() const {
145  check_start_tag<T>(true);
146  }
147 
148  template <class T>
150  load(T* t, long n) const {
151  for (long i=0; i<n; ++i) is >> t[i];
152  eat_eol();
153  }
154 
155  void load(unsigned char* t, long n) const;
156 
157  void load(char* t, long n) const;
158 
159  void open(const char* filename,
160  std::ios_base::openmode mode = std::ios_base::in);
161 
162  void close() {
163  is.close();
164  }
165  }; // class TextFstreamInputArchive
166 
167  template <class T>
169  static void preamble_store(const TextFstreamOutputArchive& ar) {
170  ar.store_start_tag<T>();
171  }
172  static inline void postamble_store(const TextFstreamOutputArchive& ar) {
173  ar.store_end_tag<T>();
174  }
175  }; // struct ArchivePrePostImpl<TextFstreamOutputArchive,T>
176 
177  template <class T>
179  static inline void preamble_load(const TextFstreamInputArchive& ar) {
180  ar.check_start_tag<T>();
181  }
182  static inline void postamble_load(const TextFstreamInputArchive& ar) {
183  ar.check_end_tag<T>();
184  }
185  }; // struct ArchivePrePostImpl<TextFstreamInputArchive,T>
186  }
187 }
188 
189 #endif // MADNESS_WORLD_TEXTFSAR_H__INCLUDED
Wraps an archive around a text file stream for output.
Definition: textfsar.h:50
void store_end_tag() const
Definition: textfsar.h:71
void close()
Definition: textfsar.cc:83
TextFstreamOutputArchive(const char *filename=0, std::ios_base::openmode mode=std::ios_base::binary|std::ios_base::out|std::ios_base::trunc)
Definition: textfsar.h:54
static void postamble_store(const TextFstreamOutputArchive &ar)
Definition: textfsar.h:172
Interface templates for the archives (serialization)
void check_end_tag() const
Definition: textfsar.h:144
TextFstreamInputArchive(const char *filename=0, std::ios_base::openmode mode=std::ios_base::in)
Definition: textfsar.h:116
Wraps an archive around a text file stream for input.
Definition: textfsar.h:108
Default implementation of pre/postamble.
Definition: archive.h:641
madness::enable_if< madness::is_serializable< T > >::type store(const T *t, long n) const
Definition: textfsar.h:80
void flush()
Definition: textfsar.h:96
void store(const unsigned char *t, long n) const
Definition: textfsar.h:87
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::out|std::ios_base::trunc)
Definition: textfsar.cc:63
void close()
Definition: textfsar.h:162
const char * archive_type_names[256]
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
madness::enable_if< madness::is_serializable< T > >::type load(T *t, long n) const
Definition: textfsar.h:150
Base class for output archives classes.
Definition: archive.h:583
Base class for input archives classes.
Definition: archive.h:576
void check_start_tag(bool end=false) const
Definition: textfsar.h:124
static void preamble_store(const TextFstreamOutputArchive &ar)
Definition: textfsar.h:169
const mpreal trunc(const mpreal &v)
Definition: mpreal.h:2618
#define MAD_ARCHIVE_DEBUG(s)
Definition: archive.h:438
~TextFstreamOutputArchive()
Definition: textfsar.h:101
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in)
Definition: textfsar.cc:137
void store_start_tag() const
Definition: textfsar.h:62
static void preamble_load(const TextFstreamInputArchive &ar)
Definition: textfsar.h:179
Definition: archive.h:471
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
enable_if from Boost for conditionally instantiating templates based on type
Definition: enable_if.h:60
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
static void postamble_load(const TextFstreamInputArchive &ar)
Definition: textfsar.h:182