- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / tools / wrc / newstruc.c
1 /*
2  * Create dynamic new structures of various types
3  * and some utils in that trend.
4  *
5  * Copyright 1998 Bertho A. Stultiens
6  *
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <assert.h>
12
13 #include <config.h>
14 #include "wrc.h"
15 #include "newstruc.h"
16 #include "utils.h"
17 #include "parser.h"
18
19 /* Generate new_* functions that have no parameters (NOTE: no ';') */
20 __NEW_STRUCT_FUNC(dialog)
21 __NEW_STRUCT_FUNC(dialogex)
22 __NEW_STRUCT_FUNC(name_id)
23 __NEW_STRUCT_FUNC(menu)
24 __NEW_STRUCT_FUNC(menuex)
25 __NEW_STRUCT_FUNC(menu_item)
26 __NEW_STRUCT_FUNC(menuex_item)
27 __NEW_STRUCT_FUNC(control)
28 __NEW_STRUCT_FUNC(icon)
29 __NEW_STRUCT_FUNC(cursor)
30 __NEW_STRUCT_FUNC(versioninfo)
31 __NEW_STRUCT_FUNC(ver_value)
32 __NEW_STRUCT_FUNC(ver_block)
33 __NEW_STRUCT_FUNC(stt_entry)
34 __NEW_STRUCT_FUNC(accelerator)
35 __NEW_STRUCT_FUNC(event)
36 __NEW_STRUCT_FUNC(raw_data)
37 __NEW_STRUCT_FUNC(lvc)
38 __NEW_STRUCT_FUNC(res_count)
39 __NEW_STRUCT_FUNC(string)
40 __NEW_STRUCT_FUNC(toolbar_item)
41
42 /* New instances for all types of structures */
43 /* Very inefficient (in size), but very functional :-]
44  * Especially for type-checking.
45  */
46 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
47 {
48         resource_t *r = (resource_t *)xmalloc(sizeof(resource_t));
49         r->type = t;
50         r->res.overlay = res;
51         r->memopt = memopt;
52         r->lan = lan;
53         return r;
54 }
55
56 version_t *new_version(DWORD v)
57 {
58         version_t *vp = (version_t *)xmalloc(sizeof(version_t));
59         *vp = v;
60         return vp;
61 }
62
63 characts_t *new_characts(DWORD c)
64 {
65         characts_t *cp = (characts_t *)xmalloc(sizeof(characts_t));
66         *cp = c;
67         return cp;
68 }
69
70 language_t *new_language(int id, int sub)
71 {
72         language_t *lan = (language_t *)xmalloc(sizeof(language_t));
73         lan->id = id;
74         lan->sub = sub;
75         return lan;
76 }
77
78 language_t *dup_language(language_t *l)
79 {
80         if(!l) return NULL;
81         return new_language(l->id, l->sub);
82 }
83
84 version_t *dup_version(version_t *v)
85 {
86         if(!v) return NULL;
87         return new_version(*v);
88 }
89
90 characts_t *dup_characts(characts_t *c)
91 {
92         if(!c) return NULL;
93         return new_characts(*c);
94 }
95
96 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
97 {
98         rcdata_t *rc = (rcdata_t *)xmalloc(sizeof(rcdata_t));
99         rc->data = rd;
100         if(memopt)
101         {
102                 rc->memopt = *memopt;
103                 free(memopt);
104         }
105         else
106                 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
107         return rc;
108 }
109
110 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
111 {
112         font_id_t *fid = (font_id_t *)xmalloc(sizeof(font_id_t));
113         fid->name = face;
114         fid->size = size;
115         fid->weight = weight;
116         fid->italic = italic;
117         return fid;
118 }
119
120 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
121 {
122         user_t *usr = (user_t *)xmalloc(sizeof(user_t));
123         usr->data = rd;
124         if(memopt)
125         {
126                 usr->memopt = *memopt;
127                 free(memopt);
128         }
129         else
130                 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
131         usr->type = type;
132         return usr;
133 }
134
135 font_t *new_font(raw_data_t *rd, int *memopt)
136 {
137         font_t *fnt = (font_t *)xmalloc(sizeof(font_t));
138         fnt->data = rd;
139         if(memopt)
140         {
141                 fnt->memopt = *memopt;
142                 free(memopt);
143         }
144         else
145                 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
146         return fnt;
147 }
148
149 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
150 {
151         icon_group_t *icog = (icon_group_t *)xmalloc(sizeof(icon_group_t));
152         if(memopt)
153         {
154                 icog->memopt = *memopt;
155                 free(memopt);
156         }
157         else
158                 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
159         icog->lvc.language = dup_language(currentlanguage);
160         split_icons(rd, icog, &(icog->nicon));
161         free(rd->data);
162         free(rd);
163         return icog;
164 }
165
166 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
167 {
168         cursor_group_t *curg = (cursor_group_t *)xmalloc(sizeof(cursor_group_t));
169         if(memopt)
170         {
171                 curg->memopt = *memopt;
172                 free(memopt);
173         }
174         else
175                 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
176         curg->lvc.language = dup_language(currentlanguage);
177         split_cursors(rd, curg, &(curg->ncursor));
178         free(rd->data);
179         free(rd);
180         return curg;
181 }
182
183 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
184 {
185         bitmap_t *bmp = (bitmap_t *)xmalloc(sizeof(bitmap_t));
186         bmp->data = rd;
187         if(memopt)
188         {
189                 bmp->memopt = *memopt;
190                 free(memopt);
191         }
192         else
193                 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
194         return bmp;
195 }
196
197 ver_words_t *new_ver_words(int i)
198 {
199         ver_words_t *w = (ver_words_t *)xmalloc(sizeof(ver_words_t));
200         w->words = (WORD *)xmalloc(sizeof(WORD));
201         w->words[0] = (WORD)i;
202         w->nwords = 1;
203         return w;
204 }
205
206 ver_words_t *add_ver_words(ver_words_t *w, int i)
207 {
208         w->words = (WORD *)xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
209         w->words[w->nwords] = (WORD)i;
210         w->nwords++;
211         return w;
212 }
213
214 messagetable_t *new_messagetable(raw_data_t *rd)
215 {
216         messagetable_t *msg = (messagetable_t *)xmalloc(sizeof(messagetable_t));
217         msg->data = rd;
218         return msg;
219 }
220
221 void copy_raw_data(raw_data_t *dst, raw_data_t *src, int offs, int len)
222 {
223         assert(offs <= src->size);
224         assert(offs + len <= src->size);
225         if(!dst->data)
226         {
227                 dst->data = (char *)xmalloc(len);
228                 dst->size = 0;
229         }
230         else
231                 dst->data = (char *)xrealloc(dst->data, dst->size + len);
232         /* dst->size holds the offset to copy to */
233         memcpy(dst->data + dst->size, src->data + offs, len);
234         dst->size += len;
235 }
236
237 int *new_int(int i)
238 {
239         int *ip = (int *)xmalloc(sizeof(int));
240         *ip = i;
241         return ip;
242 }
243
244 stringtable_t *new_stringtable(lvc_t *lvc)
245 {
246         stringtable_t *stt = (stringtable_t *)xmalloc(sizeof(stringtable_t));
247
248         if(lvc)
249                 stt->lvc = *lvc;
250
251         return stt;
252 }
253
254 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
255 {
256         toolbar_t *tb = (toolbar_t *)xmalloc(sizeof(toolbar_t));
257         tb->button_width = button_width;
258         tb->button_height = button_height;
259         tb->nitems = nitems;
260         tb->items = items;
261         return tb;
262 }