second (and hopefully) final part of changes to respond to header format changes...
[ArdourMidi.git] / libs / ardour / template_utils.cc
blobaa0a583af4d982771658c92786720ad4108e2448
1 #include <algorithm>
2 #include <cstring>
4 #include "pbd/filesystem.h"
5 #include "pbd/basename.h"
6 #include "pbd/pathscanner.h"
7 #include "pbd/xml++.h"
9 #include "ardour/template_utils.h"
10 #include "ardour/directory_names.h"
11 #include "ardour/filesystem_paths.h"
12 #include "ardour/filename_extensions.h"
13 #include "ardour/io.h"
15 using namespace std;
16 using namespace PBD;
18 namespace ARDOUR {
20 sys::path
21 system_template_directory ()
23 SearchPath spath(system_data_search_path());
24 spath.add_subdirectory_to_paths(templates_dir_name);
26 // just return the first directory in the search path that exists
27 SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
29 if (i == spath.end()) return sys::path();
31 return *i;
34 sys::path
35 system_route_template_directory ()
37 SearchPath spath(system_data_search_path());
38 spath.add_subdirectory_to_paths(route_templates_dir_name);
40 // just return the first directory in the search path that exists
41 SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
43 if (i == spath.end()) return sys::path();
45 return *i;
48 sys::path
49 user_template_directory ()
51 sys::path p(user_config_directory());
52 p /= templates_dir_name;
54 return p;
57 sys::path
58 user_route_template_directory ()
60 sys::path p(user_config_directory());
61 p /= route_templates_dir_name;
63 return p;
66 static bool
67 template_filter (const string &str, void */*arg*/)
69 cerr << "Checking into " << str << " using " << template_suffix << endl;
70 return (str.length() > strlen(template_suffix) &&
71 str.find (template_suffix) == (str.length() - strlen (template_suffix)));
74 void
75 find_session_templates (vector<TemplateInfo>& template_names)
77 vector<string *> *templates;
78 PathScanner scanner;
79 SearchPath spath (system_template_directory());
80 spath += user_template_directory ();
82 templates = scanner (spath.to_string(), template_filter, 0, false, true);
84 if (!templates) {
85 cerr << "Found nothing along " << spath.to_string() << endl;
86 return;
89 cerr << "Found " << templates->size() << " along " << spath.to_string() << endl;
91 for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
92 string fullpath = *(*i);
94 XMLTree tree;
96 if (!tree.read (fullpath.c_str())) {
97 continue;
100 TemplateInfo rti;
102 rti.name = basename_nosuffix (fullpath);
103 rti.path = fullpath;
105 template_names.push_back (rti);
108 delete templates;
111 void
112 find_route_templates (vector<TemplateInfo>& template_names)
114 vector<string *> *templates;
115 PathScanner scanner;
116 SearchPath spath (system_route_template_directory());
117 spath += user_route_template_directory ();
119 templates = scanner (spath.to_string(), template_filter, 0, false, true);
121 if (!templates) {
122 return;
125 for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
126 string fullpath = *(*i);
128 XMLTree tree;
130 if (!tree.read (fullpath.c_str())) {
131 continue;
134 XMLNode* root = tree.root();
136 TemplateInfo rti;
138 rti.name = IO::name_from_state (*root->children().front());
139 rti.path = fullpath;
141 template_names.push_back (rti);
144 delete templates;