patch to load/save filters
- From: Emmanuel <e allaud wanadoo fr>
- To: balsa-list gnome org
- Subject: patch to load/save filters
- Date: Sun, 24 Jun 2001 13:40:22 +0200
Hi all,
I apologize for the preceding patch which was totally messed up.
Here is the good one. It's against 1.1.5, sorry. Moreover I couldn't
actually test or compile it I'm still struggling with libs dependencies.
Please give it a try and comment it.
I had to change the prototype of filter_load(GList *,...) to
filter_load(GList **,...) to be able to return the newly allocated GList
during the load.
I also correct in filter-funcs.c the filter_new function to initialize
the "name" field to NULL.
Bye
Manu
--- filter-funcs.c Fri Oct 6 17:50:50 2000
+++ filter-funcs.c.corr Fri Jun 22 08:48:24 2001
@@ -158,6 +158,7 @@
return (NULL);
}
+ newfil->name=NULL;
newfil->type = FILTER_NONE;
newfil->flags = FILTER_EMPTY;
newfil->match_fields = FILTER_EMPTY;
--- filter-file.c Fri Oct 6 17:50:50 2000
+++ filter-file.c.corr Sun Jun 24 13:28:14 2001
@@ -51,11 +51,13 @@
* Returns:
* int - 0 for success, -1 for error. Sets filter_errno on error.
*/
-gint filter_load(GList * filter_list, gchar * filter_file)
+gint filter_load(GList ** filter_list, gchar * filter_file)
{
FILE *fp;
- gchar *buf;
+ gchar *buf,*cur,*p;
+ filter *new_filter;
size_t len;
+ gboolean syn_error=FALSE;
if ((!filter_file) || (filter_file[0] == '\0')) {
filter_errno = FILTER_ENOFILE;
@@ -74,9 +76,114 @@
return (-FILTER_ENOREAD);
}
+ filter_errno=FILTER_NOERR;
+ filter_clear_filters(*filter_list);
+ *filter_list=NULL;
len = libbalsa_readfile(fp, &buf);
+ cur=buf;
+ while (!syn_error && *cur) {
+ new_filter=filter_new();
+ /* First we read the fixed part of the filter (all but the regex list */
+ field=1;
+ while (!syn_error && (field<=11)) {
+ p=strchr(cur,'\n');
+ if (p) {
+ *p='\0';
+ /* We read the field */
+ switch (field) {
+ case 1:
+ new_filter->group=atoi(cur);
+ /* FIXME : chek value ????*/
+ break;
+ case 2:
+ new_filter->name=g_strdup(cur);
+ break;
+ case 3:
+ new_filter->type=atoi(cur);
+ syn_error=
+ (new_filter->type<FILTER_NONE)
+ || (new_filter->type>FILTER_EXEC);
+ break;
+ case 4:
+ new_filter->match_when=atoi(cur);
+ syn_error=
+ (new_filter->match_when<FILTER_MATCHES)
+ || (new_filter->match_when>FILTER_ALWAYS);
+ break;
+ case 5:
+ new_filter->flags=atoi(cur);
+ /* FIXME : chek value ????*/
+ break;
+ case 6:
+ strncpy(new_filter->match.string,cur,1024);
+ /* Be sure it's null terminated */
+ new_filter->match.string[1023]='\0';
+ break;
+ case 7:
+ if (new_filter->type==FILTER_SIMPLE) {
+ new_filter->match_fields=atoi(cur);
+ break
+ }
+ /* If filter is not of type FILTER_SIMPLE
+ we pass directly to next field */
+ else field++;
+ case 8:
+ strncpy(new_filter->sound,cur,PATH_MAX);
+ /* Be sure it's null terminated */
+ new_filter->sound[PATH_MAX-1]='\0';
+ /* FIXME : Verify sound path??? */
+ break;
+ case 9:
+ strncpy(new_filter->popup_text,cur,256);
+ /* Be sure it's null terminated */
+ new_filter->popup_text[255]='\0';
+ break;
+ case 10:
+ new_filter->action=atoi(cur);
+ syn_error=
+ (new_filter->type<FILTER_NOTHING)
+ || (new_filter->type>FILTER_TRASH);
+ break;
+ case 11:
+ strncpy(new_filter->action_string,cur,PATH_MAX);
+ /* Be sure it's null terminated */
+ new_filter->action_string[PATH_MAX-1]='\0';
+ break;
+ }
+ /* Next field */
+ cur=p++;field++;
+ }
+ /* Syntax error : we lack fields */
+ else syn_error=TRUE;
+ }
+ /*If syntax is still correct, we read the regex list */
+ if (!syn_error) {
+ while (*cur && (filter_errno==FILTER_NOERR) &&
+ (strncmp(cur,"END OF REGEX LIST",17)!=0)) {
+ p=strchr(cur,'\n');
+ if (p) {
+ *p='\0';
+ filter_errno=filter_append_regex(new_filter,cur);
+ /* Next regex */
+ cur=p++;
+ }
+ /* File must finish by a \n */
+ else syn_error=TRUE;
+ }
+ }
+ /* We append the new filter even if it is not correct because
+ of a syntax error, anyway it will be freed in that case (see below) */
+ *filter_list=g_list_append(*filter_list,new_filter);
+ }
+ g_free(buf);
fclose(fp);
-
+ if (syn_error || (filter_errno!=FILTER_NOERR)) {
+ filter_clear_filters(*filter_list);
+ *filter_list=NULL;
+ filter_errno=
+ (filter_errno!=FILTER_NOERR) ? filter_errno : FILTER_EFILESYN;
+ return -filter_errno;
+ }
return (0);
}
@@ -84,7 +191,7 @@
/*
* filter_save()
*
- * Saves the filters into the defninition file
+ * Saves the filters into the definition file
*
* Arguments:
* GList *filter_list - the list of filters to save
@@ -95,5 +202,105 @@
*/
gint filter_save(GList * filter_list, gchar * filter_file)
{
- return 0;
+ FILE *fp;
+ int fd,field,err;
+ gchar buf[SHORT_STRING];
+ GList * lst,* regexs;
+
+ if ((!filter_file) || (filter_file[0] == '\0')) {
+ filter_errno = FILTER_ENOFILE;
+ return (-FILTER_ENOFILE);
+ }
+
+ if (!(fp = fopen(filter_file, "w"))) {
+ gchar filter_file_error[1024];
+
+ g_snprintf(filter_file_error,
+ 1024, "Unable to save filters to file %s", filter_file);
+ perror(filter_file_error);
+ filter_errno = FILTER_ENOWRITE;
+ return (-filter_errno);
+ }
+
+ fd = fileno(fp);
+ err=0;
+ lst=filter_list;
+ while (lst && (err>=0)) {
+ field=1;
+ while ((field <=11) && (err>=0)) {
+ switch (field) {
+ case 1:
+ g_sprintf(buf,"%u\n",((filter *)lst->data)->group);
+ err=write(fd,buf,strlen(buf));
+ break;
+ case 2:
+ err=write(fd,((filter *)lst->data)->name,
+ strlen((filter *)lst->data)->name));
+ if (err>=0) err=write(fd,"\n",1);
+ break;
+ case 3:
+ g_sprintf(buf,"%u\n",((filter *)lst->data)->type);
+ err=write(fd,buf,strlen(buf));
+ break;
+ case 4:
+ g_sprintf(buf,"%u\n",((filter *)lst->data)->match_when);
+ err=write(fd,buf,strlen(buf));
+ break;
+ case 5:
+ g_sprintf(buf,"%u\n",((filter *)lst->data)->flags);
+ err=write(fd,buf,strlen(buf));
+ break;
+ case 6:
+ err=write(fd,((filter *)lst->data)->match.string,
+ strlen((filter *)lst->data)->match.string));
+ if (err>=0) err=write(fd,"\n",1);
+ break;
+ case 7:
+ if (new_filter->type==FILTER_SIMPLE) {
+ g_sprintf(buf,"%u\n",((filter *)lst->data)->match_fields);
+ err=write(fd,buf,strlen(buf));
+ break;
+ }
+ /* If filter is not of type FILTER_SIMPLE
+ we pass directly to next field */
+ else field++;
+ case 8:
+ err=write(fd,((filter *)lst->data)->sound,
+ strlen((filter *)lst->data)->sound));
+ if (err>=0) err=write(fd,"\n",1);
+ break;
+ case 9:
+ err=write(fd,((filter *)lst->data)->popup_text,
+ strlen((filter *)lst->data)->popup_text));
+ if (err>=0) err=write(fd,"\n",1);
+ break;
+ case 10:
+ g_sprintf(buf,"%u\n",((filter *)lst->data)->action);
+ err=write(fd,buf,strlen(buf));
+ break;
+ case 11:
+ err=write(fd,((filter *)lst->data)->action_string,
+ strlen((filter *)lst->data)->action_string));
+ if (err>=0) err=write(fd,"\n",1);
+ break;
+ }
+ /* Next field */
+ field++;
+ }
+/* We save each reg exp on one line */
+ regexs=((filter *)lst->data)->regex;
+ while (regexs && (err>=0)) {
+ err=write(fd,((filter_regex *)regexs->data)->string,
+ strlen((filter_regex *)regexs->data)->string));
+ if (err>=0) err=write(fd,"\n",1);
+ regexs=g_list_next(regexs);
+ }
+/* We mark the end of the reg exp list */
+ err=write(fd,"END OF REGEX LIST\n",18);
+/* Next filter */
+ lst=g_list_next(lst);
+ }
+ fclose(fp);
+ filter_errno=(err>=0) ? FILTER_NOERR : FILTER_ENOWRITE;
+ return (filter_errno==FILTER_ENOERR) ? 0 : -filter_errno;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]