Subversion Repositories shark

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/* Project:     OSLib
2
 * Description: The OS Construction Kit
3
 * Date:                1.6.2000
4
 * Idea by:             Luca Abeni & Gerardo Lamastra
5
 *
6
 * OSLib is an SO project aimed at developing a common, easy-to-use
7
 * low-level infrastructure for developing OS kernels and Embedded
8
 * Applications; it partially derives from the HARTIK project but it
9
 * currently is independently developed.
10
 *
11
 * OSLib is distributed under GPL License, and some of its code has
12
 * been derived from the Linux kernel source; also some important
13
 * ideas come from studying the DJGPP go32 extender.
14
 *
15
 * We acknowledge the Linux Community, Free Software Foundation,
16
 * D.J. Delorie and all the other developers who believe in the
17
 * freedom of software and ideas.
18
 *
19
 * For legalese, check out the included GPL license.
20
 */
21
 
22
#include <ll/i386/stdlib.h>
23
#include <ll/i386/string.h>
24
#include <ll/i386/limits.h>
25
#include <ll/stdarg.h>
26
#include <ll/ctype.h>
27
#include <ll/math.h>
28
#include "sprintf.h"
29
 
30
FILE(sscanf);
31
 
32
#define STD_SIZE        0
33
#define SHORT_SIZE      1
34
#define LONG_SIZE       2
35
 
36
int vsscanf(char *buf,char *fmt,va_list parms)
37
{
38
    int scanned = 0,size = 0,suppress = 0;
39
    int w = 0,flag = 0,l = 0;
40
    char c,*c_ptr;
41
    long int n1,*n1l;    
42
    int *n1b;
43
    short int *n1s;
44
    long unsigned n2,*n2l,parsing = 0;
45
    unsigned *n2b;
46
    short unsigned *n2s;
47
    double n3,*n3l;
48
    float *n3s;
49
    char *base = buf;
50
    while (*fmt != 0) {
51
        if (*fmt != '%' && !parsing) {
52
            /* No token detected */
53
            fmt++;
54
        }
55
        else {
56
            /* We need to make a conversion */
57
            if (*fmt == '%') {
58
                fmt++;
59
                parsing = 1;
60
                size = STD_SIZE;
61
                suppress = 0;
62
                w = 0;
63
                flag = 0;
64
                l = 0;
65
            }
66
            /* Parse token */
67
            switch(*fmt) {
68
                case '1' :
69
                case '2' :
70
                case '3' :
71
                case '4' :
72
                case '5' :
73
                case '6' :
74
                case '7' :
75
                case '8' :
76
                case '9' :
77
                case '0' : if (parsing == 1) {
78
                              w = strtou(fmt,10,&base);
79
                              /* We use SPACE_PAD to parse %10s
80
                                 commands where the number is the
81
                                 maximum number of char to store!
82
                              */
83
                              flag |= SPACE_PAD;
84
                              fmt = base-1;
85
                           }
86
                           break;
87
                case 'c' : c = *buf++;
88
                           c_ptr = va_arg(parms, char *);
89
                           *c_ptr = c;
90
                           scanned++;
91
                           parsing = 0;
92
                           break;
93
                case 's' : c_ptr = va_arg(parms, char *);
94
                           while (*buf != 0 && isspace(*buf)) buf++;
95
                           l = 0;
96
                           while (*buf != 0 && !isspace(*buf)) {
97
                               if (!(flag & SPACE_PAD)) *c_ptr++ = *buf;
98
                               else if (l < w) {
99
                                   *c_ptr++ = *buf;
100
                                   l++;
101
                               }
102
                               buf++;        
103
                           }
104
                           *c_ptr = 0;
105
                           scanned++;
106
                           parsing = 0;
107
                           break;
108
                case 'i' :
109
                case 'd' : buf = strscn(buf,"1234567890-+");
110
                           n1 = strtoi(buf,10,&base);
111
                           buf = base;
112
                           if (!suppress) {
113
                               switch (size) {
114
                                   case STD_SIZE : n1b = va_arg(parms, int *);
115
                                                   *n1b = (int)n1;
116
                                                   break;
117
                                   case LONG_SIZE : n1l = va_arg(parms, long int *);
118
                                                    *n1l = n1;
119
                                                    break;
120
                                   case SHORT_SIZE : n1s = va_arg(parms, short int *);
121
                                                     *n1s = (short)(n1);
122
                                                     break;
123
                               }
124
                               scanned++;
125
                           }
126
                           parsing = 0;
127
                           break;
128
                case 'u' : buf = strscn(buf,"1234567890");
129
                           n2 = strtou(buf,10,&base);
130
                           buf = base;
131
                           if (!suppress) {
132
                              switch (size) {
133
                                  case STD_SIZE : n2b = va_arg(parms, unsigned *);
134
                                                  *n2b = (unsigned)n2;
135
                                                  break;
136
                                  case LONG_SIZE : n2l = va_arg(parms, long unsigned *);
137
                                                   *n2l = n2;
138
                                                   break;
139
                                  case SHORT_SIZE : n2s = va_arg(parms, short unsigned *);
140
                                                    *n2s = (short)(n2);
141
                                                    break;
142
                              }
143
                              scanned++;
144
                           }
145
                           parsing = 0;
146
                           break;
147
                case 'x' : buf = strscn(buf,"1234567890xabcdefABCDEF");
148
                           n2 = strtou(buf,16,&base);
149
                           buf = base;
150
                           if (!suppress) {
151
                              switch (size) {                            
152
                                  case STD_SIZE : n2b = va_arg(parms, unsigned *);
153
                                                  *n2b = (unsigned)n2;
154
                                                  break;
155
                                  case LONG_SIZE : n2l = va_arg(parms, long unsigned *);
156
                                                   *n2l = n2;
157
                                                   break;
158
                                  case SHORT_SIZE : n2s = va_arg(parms, short unsigned *);
159
                                                    *n2s = (short)(n2);
160
                                                    break;
161
                              }
162
                              scanned++;
163
                           }
164
                           parsing = 0;
165
                           break;
166
                case 'f' :
167
                case 'g' :
168
                case 'e' : buf = strscn(buf,"1234567890.e+-");
169
                           n3 = strtod(buf,&base);
170
                           buf = base;
171
                           if (!suppress) {                            
172
                               switch (size) {                                                           
173
                                   case STD_SIZE : n3l = va_arg(parms, double *);
174
                                                   *n3l = n3;
175
                                                   break;
176
                                   case LONG_SIZE : n3l = va_arg(parms, double *);
177
                                                    *n3l = n3;
178
                                                    break;
179
                                   case SHORT_SIZE : n3s = va_arg(parms, float *);
180
                                                     *n3s = (float)(n3);
181
                                                     break;
182
                               }
183
                               scanned++;
184
                           }
185
                           parsing = 0;
186
                           break;
187
                case 'l' : size = LONG_SIZE;
188
                           break;
189
                case 'h' :
190
                case 'n' : size = SHORT_SIZE;
191
                           break;
192
                case '*' : suppress = 1;
193
                           break;
194
                default :  parsing = 0;
195
                           break;
196
            }
197
            fmt++;
198
        }
199
    }
200
    return(scanned);
201
}
202
 
203
int sscanf(char *buf,char *fmt,...)
204
{
205
    va_list parms;
206
    int result;
207
    va_start(parms,fmt);
208
    result = vsscanf(buf,fmt,parms);
209
    va_end(parms);
210
    return(result);
211
}