2 * Copyright (C) 2001-2008, Parrot Foundation.
6 * Miscellaneous functions, mainly the Parrot_sprintf family
7 * Data Structure and Algorithms:
8 * Uses a generalized formatting algorithm (spf_render.c) with
9 * a specialized vtable (spf_vtable.c) to handle argument extraction.
11 * This file used to be a simple one, with just a few function prototypes
12 * in it. When misc.c was split in three, this file took on the job of
13 * being the internal header as well.
14 * To expose the guts of Parrot_sprintf, define IN_SPF_SYSTEM before
15 * including this file.
17 * References: misc.c, spf_vtable.c, spf_render.c
20 #ifndef PARROT_MISC_H_GUARD
21 #define PARROT_MISC_H_GUARD
23 #include "parrot/parrot.h"
25 #define FLOAT_IS_ZERO(f) ((f) == 0.0)
27 #ifndef PARROT_HAS_C99_SNPRINTF
28 # define snprintf Parrot_secret_snprintf
31 typedef int (*reg_move_func
)(PARROT_INTERP
, unsigned char d
, unsigned char s
, void *);
33 /* HEADERIZER BEGIN: src/utils.c */
34 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
37 INTVAL
Parrot_byte_index(SHIM_INTERP
,
38 ARGIN(const STRING
*base
),
39 ARGIN(const STRING
*search
),
41 __attribute__nonnull__(2)
42 __attribute__nonnull__(3);
45 PARROT_WARN_UNUSED_RESULT
46 INTVAL
Parrot_byte_rindex(SHIM_INTERP
,
47 ARGIN(const STRING
*base
),
48 ARGIN(const STRING
*search
),
50 __attribute__nonnull__(2)
51 __attribute__nonnull__(3);
54 FLOATVAL
Parrot_float_rand(INTVAL how_random
);
57 INTVAL
Parrot_int_rand(INTVAL how_random
);
60 INTVAL
Parrot_range_rand(INTVAL from
, INTVAL to
, INTVAL how_random
);
63 void Parrot_register_move(PARROT_INTERP
,
65 ARGOUT(unsigned char *dest_regs
),
66 ARGIN(unsigned char *src_regs
),
67 unsigned char temp_reg
,
69 reg_move_func mov_alt
,
71 __attribute__nonnull__(1)
72 __attribute__nonnull__(3)
73 __attribute__nonnull__(4)
74 __attribute__nonnull__(8)
75 FUNC_MODIFIES(*dest_regs
);
78 void Parrot_srand(INTVAL seed
);
81 INTVAL
Parrot_uint_rand(INTVAL how_random
);
84 FLOATVAL
floatval_mod(FLOATVAL n2
, FLOATVAL n3
);
87 INTVAL
intval_mod(INTVAL i2
, INTVAL i3
);
89 void Parrot_quicksort(PARROT_INTERP
,
93 __attribute__nonnull__(1)
94 __attribute__nonnull__(2)
95 __attribute__nonnull__(4)
98 PARROT_WARN_UNUSED_RESULT
99 PARROT_CANNOT_RETURN_NULL
100 PMC
* tm_to_array(PARROT_INTERP
, ARGIN(const struct tm
*tm
))
101 __attribute__nonnull__(1)
102 __attribute__nonnull__(2);
104 #define ASSERT_ARGS_Parrot_byte_index __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
105 PARROT_ASSERT_ARG(base) \
106 , PARROT_ASSERT_ARG(search))
107 #define ASSERT_ARGS_Parrot_byte_rindex __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
108 PARROT_ASSERT_ARG(base) \
109 , PARROT_ASSERT_ARG(search))
110 #define ASSERT_ARGS_Parrot_float_rand __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
111 #define ASSERT_ARGS_Parrot_int_rand __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
112 #define ASSERT_ARGS_Parrot_range_rand __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
113 #define ASSERT_ARGS_Parrot_register_move __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
114 PARROT_ASSERT_ARG(interp) \
115 , PARROT_ASSERT_ARG(dest_regs) \
116 , PARROT_ASSERT_ARG(src_regs) \
117 , PARROT_ASSERT_ARG(info))
118 #define ASSERT_ARGS_Parrot_srand __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
119 #define ASSERT_ARGS_Parrot_uint_rand __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
120 #define ASSERT_ARGS_floatval_mod __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
121 #define ASSERT_ARGS_intval_mod __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
122 #define ASSERT_ARGS_Parrot_quicksort __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
123 PARROT_ASSERT_ARG(interp) \
124 , PARROT_ASSERT_ARG(data) \
125 , PARROT_ASSERT_ARG(cmp))
126 #define ASSERT_ARGS_tm_to_array __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
127 PARROT_ASSERT_ARG(interp) \
128 , PARROT_ASSERT_ARG(tm))
129 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
130 /* HEADERIZER END: src/utils.c */
133 /* HEADERIZER BEGIN: src/misc.c */
134 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
137 PARROT_WARN_UNUSED_RESULT
138 PARROT_CANNOT_RETURN_NULL
139 STRING
* Parrot_psprintf(PARROT_INTERP
,
142 __attribute__nonnull__(1)
143 __attribute__nonnull__(2)
144 __attribute__nonnull__(3)
148 int Parrot_secret_snprintf(
149 ARGOUT(char *buffer
),
150 NULLOK(const size_t len
),
151 ARGIN(const char *format
),
153 __attribute__nonnull__(1)
154 __attribute__nonnull__(3)
155 FUNC_MODIFIES(*buffer
);
158 void Parrot_snprintf(PARROT_INTERP
,
161 ARGIN(const char *pat
),
163 __attribute__nonnull__(1)
164 __attribute__nonnull__(2)
165 __attribute__nonnull__(4)
166 FUNC_MODIFIES(*targ
);
169 PARROT_WARN_UNUSED_RESULT
170 PARROT_CANNOT_RETURN_NULL
171 STRING
* Parrot_sprintf_c(PARROT_INTERP
, ARGIN(const char *pat
), ...)
172 __attribute__nonnull__(1)
173 __attribute__nonnull__(2);
176 PARROT_WARN_UNUSED_RESULT
177 PARROT_CANNOT_RETURN_NULL
178 STRING
* Parrot_sprintf_s(PARROT_INTERP
, ARGIN(STRING
*pat
), ...)
179 __attribute__nonnull__(1)
180 __attribute__nonnull__(2);
183 void Parrot_vsnprintf(PARROT_INTERP
,
186 ARGIN(const char *pat
),
188 __attribute__nonnull__(1)
189 __attribute__nonnull__(2)
190 __attribute__nonnull__(4)
191 FUNC_MODIFIES(*targ
);
194 PARROT_CANNOT_RETURN_NULL
195 STRING
* Parrot_vsprintf_c(PARROT_INTERP
,
196 ARGIN(const char *pat
),
198 __attribute__nonnull__(1)
199 __attribute__nonnull__(2);
202 PARROT_WARN_UNUSED_RESULT
203 PARROT_CANNOT_RETURN_NULL
204 STRING
* Parrot_vsprintf_s(PARROT_INTERP
, ARGIN(STRING
*pat
), va_list args
)
205 __attribute__nonnull__(1)
206 __attribute__nonnull__(2);
208 #define ASSERT_ARGS_Parrot_psprintf __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
209 PARROT_ASSERT_ARG(interp) \
210 , PARROT_ASSERT_ARG(pat) \
211 , PARROT_ASSERT_ARG(ary))
212 #define ASSERT_ARGS_Parrot_secret_snprintf __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
213 PARROT_ASSERT_ARG(buffer) \
214 , PARROT_ASSERT_ARG(format))
215 #define ASSERT_ARGS_Parrot_snprintf __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
216 PARROT_ASSERT_ARG(interp) \
217 , PARROT_ASSERT_ARG(targ) \
218 , PARROT_ASSERT_ARG(pat))
219 #define ASSERT_ARGS_Parrot_sprintf_c __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
220 PARROT_ASSERT_ARG(interp) \
221 , PARROT_ASSERT_ARG(pat))
222 #define ASSERT_ARGS_Parrot_sprintf_s __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
223 PARROT_ASSERT_ARG(interp) \
224 , PARROT_ASSERT_ARG(pat))
225 #define ASSERT_ARGS_Parrot_vsnprintf __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
226 PARROT_ASSERT_ARG(interp) \
227 , PARROT_ASSERT_ARG(targ) \
228 , PARROT_ASSERT_ARG(pat))
229 #define ASSERT_ARGS_Parrot_vsprintf_c __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
230 PARROT_ASSERT_ARG(interp) \
231 , PARROT_ASSERT_ARG(pat))
232 #define ASSERT_ARGS_Parrot_vsprintf_s __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
233 PARROT_ASSERT_ARG(interp) \
234 , PARROT_ASSERT_ARG(pat))
235 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
236 /* HEADERIZER END: src/misc.c */
241 /* The size of all our buffers (and the maximum width of a float
244 # define PARROT_SPRINTF_BUFFER_SIZE 4096
246 /* The maximum floating-point precision--hopefully this and the
247 * width cap will help head off buffer overruns.
249 # define PARROT_SPRINTF_MAX_PREC 3 * PARROT_SPRINTF_BUFFER_SIZE / 4
251 # define cstr2pstr(cstr) string_make(interp, (cstr), strlen(cstr), "ascii", 0)
252 # define char2pstr(ch) string_make(interp, &(ch), 1, "ascii", 0)
254 /* SPRINTF DATA STRUCTURE AND FLAGS */
267 /* SPRINTF ARGUMENT OBJECT */
269 typedef struct sprintf_obj SPRINTF_OBJ
;
271 typedef STRING
*(*sprintf_getchar_t
) (PARROT_INTERP
, INTVAL
, ARGIN(SPRINTF_OBJ
*));
272 typedef HUGEINTVAL(*sprintf_getint_t
) (PARROT_INTERP
, INTVAL
, ARGIN(SPRINTF_OBJ
*));
273 typedef UHUGEINTVAL(*sprintf_getuint_t
) (PARROT_INTERP
, INTVAL
, ARGIN(SPRINTF_OBJ
*));
274 typedef HUGEFLOATVAL(*sprintf_getfloat_t
) (PARROT_INTERP
, INTVAL
, ARGIN(SPRINTF_OBJ
*));
275 typedef STRING
*(*sprintf_getstring_t
) (PARROT_INTERP
, INTVAL
, ARGIN(SPRINTF_OBJ
*));
276 typedef void *(*sprintf_getptr_t
) (PARROT_INTERP
, INTVAL
, ARGIN(SPRINTF_OBJ
*));
282 sprintf_getchar_t getchr
;
283 sprintf_getint_t getint
;
284 sprintf_getuint_t getuint
;
285 sprintf_getfloat_t getfloat
;
286 sprintf_getstring_t getstring
;
287 sprintf_getptr_t getptr
;
290 extern SPRINTF_OBJ pmc_core
;
291 extern const SPRINTF_OBJ va_core
;
293 /* HEADERIZER BEGIN: src/spf_render.c */
294 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
296 PARROT_WARN_UNUSED_RESULT
297 PARROT_CANNOT_RETURN_NULL
298 STRING
* Parrot_sprintf_format(PARROT_INTERP
,
300 ARGIN(SPRINTF_OBJ
*obj
))
301 __attribute__nonnull__(1)
302 __attribute__nonnull__(2)
303 __attribute__nonnull__(3);
305 #define ASSERT_ARGS_Parrot_sprintf_format __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
306 PARROT_ASSERT_ARG(interp) \
307 , PARROT_ASSERT_ARG(pat) \
308 , PARROT_ASSERT_ARG(obj))
309 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
310 /* HEADERIZER END: src/spf_render.c */
312 #endif /* IN_SPF_SYSTEM */
316 * generated src/revision.c
318 PARROT_EXPORT
int Parrot_config_revision(void);
320 #endif /* PARROT_MISC_H_GUARD */
324 * c-file-style: "parrot"
326 * vim: expandtab shiftwidth=4: