Difference between revisions of "Filename translation"
m (1 revision) |
m (→See also) |
||
(One intermediate revision by the same user not shown) | |||
Line 10: | Line 10: | ||
such as ''H:/docs/../start*/file0?.doc'', ''$.info.^.set*.make##/src''. If | 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 | you known that you will only be dealing with simple full filenames - known | ||
− | as ''canonicalised paths'' - such as ''info.setup.make01/src'' then simpler | + | as ''canonicalised paths'' - such as ''info.setup.make01/src'', or just |
− | conversion code can be used. | + | leafnames - such as ''docs/zip'' or ''docs.zip'' - then the simpler |
+ | conversion code here can be used. | ||
The [http://mdfs.net/System/Library/BLib#fname FName] library provides | The [http://mdfs.net/System/Library/BLib#fname FName] library provides | ||
Line 19: | Line 20: | ||
===BBC and Zip=== | ===BBC and Zip=== | ||
BBC and Zip/Unix/URL filenames can be converted bidirectionally between each | BBC and Zip/Unix/URL filenames can be converted bidirectionally between each | ||
− | other by simply swapping characters ''' | + | other by simply swapping characters |
− | + | '''#''' '''.''' '''$''' '''^''' '''&''' '''@''' '''%''' with | |
+ | '''?''' '''/''' '''<''' '''>''' '''+''' '''=''' ''';'''. | ||
REM Swap between BBC and ZIP/Unix/URL path | REM Swap between BBC and ZIP/Unix/URL path | ||
− | DEFFNfn_bbczip( | + | DEFFNfn_bbczip(B$):IF B$="":="" |
− | LOCAL A | + | 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$ | |
− | |||
− | IF B% | ||
− | |||
− | NEXT:= | ||
===BBC and DOS/Windows=== | ===BBC and DOS/Windows=== | ||
BBC and DOS/Windows filenames cannot be bidirectionally swapped with the same | BBC and DOS/Windows filenames cannot be bidirectionally swapped with the same | ||
code as the three characters '''.''' '''/''' '''\''' have to be interchanged. | code as the three characters '''.''' '''/''' '''\''' have to be interchanged. | ||
+ | However, if you know that you are only dealing with leafnames, <code>FNfn_bbczip()</code> | ||
+ | can be used. | ||
REM Convert a DOS path to a BBC path | REM Convert a DOS path to a BBC path | ||
− | DEFFNfn_dosbbc( | + | DEFFNfn_dosbbc(B$):IF B$="":="" |
− | LOCAL A | + | 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$ | |
− | IF B% | ||
− | |||
− | |||
− | IF B%= | ||
− | NEXT:= | ||
: | : | ||
REM Convert a BBC path to a DOS path | REM Convert a BBC path to a DOS path | ||
− | DEFFNfn_bbcdos( | + | DEFFNfn_bbcdos(B$):IF B$="":="" |
− | LOCAL | + | 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$ | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | FOR A%=1 TO LEN | ||
− | |||
− | |||
− | |||
− | |||
− | IF B% | ||
− | |||
− | |||
− | |||
− | |||
− | IF B | ||
− | NEXT:= | ||
===DOS and Zip/Unix/URL=== | ===DOS and Zip/Unix/URL=== | ||
Line 91: | Line 62: | ||
LOCAL A%,B% | LOCAL A%,B% | ||
FOR A%=1 TO LEN A$:B%=ASCMID$(A$,A%,1) | 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 / \ | + | IF B%=47 OR B%=92:A$=LEFT$(A$,A%-1)+CHR$(B%EOR115)+MID$(A$,A%+1):REM Swap / \ |
NEXT:=A$ | NEXT:=A$ | ||
==See also== | ==See also== | ||
* [[Multiplatform programming]] | * [[Multiplatform programming]] | ||
− | * [[ | + | * [[Pathname translation]] |
* [[Filename character mapping]] | * [[Filename character mapping]] | ||
* [http://mdfs.net/blib#fname FName] BASIC library | * [http://mdfs.net/blib#fname FName] BASIC library | ||
+ | * [http://mdfs.net/blib#filename Filename] BASIC library | ||
[[User:Jgharston|Jgharston]] 03:28, 14 January 2012 (UTC) | [[User:Jgharston|Jgharston]] 03:28, 14 January 2012 (UTC) | ||
+ | [[User:Jgharston|Jgharston]] ([[User talk:Jgharston|talk]]) 06:13, 3 April 2018 (CEST) |
Latest revision as of 05:29, 3 April 2018
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.
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
- Multiplatform programming
- Pathname translation
- Filename character mapping
- FName BASIC library
- Filename BASIC library
Jgharston 03:28, 14 January 2012 (UTC) Jgharston (talk) 06:13, 3 April 2018 (CEST)