【问题标题】:What is opposite of ExtractRelativePath in Pascal?Pascal 中 ExtractRelativePath 的对立面是什么?
【发布时间】:2014-01-17 17:35:04
【问题描述】:

我经常使用 ExtractRelativePath 来获取两条路径之间的相对路径。但我看不到任何与之相反的功能。这是来自 freepascal.org 的示例:

Uses sysutils;

Procedure Testit (FromDir,ToDir : String);

begin
  Write ('From "',FromDir,'" to "',ToDir,'" via "');
  Writeln (ExtractRelativePath(FromDir,ToDir),'"');
end;

Begin
 Testit ('/pp/src/compiler','/pp/bin/win32/ppc386');
 Testit ('/pp/bin/win32/ppc386','/pp/src/compiler');
 Testit ('e:/pp/bin/win32/ppc386','d:/pp/src/compiler');
 Testit ('e:\pp\bin\win32\ppc386','d:\pp\src\compiler');
End.

这个程序的输出

From "/pp/src/compiler" to "/pp/bin/win32/ppc386" via "../bin/win32/ppc386"
From "/pp/bin/win32/ppc386" to "/pp/src/compiler" via "../../src/compiler"
From "e:/pp/bin/win32/ppc386" to "d:/pp/src/compiler" via "../../src/compiler"
From "e:\pp\bin\win32\ppc386" to "d:\pp\src\compiler" via "../../src/compiler"

我需要一个函数F来执行ExtractRelativePath的反向动作,例如:

F('/pp/src/compiler', '../bin/win32/ppc386') return '/pp/bin/win32/ppc386'.

你知道这样的功能吗?提前谢谢你。

【问题讨论】:

标签: freepascal lazarus


【解决方案1】:

是的,当然。 http://docwiki.embarcadero.com/Libraries/XE5/en/System.IOUtils.TPath.Combine

System.IOUtils.TPath.Combine

  class function Combine(const Path1, Path2: string): string; inline; static;

说明

组合两个路径字符串。

调用 Combine 以从两条不同的路径中获取新的组合路径。如果 第二个路径是绝对路径,Combine 直接返回;否则 组合返回与第二个连接的第一个路径。


上面是标记问题时写的

现在,对于 FPC,通过 SysUtils 源的简单扫描将您带到

  • c:\codetyphon\fpcsrc\rtl\objpas\sysutils\finah.inc

function ConcatPaths(const Paths: array of String): String;

记录在

连接路径

连接一组路径以形成单个路径

声明

来源位置:finah.inc 第 42 行 function ConcatPaths( const Paths: array of ):;

说明

ConcatPaths 会将 Paths 中的不同路径组件连接到 单一路径。它将在各个目录之间插入目录分隔符 路径的组件根据需要。没有目录分隔符 添加到路径的开头或结尾,并且不会被采取 离开。

例子

program ex96;

{ This program demonstrates the Concatpaths function }

uses sysutils;

begin
  // will write /this/path/more/levels/
  Writeln(ConcatPaths(['/this/','path','more/levels/']));
  // will write this/path/more/levels/
  Writeln(ConcatPaths(['this/','path','more/levels/']));
  // will write this/path/more/levels
  Writeln(ConcatPaths(['this/','path','more/levels']));
end.

【讨论】:

  • 问题是关于 FreePascal 和 Lazarus
  • @DavidHeffernan 哦,确实。感谢您修复标签。但是,让这个答案保留一段时间,以警告更多读者
  • 我正在使用 Lazarus,但没有找到 System.IOUtils.pas。对于ConcatPaths,如果 A 和 B 都是绝对路径,则 ConcatPaths 返回 A+[+'/']+B 但需要 B。有没有更好的功能?
  • ConcatPaths 可以,但需要更多说明。
  • 是的,它可能至少需要 IsPathAbsoluteOrRelative - 它存在于 IOUtils 中,但不知道是否在 lazarus 中
猜你喜欢
  • 1970-01-01
  • 2013-06-22
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 2021-03-19
  • 2020-02-22
相关资源
最近更新 更多