2008-04-18 Miguel de Icaza <miguel@novell.com>
[mono/afaerber.git] / support / x-struct-str.c
blob89d5a61b120b6227f36aaad323efd33810fcf631
1 /*
2 * A helper routine to copy the strings between differing structures.
3 */
5 #include <stdlib.h>
6 #include <string.h>
7 #include <limits.h>
9 #include "mph.h"
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) \
20 : lstr_at(p, n) \
23 char* MPH_INTERNAL
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,
27 size_t num_strings)
29 int i;
30 size_t buflen;
31 int len[MAX_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;
40 buflen = num_strings;
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)
44 buflen += len[i];
45 else
46 len[i] = -1;
49 cur = buf = malloc (buflen);
50 if (buf == NULL) {
51 return NULL;
54 for (i = 0; i < num_strings; ++i) {
55 if (len[i] > 0) {
56 lstr_at (to, to_offsets[i]) =
57 strcpy (cur, str_at (from, from_offsets[i]));
58 cur += (len[i] +1);
62 return buf;
65 #ifdef TEST
67 #include <stdio.h>
69 struct foo {
70 char *a;
71 int b;
72 char *c;
73 char d[10];
76 struct bar {
77 int b;
78 char *a;
79 double d;
80 char *c;
81 char *e;
84 int
85 main ()
87 /* test copying foo to bar */
88 struct foo f = {"hello", 42, "world", "!!"};
89 struct bar b;
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)
100 char *buf;
102 buf = _mph_copy_structure_strings (&b, bar_offsets,
103 &f, foo_offsets, 3);
104 printf ("b.a=%s\n", b.a);
105 printf ("b.c=%s\n", b.c);
106 printf ("b.e=%s\n", b.e);
108 return 0;
110 #endif