Localising pathnames

From BeebWiki
Jump to: navigation, search

If you are writing a BBC BASIC program on a non-BBC platform, such as CPM, DOS, Windows, Unix, etc., the program should not make any special tests and should accept and use filenames and pathnames in the format relevant to the platform the program is running on.

There are some cases where a program will be presented with a BBC-format pathname and it will need to localise this to the platform it is running on. An example would be a server running on a non-BBC platform serving a BBC client. The function FNfn_localise() will convert BBC format pathnames to the format of the local platform.

 DEFFNfn_localise(A$):IFA$=""OR(os%AND40)=0:=A$
 LOCALA%,B%
 IFLEFT$(A$,1)=":":A%=INSTR(A$+".","."):A$=MID$(A$,2,A%-2)+":"+MID$(A$,A%-(MID$(A$,A%+1,1)="$"))
 A%=1:REPEAT:B%=ASCMID$(A$,A%,1)
 IF(B%=35ORB%=63):A$=LEFT$(A$,A%-1)+CHR$(B%EOR28)+MID$(A$,A%+1)
 IFB%=ASC"$":IF(os%AND40)=8:A$=LEFT$(A$,A%-1)+"/"+MID$(A$,A%+1-(A%<>LENA$))
 IFB%=ASC"$":IF(os%AND32):A$=LEFT$(A$,A%-1)+"\"+MID$(A$,A%+1-(A%<>LENA$))
 IFB%=ASC"@":A$=LEFT$(A$,A%-1)+"."+MID$(A$,A%+1)
 IFB%=ASC"^":A$=LEFT$(A$,A%-1)+".."+MID$(A$,A%+1):A%=A%+1
 IFB%=ASC"/":A$=LEFT$(A$,A%-1)+"."+MID$(A$,A%+1)
 IFB%=ASC".":IF(os%AND40)=8:A$=LEFT$(A$,A%-1)+"/"+MID$(A$,A%+1)
 IFB%=ASC".":IF(os%AND32):A$=LEFT$(A$,A%-1)+"\"+MID$(A$,A%+1)
 IFB%=ASC"\":IF(os%AND32):A$=LEFT$(A$,A%-1)+"/"+MID$(A$,A%+1)
 IFB%=ASC"&":A$=LEFT$(A$,A%-1)+"%HOME%"+MID$(A$,A%+1)
 A%=A%+1:UNTILA%>LENA$:=A$

The variable os% needs to be set to the platform type, as set with A%=0:X%=1:os%=((USR&FFF4)AND&FF00)DIV256 or by the FNOS_GetEnv function in the ProgEnv library.

Examples

  BBC                    DOS/Win                 Unix
  Documents.Letter/txt   Documents\Letters.txt   Documents/Letters.txt
  ^.Info                 ..\Info                 ../Info
  $.System               \System                 /System
  :A.$.Apps              A:\Apps                 A:/Apps
  :System.Utils          System:\Utils           System:/Utils

See also