Merge branch 'master' into windows
[ardour.git] / libs / ardour / find_session.cc
index 90a54b1390d3354d5156518611977ce7223a05f7..afcbe7393b0f50c32e06fb0e6630d16b9e44b808 100644 (file)
@@ -1,3 +1,22 @@
+/*
+    Copyright (C) 2012 Paul Davis 
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
 #include <unistd.h>
 #include <sys/stat.h>
 
 #include <climits>
 #include <cerrno>
 
+#include <glibmm/miscutils.h>
+
 #include "pbd/compose.h"
+#include "pbd/pathexpand.h"
 #include "pbd/error.h"
 
-#include "ardour/session_utils.h"
 #include "ardour/filename_extensions.h"
 #include "ardour/utils.h"
 
 using namespace std;
 using namespace PBD;
 
+namespace ARDOUR {
+
 int
-ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
+find_session (string str, string& path, string& snapshot, bool& isnew)
 {
        struct stat statbuf;
-       char buf[PATH_MAX+1];
 
        isnew = false;
 
-       if (!realpath (str.c_str(), buf) && (errno != ENOENT && errno != ENOTDIR)) {
-               error << string_compose (_("Could not resolve path: %1 (%2)"), buf, strerror(errno)) << endmsg;
-               return -1;
-       }
-
-       str = buf;
+       str = canonical_path (str);
 
        /* check to see if it exists, and what it is */
 
@@ -53,17 +70,13 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
                if (S_ISDIR (statbuf.st_mode)) {
 
-                       string::size_type slash = str.find_last_of ('/');
+                       string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
 
                        if (slash == string::npos) {
 
                                /* a subdirectory of cwd, so statefile should be ... */
 
-                               string tmp;
-                               tmp = str;
-                               tmp += '/';
-                               tmp += str;
-                               tmp += statefile_suffix;
+                               string tmp = Glib::build_filename (str, str+statefile_suffix);
 
                                /* is it there ? */
 
@@ -90,7 +103,7 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
                } else if (S_ISREG (statbuf.st_mode)) {
 
-                       string::size_type slash = str.find_last_of ('/');
+                       string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
                        string::size_type suffix;
 
                        /* remove the suffix */
@@ -148,7 +161,7 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
                   as "dirname" does.
                */
 
-               string::size_type slash = str.find_last_of ('/');
+               string::size_type slash = str.find_last_of (G_DIR_SEPARATOR);
 
                if (slash == string::npos) {
 
@@ -166,3 +179,5 @@ ARDOUR::find_session (string str, string& path, string& snapshot, bool& isnew)
 
        return 0;
 }
+
+}  // namespace ARDOUR