Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / find_session.cc
blobc635b86545ad442c457d76d6290f0c0e9fa76f91
1 #include <unistd.h>
2 #include <sys/stat.h>
4 #include <cstring>
5 #include <climits>
6 #include <cerrno>
8 #include <glibmm/miscutils.h>
10 #include "pbd/compose.h"
11 #include "pbd/error.h"
13 #include "ardour/session_utils.h"
14 #include "ardour/filename_extensions.h"
15 #include "ardour/utils.h"
17 #include "i18n.h"
19 using namespace std;
20 using namespace PBD;
22 int
23 ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
25 struct stat statbuf;
26 char buf[PATH_MAX+1];
28 isnew = false;
30 if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
31 error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
32 return -1;
35 str = buf;
37 /* check to see if it exists, and what it is */
39 if (stat (str.c_str(), &statbuf)) {
40 if (errno == ENOENT) {
41 isnew = true;
42 } else {
43 error << string_compose (_("cannot check session path %1 (%2)"), str, strerror (errno))
44 << endmsg;
45 return -1;
49 if (!isnew) {
51 /* it exists, so it must either be the name
52 of the directory, or the name of the statefile
53 within it.
56 if (S_ISDIR (statbuf.st_mode)) {
58 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
60 if (slash == string::npos) {
62 /* a subdirectory of cwd, so statefile should be ... */
64 string tmp = Glib::build_filename (str, str+statefile_suffix);
66 /* is it there ? */
68 if (stat (tmp.c_str(), &statbuf)) {
69 error << string_compose (_("cannot check statefile %1 (%2)"), tmp, strerror (errno))
70 << endmsg;
71 return -1;
74 path = str;
75 snapshot = str;
77 } else {
79 /* some directory someplace in the filesystem.
80 the snapshot name is the directory name
81 itself.
84 path = str;
85 snapshot = str.substr (slash+1);
89 } else if (S_ISREG (statbuf.st_mode)) {
91 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
92 string::size_type suffix;
94 /* remove the suffix */
96 if (slash != string::npos) {
97 snapshot = str.substr (slash+1);
98 } else {
99 snapshot = str;
102 suffix = snapshot.find (statefile_suffix);
104 if (suffix == string::npos) {
105 error << string_compose (_("%1 is not a snapshot file"), str) << endmsg;
106 return -1;
109 /* remove suffix */
111 snapshot = snapshot.substr (0, suffix);
113 if (slash == string::npos) {
115 /* we must be in the directory where the
116 statefile lives. get it using cwd().
119 char cwd[PATH_MAX+1];
121 if (getcwd (cwd, sizeof (cwd)) == 0) {
122 error << string_compose (_("cannot determine current working directory (%1)"), strerror (errno))
123 << endmsg;
124 return -1;
127 path = cwd;
129 } else {
131 /* full path to the statefile */
133 path = str.substr (0, slash);
136 } else {
138 /* what type of file is it? */
139 error << string_compose (_("unknown file type for session %1"), str) << endmsg;
140 return -1;
143 } else {
145 /* its the name of a new directory. get the name
146 as "dirname" does.
149 string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
151 if (slash == string::npos) {
153 /* no slash, just use the name, but clean it up */
155 path = legalize_for_path (str);
156 snapshot = path;
158 } else {
160 path = str;
161 snapshot = str.substr (slash+1);
165 return 0;