PatchworkOS
Loading...
Searching...
No Matches
vsscanf.c
Go to the documentation of this file.
1#include <ctype.h>
2#include <stdarg.h>
3#include <stdio.h>
4
5#include "common/scan.h"
6
7int vsscanf(const char* _RESTRICT s, const char* _RESTRICT format, va_list arg)
8{
9 /* TODO: This function should interpret format as multibyte characters. */
10 _format_ctx_t ctx;
11 ctx.base = 0;
12 ctx.flags = 0;
13 ctx.maxChars = 0;
14 ctx.totalChars = 0;
15 ctx.currentChars = 0;
16 ctx.buffer = (char*)s;
17 ctx.width = 0;
18 ctx.precision = EOF;
19 ctx.stream = NULL;
20 va_copy(ctx.arg, arg);
21
22 while (*format != '\0')
23 {
24 const char* rc;
25
26 if ((*format != '%') || ((rc = _scan(format, &ctx)) == format))
27 {
28 /* No conversion specifier, match verbatim */
29 if (isspace((unsigned char)*format))
30 {
31 /* Whitespace char in format string: Skip all whitespaces */
32 /* No whitespaces in input do not result in matching error */
33 while (isspace((unsigned char)*ctx.buffer))
34 {
35 ++ctx.buffer;
36 ++ctx.totalChars;
37 }
38 }
39 else
40 {
41 /* Non-whitespace char in format string: Match verbatim */
42 if (*ctx.buffer != *format)
43 {
44 if (*ctx.buffer == '\0' && ctx.maxChars == 0)
45 {
46 /* Early input error */
47 return EOF;
48 }
49
50 /* Matching error */
51 return ctx.maxChars;
52 }
53 else
54 {
55 ++ctx.buffer;
56 ++ctx.totalChars;
57 }
58 }
59
60 ++format;
61 }
62 else
63 {
64 /* NULL return code indicates error */
65 if (rc == NULL)
66 {
67 if ((*ctx.buffer == '\n') && (ctx.maxChars == 0))
68 {
69 ctx.maxChars = EOF;
70 }
71
72 break;
73 }
74
75 /* Continue parsing after conversion specifier */
76 format = rc;
77 }
78 }
79
80 va_end(ctx.arg);
81 return ctx.maxChars;
82}
_PUBLIC int isspace(int c)
Definition isspace.c:5
#define NULL
Pointer error value.
Definition NULL.h:23
#define _RESTRICT
Definition config.h:17
const char * _scan(const char *spec, _format_ctx_t *ctx)
Definition scan.c:117
#define va_copy(dest, src)
Definition stdarg.h:12
#define va_end(ap)
Definition stdarg.h:13
__builtin_va_list va_list
Definition stdarg.h:9
#define EOF
Definition stdio.h:25
va_list arg
Definition format.h:45
int64_t precision
Definition format.h:43
FILE * stream
Definition format.h:44
uint64_t maxChars
Definition format.h:38
uint64_t width
Definition format.h:42
_format_flags_t flags
Definition format.h:37
int32_t base
Definition format.h:36
uint64_t currentChars
Definition format.h:40
char * buffer
Definition format.h:41
uint64_t totalChars
Definition format.h:39
int vsscanf(const char *_RESTRICT s, const char *_RESTRICT format, va_list arg)
Definition vsscanf.c:7