ParseFnm

Unlike Win/DOS, breaking a filename into components is not a trivial task under Qdos/SMSQ. Is the underscore part of a sub directory or file name? Or is it a directory, or even an extension separator? The only way to tell is to try to open the file. Even then, some guesswork is required, and the result is not guaranteed.

This ancient workhorse of mine may not be elegant or perfect, but it has done the job (with small tweaks) over the years. It seems to work in most cases! A new model is in the works, but the underlying fuzzyness of the system has not changed.

This model only works on a complete and existing filename, ie a directory name on its own wont work, nor can the name of a non-existant file be properly parsed.

10 REPeat lp
11  INPUT fnm$: IF fnm$ = '': EXIT lp
12  CLS
13  ERT ParseFnm(fnm$, v$, d$, n$, x$)
14  PRINT fnm$\ v$\ d$\ n$\ x$\
15 END REPeat lp
16 :
17 :
100 DEFine FuNction ParseFnm(f$, v$, d$, n$, e$)
101 LOCal c, p%
102 rem Split (existing, full) file name into components
103 rem V0.03 Ext sep returned with extension
104 rem V0.04 Removed all external references (apart from REMOVE$)
105 rem V0.05, Toolkit-free version
106 :
107 c = FOP_DIR(f$): IF c < 0: RETurn c
108 d$ = FNAME$(#c): CLOSE#c
109 IF LEN(d$) THEN
110  p% = d$ INSTR f$: IF p% = 0: RETurn -7
111  d$ = d$ & '_'
112 ELSE
113  p% = ('_' INSTR f$) + 1: IF p% = 1: RETurn -7
114 END IF
115 v$ = REMV$(p%, 99, f$)
116 IF LEN(v$) < 3: RETurn -12
117 IF v$ = f$: n$ = '': e$ = n$: RETurn 0
118 n$ = REMV$(0, p% + LEN(d$) - 1, f$)
119 FOR p% = LEN(n$) TO 1 STEP -1
120  IF n$(p%) INSTR '_.': EXIT p%
121 END FOR p%
122 e$ = REMV$(0, p% - 1, n$)
123 n$ = REMV$(p%, 99, n$)
124 IF n$ = '' AND e$ <> '': n$ = e$: e$ = ''
125 RETurn 0
126 END DEFine
127 :
128 DEFine FuNction REMV$(from%, too%, str$)
129 REMark V0.02
130 :
131 IF from% < 2 THEN
132  IF too% >= LEN(str$): RETurn ''
133  RETurn str$(too% + 1 TO LEN(str$))
134 END IF
135 IF too% >= LEN(str$) THEN
136  RETurn str$(1 TO from% - 1)
137 ELSE
138  RETurn str$(1 TO from% - 1) & str$(too% + 1 TO LEN(str$))
139 END IF
140 END DEFine
141 :
  
Generated with sb2htm on 2019 Mar 28
©pjwitte March 2oi9