Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / libs / ardour / template_utils.cc
1 #include <algorithm>
2 #include <cstring>
3
4 #include "pbd/filesystem.h"
5 #include "pbd/basename.h"
6 #include "pbd/pathscanner.h"
7 #include "pbd/xml++.h"
8
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"
14
15 using namespace std;
16 using namespace PBD;
17
18 namespace ARDOUR {
19
20 sys::path
21 system_template_directory ()
22 {
23         SearchPath spath(system_data_search_path());
24         spath.add_subdirectory_to_paths(templates_dir_name);
25
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);
28
29         if (i == spath.end()) return sys::path();
30
31         return *i;
32 }
33
34 sys::path
35 system_route_template_directory ()
36 {
37         SearchPath spath(system_data_search_path());
38         spath.add_subdirectory_to_paths(route_templates_dir_name);
39
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);
42
43         if (i == spath.end()) return sys::path();
44
45         return *i;
46 }
47
48 sys::path
49 user_template_directory ()
50 {
51         sys::path p(user_config_directory());
52         p /= templates_dir_name;
53
54         return p;
55 }
56
57 sys::path
58 user_route_template_directory ()
59 {
60         sys::path p(user_config_directory());
61         p /= route_templates_dir_name;
62
63         return p;
64 }
65
66 static bool
67 template_filter (const string &str, void */*arg*/)
68 {
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)));
72 }
73
74 void
75 find_session_templates (vector<TemplateInfo>& template_names)
76 {
77         vector<string *> *templates;
78         PathScanner scanner;
79         SearchPath spath (system_template_directory());
80         spath += user_template_directory ();
81
82         templates = scanner (spath.to_string(), template_filter, 0, false, true);
83
84         if (!templates) {
85                 cerr << "Found nothing along " << spath.to_string() << endl;
86                 return;
87         }
88
89         cerr << "Found " << templates->size() << " along " << spath.to_string() << endl;
90
91         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
92                 string fullpath = *(*i);
93
94                 XMLTree tree;
95
96                 if (!tree.read (fullpath.c_str())) {
97                         continue;
98                 }
99
100                 TemplateInfo rti;
101
102                 rti.name = basename_nosuffix (fullpath);
103                 rti.path = fullpath;
104
105                 template_names.push_back (rti);
106         }
107
108         delete templates;
109 }
110
111 void
112 find_route_templates (vector<TemplateInfo>& template_names)
113 {
114         vector<string *> *templates;
115         PathScanner scanner;
116         SearchPath spath (system_route_template_directory());
117         spath += user_route_template_directory ();
118
119         templates = scanner (spath.to_string(), template_filter, 0, false, true);
120
121         if (!templates) {
122                 return;
123         }
124
125         for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
126                 string fullpath = *(*i);
127
128                 XMLTree tree;
129
130                 if (!tree.read (fullpath.c_str())) {
131                         continue;
132                 }
133
134                 XMLNode* root = tree.root();
135
136                 TemplateInfo rti;
137
138                 rti.name = IO::name_from_state (*root->children().front());
139                 rti.path = fullpath;
140
141                 template_names.push_back (rti);
142         }
143
144         delete templates;
145 }
146
147 }