2 * A helper routine to copy the strings between differing structures.
11 #define MAX_OFFSETS 10
13 #define OFFSET_SHIFT 1
15 #define lstr_at(p, n) (*(char**)(((char*)(p))+(n >> OFFSET_SHIFT)))
17 #define str_at(p, n) ( \
18 (((n) & MPH_STRING_OFFSET_MASK) == MPH_STRING_OFFSET_ARRAY) \
19 ? (char*)(p) + (n >> OFFSET_SHIFT) \
24 _mph_copy_structure_strings (
25 void *to
, const mph_string_offset_t
*to_offsets
,
26 const void *from
, const mph_string_offset_t
*from_offsets
,
32 char *buf
, *cur
= NULL
;
34 g_assert (num_strings
< MAX_OFFSETS
);
36 for (i
= 0; i
< num_strings
; ++i
) {
37 lstr_at (to
, to_offsets
[i
]) = NULL
;
41 for (i
= 0; i
< num_strings
; ++i
) {
42 len
[i
] = strlen (str_at(from
, from_offsets
[i
]));
43 if (len
[i
] < INT_MAX
- buflen
)
49 cur
= buf
= malloc (buflen
);
54 for (i
= 0; i
< num_strings
; ++i
) {
56 lstr_at (to
, to_offsets
[i
]) =
57 strcpy (cur
, str_at (from
, from_offsets
[i
]));
87 /* test copying foo to bar */
88 struct foo f
= {"hello", 42, "world", "!!"};
90 mph_string_offset_t foo_offsets
[] = {
91 MPH_STRING_OFFSET(struct foo
, a
, MPH_STRING_OFFSET_PTR
),
92 MPH_STRING_OFFSET(struct foo
, c
, MPH_STRING_OFFSET_PTR
),
93 MPH_STRING_OFFSET(struct foo
, d
, MPH_STRING_OFFSET_ARRAY
)
95 mph_string_offset_t bar_offsets
[] = {
96 MPH_STRING_OFFSET(struct bar
, a
, MPH_STRING_OFFSET_PTR
),
97 MPH_STRING_OFFSET(struct bar
, c
, MPH_STRING_OFFSET_PTR
),
98 MPH_STRING_OFFSET(struct bar
, e
, MPH_STRING_OFFSET_PTR
)
102 buf
= _mph_copy_structure_strings (&b
, bar_offsets
,
104 printf ("b.a=%s\n", b
.a
);
105 printf ("b.c=%s\n", b
.c
);
106 printf ("b.e=%s\n", b
.e
);