- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/Backup/DXGfxLib/StringUtil.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    00017 using System;
    00018 using System.Collections.Generic;
    00019 using System.Collections;
    00020 using System.Text;
    00021
    00022 namespace DXGfxLib
    00023 {
    00030     public class StringUtil
    00031     {
    00035         public StringUtil()
    00036         {
    00037         }
    00038
    00045         public static string GetExtension(string fileName)
    00046         {
    00047             // I know I am a paranoid man but I still prefer to check even if performances can be affected.
    00048             // Several years as support engineer deeply affected the way I program (not that I don't have any
    00049             // defect or bad habit, but I try to be cautious).
    00050             if (fileName != null)
    00051             {
    00052                 int lind = fileName.LastIndexOf(".");
    00053                 if (lind != -1)
    00054                 {
    00055                     return fileName.Remove(0, lind);
    00056                 }
    00057                 else
    00058                 {
    00059                     // if lind = -1 it means there was no . in the fileName so there is no
    00060                     // extention to return.
    00061                     return "";
    00062                 }
    00063             }
    00064             return null;
    00065         }
    00066
    00073         public static string GetName(string fileName)
    00074         {
    00075             if (fileName != null)
    00076             {
    00077                 int lind = fileName.LastIndexOf("\\");
    00078                 if (lind != -1)
    00079                 {
    00080                     return fileName.Remove(0, lind + 1);
    00081                 }
    00082                 else
    00083                 {
    00084                     // if lind = -1 it means there was no \ in the fileName so our fileName can already be considered
    00085                     // only a name of a file or of an entity.
    00086                     return fileName;
    00087                 }
    00088             }
    00089             return null;
    00090         }
    00091
    00098         public static string GetRelativePath(string basePath, string fileName)
    00099         {
    00100             if ((fileName != null) && (basePath != null))
    00101             {
    00102                 int lind = fileName.IndexOf(basePath);
    00103                 if (lind != -1)
    00104                 {
    00105                     return fileName.Remove(lind, basePath.Length);
    00106                 }
    00107                 else
    00108                 {
    00109                     // if lind = -1 it means the basePath is not  in fileName so our fileName can already be considered
    00110                     // a relative path. Hum not really so let's return null which means there was a problem!
    00111                     return null;
    00112                 }
    00113             }
    00114             return null;
    00115         }
    00116
    00123         public static string GetRelativePathWithoutStartingSlash(string basePath, string fileName)
    00124         {
    00125             if ((fileName != null) && (basePath != null))
    00126             {
    00127                 int lind = fileName.IndexOf(basePath);
    00128                 if (lind != -1)
    00129                 {
    00130                     return fileName.Remove(lind, basePath.Length+1);
    00131                 }
    00132                 else
    00133                 {
    00134                     // if lind = -1 it means the basePath is not  in fileName so our fileName can already be considered
    00135                     // a relative path. Hum not really so let's return null which means there was a problem!
    00136                     return null;
    00137                 }
    00138             }
    00139             return null;
    00140         }
    00141
    00148         public static string GetPath(string fileName)
    00149         {
    00150             if (fileName != null)
    00151             {
    00152                 int lind = fileName.LastIndexOf("\\");
    00153                 if (lind != -1)
    00154                 {
    00155                     return fileName.Remove(lind + 1, fileName.Length - (lind + 1));
    00156                 }
    00157                 else
    00158                 {
    00159                     // if lind = -1 it means there is a problem somewhere.
    00160                     return null;
    00161                 }
    00162             }
    00163             return null;
    00164         }
    00165
    00171         public static string GetPathWithoutTrailingSlash(string fileName)
    00172         {
    00173             if (fileName != null)
    00174             {
    00175                 int lind = fileName.LastIndexOf("\\");
    00176                 if (lind != -1)
    00177                 {
    00178                     return fileName.Remove(lind, fileName.Length - (lind));
    00179                 }
    00180                 else
    00181                 {
    00182                     // if lind = -1 it means there is a problem somewhere.
    00183                     return null;
    00184                 }
    00185             }
    00186             return null;
    00187         }
    00188
    00194         public static string AppendDirectorySeparator(string path)
    00195         {
    00196             return (string)(path + "\\");
    00197         }
    00198
    00205         public static string GetNewIndexedName(string name, Hashtable hash)
    00206         {
    00207             if (hash.Contains(name))
    00208             {
    00209                 int index = 1;
    00210                 while (hash.Contains(name + index))
    00211                     index++;
    00212
    00213                 return name + index;
    00214             }
    00215             else
    00216                 return name;
    00217         }
    00218     }
    00219 }