All pastes #437904 Raw Edit

Someone

public c v1 · immutable
·published 2007-04-13 10:04 UTC
rendered paste body
diff --git a/plugins/ini.c b/plugins/ini.cindex c55d897..c79f2ec 100644--- a/plugins/ini.c+++ b/plugins/ini.c@@ -143,9 +143,11 @@ static IniFileData * iniGetFileDataFromFilename (CompDisplay *d, 			    const char *filename) {-    int len, i;-    int pluginSep = 0, screenSep = 0;-    char *pluginStr, *screenStr;+    int fnLen, lenght;+    char *pluginSep;+    char *screenSep;+    char *pluginStr;+    char *screenStr;     IniFileData *fd;      INI_DISPLAY (d);@@ -153,35 +155,27 @@ iniGetFileDataFromFilename (CompDisplay *d,     if (!filename) 	return NULL; -    len = strlen (filename);+    fnLen = strlen(filename); -    if (len < (strlen(FILE_SUFFIX) + 2))+    if (fnLen < (strlen(FILE_SUFFIX) + 2)) 	return NULL; -    if ((filename[0]=='.') || (filename[len-1]=='~'))+    if ((filename[0] == '.') || (filename[fnLen-1] == '~')) 	return NULL;      for (fd = id->fileData; fd; fd = fd->next) 	if (strcmp (fd->filename, filename) == 0) 	    return fd; -    for (i=0; i<len; i++)-    {-	if (filename[i] == '-')-	{-	    if (!pluginSep)-		pluginSep = i-1;-	    else-		return NULL; /*found a second dash */-	}-	else if (filename[i] == '.')-	{-	    if (!screenSep)-		screenSep = i-1;-	    else-		return NULL; /*found a second dot */-	}-    }+    /* the split point for the plugin name */+    pluginSep = strrchr(filename, '-');+    if (!pluginSep)+	return NULL;++    /* the split point for the screen name */+    screenSep = strrchr(filename, '.');+    if (!screenSep)+	return NULL;      if (!pluginSep || !screenSep) 	return NULL;@@ -202,17 +196,12 @@ iniGetFileDataFromFilename (CompDisplay *d,      newFd->filename = strdup (filename); -    pluginStr = malloc (sizeof (char) * pluginSep + 1);-    screenStr = malloc (sizeof (char) * (screenSep - pluginSep) + 1);--    if (!pluginStr || !screenStr)-	return NULL;--    strncpy (pluginStr, filename, pluginSep + 1);-    pluginStr[pluginSep + 1] = '\0';--    strncpy (screenStr, &filename[pluginSep+2], (screenSep - pluginSep) + 1);-    screenStr[(screenSep - pluginSep) -1] = '\0';+    /* get plugin and screen name */+    lenght = strlen(filename) - strlen(pluginSep);+    pluginStr = strndup(filename, lenght);+    pluginSep++; /* skip the '-' */+    lenght = strlen(pluginSep) - strlen(screenSep);+    screenStr = strndup(pluginSep, lenght);      if (strcmp (pluginStr, CORE_NAME) == 0) 	newFd->plugin = NULL;@@ -330,7 +319,7 @@ iniGetFilename (CompDisplay *d,     if (fn)     { 	sprintf (fn, "%s-%s%s",-		 plugin?plugin:CORE_NAME, screenStr, FILE_SUFFIX);+		 plugin ? plugin : CORE_NAME, screenStr, FILE_SUFFIX);  	*filename = strdup (fn); @@ -348,48 +337,20 @@ iniGetFilename (CompDisplay *d, static Bool iniParseLine (char *line, char **optionName, char **optionValue) {-    int  pos = 0;-    int  splitPos = 0;-    int  endPos = 0;-    char tmpName[MAX_OPTION_LENGTH];-    char tmpValue[MAX_OPTION_LENGTH];+    char *split_pos;+    int lenght;      if (line[0] == '\0' || line[0] == '\n') 	return FALSE; -    while (pos < strlen(line))-    {-	if (!splitPos && line[pos] == '=')-	    splitPos = pos;-	if (line[pos] == '\n')-	{-	    endPos = pos;-	    break;-	}-	pos++;-    }--    if (splitPos && endPos)-    {-	tmpName[0] = '\0';-	tmpValue[0] = '\0';--	int i;-	for (i=0; i < splitPos; i++)-	    tmpName[i] = line[i];-	tmpName[splitPos] = '\0';--	for (i=splitPos+1; i<endPos; i++)-	    tmpValue[i - (splitPos+1)] = line[i];-	tmpValue[endPos - (splitPos+1)] = '\0';--	*optionName = strdup (tmpName);-	*optionValue = strdup (tmpValue);-    }-    else-    {+    split_pos = strchr(line, '=');+    if (!split_pos) 	return FALSE;-    }++    lenght = strlen(line) - strlen(split_pos);+    *optionName = strndup(line, lenght);+    split_pos++;+    *optionValue = strndup(split_pos, strlen(split_pos)-1);      return TRUE; }@@ -397,53 +358,48 @@ iniParseLine (char *line, char **optionName, char **optionValue) static Bool csvToList (char *csv, CompListValue *list, CompOptionType type) {-    char *csvtmp, *split, *item = NULL;-    int  count = 1, i, itemLength;+    char *split_start = NULL;+    char *split_end = NULL;+    char *item = NULL;+    int itemLength;+    int count;+    int i;      if (csv[0] == '\0')     { 	list->nValue = 0; 	return FALSE;     }--    csvtmp = strdup (csv);-    csvtmp = strchr (csv, ',');--    while (csvtmp)-    {-	csvtmp++;  /* avoid the comma */-	count++;-	csvtmp = strchr (csvtmp, ',');-    }-+ +    int lenght = strlen(csv);+    count = 1;+    for (i = 0; csv[i] != '\0'; i++)+	if (csv[i] == ',' && i != lenght-1)+	    count++;++    split_start = csv;     list->value = malloc (sizeof (CompOptionValue) * count);     if (list->value)     {-	for (i=0; i<count; i++)+	for (i = 0; i < count; i++) 	{-	    split = strchr (csv, ',');-	    if (split)+	    split_end = strchr(split_start, ',');++	    if (split_end) 	    {-		/* > 1 value */-		itemLength = strlen(csv) - strlen(split);-		item = realloc (item, sizeof (char) * (itemLength+1));-		strncpy (item, csv, itemLength);-		item[itemLength] = '\0';-		csv += itemLength + 1;+		itemLength = strlen(split_start) - strlen(split_end);+		item = strndup(split_start, itemLength); 	    }-	    else+	    else // last value 	    {-		/* 1 value only */-		itemLength = strlen(csv);-		item = realloc (item, sizeof (char) * (itemLength+1));-		strncpy (item, csv, itemLength);-		item[itemLength] = '\0';+		item = strdup(split_start); 	    }  	    switch (type) 	    { 		case CompOptionTypeString:-		    list->value[i].s = strdup (item);+		    if (item[0] != '\0')+			list->value[i].s = strdup (item); 		    break; 		case CompOptionTypeBool: 		    if (item[0] != '\0')@@ -464,13 +420,14 @@ csvToList (char *csv, CompListValue *list, CompOptionType type) 		default: 		    break; 	    }++	    split_start = ++split_end;+	    if (item)+		free(item); 	} 	list->nValue = count;     } -    if (item)-	free (item);-     return TRUE; } @@ -639,7 +596,7 @@ iniLoadOptionsFromFile (CompDisplay *d,     CompScreen *s = NULL;     CompPlugin *p = NULL;     Bool status = FALSE;-    Bool hv = FALSE;+    Bool has_value = FALSE;     CompOptionValue value;      if (plugin)@@ -688,12 +645,13 @@ iniLoadOptionsFromFile (CompDisplay *d,      IniAction action;     action.realOptionName = NULL;-    Bool continueReading = FALSE;-    while (fgets (&tmp[0], MAX_OPTION_LENGTH, optionFile) != NULL)+    Bool continueReading;+    while (fgets (tmp, MAX_OPTION_LENGTH, optionFile) != NULL)     { 	status = FALSE;+	continueReading = FALSE; -	if (!iniParseLine (&tmp[0], &optionName, &optionValue))+	if (!iniParseLine (tmp, &optionName, &optionValue)) 	{ 	    fprintf(stderr, 		    "Ignoring line '%s' in %s %i\n", tmp, plugin, screen);@@ -710,29 +668,29 @@ iniLoadOptionsFromFile (CompDisplay *d, 		switch (o->type) 		{ 		case CompOptionTypeBool:-		    hv = TRUE;+		    has_value = TRUE; 		    value.b = (Bool) atoi (optionValue); 			break; 		case CompOptionTypeInt:-		    hv = TRUE;+		    has_value = TRUE; 		    value.i = atoi (optionValue); 			break; 		case CompOptionTypeFloat:-		    hv = TRUE;+		    has_value = TRUE; 		    value.f = atof (optionValue); 			break; 		case CompOptionTypeString:-		    hv = TRUE;+		    has_value = TRUE; 		    value.s = strdup (optionValue); 			break; 		case CompOptionTypeColor:-		    hv = stringToColor (optionValue, value.c);+		    has_value = stringToColor (optionValue, value.c); 			break; 		case CompOptionTypeList:-		    hv = csvToList (optionValue, &value.list, value.list.type);+		    has_value = csvToList (optionValue, &value.list, value.list.type); 			break; 		case CompOptionTypeMatch:-		    hv = TRUE;+		    has_value = TRUE; 		    matchInit (&value.match); 		    matchAddFromString (&value.match, optionValue); 			break;@@ -740,7 +698,7 @@ iniLoadOptionsFromFile (CompDisplay *d, 			break; 		} -		if (hv)+		if (has_value) 		{ 		    if (plugin && p) 		    {@@ -830,7 +788,6 @@ iniLoadOptionsFromFile (CompDisplay *d, 	    free (optionName); 	if (optionValue) 	    free (optionValue);-	continueReading = FALSE;     }       return TRUE;@@ -971,16 +928,12 @@ iniSaveOptions (CompDisplay *d,  	    strVal = malloc (sizeof(char) * MAX_OPTION_LENGTH); 	    strcpy (strVal, "");-	    firstInList = TRUE; 	    for (i = 0; i < SCREEN_EDGE_NUM; i++) 	    { 		if (option->value.action.edgeMask & (1 << i)) 		{-		    if (!firstInList)-		    	strncat (strVal, ",", MAX_OPTION_LENGTH);-		    firstInList = FALSE;-		     		    strncat (strVal, edgeToString (i), MAX_OPTION_LENGTH);+		    strncat (strVal, ",", MAX_OPTION_LENGTH); 		} 	    } 	    fprintf (optionFile, "%s_%s=%s\n", option->name, "edge", strVal);@@ -1012,22 +965,19 @@ iniSaveOptions (CompDisplay *d, 		if (!strVal) 		    return FALSE; 		strcpy (strVal, "");-		firstInList = TRUE;  		for (i = 0; i < option->value.list.nValue; i++) 		{ 		    itemVal = iniOptionValueToString ( 						&option->value.list.value[i], 						option->value.list.type);-		    if (!firstInList)-		        strncat (strVal, ",", stringLen);-		    firstInList = FALSE;-			+ 		    if (itemVal) 		    { 			strncat (strVal, itemVal, stringLen); 			free (itemVal); 		    }+		    strncat (strVal, ",", stringLen); 		}  		fprintf (optionFile, "%s=%s\n", option->name, strVal);