Use std::transform with sys::basename in ARDOUR::get_file_names_no_extension
[ardour.git] / libs / ardour / session_state_utils.cc
1 /*
2         Copyright (C) 2007 Tim Mayberry 
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 #include <algorithm>
20
21 #include <pbd/compose.h>
22 #include <pbd/error.h>
23 #include <pbd/file_utils.h>
24
25 #include <ardour/session_state_utils.h>
26 #include <ardour/filename_extensions.h>
27
28 #include "i18n.h"
29
30 namespace ARDOUR {
31
32 bool
33 create_backup_file (const sys::path & file_path)
34 {
35         if (!sys::exists (file_path)) return false;
36
37         sys::path backup_path(file_path.to_string() + backup_suffix);
38
39         try
40         {
41                 sys::copy_file (file_path, backup_path);
42         }
43         catch(sys::filesystem_error& ex)
44         {
45                 error << string_compose (_("Unable to create a backup copy of file %1 (%2)"),
46                                 file_path.to_string(), ex.what())
47                         << endmsg;
48                 return false;
49         }
50         return true;
51 }
52
53 void
54 get_state_files_in_directory (const sys::path & directory_path,
55                 vector<sys::path> & result)
56 {
57         Glib::PatternSpec state_file_pattern('*' + string(statefile_suffix));
58         
59         find_matching_files_in_directory (directory_path, state_file_pattern,
60                         result);
61 }
62
63 vector<string>
64 get_file_names_no_extension (const vector<sys::path> & file_paths)
65 {
66         vector<string> result;
67
68         std::transform (file_paths.begin(), file_paths.end(),
69                         std::back_inserter(result), sys::basename);
70
71         sort (result.begin(), result.end(), std::less<string>());
72
73         return result;
74 }
75
76 } // namespace ARDOUR