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