Hi,

well after some ever further investigation of some bugs, I ended up
cleaning up the code, again. I also fixed some really heavy memory
leaks in csvToList and made it in general more stable.

Regards,
Patrick "Marex" Niklaus
diff --git a/plugins/ini.c b/plugins/ini.c
index c55d897..3dda8a8 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, length;
+    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 */
+    length = strlen(filename) - strlen(pluginSep);
+    pluginStr = strndup(filename, length);
+    pluginSep++; /* skip the '-' */
+    length = strlen(pluginSep) - strlen(screenSep);
+    screenStr = strndup(pluginSep, length);
 
     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 *splitPos;
+    int length;
 
     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
-    {
+    splitPos = strchr(line, '=');
+    if (!splitPos)
 	return FALSE;
-    }
+
+    length = strlen(line) - strlen(splitPos);
+    *optionName = strndup(line, length);
+    splitPos++;
+    *optionValue = strndup(splitPos, strlen(splitPos)-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 *splitStart = NULL;
+    char *splitEnd = 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 length = strlen(csv);
+    count = 1;
+    for (i = 0; i < length; i++)
+	if (csv[i] == ',' && i != length-1)
+	    count++;
+
+    splitStart = 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)
+	    splitEnd = strchr(splitStart, ',');
+
+	    if (splitEnd)
 	    {
-		/* > 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(splitStart) - strlen(splitEnd);
+		item = strndup(splitStart, 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(splitStart);
 	    }
 
 	    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;
 	    }
+
+	    splitStart = ++splitEnd;
+	    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 hasValue = 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;
+		    hasValue = TRUE;
 		    value.b = (Bool) atoi (optionValue);
 			break;
 		case CompOptionTypeInt:
-		    hv = TRUE;
+		    hasValue = TRUE;
 		    value.i = atoi (optionValue);
 			break;
 		case CompOptionTypeFloat:
-		    hv = TRUE;
+		    hasValue = TRUE;
 		    value.f = atof (optionValue);
 			break;
 		case CompOptionTypeString:
-		    hv = TRUE;
+		    hasValue = TRUE;
 		    value.s = strdup (optionValue);
 			break;
 		case CompOptionTypeColor:
-		    hv = stringToColor (optionValue, value.c);
+		    hasValue = stringToColor (optionValue, value.c);
 			break;
 		case CompOptionTypeList:
-		    hv = csvToList (optionValue, &value.list, value.list.type);
+		    hasValue = csvToList (optionValue, &value.list, value.list.type);
 			break;
 		case CompOptionTypeMatch:
-		    hv = TRUE;
+		    hasValue = TRUE;
 		    matchInit (&value.match);
 		    matchAddFromString (&value.match, optionValue);
 			break;
@@ -740,7 +698,7 @@ iniLoadOptionsFromFile (CompDisplay *d,
 			break;
 		}
 
-		if (hv)
+		if (hasValue)
 		{
 		    if (plugin && p)
 		    {
@@ -830,7 +788,6 @@ iniLoadOptionsFromFile (CompDisplay *d,
 	    free (optionName);
 	if (optionValue)
 	    free (optionValue);
-	continueReading = FALSE;
     } 
 
     return TRUE;
_______________________________________________
compiz mailing list
[EMAIL PROTECTED]
http://lists.freedesktop.org/mailman/listinfo/compiz

Reply via email to