RosettaCodeData/Task/Find-common-directory-path/PowerBASIC/find-common-directory-path....

69 lines
1.7 KiB
Plaintext

#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
$PATH_SEPARATOR = "/"
FUNCTION CommonDirectoryPath(Paths() AS STRING) AS STRING
LOCAL s AS STRING
LOCAL i, j, k AS LONG
k = 1
DO
FOR i = 0 TO UBOUND(Paths)
IF i THEN
IF INSTR(k, Paths(i), $PATH_SEPARATOR) <> j THEN
EXIT DO
ELSEIF LEFT$(Paths(i), j) <> LEFT$(Paths(0), j) THEN
EXIT DO
END IF
ELSE
j = INSTR(k, Paths(i), $PATH_SEPARATOR)
IF j = 0 THEN
EXIT DO
END IF
END IF
NEXT i
s = LEFT$(Paths(0), j + CLNG(k <> 1))
k = j + 1
LOOP
FUNCTION = s
END FUNCTION
FUNCTION PBMAIN () AS LONG
' testing the above function
LOCAL s() AS STRING
LOCAL i AS LONG
REDIM s(0 TO 2)
ARRAY ASSIGN s() = "/home/user1/tmp/coverage/test", _
"/home/user1/tmp/covert/operator", _
"/home/user1/tmp/coven/members"
FOR i = 0 TO UBOUND(s()): CON.PRINT s(i): NEXT i
CON.PRINT CommonDirectoryPath(s()) & " <- common"
CON.PRINT
REDIM s(0 TO 3)
ARRAY ASSIGN s() = "/home/user1/tmp/coverage/test", _
"/home/user1/tmp/covert/operator", _
"/home/user1/tmp/coven/members", _
"/home/user1/abc/coven/members"
FOR i = 0 TO UBOUND(s()): CON.PRINT s(i): NEXT i
CON.PRINT CommonDirectoryPath(s()) & " <- common"
CON.PRINT
REDIM s(0 TO 2)
ARRAY ASSIGN s() = "/home/user1/tmp/coverage/test", _
"/hope/user1/tmp/covert/operator", _
"/home/user1/tmp/coven/members"
FOR i = 0 TO UBOUND(s()): CON.PRINT s(i): NEXT i
CON.PRINT CommonDirectoryPath(s()) & " <- common"
CON.PRINT
CON.PRINT "hit any key to end program"
CON.WAITKEY$
END FUNCTION