File: utils.c

package info (click to toggle)
libapt-pkg-perl 0.1.36
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 304 kB
  • sloc: perl: 1,233; ansic: 201; makefile: 41
file content (258 lines) | stat: -rw-r--r-- 4,082 bytes parent folder | download | duplicates (5)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"

#include <stdarg.h>
#include "utils.h"

/*
 * Parse an array reference in a similar manner to python's ParseTuple
 *
 *   p_avref  a pointer to an SV containing (hopefully) an AV reference
 *   fmt      a format string defining how to parse the elements of the
 *	      AV:
 *
 *		s	String (char const *).  If followed by `#', the
 *		s#	length is placed into the argument following
 *			(STRLEN *).
 *
 *		z	A variant of `s' in which permits the value to
 *		z#	be undefined (converted to NULL).
 *
 *		h	Short integer (short int *).
 *		i	Integer (int *).
 *		l	Long integer (long *).
 *		c	The first character of a string (char *).
 *		f	Floating point number (float *).
 *		d	Double precision floating point number (double *).
 *		@	Array reference (AV **).
 *		%	Hash reference (HV **).
 *		&	Code reference (CV **).
 *		|	Optional elements follow.
 *
 * The return value is a string giving the first error encountered (if
 * any).
 */

char const *parse_avref(pTHX_ SV **p_avref, char const *fmt, ...)
{
    va_list ap;
    char const *err = 0;
    AV *av;
    I32 len = 0;

    va_start(ap, fmt);
    if (p_avref && SvROK(*p_avref) && SvTYPE(SvRV(*p_avref)) == SVt_PVAV)
    {
	av = (AV *) SvRV(*p_avref);
	len = av_len(av) + 1;
    }
    else
	err = "array reference required";

    I32 i = 0;
    int opt = 0;

    while (*fmt)
    {
	SV *v = 0;
	char type = *fmt++;

	if (!err)
	{
	    if (i < len)
	    {
		SV **tmp = av_fetch(av, i++, 0);
		if (tmp)
		    v = *tmp;
	    }
	    else if (!opt)
	    {
		err = "too few elements";
	    }
	}

	switch (type)
	{
	case 's':
	case 'z':
	    {
		char const **s = va_arg(ap, char const **);
		STRLEN *l = &PL_na;
		if (*fmt == '#')
		{
		    l = va_arg(ap, STRLEN *);
		    fmt++;
		}

		*s = 0;
		*l = 0;
		if (v)
		{
		    if (SvOK(v))
		    {
			if (SvPOK(v) || SvNIOK(v))
			    *s = SvPV(v, *l);
			else
			    err = "invalid string";
		    }
		    else if (type != 'z')
			err = "undefined element";
		}
	    }
	    break;

	case 'h':
	    {
		short *s = va_arg(ap, short *);
		*s = 0;

		if (v)
		{
		    if (SvNIOK(v))
			*s = (short) SvIV(v);
		    else
			err = "short int expected";
		}
	    }
	    break;

	case 'i':
	    {
		int *i = va_arg(ap, int *);
		*i = 0;

		if (v)
		{
		    if (SvNIOK(v))
			*i = (int) SvIV(v);
		    else
			err = "integer expected";
		}
	    }
	    break;

	case 'l':
	    {
		long *l = va_arg(ap, long *);
		*l = 0;

		if (v)
		{
		    if (SvNIOK(v))
			*l = (long) SvIV(v);
		    else
			err = "long integer expected";
		}
	    }
	    break;

	case 'c':
	    {
		char *c = va_arg(ap, char *);
		*c = 0;

		if (v)
		{
		    char const *s;
		    STRLEN l;

		    if ((SvPOK(v) || SvNIOK(v)) && (s = SvPV(v, l)) && l)
			*c = *s;
		}
	    }
	    break;

	case 'f':
	    {
		float *f = va_arg(ap, float *);
		*f = 0;

		if (v)
		{
		    if (SvNIOK(v))
			*f = (float) SvNV(v);
		    else
			err = "float expected";
		}
	    }
	    break;

	case 'd':
	    {
		double *d = va_arg(ap, double *);
		*d = 0;

		if (v)
		{
		    if (SvNIOK(v))
			*d = (double) SvNV(v);
		    else
			err = "double expected";
		}
	    }
	    break;

	case '@':
	    {
		AV **a = va_arg(ap, AV **);
		*a = 0;

		if (v)
		{
		    if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)
			*a = (AV *) SvRV(v);
		    else
			err = "array ref expected";
		}
	    }
	    break;

	case '%':
	    {
		HV **h = va_arg(ap, HV **);
		*h = 0;

		if (v)
		{
		    if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV)
			*h = (HV *) SvRV(v);
		    else
			err = "hash ref expected";
		}
	    }
	    break;

	case '&':
	    {
		CV **c = va_arg(ap, CV **);
		*c = 0;

		if (v)
		{
		    if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV)
			*c = (CV *) SvRV(v);
		    else
			err = "code ref expected";
		}
	    }
	    break;

	default:
	    croak("parse_avref: invalid format character `%c'", type);
	}

	/* "|" = args following are optional */
	if (*fmt == '|')
	{
	    fmt++;
	    opt++;
	}
    }

    if (!err && i < len)
	err = "extra elements";

    va_end(ap);
    return err;
}