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