Updated config.guess file
[ardour.git] / libs / pbd / stateful.cc
1 /*
2     Copyright (C) 2000-2001 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: stateful.cc 629 2006-06-21 23:01:03Z paul $
19 */
20
21 #include <unistd.h>
22
23 #include <pbd/stateful.h>
24 #include <pbd/xml++.h>
25 #include <pbd/error.h>
26
27 #include "i18n.h"
28
29 using namespace PBD;
30
31 Stateful::Stateful ()
32 {
33         _extra_xml = 0;
34         _instant_xml = 0;
35 }
36
37 Stateful::~Stateful ()
38 {
39         // Do not delete _extra_xml.  The use of add_child_nocopy() 
40         // means it needs to live on indefinately.
41
42         if (_instant_xml) {
43                 delete _instant_xml;
44         }
45 }
46
47 void
48 Stateful::add_extra_xml (XMLNode& node)
49 {
50         if (_extra_xml == 0) {
51                 _extra_xml = new XMLNode ("extra");
52         }
53
54         _extra_xml->remove_nodes (node.name());
55         _extra_xml->add_child_nocopy (node);
56 }
57
58 XMLNode *
59 Stateful::extra_xml (const string& str)
60 {
61         if (_extra_xml == 0) {
62                 return 0;
63         }
64
65         const XMLNodeList& nlist = _extra_xml->children();
66         XMLNodeConstIterator i;
67
68         for (i = nlist.begin(); i != nlist.end(); ++i) {
69                 if ((*i)->name() == str) {
70                         return (*i);
71                 }
72         }
73
74         return 0;
75 }
76
77 void
78 Stateful::add_instant_xml (XMLNode& node, const string& dir)
79 {
80         if (_instant_xml == 0) {
81                 _instant_xml = new XMLNode ("instant");
82         }
83
84         _instant_xml->remove_nodes_and_delete (node.name());
85         _instant_xml->add_child_nocopy (node);
86         
87         XMLTree tree;
88         tree.set_filename(dir+"/instant.xml");
89
90         /* Important: the destructor for an XMLTree deletes
91            all of its nodes, starting at _root. We therefore
92            cannot simply hand it our persistent _instant_xml 
93            node as its _root, because we will lose it whenever
94            the Tree goes out of scope.
95
96            So instead, copy the _instant_xml node (which does 
97            a deep copy), and hand that to the tree.
98         */
99
100         XMLNode* copy = new XMLNode (*_instant_xml);
101         tree.set_root (copy);
102
103         if (!tree.write()) {
104                 error << string_compose(_("Error: could not write %1"), dir+"/instant.xml") << endmsg;
105         }
106 }
107
108 XMLNode *
109 Stateful::instant_xml (const string& str, const string& dir)
110 {
111         if (_instant_xml == 0) {
112                 string instant_file = dir + "/instant.xml";
113                 if (access(instant_file.c_str(), F_OK) == 0) {
114                         XMLTree tree;
115                         if (tree.read(dir+"/instant.xml")) {
116                                 _instant_xml = new XMLNode(*(tree.root()));
117                         } else {
118                                 warning << string_compose(_("Could not understand XML file %1"), instant_file) << endmsg;
119                                 return 0;
120                         }
121                 } else {
122                         return 0;
123                 }
124         }
125
126         const XMLNodeList& nlist = _instant_xml->children();
127         XMLNodeConstIterator i;
128
129         for (i = nlist.begin(); i != nlist.end(); ++i) {
130                 if ((*i)->name() == str) {
131                         return (*i);
132                 }
133         }
134
135         return 0;
136 }
137