| 1 | // WindowsPathUtils.cs
|
|---|
| 2 | //
|
|---|
| 3 | // Copyright 2007 John Reilly
|
|---|
| 4 | //
|
|---|
| 5 | // This program is free software; you can redistribute it and/or
|
|---|
| 6 | // modify it under the terms of the GNU General Public License
|
|---|
| 7 | // as published by the Free Software Foundation; either version 2
|
|---|
| 8 | // of the License, or (at your option) any later version.
|
|---|
| 9 | //
|
|---|
| 10 | // This program is distributed in the hope that it will be useful,
|
|---|
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | // GNU General Public License for more details.
|
|---|
| 14 | //
|
|---|
| 15 | // You should have received a copy of the GNU General Public License
|
|---|
| 16 | // along with this program; if not, write to the Free Software
|
|---|
| 17 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 18 | //
|
|---|
| 19 | // Linking this library statically or dynamically with other modules is
|
|---|
| 20 | // making a combined work based on this library. Thus, the terms and
|
|---|
| 21 | // conditions of the GNU General Public License cover the whole
|
|---|
| 22 | // combination.
|
|---|
| 23 | //
|
|---|
| 24 | // As a special exception, the copyright holders of this library give you
|
|---|
| 25 | // permission to link this library with independent modules to produce an
|
|---|
| 26 | // executable, regardless of the license terms of these independent
|
|---|
| 27 | // modules, and to copy and distribute the resulting executable under
|
|---|
| 28 | // terms of your choice, provided that you also meet, for each linked
|
|---|
| 29 | // independent module, the terms and conditions of the license of that
|
|---|
| 30 | // module. An independent module is a module which is not derived from
|
|---|
| 31 | // or based on this library. If you modify this library, you may extend
|
|---|
| 32 | // this exception to your version of the library, but you are not
|
|---|
| 33 | // obligated to do so. If you do not wish to do so, delete this
|
|---|
| 34 | // exception statement from your version.
|
|---|
| 35 |
|
|---|
| 36 | using System;
|
|---|
| 37 |
|
|---|
| 38 | namespace ICSharpCode.SharpZipLib.Core
|
|---|
| 39 | {
|
|---|
| 40 | /// <summary>
|
|---|
| 41 | /// WindowsPathUtils provides simple utilities for handling windows paths.
|
|---|
| 42 | /// </summary>
|
|---|
| 43 | public abstract class WindowsPathUtils
|
|---|
| 44 | {
|
|---|
| 45 | /// <summary>
|
|---|
| 46 | /// Initializes a new instance of the <see cref="WindowsPathUtils"/> class.
|
|---|
| 47 | /// </summary>
|
|---|
| 48 | internal WindowsPathUtils()
|
|---|
| 49 | {
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /// <summary>
|
|---|
| 53 | /// Remove any path root present in the path
|
|---|
| 54 | /// </summary>
|
|---|
| 55 | /// <param name="path">A <see cref="string"/> containing path information.</param>
|
|---|
| 56 | /// <returns>The path with the root removed if it was present; path otherwise.</returns>
|
|---|
| 57 | /// <remarks>Unlike the <see cref="System.IO.Path"/> class the path isnt otherwise checked for validity.</remarks>
|
|---|
| 58 | public static string DropPathRoot(string path)
|
|---|
| 59 | {
|
|---|
| 60 | string result = path;
|
|---|
| 61 |
|
|---|
| 62 | if ( (path != null) && (path.Length > 0) ) {
|
|---|
| 63 | if ((path[0] == '\\') || (path[0] == '/')) {
|
|---|
| 64 | // UNC name ?
|
|---|
| 65 | if ((path.Length > 1) && ((path[1] == '\\') || (path[1] == '/'))) {
|
|---|
| 66 | int index = 2;
|
|---|
| 67 | int elements = 2;
|
|---|
| 68 |
|
|---|
| 69 | // Scan for two separate elements \\machine\share\restofpath
|
|---|
| 70 | while ((index <= path.Length) &&
|
|---|
| 71 | (((path[index] != '\\') && (path[index] != '/')) || (--elements > 0))) {
|
|---|
| 72 | index++;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | index++;
|
|---|
| 76 |
|
|---|
| 77 | if (index < path.Length) {
|
|---|
| 78 | result = path.Substring(index);
|
|---|
| 79 | }
|
|---|
| 80 | else {
|
|---|
| 81 | result = "";
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | else if ((path.Length > 1) && (path[1] == ':')) {
|
|---|
| 86 | int dropCount = 2;
|
|---|
| 87 | if ((path.Length > 2) && ((path[2] == '\\') || (path[2] == '/'))) {
|
|---|
| 88 | dropCount = 3;
|
|---|
| 89 | }
|
|---|
| 90 | result = result.Remove(0, dropCount);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return result;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|