7 #include <libanjuta/anjuta-utils.h>
8 #include <libanjuta/anjuta-debug.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;
21 while ((len
> 1) && lenData
> 0) {
25 if ((ch
== '\r') || (ch
== '\n')) {
27 if ((lenData
> 0) && (ch
== '\r') && ((*fpc
) == '\n')) {
28 // munch the second half of a crlf
35 } else if ((ch
== '\\') && (lenData
> 0) && ((*fpc
== '\r') || (*fpc
== '\n'))) {
47 bool PropSetFile::ReadLine(char *linebuf
, bool ifIsTrue
, const char *directoryForImports
) {
48 if (isalpha(linebuf
[0])) // If clause ends with first non-indented line
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])) {
61 } else if (isspace(linebuf
[0]) && ifIsTrue
) {
67 void PropSetFile::ReadFromMemory(const char *data
, int len
, const char *directoryForImports
) {
68 const char *pd
= data
;
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];
80 FILE *rcfile
= fopen(filename
, "r");
82 FILE *rcfile
= fopen(filename
, "rb");
85 int lenFile
= fread(propsData
, 1, sizeof(propsData
), rcfile
);
87 ReadFromMemory(propsData
, lenFile
, directoryForImports
);
89 //printf("Could not open <%s>\n", filename);
94 // Global property bank for anjuta.
95 static GList
*anjuta_propset
;
98 get_propset(PropsID pi
)
101 if(pi
< 0 || (guint
)pi
>= g_list_length(anjuta_propset
))
103 DEBUG_PRINT("Invalid PropSetFile handle");
106 p
= (PropSetFile
*)g_list_nth_data(anjuta_propset
, pi
);
108 DEBUG_PRINT("Trying to access already destroyed PropSetFile object");
112 // Followings are the C++ to C interface for the PropSetFile
115 sci_prop_set_new(void)
121 length
= g_list_length(anjuta_propset
);
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");
134 sci_prop_get_pointer(PropsID handle
)
137 p
= get_propset(handle
);
142 sci_prop_set_destroy(PropsID handle
)
145 p
= get_propset(handle
);
147 g_list_nth(anjuta_propset
, handle
)->data
= NULL
;
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;
162 sci_prop_set_with_key(PropsID handle
, const gchar
*key
, const gchar
*val
)
165 p
= get_propset(handle
);
174 sci_prop_set_int_with_key (PropsID p
, const gchar
*key
, int value
)
177 str
= g_strdup_printf ("%d", value
);
178 sci_prop_set_with_key (p
, key
, str
);
183 sci_prop_set(PropsID handle
, const gchar
*keyval
)
186 p
= get_propset (handle
);
192 sci_prop_get(PropsID handle
, const gchar
*key
)
196 if (!key
) return NULL
;
197 p
= get_propset(handle
);
200 if (strlen(s
.c_str()) == 0) return NULL
;
201 return g_strdup(s
.c_str());
205 sci_prop_get_expanded(PropsID handle
, const gchar
*key
)
209 p
= get_propset(handle
);
211 s
= p
->GetExpanded(key
);
212 if (strlen(s
.c_str()) == 0) return NULL
;
213 return g_strdup(s
.c_str());
217 sci_prop_expand(PropsID handle
, const gchar
*withvars
)
221 p
= get_propset(handle
);
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)
232 p
= get_propset(handle
);
233 if(!p
) return defaultValue
;
234 return p
->GetInt(key
, defaultValue
);
238 sci_prop_get_wild(PropsID handle
, const gchar
*keybase
, const gchar
*filename
)
242 p
= get_propset(handle
);
244 s
= p
->GetWild(keybase
, filename
);
245 if (strlen(s
.c_str()) == 0) return NULL
;
246 return g_strdup(s
.c_str());
250 sci_prop_get_new_expand(PropsID handle
, const gchar
*keybase
, const gchar
*filename
)
254 p
= get_propset(handle
);
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 */
263 sci_prop_glist_from_string (const gchar
*string
)
265 gchar
*str
, *temp
, buff
[256];
267 gchar
*word_start
, *word_end
;
272 temp
= g_strdup (string
);
282 /* Remove leading spaces */
283 while (isspace (*str
) && *str
!= '\0')
288 /* Find start and end of word */
290 while (!isspace (*str
) && *str
!= '\0')
294 /* Copy the word into the buffer */
295 for (ptr
= word_start
, i
= 0; ptr
< word_end
; ptr
++, i
++)
299 list
= g_list_append (list
, g_strdup (buff
));
308 /* Get the list of strings as GList from a property value.
309 Strings are splitted from white spaces */
311 sci_prop_glist_from_data (guint props
, const gchar
*id
)
316 str
= sci_prop_get (props
, id
);
317 list
= sci_prop_glist_from_string (str
);
323 sci_prop_clear(PropsID handle
)
326 p
= get_propset(handle
);
332 sci_prop_read_from_memory(PropsID handle
, const gchar
*data
, gint len
,
333 const gchar
*directoryForImports
=0)
336 p
= get_propset(handle
);
338 p
->ReadFromMemory(data
, len
, directoryForImports
);
342 sci_prop_read(PropsID handle
, const gchar
*filename
, const gchar
*directoryForImports
)
345 p
= get_propset(handle
);
347 p
->Read( filename
, directoryForImports
);