for TARGET_J2EE only:
[mono.git] / support / x-struct-str.c
1 /*
2  * A helper routine to copy the strings between differing structures.
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <limits.h>
8
9 #include "mph.h"
10
11 #define MAX_OFFSETS 10
12
13 #define OFFSET_SHIFT 1
14
15 #define lstr_at(p, n) (*(char**)(((char*)(p))+(n >> OFFSET_SHIFT)))
16
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)                                             \
21 )
22
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)
28 {
29         int i;
30         size_t buflen;
31         int len[MAX_OFFSETS];
32         char *buf, *cur = NULL;
33
34         g_assert (num_strings < MAX_OFFSETS);
35
36         for (i = 0; i < num_strings; ++i) {
37                 lstr_at (to, to_offsets[i]) = NULL;
38         }
39
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;
47         }
48
49         cur = buf = malloc (buflen);
50         if (buf == NULL) {
51                 return NULL;
52         }
53
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);
59                 }
60         }
61
62         return buf;
63 }
64
65 #ifdef TEST
66
67 #include <stdio.h>
68
69 struct foo {
70         char *a;
71         int   b;
72         char *c;
73         char d[10];
74 };
75
76 struct bar {
77         int    b;
78         char  *a;
79         double d;
80         char  *c;
81         char  *e;
82 };
83
84 int
85 main ()
86 {
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)
94         };
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)
99         };
100         char *buf;
101
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);
107
108         return 0;
109 }
110 #endif
111