/*
 * FilePathUtils.java
 *
 * Copyright (C) 2009-12 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

package org.rstudio.studio.client.common;

import org.rstudio.core.client.files.FileSystemItem;

public class FilePathUtils
{
   public static String friendlyFileName(String unfriendlyFileName)
   {
      int idx = unfriendlyFileName.lastIndexOf("/");
      if (idx < 0)
      {
         idx = unfriendlyFileName.lastIndexOf("\\");
      }
      return unfriendlyFileName.substring(
              idx + 1, unfriendlyFileName.length()).trim();
   }
   
   public static String normalizePath (String path, String workingDirectory)
   {
      // absolute paths don't need to be normalized
      if (path.startsWith(FileSystemItem.HOME_PREFIX) ||
          path.startsWith("/"))
      {
         return path;
      }
      
      // if the path appears to be relative, prepend the working directory
      // (consider: should we try to handle ..-style relative notation here?)
      String prefix = new String(workingDirectory + 
            (workingDirectory.endsWith("/") ? "" : "/"));
      String relative = new String(path.startsWith("./") ? 
            path.substring(2, path.length()) : path);
      
      return prefix + relative;
   }
}

@@ -1125,50 +1125,243 @@
  // 
-absolute paths don't need to be normalized
+Examine the path to see if it appears to be absolute. An absolute path%0A%09  // - begins with ~ , or %0A%09  // - begins with / (Unix-like systems), or%0A%09  // - begins with F:/ (Windows systems), where F is an alphabetic drive %0A%09  //   letter.
 %0A   
@@ -1413,16 +1413,17 @@
 EFIX) %7C%7C
+ 
 %0A       
@@ -1445,16 +1445,85 @@
 ith(%22/%22)
+ %7C%7C                        %0A          path.matches(%22%5E%5Ba-zA-Z%5D:%5C%5C/.*%22)
 )%0A      
@@ -1554,22 +1554,16 @@
       %7D%0A
-      
 %0A      /

fix detection of absolute paths for drive-letter paths on Windows