Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tscanf1.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
/* test to see sfvscanf still works ok with a discipline that
23
** returns one byte at a time.
24
*/
25
26
typedef struct _mydisc_s
27
{
28
Sfdisc_t disc;
29
Sfio_t* f;
30
} Mydisc_t;
31
32
#if __STD_C
33
static ssize_t oneread(Sfio_t* f, Void_t* buf, size_t size, Sfdisc_t* disc)
34
#else
35
static ssize_t oneread(f, buf, size, disc)
36
Sfio_t* f;
37
Void_t* buf;
38
size_t size;
39
Sfdisc_t* disc;
40
#endif
41
{
42
Mydisc_t *dc = (Mydisc_t*)disc;
43
44
if(!f || size <= 0)
45
return -1;
46
return sfread(dc->f, buf, 1);
47
}
48
49
static Mydisc_t Mydisc;
50
51
tmain()
52
{
53
int n;
54
double d;
55
int i;
56
char s[10];
57
Sfio_t *f, *str;
58
59
if((n = sfsscanf("ten 10 10", "%s %d %lf", s, &i, &d)) != 3)
60
terror("Bad scanning %d", n);
61
if(strncmp("ten",s,3) != 0 || i != 10 || d != 10)
62
terror("Bad scanned values ");
63
64
if(!(f = sfnew((Sfio_t*)0, (Void_t*)0, (size_t)SF_UNBOUND, 0, SF_READ) ) )
65
terror("Can't create stream f");
66
if(!(str = sfopen(0, "ten 10 10", "s")) )
67
terror("Can't open string stream str");
68
Mydisc.disc.readf = oneread;
69
Mydisc.f = str;
70
sfdisc(f,&Mydisc.disc);
71
72
s[0] = 0; i = 0; d = 0;
73
if((n = sfscanf(f, "%s %d %lf", s, &i, &d)) != 3)
74
terror("Bad scanning %d", n);
75
if(strncmp("ten",s,3) != 0 || i != 10 || d != 10)
76
terror("Bad scanned values2");
77
78
return 0;
79
}
80
81