plugin config save is done via raw file access, now works with multiple vars

This commit is contained in:
Berke Viktor 2011-11-30 01:41:34 +01:00
parent b16ca3fa64
commit f9fa102690
3 changed files with 112 additions and 57 deletions

View File

@ -142,8 +142,7 @@ struct _xchat_plugin
char *value); char *value);
int (*xchat_get_plugin_pref) (xchat_plugin *ph, int (*xchat_get_plugin_pref) (xchat_plugin *ph,
char *var, char *var,
char *dest, char *dest);
int dest_len);
}; };
#endif #endif
@ -307,8 +306,7 @@ xchat_set_plugin_pref (xchat_plugin *ph,
int int
xchat_get_plugin_pref (xchat_plugin *ph, xchat_get_plugin_pref (xchat_plugin *ph,
char *var, char *var,
char *dest, char *dest);
int dest_len);
#if !defined(PLUGIN_C) && defined(WIN32) #if !defined(PLUGIN_C) && defined(WIN32)
#ifndef XCHAT_PLUGIN_HANDLE #ifndef XCHAT_PLUGIN_HANDLE

View File

@ -1578,22 +1578,42 @@ xchat_free (xchat_plugin *ph, void *ptr)
int int
xchat_set_plugin_pref (xchat_plugin *pl, char *var, char *value) xchat_set_plugin_pref (xchat_plugin *pl, char *var, char *value)
{ {
int fh; FILE *fpIn;
char confname[32]; int fhOut;
int prevConfig;
char confname[64];
char confname_tmp[69];
char buffer[512]; /* the same as in cfg_put_str */
char buffer_tmp[512];
char *canon; char *canon;
canon = g_strdup (pl->name); canon = g_strdup (pl->name);
canonalize_key (canon); canonalize_key (canon);
sprintf (confname, "plugin_%s.conf", canon); sprintf (confname, "plugin_%s.conf", canon);
g_free (canon); g_free (canon);
sprintf (confname_tmp, "%s.new", confname);
/* partly borrowed from palette.c */ fhOut = xchat_open_file (confname_tmp, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE);
fh = xchat_open_file (confname, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE); fpIn = xchat_fopen_file (confname, "r", 0);
if (fh != -1)
if (fhOut == -1) /* unable to save, abort */
{ {
cfg_put_str (fh, var, value); return 0;
close (fh); }
else if (fpIn == NULL) /* no previous config, no parsing */
{
sprintf (buffer, "%s = %s\n", var, value);
write (fhOut, buffer, strlen (buffer));
close (fhOut);
#ifdef WIN32
sprintf (buffer, "%s/%s", get_xdir_fs (), confname);
unlink (buffer);
#endif
sprintf (buffer_tmp, "%s/%s", get_xdir_fs (), confname_tmp);
if (rename (buffer_tmp, buffer) == 0)
{
return 1; return 1;
} }
else else
@ -1601,15 +1621,61 @@ xchat_set_plugin_pref (xchat_plugin *pl, char *var, char *value)
return 0; return 0;
} }
} }
else /* existing config, preserve settings and find & replace current var value if any */
{
prevConfig = 0;
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 */
if (strncmp (buffer_tmp, buffer, strlen (var) + 1) == 0) /* given setting already exists */
{
sprintf (buffer, "%s = %s\n", var, value);
prevConfig = 1;
}
else
{
strcat (buffer, "\n");
}
write (fhOut, buffer, strlen (buffer));
}
fclose (fpIn);
if (!prevConfig)
{
sprintf (buffer, "%s = %s\n", var, value);
write (fhOut, buffer, strlen (buffer));
}
close (fhOut);
#ifdef WIN32
sprintf (buffer, "%s/%s", get_xdir_fs (), confname);
unlink (buffer);
#endif
sprintf (buffer_tmp, "%s/%s", get_xdir_fs (), confname_tmp);
if (rename (buffer_tmp, buffer) == 0)
{
return 1;
}
else
{
return 0;
}
}
}
int int
xchat_get_plugin_pref (xchat_plugin *pl, char *var, char *dest, int dest_len) xchat_get_plugin_pref (xchat_plugin *pl, char *var, char *dest)
{ {
//cfg_get_str (char *cfg, char *var, char *dest, int dest_len)
int fh; int fh;
int l; int l;
char confname[32]; char confname[64];
//char *buffer;
char *canon; char *canon;
char *cfg; char *cfg;
struct stat st; struct stat st;
@ -1619,18 +1685,22 @@ xchat_get_plugin_pref (xchat_plugin *pl, char *var, char *dest, int dest_len)
sprintf (confname, "plugin_%s.conf", canon); sprintf (confname, "plugin_%s.conf", canon);
g_free (canon); g_free (canon);
//buffer = (char*) malloc (dest_len);
/* partly borrowed from palette.c */ /* partly borrowed from palette.c */
fh = xchat_open_file (confname, O_RDONLY, 0, 0); fh = xchat_open_file (confname, _O_RDONLY, 0, 0);
if (fh != -1) if (fh == -1)
{ {
return 0;
}
fstat (fh, &st); fstat (fh, &st);
cfg = malloc (st.st_size + 1); cfg = malloc (st.st_size + 1);
if (cfg) if (!cfg)
{ {
return 0;
}
cfg[0] = '\0'; cfg[0] = '\0';
l = read (fh, cfg, st.st_size); l = read (fh, cfg, st.st_size);
@ -1639,23 +1709,12 @@ xchat_get_plugin_pref (xchat_plugin *pl, char *var, char *dest, int dest_len)
cfg[l] = '\0'; cfg[l] = '\0';
} }
if (!cfg_get_str (cfg, var, dest, dest_len)) if (!cfg_get_str (cfg, var, dest, 512)) /* dest_len is the same as buffer size in set */
{ {
return 0; return 0;
} }
free (cfg); free (cfg);
}
else
{
return 0;
}
close (fh); close (fh);
return 1; return 1;
} }
else
{
return 0;
}
}

View File

@ -142,8 +142,7 @@ struct _xchat_plugin
char *value); char *value);
int (*xchat_get_plugin_pref) (xchat_plugin *ph, int (*xchat_get_plugin_pref) (xchat_plugin *ph,
char *var, char *var,
char *dest, char *dest);
int dest_len);
}; };
#endif #endif
@ -307,8 +306,7 @@ xchat_set_plugin_pref (xchat_plugin *ph,
int int
xchat_get_plugin_pref (xchat_plugin *ph, xchat_get_plugin_pref (xchat_plugin *ph,
char *var, char *var,
char *dest, char *dest);
int dest_len);
#if !defined(PLUGIN_C) && defined(WIN32) #if !defined(PLUGIN_C) && defined(WIN32)
#ifndef XCHAT_PLUGIN_HANDLE #ifndef XCHAT_PLUGIN_HANDLE