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