Filename translation

From BeebWiki
Jump to: navigation, search

Usually a program does not, and should not, need to know what type of filenames the underlying filing system is using. The program should accept whatever filenames the user supplies, and pass them onto the filing system. However, there are some cases where the actual filename format is needed and the program needs to be able to translate filenames.

Pathname translation deals with translating full pathnames - including references to arbitary root directories, parent directories, drives, etc., such as H:/docs/../start*/file0?.doc, $.info.^.set*.make##/src. If you known that you will only be dealing with simple full filenames - known as canonicalised paths - such as info.setup.make01/src, or just leafnames - such as docs/zip or docs.zip - then the simpler conversion code here can be used.

The FName library provides functions for converting simple filenames between BBC, DOS/Windows and Unix/Zip/URL formats.

Contents

BBC and Zip

BBC and Zip/Unix/URL filenames can be converted bidirectionally between each other by simply swapping characters # . $ ^ & @ % with ? / < > + = ;.

   REM Swap between BBC and ZIP/Unix/URL path
   DEFFNfn_bbczip(B$):IF B$="":=""
   LOCAL A$,B%,A%:A$="/.#?$<^>&+@=%; "
   FOR A%=1 TO LEN B$:B%=INSTR(A$,MID$(B$,A%,1))-1
     IF B%>TRUE:B$=LEFT$(B$,A%-1)+MID$(A$+"_",(B%EOR1)+1,1)+MID$(B$,A%+1)
   NEXT:=B$

BBC and DOS/Windows

BBC and DOS/Windows filenames cannot be bidirectionally swapped with the same code as the three characters . / \ have to be interchanged. However, if you know that you are only dealing with leafnames, FNfn_bbczip() can be used.

   REM Convert a DOS path to a BBC path
   DEFFNfn_dosbbc(B$):IF B$="":=""
   LOCAL A$,B%,A%:A$=".\#?$<^>&+@=%; "
   FOR A%=1 TO LEN B$:B%=INSTR(A$,MID$(B$,A%,1))-1
     IF B%>0:B$=LEFT$(B$,A%-1)+MID$(A$+"_",(B%EOR1)+1,1)+MID$(B$,A%+1)
     IF B%=0:B$=LEFT$(B$,A%-1)+"/"+MID$(B$,A%+1)
   NEXT:=B$
   :
   REM Convert a BBC path to a DOS path
   DEFFNfn_bbcdos(B$):IF B$="":=""
   LOCAL A$,B%,A%:A$="./#?$<^>&+@=%; "
   FOR A%=1 TO LEN B$:B%=INSTR(A$,MID$(B$,A%,1))-1
     IF B%>0:B$=LEFT$(B$,A%-1)+MID$(A$+"_",(B%EOR1)+1,1)+MID$(B$,A%+1)
     IF B%=0:B$=LEFT$(B$,A%-1)+"\"+MID$(B$,A%+1)
   NEXT:=B$

DOS and Zip/Unix/URL

Converting between DOS and Zip/Unix/URL filenames is the simplest of all, as all it requires is the \ and / characters to be bidrectionally swapped.

   REM Swap between DOS and Zip/Unix/URL path
   DEFFNfn_doszip(A$):IFA$="":=""
   LOCAL A%,B%
   FOR A%=1 TO LEN A$:B%=ASCMID$(A$,A%,1)
     IF B%=47 OR B%=92:A$=LEFT$(A$,A%-1)+CHR$(B%EOR115)+MID$(A$,A%+1):REM Swap / \
   NEXT:=A$

See also

Jgharston 03:28, 14 January 2012 (UTC) Jgharston (talk) 06:13, 3 April 2018 (CEST)