]> git.sur5r.net Git - cc65/blob - libsrc/cbm/filename.s
Moved the 'disk' files from 'geos-cbm' to 'geos-common' which are believed to work...
[cc65] / libsrc / cbm / filename.s
1 ;
2 ; Ullrich von Bassewitz, 16.11.2002
3 ;
4 ; File name handling for CBM file I/O
5 ;
6
7         .export         fnparse, fnparsename, fnset
8         .export         fnadd, fnaddmode, fncomplete, fndefunit
9         .export         fnunit, fnlen, fncmd, fnbuf
10
11         .import         SETNAM
12         .import         __curunit, __filetype
13         .importzp       ptr1, tmp1
14
15         .include        "ctype.inc"
16
17
18 ;------------------------------------------------------------------------------
19 ; fnparsename: Parse a filename (without drive spec) passed in in ptr1 and y.
20
21 .proc   fnparsename
22
23         lda     #0
24         sta     tmp1            ; Remember length of name
25
26 nameloop:
27         lda     (ptr1),y        ; Get next char from filename
28         beq     namedone        ; Jump if end of name reached
29
30 ; Check the maximum length, store the character
31
32         ldx     tmp1
33         cpx     #16             ; Maximum length reached?
34         bcs     invalidname
35         lda     (ptr1),y        ; Reload char
36         jsr     fnadd           ; Add character to name
37         iny                     ; Next char from name
38         inc     tmp1            ; Increment length of name
39         bne     nameloop        ; Branch always
40
41 ; Invalid file name
42
43 invalidname:
44         lda     #33             ; Invalid file name
45
46 ; Done, we've successfully parsed the name.
47
48 namedone:
49         rts
50
51 .endproc
52
53
54 ;------------------------------------------------------------------------------
55 ; fnparse: Parse a full filename passed in in a/x. Will set the following
56 ; variables:
57 ;
58 ;       fnlen   -> length of filename
59 ;       fnbuf   -> filename including drive spec
60 ;       fnunit  -> unit from spec or default unit
61 ;
62 ; Returns an error code in A or zero if all is ok.
63
64 .proc   fnparse
65
66         sta     ptr1
67         stx     ptr1+1          ; Save pointer to name
68
69 ; For now we will always use the default unit
70
71         jsr     fndefunit
72
73 ; Check the name for a drive spec
74
75         ldy     #0
76         lda     (ptr1),y
77         cmp     #'0'
78         beq     digit
79         cmp     #'1'
80         bne     nodrive
81
82 digit:  sta     fnbuf+0
83         iny
84         lda     (ptr1),y
85         cmp     #':'
86         bne     nodrive
87
88 ; We found a drive spec, copy it to the buffer
89
90         sta     fnbuf+1
91         iny                     ; Skip colon
92         bne     drivedone       ; Branch always
93
94 ; We did not find a drive spec, always use drive zero
95
96 nodrive:
97         lda     #'0'
98         sta     fnbuf+0
99         lda     #':'
100         sta     fnbuf+1
101         ldy     #$00            ; Reposition to start of name
102
103 ; Drive spec done. We do now have a drive spec in the buffer.
104
105 drivedone:
106         lda     #2              ; Length of drive spec
107         sta     fnlen
108
109 ; Copy the name into the file name buffer. The subroutine returns an error
110 ; code in A and zero flag set if the were no errors.
111
112         jmp     fnparsename
113
114 .endproc
115
116 ;--------------------------------------------------------------------------
117 ; fndefunit: Use the default unit
118
119 .proc   fndefunit
120
121         lda     __curunit
122         sta     fnunit
123         rts
124
125 .endproc
126
127 ;--------------------------------------------------------------------------
128 ; fnset: Tell the kernal about the file name
129
130 .proc   fnset
131
132         lda     fnlen
133         ldx     #<fnbuf
134         ldy     #>fnbuf
135         jmp     SETNAM
136
137 .endproc
138
139 ;--------------------------------------------------------------------------
140 ; fncomplete: Complete a filename by adding ",t,m" where t is the file type
141 ; and m is the access mode passed in in the A register
142 ;
143 ; fnaddmode: Add ",m" to a filename, where "m" is passed in A
144
145 fncomplete:
146         pha                     ; Save mode
147         lda     __filetype
148         jsr     fnaddmode       ; Add the type
149         pla
150 fnaddmode:
151         pha
152         lda     #','
153         jsr     fnadd
154         pla
155
156 fnadd:  ldx     fnlen
157         inc     fnlen
158         sta     fnbuf,x
159         rts
160
161 ;--------------------------------------------------------------------------
162 ; Data
163
164 .bss
165
166 fnunit: .res    1
167 fnlen:  .res    1
168
169 .data
170 fncmd:  .byte   's'     ; Use as scratch command
171 fnbuf:  .res    35      ; Either 0:0123456789012345,t,m
172                         ; Or     0:0123456789012345=0123456789012345