fix compose mess, and a number of 64 bit printf specs
[ardour.git] / libs / ardour / 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$
19 */
20
21 #include <cstdio>
22 #include <unistd.h>
23
24 #include <ardour/stateful.h>
25 #include <ardour/utils.h>
26 #include <pbd/xml++.h>
27
28 #include "i18n.h"
29
30 Stateful::Stateful ()
31 {
32         _extra_xml = 0;
33         _instant_xml = 0;
34 }
35
36 Stateful::~Stateful ()
37 {
38         // Do not delete _extra_xml.  The use of add_child_nocopy() 
39         // means it needs to live on indefinately.
40         delete _instant_xml;
41 }
42
43 void
44 Stateful::add_extra_xml (XMLNode& node)
45 {
46         if (_extra_xml == 0) {
47                 _extra_xml = new XMLNode ("extra");
48         }
49
50         _extra_xml->remove_nodes (node.name());
51         _extra_xml->add_child_nocopy (node);
52 }
53
54 XMLNode *
55 Stateful::extra_xml (const string& str)
56 {
57         if (_extra_xml == 0) {
58                 return 0;
59         }
60
61         const XMLNodeList& nlist = _extra_xml->children();
62         XMLNodeConstIterator i;
63
64         for (i = nlist.begin(); i != nlist.end(); ++i) {
65                 if ((*i)->name() == str) {
66                         return (*i);
67                 }
68         }
69
70         return 0;
71 }
72
73 void
74 Stateful::add_instant_xml (XMLNode& node, const string& dir)
75 {
76         if (_instant_xml == 0) {
77                 _instant_xml = new XMLNode ("instant");
78         }
79
80         _instant_xml->remove_nodes_and_delete (node.name());
81         _instant_xml->add_child_copy (node);
82         
83         XMLTree tree;
84         tree.set_filename(dir+"/instant.xml");
85
86         /* Important: the destructor for an XMLTree deletes
87            all of its nodes, starting at _root. We therefore
88            cannot simply hand it our persistent _instant_xml 
89            node as its _root, because we will lose it whenever
90            the Tree goes out of scope.
91
92            So instead, copy the _instant_xml node (which does 
93            a deep copy), and hand that to the tree.
94         */
95
96         XMLNode* copy = new XMLNode (*_instant_xml);
97         tree.set_root (copy);
98
99         if (!tree.write()) {
100                 error << string_compose(_("Error: could not write %1"), dir+"/instant.xml") << endmsg;
101         }
102 }
103
104 XMLNode *
105 Stateful::instant_xml (const string& str, const string& dir)
106 {
107         if (_instant_xml == 0) {
108                 string instant_file = dir + "/instant.xml";
109                 if (access(instant_file.c_str(), F_OK) == 0) {
110                         XMLTree tree;
111                         if (tree.read(dir+"/instant.xml")) {
112                                 _instant_xml = new XMLNode(*(tree.root()));
113                         } else {
114                                 warning << string_compose(_("Could not understand XML file %1"), instant_file) << endmsg;
115                                 return 0;
116                         }
117                 } else {
118                         return 0;
119                 }
120         }
121
122         const XMLNodeList& nlist = _instant_xml->children();
123         XMLNodeConstIterator i;
124
125         for (i = nlist.begin(); i != nlist.end(); ++i) {
126                 if ((*i)->name() == str) {
127                         return (*i);
128                 }
129         }
130
131         return 0;
132 }
133