2 * A helper routine to copy the strings between differing structures.
11 #define MAX_OFFSETS 10
13 #define str_at(p, n) (*(char**)(((char*)p)+n))
16 _mph_copy_structure_strings (
17 void *to, const size_t *to_offsets,
18 const void *from, const size_t *from_offsets,
24 char *buf, *cur = NULL;
26 g_assert (num_strings < MAX_OFFSETS);
28 for (i = 0; i < num_strings; ++i) {
29 str_at (to, to_offsets[i]) = NULL;
33 for (i = 0; i < num_strings; ++i) {
34 len[i] = strlen (str_at(from, from_offsets[i]));
35 if (len[i] < INT_MAX - buflen)
41 cur = buf = malloc (buflen);
46 for (i = 0; i < num_strings; ++i) {
48 str_at (to, to_offsets[i]) =
49 strcpy (cur, str_at (from, from_offsets[i]));
77 /* test copying foo to bar */
78 struct foo f = {"hello", 42, "world"};
80 size_t foo_offsets[] = {offsetof(struct foo, a), offsetof(struct foo, c)};
81 size_t bar_offsets[] = {offsetof(struct bar, a), offsetof(struct bar, c)};
84 buf = _mph_copy_structure_strings (&b, bar_offsets,
86 printf ("b.a=%s\n", b.a);
87 printf ("b.c=%s\n", b.c);