From 81781243c58e0de92b0aace5e21796cc78bb25c5 Mon Sep 17 00:00:00 2001 From: Irwin Rodriguez Date: Sun, 15 Nov 2026 11:51:43 +0100 Subject: [PATCH] SET PATH TO with ADDITIVE adds the folders to the path --- src/Common/FoxProSet.xh | 10 + .../XSharp.Core/Functions/FileSearch.prg | 8 + src/Runtime/XSharp.VFP.Tests/SetPathTests.prg | 293 ++++++++++++++++++ src/Runtime/XSharp.VFP/Commands.prg | 50 +++ src/Runtime/XSharp.VFP/StringFunctions.prg | 12 + 5 files changed, 373 insertions(+) create mode 100644 src/Runtime/XSharp.VFP.Tests/SetPathTests.prg diff --git a/src/Common/FoxProSet.xh b/src/Common/FoxProSet.xh index 07c969f4b3..e5365e07ff 100644 --- a/src/Common/FoxProSet.xh +++ b/src/Common/FoxProSet.xh @@ -19,6 +19,16 @@ #command SET DIRECTORY TO <*path*> => Set( Set.Default, <(path)> ) #command SET DIRECTORY TO => Set( Set.Default, "" ) +// SET PATH TO ADDITIVE +// VFP reads ADDITIVE as a clause only when the path is an expression: a string, +// a function call or something between parentheses. After a plain name the +// word is part of the path, SET PATH TO data ADDITIVE sets the path to +// "DATA ADDITIVE". The last rule keeps it like that. +#command SET PATH TO ADDITIVE => __VfpSetPathAdditive( ) +#command SET PATH TO ADDITIVE => __VfpSetPathAdditive( <(path)> ) +#command SET PATH TO <%path%> ADDITIVE => Set( Set.Path, <"path"> + " ADDITIVE" ) +#command SET PATH TO \[ <*path*> \] ADDITIVE => __VfpSetPathAdditive( # ) + /////////////////////////////////////////////////////////////////////////////// // SET commands for the Old Get system and Console output /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Runtime/XSharp.Core/Functions/FileSearch.prg b/src/Runtime/XSharp.Core/Functions/FileSearch.prg index 1798002b61..09721efaf8 100644 --- a/src/Runtime/XSharp.Core/Functions/FileSearch.prg +++ b/src/Runtime/XSharp.Core/Functions/FileSearch.prg @@ -329,6 +329,14 @@ ENDIF cPath := SetPath() IF String.IsNullOrEmpty(cPath) cPath := GetEnv("PATH") +ELSEIF RuntimeState.Dialect == XSharpDialect.FoxPro + // SET PATH in FoxPro also takes a comma between the folders, and blanks around them + FOREACH VAR cElement IN cPath:Split({ ';', ',' }, StringSplitOptions.RemoveEmptyEntries ) + IF !String.IsNullOrWhiteSpace(cElement) + aPaths:Add(cElement:Trim()) + ENDIF + NEXT + cPath := NULL ENDIF IF !String.IsNullOrEmpty(cPath) VAR aElements := cPath:Split({ ';' }, StringSplitOptions.RemoveEmptyEntries ) diff --git a/src/Runtime/XSharp.VFP.Tests/SetPathTests.prg b/src/Runtime/XSharp.VFP.Tests/SetPathTests.prg new file mode 100644 index 0000000000..ee94418018 --- /dev/null +++ b/src/Runtime/XSharp.VFP.Tests/SetPathTests.prg @@ -0,0 +1,293 @@ +// +// Copyright (c) XSharp B.V. All Rights Reserved. +// Licensed under the Apache License, Version 2.0. +// See License.txt in the project root for license information. +// +// SET PATH TO ... ADDITIVE and a path with several folders. The expected values +// were measured on VFP 9 SP2, except for the case of the letters: VFP returns +// the path in upper case and X# keeps it as it was written. + +USING System +USING System.IO +USING XUnit + +BEGIN NAMESPACE XSharp.VFP.Tests + CLASS SetPathTests + + STATIC CONSTRUCTOR + XSharp.RuntimeState.Dialect := XSharpDialect.FoxPro + RegisterFoxMemVarSupport() + END CONSTRUCTOR + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveAddsTheFolder AS VOID + LOCAL cOld, cA, cB AS STRING + cOld := SELF:SavePath() + TRY + cA := "c:\temp\gmA" + cB := "c:\temp\gmB" + SET PATH TO (cA) + SET PATH TO (cB) ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + // an empty path has nothing to separate + SET PATH TO + SET PATH TO (cA) ADDITIVE + Assert.Equal("c:\temp\gmA", SetPath()) + // and an empty string adds nothing + SET PATH TO ("") ADDITIVE + Assert.Equal("c:\temp\gmA", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveSkipsAFolderOfThePath AS VOID + LOCAL cOld, cA, cB AS STRING + cOld := SELF:SavePath() + TRY + cA := "c:\temp\gmA" + cB := "c:\temp\gmB" + SET PATH TO (cA) + SET PATH TO (cB) ADDITIVE + SET PATH TO (cB) ADDITIVE + SET PATH TO ("C:\TEMP\GMB") ADDITIVE + SET PATH TO ("c:\temp\gmB\") ADDITIVE + SET PATH TO (" c:\temp\gmA") ADDITIVE + SET PATH TO ("c:\temp\gmA\..\gmA") ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + // also when the path has commas and the folder is in the middle + SET PATH TO ("c:\temp\gmA,c:\temp\gmB,c:\temp\gmC") + SET PATH TO (cB) ADDITIVE + Assert.Equal("c:\temp\gmA,c:\temp\gmB,c:\temp\gmC", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveWithAListOfFolders AS VOID + LOCAL cOld AS STRING + cOld := SELF:SavePath() + TRY + // the folders are added one by one, the ones of the path are skipped + SET PATH TO ("c:\temp\gmA;c:\temp\gmB") + SET PATH TO ("c:\temp\gmC,c:\temp\gmA;c:\temp\gmD") ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB;c:\temp\gmC;c:\temp\gmD", SetPath()) + // empty elements are dropped + SET PATH TO ("c:\temp\gmA") + SET PATH TO ("c:\temp\gmB,,;c:\temp\gm C") ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB;c:\temp\gm C", SetPath()) + // a folder that comes twice in the new list is added twice + SET PATH TO ("c:\temp\gmA") + SET PATH TO ("c:\temp\gmB;c:\temp\gmB") ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB;c:\temp\gmB", SetPath()) + // the blanks of the new folder stay + SET PATH TO ("c:\temp\gmA") + SET PATH TO (" c:\temp\gmB ") ADDITIVE + Assert.Equal("c:\temp\gmA; c:\temp\gmB ", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveKeepsThePathThatWasThere AS VOID + LOCAL cOld, cB AS STRING + cOld := SELF:SavePath() + TRY + cB := "c:\temp\gmB" + SET PATH TO ("c:\temp\gmA,c:\temp\gmC") + SET PATH TO (cB) ADDITIVE + Assert.Equal("c:\temp\gmA,c:\temp\gmC;c:\temp\gmB", SetPath()) + // no second separator when the path ends with one + SET PATH TO ("c:\temp\gmA;") + SET PATH TO (cB) ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + SET PATH TO ("c:\temp\gmA,") + SET PATH TO (cB) ADDITIVE + Assert.Equal("c:\temp\gmA,c:\temp\gmB", SetPath()) + SET PATH TO ("c:\temp\gmA ") + SET PATH TO (cB) ADDITIVE + Assert.Equal("c:\temp\gmA ;c:\temp\gmB", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveAfterAnExpression AS VOID + LOCAL cOld, cA, cB AS STRING + cOld := SELF:SavePath() + TRY + cA := "c:\temp\gmA" + cB := "c:\temp\gmB" + SET PATH TO (cA) + SET PATH TO "c:\temp\gm C" ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gm C", SetPath()) + SET PATH TO (cA) + SET PATH TO [c:\temp\gm C] ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gm C", SetPath()) + SET PATH TO (cA) + SET PATH TO (cB) + "\sub" ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB\sub", SetPath()) + SET PATH TO (cA) + SET PATH TO "c:\temp\gmB" + "\sub" ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB\sub", SetPath()) + SET PATH TO (cA) + SET PATH TO JustPath(cB) ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp", SetPath()) + SET PATH TO (cA) + SET PATH TO (cB) additive + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + SET PATH TO (cA) + SET PATH TO (cB) ADDI + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveAfterANameIsPartOfThePath AS VOID + // VFP does the same: without quotes or parentheses the word is not a clause + LOCAL cOld, cA AS STRING + PRIVATE cMacro + cOld := SELF:SavePath() + TRY + cA := "c:\temp\gmA" + SET PATH TO (cA) + SET PATH TO data ADDITIVE + Assert.Equal("data ADDITIVE", SetPath()) + SET PATH TO (cA) + SET PATH TO c:\temp\gmB ADDITIVE + Assert.Equal("c:\temp\gmB ADDITIVE", SetPath()) + // not like VFP: with a macro VFP also takes the word as part of + // the path, here the folder is added + cMacro := "c:\temp\gmM" + SET PATH TO (cA) + SET PATH TO &cMacro ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmM", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveTakesTheQuotesOfTheValueAway AS VOID + // the help of VFP tells to write the macro with the quotes inside + LOCAL cOld, cA, cQuoted AS STRING + PRIVATE cMacro + cOld := SELF:SavePath() + TRY + cA := "c:\temp\gmA" + cMacro := '"c:\temp\gm B"' + SET PATH TO (cA) + SET PATH TO &cMacro ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gm B", SetPath()) + cMacro := "[c:\temp\gmB]" + SET PATH TO (cA) + SET PATH TO &cMacro ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + cQuoted := "'c:\temp\gmB'" + SET PATH TO (cA) + SET PATH TO (cQuoted) ADDITIVE + Assert.Equal("c:\temp\gmA;c:\temp\gmB", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD AdditiveNeedsAString AS VOID + LOCAL cOld AS STRING + LOCAL lError := FALSE AS LOGIC + cOld := SELF:SavePath() + TRY + SET PATH TO ("c:\temp\gmA") + TRY + SET PATH TO (123) ADDITIVE + CATCH + lError := TRUE + END TRY + Assert.True(lError) + Assert.Equal("c:\temp\gmA", SetPath()) + FINALLY + SetPath(cOld) + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD FoldersOfThePathAreSearched AS VOID + LOCAL cOld, cRoot, cA, cB, cC AS STRING + cOld := SELF:SavePath() + cRoot := Path.Combine(Path.GetTempPath(), "VfpSetPath_" + Guid.NewGuid():ToString("N")) + cA := Path.Combine(cRoot, "a") + cB := Path.Combine(cRoot, "b") + cC := Path.Combine(cRoot, "c d") + TRY + XSharp.RuntimeState.Dialect := XSharpDialect.FoxPro + System.IO.Directory.CreateDirectory(cA) + System.IO.Directory.CreateDirectory(cB) + System.IO.Directory.CreateDirectory(cC) + System.IO.File.WriteAllText(Path.Combine(cB, "xs_setpath_b.txt"), "x") + System.IO.File.WriteAllText(Path.Combine(cC, "xs_setpath_c.txt"), "x") + + SET PATH TO (cA) + Assert.False(File("xs_setpath_b.txt")) + SET PATH TO (cB) ADDITIVE + Assert.True(File("xs_setpath_b.txt")) + Assert.False(File("xs_setpath_c.txt")) + SET PATH TO (cC) ADDITIVE + Assert.True(File("xs_setpath_c.txt")) + FINALLY + SetPath(cOld) + IF System.IO.Directory.Exists(cRoot) + System.IO.Directory.Delete(cRoot, TRUE) + ENDIF + END TRY + END METHOD + + [Fact, Trait("Category", "SetPath")]; + METHOD FoldersWithACommaAreSearched AS VOID + // a comma between the folders, and blanks around them + LOCAL cOld, cRoot, cA, cB, cC AS STRING + cOld := SELF:SavePath() + cRoot := Path.Combine(Path.GetTempPath(), "VfpSetPath_" + Guid.NewGuid():ToString("N")) + cA := Path.Combine(cRoot, "a") + cB := Path.Combine(cRoot, "b") + cC := Path.Combine(cRoot, "c d") + TRY + XSharp.RuntimeState.Dialect := XSharpDialect.FoxPro + System.IO.Directory.CreateDirectory(cA) + System.IO.Directory.CreateDirectory(cB) + System.IO.Directory.CreateDirectory(cC) + System.IO.File.WriteAllText(Path.Combine(cB, "xs_setpath_b.txt"), "x") + System.IO.File.WriteAllText(Path.Combine(cC, "xs_setpath_c.txt"), "x") + + SET PATH TO (cA + "," + cB) + Assert.True(File("xs_setpath_b.txt")) + SET PATH TO (cA + ", " + cB) + Assert.True(File("xs_setpath_b.txt")) + SET PATH TO (" " + cA + " ; " + cC + " ") + Assert.True(File("xs_setpath_c.txt")) + Assert.False(File("xs_setpath_b.txt")) + FINALLY + SetPath(cOld) + IF System.IO.Directory.Exists(cRoot) + System.IO.Directory.Delete(cRoot, TRUE) + ENDIF + END TRY + END METHOD + + PRIVATE METHOD SavePath() AS STRING + LOCAL cPath AS STRING + cPath := SetPath() + IF cPath == NULL + cPath := "" + ENDIF + RETURN cPath + END METHOD + + END CLASS +END NAMESPACE diff --git a/src/Runtime/XSharp.VFP/Commands.prg b/src/Runtime/XSharp.VFP/Commands.prg index eb9ba09b07..758fa26be6 100644 --- a/src/Runtime/XSharp.VFP/Commands.prg +++ b/src/Runtime/XSharp.VFP/Commands.prg @@ -276,3 +276,53 @@ FUNCTION __VfpRecallRecord() AS LOGIC _TALLY := 0 ENDIF RETURN __DbRecall() + +/// +/// +/// Runtime implementation of SET PATH TO expr ADDITIVE +/// Adds the folders that are not in the path yet. The path that was there +/// is kept as it is and the new folders are added with a semicolon. +/// +FUNCTION __VfpSetPathAdditive(uPath AS USUAL) AS VOID + IF !IsString(uPath) + THROW Error.ArgumentError(__FUNCTION__, nameof(uPath), ; + __VfpStr(VFPErrors.VFP_INVALID_PARAMETER, nameof(uPath), "'STRING'"), 1, {uPath}) + ENDIF + + VAR aSeparators := {c';', c','} + VAR cPath := SetPath() + IF cPath == NULL + cPath := "" + ENDIF + + // VFP takes the quotes away when they come inside the value. The help + // tells to write a macro like that: xx = ["Folder2"] and then + // SET PATH TO &xx ADDITIVE + VAR cNew := (STRING) uPath + VAR cTrimmed := cNew:Trim() + IF cTrimmed:Length > 1 + IF (cTrimmed:StartsWith(e"\"") .AND. cTrimmed:EndsWith(e"\"")) .OR. ; + (cTrimmed:StartsWith("'") .AND. cTrimmed:EndsWith("'")) .OR. ; + (cTrimmed:StartsWith("[") .AND. cTrimmed:EndsWith("]")) + cNew := cTrimmed:Substring(1, cTrimmed:Length - 2) + ENDIF + ENDIF + + // VFP compares the full name of the folders, so "data\" and ".\data" + // are the same folder as "data" + VAR aFolders := HashSet{StringComparer.OrdinalIgnoreCase} + FOREACH VAR cFolder IN cPath:Split(aSeparators, StringSplitOptions.RemoveEmptyEntries) + aFolders:Add(PathHelpers.FullFolderName(cFolder)) + NEXT + + FOREACH VAR cFolder IN cNew:Split(aSeparators, StringSplitOptions.RemoveEmptyEntries) + IF String.IsNullOrWhiteSpace(cFolder) .OR. aFolders:Contains(PathHelpers.FullFolderName(cFolder)) + LOOP + ENDIF + IF cPath:Length > 0 .AND. !cPath:EndsWith(";") .AND. !cPath:EndsWith(",") + cPath += ";" + ENDIF + cPath += cFolder + NEXT + SetPath(cPath) + RETURN diff --git a/src/Runtime/XSharp.VFP/StringFunctions.prg b/src/Runtime/XSharp.VFP/StringFunctions.prg index a04df336ea..230f886f78 100644 --- a/src/Runtime/XSharp.VFP/StringFunctions.prg +++ b/src/Runtime/XSharp.VFP/StringFunctions.prg @@ -18,6 +18,18 @@ INTERNAL STATIC CLASS PathHelpers INTERNAL STATIC PROPERTY PathChar AS STRING AUTO STATIC CONSTRUCTOR() PathChar := Path.DirectorySeparatorChar:ToString() + + // The full name of a folder without the last separator, to compare two + // folders of SET PATH that may be written in a different way + INTERNAL STATIC METHOD FullFolderName(cFolder AS STRING) AS STRING + cFolder := cFolder:Trim() + TRY + cFolder := Path.GetFullPath(cFolder) + CATCH + // not a valid folder name, compare it as it was written + NOP + END TRY + RETURN cFolder:TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) END CLASS