implement xchat_del_pluginpref

This commit is contained in:
Berke Viktor 2012-01-14 01:49:12 +01:00
parent c3821b6316
commit 17d13aa8d3
1 changed files with 38 additions and 23 deletions

View File

@ -1589,13 +1589,11 @@ xchat_free (xchat_plugin *ph, void *ptr)
} }
static int static int
xchat_set_pluginpref_str_real (xchat_plugin *pl, const char *var, const char *value, int mode) xchat_set_pluginpref_str_real (xchat_plugin *pl, const char *var, const char *value, int mode) /* mode: 0 = delete, 1 = save */
{ {
/* mode: 0 = delete, 1 = save */
FILE *fpIn; FILE *fpIn;
int fhOut; int fhOut;
int prevConfig; int prevSetting;
char confname[64]; char confname[64];
char confname_tmp[69]; char confname_tmp[69];
char buffer[512]; /* the same as in cfg_put_str */ char buffer[512]; /* the same as in cfg_put_str */
@ -1615,7 +1613,9 @@ xchat_set_pluginpref_str_real (xchat_plugin *pl, const char *var, const char *va
{ {
return 0; return 0;
} }
else if (fpIn == NULL) /* no previous config, no parsing */ else if (fpIn == NULL) /* no previous config file, no parsing */
{
if (mode)
{ {
sprintf (buffer, "%s = %s\n", var, value); sprintf (buffer, "%s = %s\n", var, value);
write (fhOut, buffer, strlen (buffer)); write (fhOut, buffer, strlen (buffer));
@ -1637,22 +1637,37 @@ xchat_set_pluginpref_str_real (xchat_plugin *pl, const char *var, const char *va
return 0; return 0;
} }
} }
else /* existing config, preserve settings and find & replace current var value if any */ else
{ {
prevConfig = 0; /* mode = 0, we want to delete but the config file and thus the given setting does not exist, we're ready */
close (fhOut);
return 1;
}
}
else /* existing config file, preserve settings and find & replace current var value if any */
{
prevSetting = 0;
while (fscanf (fpIn, " %[^\n]", &buffer) != EOF) /* read whole lines including whitespaces */ while (fscanf (fpIn, " %[^\n]", &buffer) != EOF) /* read whole lines including whitespaces */
{ {
sprintf (buffer_tmp, "%s ", var); /* add one space, this way it works against var - var2 checks too */ sprintf (buffer_tmp, "%s ", var); /* add one space, this way it works against var - var2 checks too */
if (strncmp (buffer_tmp, buffer, strlen (var) + 1) == 0) /* given setting already exists */ if (strncmp (buffer_tmp, buffer, strlen (var) + 1) == 0) /* given setting already exists */
{
if (mode) /* overwrite the existing matching setting if we are in save mode */
{ {
sprintf (buffer, "%s = %s\n", var, value); sprintf (buffer, "%s = %s\n", var, value);
prevConfig = 1; }
else /* erase the setting in delete mode */
{
strcpy (buffer, "");
}
prevSetting = 1;
} }
else else
{ {
strcat (buffer, "\n"); strcat (buffer, "\n"); /* preserve the existing different settings */
} }
write (fhOut, buffer, strlen (buffer)); write (fhOut, buffer, strlen (buffer));
@ -1660,7 +1675,7 @@ xchat_set_pluginpref_str_real (xchat_plugin *pl, const char *var, const char *va
fclose (fpIn); fclose (fpIn);
if (!prevConfig) /* var doesn't exist currently, append */ if (!prevSetting && mode) /* var doesn't exist currently, append if we're in save mode */
{ {
sprintf (buffer, "%s = %s\n", var, value); sprintf (buffer, "%s = %s\n", var, value);
write (fhOut, buffer, strlen (buffer)); write (fhOut, buffer, strlen (buffer));
@ -1689,7 +1704,7 @@ xchat_set_pluginpref_str_real (xchat_plugin *pl, const char *var, const char *va
int int
xchat_set_pluginpref_str (xchat_plugin *pl, const char *var, const char *value) xchat_set_pluginpref_str (xchat_plugin *pl, const char *var, const char *value)
{ {
return xchat_set_pluginpref_str (pl, var, value, 1); return xchat_set_pluginpref_str_real (pl, var, value, 1);
} }
int int
@ -1771,5 +1786,5 @@ xchat_get_pluginpref_int (xchat_plugin *pl, const char *var)
int int
xchat_del_pluginpref (xchat_plugin *pl, const char *var) xchat_del_pluginpref (xchat_plugin *pl, const char *var)
{ {
xchat_set_pluginpref_str_real (pl, var, 0, 0); return xchat_set_pluginpref_str_real (pl, var, 0, 0);
} }