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