Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Common/FoxProSet.xh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
#command SET DIRECTORY TO <*path*> => Set( Set.Default, <(path)> )
#command SET DIRECTORY TO => Set( Set.Default, "" )

// SET PATH TO <path> 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 <path> ADDITIVE => __VfpSetPathAdditive( <path> )
#command SET PATH TO <path:&> ADDITIVE => __VfpSetPathAdditive( <(path)> )
#command SET PATH TO <%path%> ADDITIVE => Set( Set.Path, <"path"> + " ADDITIVE" )
#command SET PATH TO \[ <*path*> \] ADDITIVE => __VfpSetPathAdditive( #<path> )

///////////////////////////////////////////////////////////////////////////////
// SET commands for the Old Get system and Console output
///////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions src/Runtime/XSharp.Core/Functions/FileSearch.prg
Original file line number Diff line number Diff line change
Expand Up @@ -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(<CHAR>{ ';', ',' }, StringSplitOptions.RemoveEmptyEntries )
IF !String.IsNullOrWhiteSpace(cElement)
aPaths:Add(cElement:Trim())
ENDIF
NEXT
cPath := NULL
ENDIF
IF !String.IsNullOrEmpty(cPath)
VAR aElements := cPath:Split(<CHAR>{ ';' }, StringSplitOptions.RemoveEmptyEntries )
Expand Down
293 changes: 293 additions & 0 deletions src/Runtime/XSharp.VFP.Tests/SetPathTests.prg
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions src/Runtime/XSharp.VFP/Commands.prg
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,53 @@ FUNCTION __VfpRecallRecord() AS LOGIC
_TALLY := 0
ENDIF
RETURN __DbRecall()

/// <exclude/>
/// <summary>
/// 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.
/// </summary>
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, <OBJECT>{uPath})
ENDIF

VAR aSeparators := <CHAR>{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<STRING>{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
12 changes: 12 additions & 0 deletions src/Runtime/XSharp.VFP/StringFunctions.prg
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down