You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2014/01/09 17:28:43 UTC
svn commit: r1556859 -
/incubator/celix/trunk/framework/private/src/properties.c
Author: pnoltes
Date: Thu Jan 9 16:28:43 2014
New Revision: 1556859
URL: http://svn.apache.org/r1556859
Log:
update properties_load and _store to support escaped #,!,: and = characters
Modified:
incubator/celix/trunk/framework/private/src/properties.c
Modified: incubator/celix/trunk/framework/private/src/properties.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/framework/private/src/properties.c?rev=1556859&r1=1556858&r2=1556859&view=diff
==============================================================================
--- incubator/celix/trunk/framework/private/src/properties.c (original)
+++ incubator/celix/trunk/framework/private/src/properties.c Thu Jan 9 16:28:43 2014
@@ -60,148 +60,115 @@ properties_pt properties_load(char *file
int outputPos = 0;
char *output = NULL;
+ if (file != NULL ) {
+ while ( fgets ( line, sizeof line, file ) != NULL ) {
- while ( fgets ( line, sizeof line, file ) != NULL ) {
-
- linePos = 0;
- precedingCharIsBackslash = false;
- isComment = false;
- output = NULL;
- outputPos = 0;
- key[0] = '\0';
- value[0] = '\0';
-
- while (line[linePos] != '\0') {
- if (line[linePos] == ' ' || line[linePos] == '\t') {
- if (output == NULL) {
- //ignore
- linePos+=1;
- continue;
+ linePos = 0;
+ precedingCharIsBackslash = false;
+ isComment = false;
+ output = NULL;
+ outputPos = 0;
+ key[0] = '\0';
+ value[0] = '\0';
+
+ while (line[linePos] != '\0') {
+ if (line[linePos] == ' ' || line[linePos] == '\t') {
+ if (output == NULL) {
+ //ignore
+ linePos += 1;
+ continue;
+ } else {
+ output[outputPos++] = line[linePos];
+ }
} else {
- output[outputPos++] = line[linePos];
- }
- } else {
- if (output == NULL) {
- output = key;
+ if (output == NULL) {
+ output = key;
+ }
}
- }
- if (line[linePos] == '=' || line[linePos] == ':' || line[linePos] == '#' || line[linePos] == '!') {
- if (precedingCharIsBackslash) {
- //escaped special character
- output[outputPos++] = line[linePos];
- } else {
- if (line[linePos] == '#' || line[linePos] == '!') {
- if (outputPos == 0) {
- isComment = true;
- break;
- } else {
- output[outputPos++] = line[linePos];
- }
- } else { // = or :
- if (output == value) { //already have a seperator
- output[outputPos++] =line[linePos];
- } else {
- output[outputPos++] = '\0';
- output = value;
- outputPos = 0;
+ if (line[linePos] == '=' || line[linePos] == ':' || line[linePos] == '#' || line[linePos] == '!') {
+ if (precedingCharIsBackslash) {
+ //escaped special character
+ output[outputPos++] = line[linePos];
+ precedingCharIsBackslash = false;
+ } else {
+ if (line[linePos] == '#' || line[linePos] == '!') {
+ if (outputPos == 0) {
+ isComment = true;
+ break;
+ } else {
+ output[outputPos++] = line[linePos];
+ }
+ } else { // = or :
+ if (output == value) { //already have a seperator
+ output[outputPos++] = line[linePos];
+ } else {
+ output[outputPos++] = '\0';
+ output = value;
+ outputPos = 0;
+ }
}
}
+ } else if (line[linePos] == '\\') {
+ if (precedingCharIsBackslash) { //double backslash -> backslash
+ output[outputPos++] = '\\';
+ }
+ precedingCharIsBackslash = true;
+ } else { //normal character
+ precedingCharIsBackslash = false;
+ output[outputPos++] = line[linePos];
}
- } else if (line[linePos] == '\\') {
- if (precedingCharIsBackslash) { //double backslash -> backslash
- output[outputPos++] = '\\';
- }
- precedingCharIsBackslash = !precedingCharIsBackslash;
- } else { //normal character
- precedingCharIsBackslash = false;
- output[outputPos++] = line[linePos];
+ linePos += 1;
+ }
+ if (output != NULL) {
+ output[outputPos] = '\0';
}
- linePos+=1;
- }
- if (output != NULL) {
- output[outputPos] = '\0';
- }
-
- if (!isComment) {
- printf("putting 'key'/'value' '%s'/'%s' in properties\n", utils_stringTrim(key), utils_stringTrim(value));
- hashMap_put(props, strdup(utils_stringTrim(key)), strdup(utils_stringTrim(value)));
- }
- }
-
- fclose(file);
-
- return props;
-}
-
-/*
-properties_pt properties_load_tmp(char * filename) {
- properties_pt props = properties_create();
- FILE *file = fopen ( filename, "r" );
-
- char * cont = strdup("\\");
-
- if (file != NULL) {
- char line [ 1024 ];
- char * key = NULL;
- char * value = NULL;
- int split = 0;
- while ( fgets ( line, sizeof line, file ) != NULL ) {
- if (!split) {
- unsigned int pos = strcspn(line, "=");
- if (pos != strlen(line)) {
- char * ival = NULL;
- key = utils_stringTrim(string_ndup((char *)line, pos));
- ival = string_ndup(line+pos+1, strlen(line));
- value = utils_stringTrim(ival);
- if (value != NULL) {
- char * cmp = string_ndup(value+strlen(value)-1, 1);
- if (strcmp(cont, cmp) == 0) {
- split = 1;
- value = string_ndup(value, strlen(value)-1);
- } else {
- char * old = hashMap_put(props, strdup(key), strdup(value));
- }
- free(cmp);
- free(ival);
- }
- free(key);
- }
- } else {
- if (strcmp(cont, string_ndup(line+strlen(line)-1, 1)) == 0) {
- split = 1;
- strcat(value, string_ndup(line, strlen(line)-1));
- } else {
- char * old = NULL;
- split = 0;
- strcat(value, utils_stringTrim(line));
- old = hashMap_put(props, strdup(key), strdup(value));
- }
+ if (!isComment) {
+ printf("putting 'key'/'value' '%s'/'%s' in properties\n", utils_stringTrim(key), utils_stringTrim(value));
+ hashMap_put(props, strdup(utils_stringTrim(key)), strdup(utils_stringTrim(value)));
}
}
+
fclose(file);
}
- free(cont);
+
return props;
}
-*/
+
/**
* Header is ignored for now, cannot handle comments yet
*/
void properties_store(properties_pt properties, char * filename, char * header) {
FILE *file = fopen ( filename, "w+" );
+ int i;
+ char *str;
+
if (file != NULL) {
if (hashMap_size(properties) > 0) {
hash_map_iterator_pt iterator = hashMapIterator_create(properties);
while (hashMapIterator_hasNext(iterator)) {
hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator);
+ str = hashMapEntry_getKey(entry);
+ for (int i = 0; i < strlen(str); i += 1) {
+ if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') {
+ fputc('\\', file);
+ }
+ fputc(str[i], file);
+ }
+
+ fputc('=', file);
+
+ str = hashMapEntry_getValue(entry);
+ for (int i = 0; i < strlen(str); i += 1) {
+ if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') {
+ fputc('\\', file);
+ }
+ fputc(str[i], file);
+ }
+
+ fputc('\n', file);
- char * line = strdup(hashMapEntry_getKey(entry));
- strcat(line, "=");
- strcat(line, strdup(hashMapEntry_getValue(entry)));
- strcat(line, "\n");
- fputs(line, file);
}
}
fclose(file);