Dir Up/Dir Down

These functions dont physically move up or down a directory tree. They merely manipulate the directory string. The directory, or a file attached to the directory string, can then be opened at the desired level.

I had to make a small fix as it turns out SuperBASIC barfs at the variable name sub$! Also took the oportunity to remove a reference to DMEDIUM_NAME$ as it doesnt help much and makes the routine incompatible with SuperBASIC.

Note:

Although the behaviour of truncating directory names to the first valid directory level is not documented anywhere (AFAIK) it has been the standard across all QL and SMSQ/E DDDs written by Tony Tebby, and has been adopted by most other implementors of such drivers, such as QPC2's DOS device and SMSQmulator's NFA and SFA devices. Q-emulator has had a recent update (V3.22) to ensure this quirk is replicated there too. So I think that by now, we could consider this behaviour a settled standard!

10 CLS
12 d$ = 'abcdef': dir$ = 'ram1_'
14 :
16 FOR i = 1 TO LEN(d$)
18  dir$ = DirDn$(dir$, d$(i))
20  PRINT 'Creating'! dir$
22  MAKE_DIR dir$
24 END FOR i
26 :
28 d$ = dir$
30 dir$ = dir$ & 'test_bas'
32 PRINT 'Saving'! dir$
34 SAVE dir$
36 :
38 PRINT 'Press a key to continue'
40 BEEP 1999, 2: PAUSE
42 :
44 dir$ = d$ & 'test_bas'
46 PRINT 'Deleting'! dir$
48 DELETE dir$
50 REPeat loop
52  dir$ = d$
54  d$ = DirUp$(dir$)
56  PRINT 'Deleting'! dir$
58  IF LEN(dir$) = 5: EXIT loop
60  DELETE dir$
62 END REPeat loop
64 PRINT 'Done!'
66 :
68 :
100 rem + ------------------------------------------------------------------------ +
110 rem |<                                 Dir Up                                 >|
120 rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
130 rem |                      "Move" up one directory level                       |
140 rem |                                                                          |
150 rem | This routine requires dir$ to contain a valid, existing, directory.      |
160 rem | It will ignore any file name appended to the given directory.            |
170 rem | If dir$ is not a valid directory it is returned unchanged, ie a DATAD$   |
180 rem | fallback is not accepted. (An alternative could be to return blank to    |
190 rem | indicate error, as the same behaviour occurs when top level is reached.) |
200 rem + ------------------------------------------------------------------------ +
210 rem | V0.01, pjw, 2001 Mar 04                                                  |
220 rem | V0.04, pjw, 2019 Aug 04, SuperBASIC compatibility restored               |
230 rem + ------------------------------------------------------------------------ +
240 :
250 DEFine FuNction DirUp$(dir$)
260 LOCal ch, l%, dr$(36), dv$(5)
270 IF LEN(dir$) < 5: RETurn dir$
280 dv$ = dir$(1 TO 5)
290 ch = FOP_DIR(dir$): dr$ = FNAME$(#ch): CLOSE#ch
300 l% = LEN(dr$) - 1: IF l% < 0: RETurn dv$
310 ch = FOP_DIR(dv$ & dr$(1 TO l%))
320 dr$ = FNAME$(#ch): CLOSE#ch
330 IF LEN(dr$) = 0: RETurn dv$
340 RETurn dv$ & dr$ & '_'
350 END DEFine DirUp$
360 :
370 rem + ------------------------------------------------------------------------ +
380 rem |<                                Dir Down                                >|
390 rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
400 rem |                    "Move" down one directory level                       |
410 rem |                                                                          |
420 rem | Does not check whether directory exists or max name length exceeded.     |
430 rem | Always returns trailing underscore.                                      |
440 rem + ------------------------------------------------------------------------ +
450 rem | V0.01, pjw, 2019 Mar 28, Based on earlier models                         |
460 rem | V0.02, pjw, 2019 Aug 04, SuperBASIC (JS) doesnt like sub$!               |
470 rem + ------------------------------------------------------------------------ +
480 :
490 DEFine FuNction DirDn$(dir$, subd$)
500 LOCal d$(41)
510 IF LEN(dir$) = 0: RETurn ''
520 IF dir$(LEN(dir$)) = '_' THEN
530  d$ = dir$ & subd$
540 ELSE
550  d$ = dir$ & '_' & subd$
560 END IF
570 IF d$(LEN(d$)) = '_': RETurn d$
580 RETurn d$ & '_'
590 END DEFine DirDn$
600 :
610 :
  
Generated with sb2htm on 2019 Aug 05
©pjwitte March 2oi9