Fix a Gtk warning when checking path input in the log viewer.
[anjuta-git-plugin.git] / plugins / editor / properties.cxx
blob0e0644fb5d23f70a81fffd64a6d0ec51bf0297c8
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <ctype.h>
7 #include <libanjuta/anjuta-utils.h>
8 #include <libanjuta/anjuta-debug.h>
10 #define GTK
11 #undef PLAT_GTK
12 #define PLAT_GTK 1
14 #include "PropSet.h"
15 #include "properties_cxx.h"
16 #include "properties.h"
18 static bool GetFullLine(const char *&fpc, int &lenData, char *s, int len) {
19 bool continuation = true;
20 s[0] = '\0';
21 while ((len > 1) && lenData > 0) {
22 char ch = *fpc;
23 fpc++;
24 lenData--;
25 if ((ch == '\r') || (ch == '\n')) {
26 if (!continuation) {
27 if ((lenData > 0) && (ch == '\r') && ((*fpc) == '\n')) {
28 // munch the second half of a crlf
29 fpc++;
30 lenData--;
32 *s = '\0';
33 return true;
35 } else if ((ch == '\\') && (lenData > 0) && ((*fpc == '\r') || (*fpc == '\n'))) {
36 continuation = true;
37 } else {
38 continuation = false;
39 *s++ = ch;
40 *s = '\0';
41 len--;
44 return false;
47 bool PropSetFile::ReadLine(char *linebuf, bool ifIsTrue, const char *directoryForImports) {
48 if (isalpha(linebuf[0])) // If clause ends with first non-indented line
49 ifIsTrue = true;
50 if (isprefix(linebuf, "if ")) {
51 const char *expr = linebuf + strlen("if") + 1;
52 ifIsTrue = GetInt(expr);
53 } else if (isprefix(linebuf, "import ") && directoryForImports) {
54 char importPath[1024];
55 strcpy(importPath, directoryForImports);
56 strcat(importPath, linebuf + strlen("import") + 1);
57 strcat(importPath, ".properties");
58 Read(importPath, directoryForImports);
59 } else if (isalpha(linebuf[0])) {
60 Set(linebuf);
61 } else if (isspace(linebuf[0]) && ifIsTrue) {
62 Set(linebuf);
64 return ifIsTrue;
67 void PropSetFile::ReadFromMemory(const char *data, int len, const char *directoryForImports) {
68 const char *pd = data;
69 char linebuf[60000];
70 bool ifIsTrue = true;
71 while (len > 0) {
72 GetFullLine(pd, len, linebuf, sizeof(linebuf));
73 ifIsTrue = ReadLine(linebuf, ifIsTrue, directoryForImports);
77 void PropSetFile::Read(const char *filename, const char *directoryForImports) {
78 char propsData[60000];
79 #ifdef __vms
80 FILE *rcfile = fopen(filename, "r");
81 #else
82 FILE *rcfile = fopen(filename, "rb");
83 #endif
84 if (rcfile) {
85 int lenFile = fread(propsData, 1, sizeof(propsData), rcfile);
86 fclose(rcfile);
87 ReadFromMemory(propsData, lenFile, directoryForImports);
88 } else {
89 //printf("Could not open <%s>\n", filename);
94 // Global property bank for anjuta.
95 static GList *anjuta_propset;
97 static PropSetFile*
98 get_propset(PropsID pi)
100 PropSetFile* p;
101 if(pi < 0 || (guint)pi >= g_list_length(anjuta_propset))
103 DEBUG_PRINT("Invalid PropSetFile handle");
104 return NULL;
106 p = (PropSetFile*)g_list_nth_data(anjuta_propset, pi);
107 if (p == NULL)
108 DEBUG_PRINT("Trying to access already destroyed PropSetFile object");
109 return p;
112 // Followings are the C++ to C interface for the PropSetFile
114 PropsID
115 sci_prop_set_new(void)
117 PropsID handle;
118 PropSetFile *p;
119 gint length;
121 length = g_list_length(anjuta_propset);
122 p = new PropSetFile;
123 anjuta_propset = g_list_append(anjuta_propset, (gpointer)p);
124 handle = g_list_length(anjuta_propset);
125 if (length == handle)
127 DEBUG_PRINT("Unable to create PropSetFile Object");
128 return -1;
130 return handle-1;
133 gpointer
134 sci_prop_get_pointer(PropsID handle)
136 PropSetFile* p;
137 p = get_propset(handle);
138 return (gpointer)p;
141 void
142 sci_prop_set_destroy(PropsID handle)
144 PropSetFile* p;
145 p = get_propset(handle);
146 if(!p) return;
147 g_list_nth(anjuta_propset, handle)->data = NULL;
148 delete p;
151 void
152 sci_prop_set_parent(PropsID handle1, PropsID handle2)
154 PropSetFile *p1, *p2;
155 p1 = get_propset(handle1);
156 p2 = get_propset(handle2);
157 if(!p1 || !p2) return;
158 p1->superPS = p2;
161 void
162 sci_prop_set_with_key(PropsID handle, const gchar *key, const gchar *val)
164 PropSetFile* p;
165 p = get_propset(handle);
166 if(!p) return;
167 if(val)
168 p->Set(key, val);
169 else
170 p->Set(key, "");
173 void
174 sci_prop_set_int_with_key (PropsID p, const gchar *key, int value)
176 gchar *str;
177 str = g_strdup_printf ("%d", value);
178 sci_prop_set_with_key (p, key, str);
179 g_free (str);
182 void
183 sci_prop_set(PropsID handle, const gchar *keyval)
185 PropSetFile* p;
186 p = get_propset (handle);
187 if(!p) return;
188 p->Set(keyval);
191 gchar*
192 sci_prop_get(PropsID handle, const gchar *key)
194 PropSetFile* p;
195 SString s;
196 if (!key) return NULL;
197 p = get_propset(handle);
198 if(!p) return NULL;
199 s = p->Get(key);
200 if (strlen(s.c_str()) == 0) return NULL;
201 return g_strdup(s.c_str());
204 gchar*
205 sci_prop_get_expanded(PropsID handle, const gchar *key)
207 PropSetFile* p;
208 SString s;
209 p = get_propset(handle);
210 if(!p) return NULL;
211 s = p->GetExpanded(key);
212 if (strlen(s.c_str()) == 0) return NULL;
213 return g_strdup(s.c_str());
216 gchar*
217 sci_prop_expand(PropsID handle, const gchar *withvars)
219 PropSetFile* p;
220 SString s;
221 p = get_propset(handle);
222 if(!p) return NULL;
223 s = p->Expand(withvars);
224 if (strlen(s.c_str()) == 0) return NULL;
225 return g_strdup(s.c_str());
229 sci_prop_get_int(PropsID handle, const gchar *key, gint defaultValue=0)
231 PropSetFile* p;
232 p = get_propset(handle);
233 if(!p) return defaultValue;
234 return p->GetInt(key, defaultValue);
237 gchar*
238 sci_prop_get_wild(PropsID handle, const gchar *keybase, const gchar *filename)
240 PropSetFile* p;
241 SString s;
242 p = get_propset(handle);
243 if(!p) return NULL;
244 s = p->GetWild(keybase, filename);
245 if (strlen(s.c_str()) == 0) return NULL;
246 return g_strdup(s.c_str());
249 gchar*
250 sci_prop_get_new_expand(PropsID handle, const gchar *keybase, const gchar *filename)
252 PropSetFile* p;
253 SString s;
254 p = get_propset(handle);
255 if(!p) return NULL;
256 s = p->GetNewExpand(keybase, filename);
257 if (strlen(s.c_str()) == 0) return NULL;
258 return g_strdup(s.c_str());
261 /* GList of strings operations */
262 static GList *
263 sci_prop_glist_from_string (const gchar *string)
265 gchar *str, *temp, buff[256];
266 GList *list;
267 gchar *word_start, *word_end;
268 gboolean the_end;
270 list = NULL;
271 the_end = FALSE;
272 temp = g_strdup (string);
273 str = temp;
274 if (!str)
275 return NULL;
277 while (1)
279 gint i;
280 gchar *ptr;
282 /* Remove leading spaces */
283 while (isspace (*str) && *str != '\0')
284 str++;
285 if (*str == '\0')
286 break;
288 /* Find start and end of word */
289 word_start = str;
290 while (!isspace (*str) && *str != '\0')
291 str++;
292 word_end = str;
294 /* Copy the word into the buffer */
295 for (ptr = word_start, i = 0; ptr < word_end; ptr++, i++)
296 buff[i] = *ptr;
297 buff[i] = '\0';
298 if (strlen (buff))
299 list = g_list_append (list, g_strdup (buff));
300 if (*str == '\0')
301 break;
303 if (temp)
304 g_free (temp);
305 return list;
308 /* Get the list of strings as GList from a property value.
309 Strings are splitted from white spaces */
310 GList *
311 sci_prop_glist_from_data (guint props, const gchar *id)
313 gchar *str;
314 GList *list;
316 str = sci_prop_get (props, id);
317 list = sci_prop_glist_from_string (str);
318 g_free(str);
319 return list;
322 void
323 sci_prop_clear(PropsID handle)
325 PropSetFile* p;
326 p = get_propset(handle);
327 if(!p) return;
328 p->Clear();
331 void
332 sci_prop_read_from_memory(PropsID handle, const gchar *data, gint len,
333 const gchar *directoryForImports=0)
335 PropSetFile* p;
336 p = get_propset(handle);
337 if(!p) return;
338 p->ReadFromMemory(data, len, directoryForImports);
341 void
342 sci_prop_read(PropsID handle, const gchar *filename, const gchar *directoryForImports)
344 PropSetFile* p;
345 p = get_propset(handle);
346 if(!p) return;
347 p->Read( filename, directoryForImports);