VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtXmlFileIo.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2026 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #ifndef DtXmlFileIo_H_
11 #define DtXmlFileIo_H_
12 
13 #include <makArchives/makArchives.h>
14 
15 #include <boost/archive/xml_iarchive.hpp>
16 #include <boost/archive/xml_oarchive.hpp>
17 #include <iostream>
18 #include <string>
19 #include <fstream>
20 
21 #include <boost/iostreams/device/mapped_file.hpp>
22 #include <boost/iostreams/stream.hpp>
23 #include <cstdio>
24 
25 namespace makArchives
26 {
27 
31  {
32 
33  public:
34 
38  template<typename T>
39  static bool write(const std::string& filePath, const T& t, const std::string& tagName = "Record")
40  {
41  // Write to a temp file first so that a crash or power loss during
42  // the write leaves the original file intact. The rename at the end
43  // is a metadata-only operation and is much shorter than the write.
44  std::string tmpPath = filePath + ".tmp";
45  try
46  {
47  {
48  std::ofstream ofs(tmpPath.c_str());
49  if(!ofs.is_open())
50  {
51  return false;
52  }
53  assert(ofs.good());
54 
55  boost::archive::xml_oarchive oa(ofs);
56  oa << boost::serialization::make_nvp(tagName.c_str(), t);
57  // xml_oarchive destructor appends closing tag
58  }
59  // On Windows std::rename fails when the destination exists,
60  // so remove it first. The window between the two calls is tiny.
61  std::remove(filePath.c_str());
62  if(std::rename(tmpPath.c_str(), filePath.c_str()) != 0)
63  {
64  std::remove(tmpPath.c_str());
65  return false;
66  }
67  }
68  catch(...)
69  {
70  std::remove(tmpPath.c_str());
71  return false;
72  }
73 
74  return true;
75  }
76 
80  template<typename T>
81  static bool read(const std::string& filePath, T& t, const std::string& tagName = "Record")
82  {
83  try
84  {
86  std::ifstream ifs(filePath.c_str());
87  if (!ifs.is_open())
88  {
89  return false;
90  }
91  assert(ifs.good());
92 
93  boost::archive::xml_iarchive ia(ifs);
95  ia >> boost::serialization::make_nvp(tagName.c_str(), t);
96  }
97  catch (boost::archive::archive_exception& ex)
98  {
99  std::cerr << "Archive Exception during deserializing:" << filePath << std::endl;
100  std::cerr << ex.what() << std::endl;
101  return false;
102  }
103  catch (...)
104  {
105  return false;
106  }
107 
108  return true;
109  }
110 
111  private:
112 
114  DtXmlFileIo();
115 
116  };
117 
118 }
119 
120 #endif
121 
Contains DLL export macros.
static bool read(const std::string &filePath, T &t, const std::string &tagName="Record")
Definition: DtXmlFileIo.h:81
A DtXmlFileIo has the static functions for reading and writing to an XML archive. ...
Definition: DtXmlFileIo.h:30
#define DT_DLL_MAKARCHIVES
Definition: makArchives.h:31
static bool write(const std::string &filePath, const T &t, const std::string &tagName="Record")
Definition: DtXmlFileIo.h:39

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)