ardour
template_utils.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 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 */
19 
20 #include <algorithm>
21 #include <cstring>
22 
23 #include <glibmm.h>
24 
25 #include "pbd/basename.h"
26 #include "pbd/file_utils.h"
27 #include "pbd/stl_delete.h"
28 #include "pbd/xml++.h"
29 
30 #include "ardour/template_utils.h"
31 #include "ardour/directory_names.h"
34 #include "ardour/search_paths.h"
35 #include "ardour/io.h"
36 
37 using namespace std;
38 using namespace PBD;
39 
40 namespace ARDOUR {
41 
42 std::string
44 {
45  return Glib::build_filename (user_config_directory(), templates_dir_name);
46 }
47 
48 std::string
50 {
51  return Glib::build_filename (user_config_directory(), route_templates_dir_name);
52 }
53 
54 static bool
55 template_filter (const string &str, void* /*arg*/)
56 {
57  if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
58  return false;
59  }
60 
61  return true;
62 }
63 
64 static bool
65 route_template_filter (const string &str, void* /*arg*/)
66 {
67  if (str.find (template_suffix) == str.length() - strlen (template_suffix)) {
68  return true;
69  }
70 
71  return false;
72 }
73 
74 string
75 session_template_dir_to_file (string const & dir)
76 {
77  return Glib::build_filename (dir, Glib::path_get_basename(dir) + template_suffix);
78 }
79 
80 
81 void
82 find_session_templates (vector<TemplateInfo>& template_names)
83 {
84  vector<string> templates;
85 
87 
88  if (templates.empty()) {
89  cerr << "Found nothing along " << template_search_path().to_string() << endl;
90  return;
91  }
92 
93  cerr << "Found " << templates.size() << " along " << template_search_path().to_string() << endl;
94 
95  for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
96  string file = session_template_dir_to_file (*i);
97 
98  XMLTree tree;
99 
100  if (!tree.read (file.c_str())) {
101  continue;
102  }
103 
104  TemplateInfo rti;
105 
106  rti.name = basename_nosuffix (*i);
107  rti.path = *i;
108 
109  template_names.push_back (rti);
110  }
111 }
112 
113 void
114 find_route_templates (vector<TemplateInfo>& template_names)
115 {
116  vector<string> templates;
117 
119 
120  if (templates.empty()) {
121  return;
122  }
123 
124  for (vector<string>::iterator i = templates.begin(); i != templates.end(); ++i) {
125  string fullpath = *i;
126 
127  XMLTree tree;
128 
129  if (!tree.read (fullpath.c_str())) {
130  continue;
131  }
132 
133  XMLNode* root = tree.root();
134 
135  TemplateInfo rti;
136 
137  rti.name = IO::name_from_state (*root->children().front());
138  rti.path = fullpath;
139 
140  template_names.push_back (rti);
141  }
142 }
143 
144 }
void find_paths_matching_filter(vector< string > &result, const Searchpath &paths, bool(*filter)(const string &, void *), void *arg, bool pass_fullpath, bool return_fullpath, bool recurse)
Definition: file_utils.cc:260
static bool route_template_filter(const string &str, void *)
string session_template_dir_to_file(string const &dir)
LIBARDOUR_API const char *const route_templates_dir_name
Definition: Beats.hpp:239
const XMLNodeList & children(const std::string &str=std::string()) const
Definition: xml++.cc:329
Definition: xml++.h:55
LIBARDOUR_API PBD::Searchpath template_search_path()
LIBARDOUR_API std::string user_config_directory(int version=-1)
LIBARDOUR_API std::string user_route_template_directory()
XMLNode * root() const
Definition: xml++.h:62
Definition: amp.h:29
LIBARDOUR_API std::string user_template_directory()
bool read()
Definition: xml++.h:71
void find_session_templates(vector< TemplateInfo > &template_names)
LIBARDOUR_API const char *const templates_dir_name
LIBARDOUR_API const char *const template_suffix
static bool template_filter(const string &str, void *)
LIBPBD_API Glib::ustring basename_nosuffix(Glib::ustring)
void find_route_templates(vector< TemplateInfo > &template_names)
Definition: xml++.h:95
LIBPBD_TEMPLATE_MEMBER_API const std::string to_string() const
Definition: search_path.cc:99
Definition: debug.h:30
void find_files_matching_filter(vector< string > &result, const Searchpath &paths, bool(*filter)(const string &, void *), void *arg, bool pass_fullpath, bool return_fullpath, bool recurse)
Definition: file_utils.cc:271
LIBARDOUR_API PBD::Searchpath route_template_search_path()