xsharp.eu • low hanging fruit... ahem functions
Page 1 of 1

low hanging fruit... ahem functions

Posted: Thu Oct 24, 2019 3:29 am
by mainhatten
Johan told me it was ok to "implement" such stuff when reading code and thinking of a vfp function with a different name. Therefore:

Code: Select all

LOCAL lcPathedFile AS STRING
lcPath = "d:xS"
lcPathedFile = lcPath + "RootPlay.dbf"
? JustExt(lcPathedFile)
? JustStem(lcPathedFile)
? JustPath(lcPathedFile)
? JustDisk(lcPathedFile)

LOCAL loEmp   
* LOCAL loEmp AS OBJECT  
* loEmp = createobject("custom")
*-- nope, not yet

? "Press Exit Key"
RETURN

FUNCTION ByRef(tnChange OUT INT) AS Boolean
	tnChange = IiF (tnChange = 42, -42, 42)
	RETURN .t.
	
FUNCTION JustDisk (tcURI AS STRING) AS STRING
	LOCAL lcDisk AS STRING
	LOCAL lcDummy AS STRING
	= _SplitPath(tcURI, OUT lcDisk, OUT lcDummy, OUT lcDummy, OUT lcDummy)
	RETURN lcDisk
	  
FUNCTION JustPath (tcURI AS STRING) AS STRING
	LOCAL lcPath AS STRING
	LOCAL lcDummy AS STRING
	= _SplitPath(tcURI, OUT lcDummy, OUT lcPath, OUT lcDummy, OUT lcDummy)
	RETURN lcPath
	  
FUNCTION JustStem (tcURI AS STRING) AS STRING
	LOCAL lcStem AS STRING
	LOCAL lcDummy AS STRING
	= _SplitPath(tcURI, OUT lcDummy, OUT lcDummy, OUT lcStem, OUT lcDummy)
	RETURN lcStem

FUNCTION JustExt (tcURI AS STRING) AS STRING
	LOCAL lcExt AS STRING
	LOCAL lcDummy AS STRING
	= _SplitPath(tcURI, OUT lcDummy, OUT lcDummy, OUT lcDummy, OUT lcExt)
	RETURN lcExt

OUT in Parameter declaration is not the vfp way - sadly, I find it safer and easier to read than @in calling line
You can expect some equally taxing Force...functions in a few H - already done, but want to look them over on empty strings and such pesky stuff when I return.

HTH
thomas

low hanging fruit... ahem functions

Posted: Thu Oct 24, 2019 4:02 am
by robert
Thomas,
This is Ok, but you're better of using the functions / static methods inside the System.IO.Path namespace, like
System.IO.Path.GetDirectory(), System.IO.Path.GetExtension() etc.
_SplitPath uses these internally, but why both getting the extension (inside _SplitPath) if you're in he JustPath() function and only interested in the path.
The most difficult part in functions like these is to check what VFP does with erroneous input (like non existing files), and then decide if you want to do the same or not.

Robert