[JEDI.NET-commits] main/run Jedi.IO.Paths.pas,1.3,1.4
Status: Pre-Alpha
Brought to you by:
jedi_mbe
From: Marcel B. <jed...@us...> - 2005-03-28 18:13:43
|
Update of /cvsroot/jedidotnet/main/run In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13540/main/run Modified Files: Jedi.IO.Paths.pas Log Message: * Added: CommonBase method to determine the common base path of two or more paths * Added MakeRelativeFrom method to determine the relative path of one path to another * Added: PathElements to retrieve the individual elements of a path (volume, directories, file name) as an array Index: Jedi.IO.Paths.pas =================================================================== RCS file: /cvsroot/jedidotnet/main/run/Jedi.IO.Paths.pas,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Jedi.IO.Paths.pas 14 Mar 2005 13:30:13 -0000 1.3 --- Jedi.IO.Paths.pas 28 Mar 2005 18:13:19 -0000 1.4 *************** *** 27,31 **** uses ! Jedi.System; {$REGION 'Path manipulations'} --- 27,35 ---- uses ! Jedi.System.SourceVersioning, ! Jedi.System.Strings, ! System.Collections, ! System.IO, ! System.Text; {$REGION 'Path manipulations'} *************** *** 62,65 **** --- 66,71 ---- strict protected class var SysConfig: PathConfig; + strict protected + class function CommonBaseImpl(paths: array of string; config: PathConfig): ArrayList; static; public class function Combine(paths: StringArray): string; overload; static; *************** *** 67,70 **** --- 73,80 ---- class function Combine(path1, path2: string): string; overload; static; class function Combine(path1, path2: string; config: PathConfig): string; overload; static; + class function CommonBase(paths: StringArray): string; overload; static; + class function CommonBase(paths: array of string; config: PathConfig): string; overload; static; + class function CommonBase(path1, path2: string): string; overload; static; + class function CommonBase(path1, path2: string; config: PathConfig): string; overload; static; class function EnsureEndingDirectorySeparator(path: string): string; overload; static; class function EnsureEndingDirectorySeparator(path: string; config: PathConfig): string; overload; static; *************** *** 77,82 **** --- 87,96 ---- class function IsVolumePath(path: string): Boolean; overload; static; class function IsVolumePath(path: string; config: PathConfig): Boolean; overload; static; + class function MakeRelativeFrom(inPath, basePath: string): string; overload; static; + class function MakeRelativeFrom(inPath, basePath: string; config: PathConfig): string; overload; static; class function Normalize(path: string): string; overload; static; class function Normalize(path: string; config: PathConfig): string; overload; static; + class function PathElements(path: string): StringArray; overload; static; + class function PathElements(path: string; config: PathConfig): StringArray; overload; static; class function SystemConfig: PathConfig; static; end; *************** *** 89,99 **** implementation - {$REGION 'implementation uses'} - uses - System.Collections, - System.IO, - System.Text; - {$ENDREGION} - {$AUTOBOX ON} --- 103,106 ---- *************** *** 186,189 **** --- 193,245 ---- end; + class function Path.CommonBase(paths: StringArray): string; + begin + Result := CommonBase(paths, SysConfig); + end; + + class function Path.CommonBase(paths: array of string; config: PathConfig): string; + begin + Result := System.String.Join(config.DirectorySeparator, StringArray(CommonBaseImpl(paths, config).ToArray(TypeOf(string)))); + end; + + class function Path.CommonBase(path1, path2: string): string; + begin + Result := CommonBase([path1, path2], SysConfig); + end; + + class function Path.CommonBase(path1, path2: string; config: PathConfig): string; + begin + Result := CommonBase([path1, path2], config); + end; + + class function Path.CommonBaseImpl(paths: array of string; config: PathConfig): ArrayList; + var + thisPath: string; + currentList: ArrayList; + idx: Integer; + begin + Result := nil; + for thisPath in paths do + begin + currentList := ArrayList.Create(&Array(PathElements(thisPath, config))); + if currentList.Count > 0 then + begin + if Result = nil then + Result := currentList + else + begin + if currentList.Count < Result.Count then + Result.RemoveRange(currentList.Count, Result.Count - currentList.Count); + idx := 0; + while (idx < Result.Count) and + (System.String.Compare(string(Result[idx]), string(currentList[idx]), True) = 0) do + Inc(idx); + if idx < Result.Count then + Result.RemoveRange(idx, Result.Count - idx); + end; + end; + end; + end; + class function Path.EnsureEndingDirectorySeparator(path: string): string; begin *************** *** 275,278 **** --- 331,362 ---- end; + class function Path.MakeRelativeFrom(inPath, basePath: string): string; + begin + Result := MakeRelativeFrom(inPath, basePath, SysConfig); + end; + + class function Path.MakeRelativeFrom(inPath, basePath: string; config: PathConfig): string; + var + basePathElementCount: Integer; + commonElements: ArrayList; + relativeElements: ArrayList; + upLevelCount: Integer; + pathElements: ArrayList; + begin + basePathElementCount := &Array(Path.PathElements(basePath, config)).Length; + commonElements := CommonBaseImpl([basePath, inPath], config); + relativeElements := ArrayList.Create; + upLevelCount := basePathElementCount - commonElements.Count; + while upLevelCount > 0 do + begin + relativeElements.Add('..'); + Dec(upLevelCount); + end; + pathElements := ArrayList.Create(&Array(Path.PathElements(inPath, config))); + pathElements.RemoveRange(0, commonElements.Count); + relativeElements.AddRange(pathElements); + Result := System.String.Join(config.DirectorySeparator, StringArray(relativeElements.ToArray(TypeOf(string)))); + end; + class function Path.Normalize(path: string): string; begin *************** *** 312,315 **** --- 396,409 ---- end; + class function Path.PathElements(path: string): StringArray; + begin + Result := PathElements(path, SysConfig); + end; + + class function Path.PathElements(path: string; config: PathConfig): StringArray; + begin + Result := Normalize(path, config).Split([config.DirectorySeparator]); + end; + class function Path.SystemConfig: PathConfig; begin |