]> git.sur5r.net Git - cc65/blob - libsrc/common/vfscanf.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / common / vfscanf.s
1 ;
2 ; int __fastcall__ vfscanf (FILE* f, const char* format, va_list ap);
3 ;
4 ; 2004-11-27, Ullrich von Bassewitz
5 ; 2004-12-21, Greg King
6 ;
7
8         .export         _vfscanf
9         .import         _fgetc, _ungetc, _ferror
10
11         .include        "zeropage.inc"
12         .include        "_scanf.inc"
13         .include        "stdio.inc"
14
15
16 count   :=      ptr3            ; Result of scan
17
18
19 ; ----------------------------------------------------------------------------
20 ; Static scanfdata structure for the _vfscanf routine
21 ;
22
23 .data
24 d:      .addr   _fgetc          ; GET
25         .addr   _ungetc         ; UNGET
26         .addr   0               ; data
27
28
29 ; ----------------------------------------------------------------------------
30 ; int __fastcall__ vfscanf (FILE* f, const char* format, va_list ap)
31 ; /* Standard C function */
32 ; {
33 ;     /* Initialize the data struct. We do only need the given file as user data,
34 ;      * because the (getfunc) and (ungetfunc) functions are crafted so that they
35 ;      * match the standard-I/O fgetc() and ungetc().
36 ;      */
37 ;     static struct scanfdata d = {
38 ;         (  getfunc)  fgetc,
39 ;         (ungetfunc) ungetc
40 ;     };
41 ;     static int count;
42 ;
43 ;     d.data = (void*) f;
44 ;
45 ;     /* Call the internal function */
46 ;     count = _scanf (&d, format, ap);
47 ;
48 ;     /* And, return the result */
49 ;     return ferror (f) ? EOF : count;
50 ; }
51 ;
52 ; Because _scanf() has the same parameter stack as vfscanf(), with f replaced
53 ; by &d, we will do exactly that.  _scanf() then will clean up the stack.
54 ; Beware: Since ap is a fastcall parameter, we must not destroy a/x.
55 ;
56
57 .code
58 _vfscanf:
59         pha                     ; Save low byte of ap
60
61 ; Swap f against &d on the stack, placing f into d.data
62
63         ldy     #2              ; Offset of f on the stack
64         lda     (sp),y
65         sta     d + SCANFDATA::DATA
66         lda     #<d
67         sta     (sp),y
68
69         iny                     ; High byte
70         lda     (sp),y
71         sta     d + SCANFDATA::DATA + 1
72         lda     #>d
73         sta     (sp),y
74
75 ; Restore the low byte of ap, and call the _scanf function
76
77         pla
78         jsr     __scanf
79         sta     count
80         stx     count+1
81
82 ; Return -1 if there was a read error during the scan
83
84         lda     d + SCANFDATA::DATA     ; Get f
85         ldx     d + SCANFDATA::DATA+1
86         jsr     _ferror
87         tay
88         beq     L1
89         lda     #<EOF
90         tax
91         rts
92
93 ; Or, return the result of the scan
94
95 L1:     lda     count
96         ldx     count+1
97         rts
98