Panda3D
config_dxml.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file config_dxml.cxx
10  * @author drose
11  * @date 2009-08-08
12  */
13 
14 #include "config_dxml.h"
15 #include "dconfig.h"
16 #include <stdio.h>
17 
18 BEGIN_PUBLISH
19 #include "tinyxml.h"
20 END_PUBLISH
21 
22 #if !defined(CPPPARSER) && !defined(LINK_ALL_STATIC) && !defined(BUILDING_PANDA_DXML)
23  #error Buildsystem error: BUILDING_PANDA_DXML not defined
24 #endif
25 
26 Configure(config_dxml);
27 NotifyCategoryDef(dxml, "");
28 
29 ConfigureFn(config_dxml) {
30  init_libdxml();
31 }
32 
33 /**
34  * Initializes the library. This must be called at least once before any of
35  * the functions or classes in this library can be used. Normally it will be
36  * called by the static initializers and need not be called explicitly, but
37  * special cases exist.
38  */
39 void
41  static bool initialized = false;
42  if (initialized) {
43  return;
44  }
45  initialized = true;
46 }
47 
48 BEGIN_PUBLISH
49 /**
50  * Reads an XML document from the indicated stream.
51  * @returns the document, or NULL on error.
52  */
54 read_xml_stream(std::istream &in) {
55  TiXmlDocument *doc = new TiXmlDocument;
56  in >> *doc;
57  if (in.fail() && !in.eof()) {
58  delete doc;
59  return nullptr;
60  }
61 
62  return doc;
63 }
64 END_PUBLISH
65 
66 BEGIN_PUBLISH
67 /**
68  * Writes an XML document to the indicated stream.
69  */
70 void
71 write_xml_stream(std::ostream &out, TiXmlDocument *doc) {
72  out << *doc;
73 }
74 END_PUBLISH
75 
76 BEGIN_PUBLISH
77 /**
78  * Writes an XML object to stdout, with formatting.
79  */
80 void
82  xnode->Print(stdout, 0);
83 }
84 END_PUBLISH
85 
86 BEGIN_PUBLISH
87 /**
88  * Writes an XML object to the indicated file, with formatting. Unfortunately
89  * the VFS cannot be supported; the file must be a real filename on disk.
90  */
91 void
92 print_xml_to_file(const Filename &filename, TiXmlNode *xnode) {
93  std::string os_name = filename.to_os_specific();
94 #ifdef _WIN32
95  FILE *file;
96  if (fopen_s(&file, os_name.c_str(), "w") != 0) {
97 #else
98  FILE *file = fopen(os_name.c_str(), "w");
99  if (file == nullptr) {
100 #endif
101  dxml_cat.error() << "Failed to open " << filename << " for writing\n";
102  }
103  xnode->Print(file, 0);
104  fclose(file);
105 }
106 END_PUBLISH
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BEGIN_PUBLISH TiXmlDocument * read_xml_stream(std::istream &in)
Reads an XML document from the indicated stream.
Definition: config_dxml.cxx:54
END_PUBLISH BEGIN_PUBLISH void print_xml_to_file(const Filename &filename, TiXmlNode *xnode)
Writes an XML object to the indicated file, with formatting.
Definition: config_dxml.cxx:92
END_PUBLISH BEGIN_PUBLISH void print_xml(TiXmlNode *xnode)
Writes an XML object to stdout, with formatting.
Definition: config_dxml.cxx:81
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
Always the top level node.
Definition: tinyxml.h:1386
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:423
END_PUBLISH BEGIN_PUBLISH void write_xml_stream(std::ostream &out, TiXmlDocument *doc)
Writes an XML document to the indicated stream.
Definition: config_dxml.cxx:71
void init_libdxml()
Initializes the library.
Definition: config_dxml.cxx:40
std::string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
Definition: filename.cxx:1123
virtual void Print(FILE *cfile, int depth) const =0
All TinyXml classes can print themselves to a filestream or the string class (TiXmlString in non-STL ...