* src/vm/resolve.c (resolve_method_lazy): Removed dependence on
[cacao.git] / src / vm / properties.c
1 /* src/vm/properties.c - handling commandline properties
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: properties.c 5049 2006-06-23 12:07:26Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <stdlib.h>
39
40 #include "vm/types.h"
41
42 #include "mm/memory.h"
43
44 #include "vm/global.h"
45 #include "native/include/java_lang_String.h"
46 #include "native/include/java_util_Properties.h"
47 #include "toolbox/list.h"
48 #include "vm/method.h"
49 #include "vm/options.h"
50 #include "vm/stringlocal.h"
51 #include "vm/vm.h"
52 #include "vm/jit/asmpart.h"
53
54
55 /* temporary property structure ***********************************************/
56
57 typedef struct list_properties_entry list_properties_entry;
58
59 struct list_properties_entry {
60         char     *key;
61         char     *value;
62         listnode linkage;
63 };
64
65
66 /* global variables ***********************************************************/
67
68 static list *list_properties = NULL;
69
70 static java_util_Properties *psystem;
71 static methodinfo *mput;
72
73
74 /* properties_init *************************************************************
75
76    Initialize the properties list.
77
78 *******************************************************************************/
79
80 bool properties_init(void)
81 {
82         list_properties = list_create(OFFSET(list_properties_entry, linkage));
83
84         /* everything's ok */
85
86         return true;
87 }
88
89
90 /* properties_postinit *********************************************************
91
92    Post-initialize the properties.  The passed properties table is the
93    Java system properties table.
94
95 *******************************************************************************/
96
97 bool properties_postinit(java_util_Properties *p)
98 {
99         /* set global Java system properties pointer */
100
101         psystem = p;
102
103         /* search for method to add properties */
104
105         mput = class_resolveclassmethod(p->header.vftbl->class,
106                                                                         utf_new_char("put"),
107                                                                         utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
108                                                                         NULL,
109                                                                         true);
110
111         if (mput == NULL)
112                 return false;
113
114         return true;
115 }
116
117
118 /* properties_add **************************************************************
119
120    Adds a property entry for a command line property definition.
121
122 *******************************************************************************/
123
124 void properties_add(char *key, char *value)
125 {
126         list_properties_entry *p;
127
128         p = NEW(list_properties_entry);
129         p->key   = key;
130         p->value = value;
131
132         list_add_last_unsynced(list_properties, p);
133 }
134
135
136 /* get_property ****************************************************************
137
138    Get a property entry from a command line property definition.
139
140 *******************************************************************************/
141
142 char *properties_get(char *key)
143 {
144         list_properties_entry *p;
145
146         /* We search backwards, so we get the newest entry for a key, as
147            the list may contain more than one entry for a specific key. */
148
149         for (p = list_last(list_properties); p != NULL;
150                  p = list_prev(list_properties, p)) {
151                 if (strcmp(p->key, key) == 0)
152                         return p->value;
153         }
154
155         return NULL;
156 }
157
158
159 /* properties_system_add *******************************************************
160
161    Adds a property to the Java system properties.
162
163 *******************************************************************************/
164
165 void properties_system_add(char *key, char *value)
166 {
167         java_lang_String *k;
168         java_lang_String *v;
169
170         k = javastring_new_from_utf_string(key);
171         v = javastring_new_from_utf_string(value);
172
173         (void) vm_call_method(mput, (java_objectheader *) psystem, k, v);
174 }
175
176
177 /* properties_system_add_all ***************************************************
178
179    Adds a all properties from the properties list to the Java system
180    properties.
181
182 *******************************************************************************/
183
184 void properties_system_add_all(void)
185 {
186         list_properties_entry *p;
187
188         for (p = list_first(list_properties); p != NULL;
189                  p = list_first(list_properties)) {
190                 /* add to the Java system properties */
191
192                 properties_system_add(p->key, p->value);
193
194                 /* remove the entry from the list */
195
196                 list_remove(list_properties, p);
197
198                 /* and free the memory */
199
200                 FREE(p, list_properties_entry);
201         }
202 }
203
204
205 /*
206  * These are local overrides for various environment variables in Emacs.
207  * Please do not remove this and leave it at the end of the file, where
208  * Emacs will automagically detect them.
209  * ---------------------------------------------------------------------
210  * Local variables:
211  * mode: c
212  * indent-tabs-mode: t
213  * c-basic-offset: 4
214  * tab-width: 4
215  * End:
216  */