-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi_xs.c
75 lines (68 loc) · 1.42 KB
/
i_xs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* This file is part of "jelio".
* jelio is an input/output library that replaces the standard C IO library.
*
* Copyright: Jens Låås, SLU 2004
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#include "jelio.h"
#include "jelio_internal.h"
#include <stdlib.h>
#include <ctype.h>
static int i_xs_conv(void *value, int n, void *inbuf)
{
struct jelio_buf *jb = inbuf;
unsigned char *sval = value;
unsigned int c;
int count=0;
int scount=0;
int flag = 0;
/* scan past whitespace */
for(c = jelio_buffer_peekc(jb);char_isspace(c);c = jelio_buffer_peekc(jb))
{
jelio_buffer_getc(jb);
count++;
}
if(n < 1) return 0;
while(1)
{
if(scount >= n)
break;
c = jelio_buffer_peekc(jb);
if(c == -1 || char_isspace(c))
break;
if( (c < '0' || c > '9') && (c < 'a' || c > 'f'))
break;
count++;
if(flag)
{
if(c >= '0' && c <= '9')
*sval++ += (c - '0');
else
*sval++ += (c - 'a' + 10);
scount++;
flag = 0;
}
else
{
if(c >= '0' && c <= '9')
*sval = (c - '0') << 4;
else
*sval = (c - 'a' + 10) << 4;
flag = 1;
}
jelio_buffer_getc(jb);
}
return count;
}
struct jelio_input *i_xs(unsigned char *s, int valsize)
{
struct jelio_input *ji;
ji = malloc(sizeof(struct jelio_input));
ji->data = s;
ji->valsize = valsize;
ji->callback = i_xs_conv;
ji->free = NULL;
return ji;
}