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