Просмотр исходного кода

de-duplicate handling of box operation (create & expunge) options

loops work just fine ...
Oswald Buddenhagen 10 лет назад
Родитель
Сommit
5af1796777
1 измененных файлов с 43 добавлено и 48 удалено
  1. 43 48
      src/config.c

+ 43 - 48
src/config.c

@@ -143,10 +143,19 @@ parse_size( conffile_t *cfile )
 	return ret;
 }
 
+static const struct {
+	int op;
+	const char *name;
+} boxOps[] = {
+	{ OP_EXPUNGE, "Expunge" },
+	{ OP_CREATE, "Create" },
+};
+
 static int
 getopt_helper( conffile_t *cfile, int *cops, channel_conf_t *conf )
 {
 	char *arg;
+	uint i;
 
 	if (!strcasecmp( "Sync", cfile->cmd )) {
 		arg = cfile->val;
@@ -188,38 +197,6 @@ getopt_helper( conffile_t *cfile, int *cops, channel_conf_t *conf )
 			}
 		while ((arg = get_arg( cfile, ARG_OPTIONAL, 0 )));
 		conf->ops[M] |= XOP_HAVE_TYPE;
-	} else if (!strcasecmp( "Expunge", cfile->cmd )) {
-		arg = cfile->val;
-		do
-			if (!strcasecmp( "Both", arg ))
-				*cops |= OP_EXPUNGE;
-			else if (!strcasecmp( "Master", arg ))
-				conf->ops[M] |= OP_EXPUNGE;
-			else if (!strcasecmp( "Slave", arg ))
-				conf->ops[S] |= OP_EXPUNGE;
-			else if (strcasecmp( "None", arg )) {
-				error( "%s:%d: invalid Expunge arg '%s'\n",
-				       cfile->file, cfile->line, arg );
-				cfile->err = 1;
-			}
-		while ((arg = get_arg( cfile, ARG_OPTIONAL, 0 )));
-		conf->ops[M] |= XOP_HAVE_EXPUNGE;
-	} else if (!strcasecmp( "Create", cfile->cmd )) {
-		arg = cfile->val;
-		do
-			if (!strcasecmp( "Both", arg ))
-				*cops |= OP_CREATE;
-			else if (!strcasecmp( "Master", arg ))
-				conf->ops[M] |= OP_CREATE;
-			else if (!strcasecmp( "Slave", arg ))
-				conf->ops[S] |= OP_CREATE;
-			else if (strcasecmp( "None", arg )) {
-				error( "%s:%d: invalid Create arg '%s'\n",
-				       cfile->file, cfile->line, arg );
-				cfile->err = 1;
-			}
-		while ((arg = get_arg( cfile, ARG_OPTIONAL, 0 )));
-		conf->ops[M] |= XOP_HAVE_CREATE;
 	} else if (!strcasecmp( "SyncState", cfile->cmd ))
 		conf->sync_state = expand_strdup( cfile->val );
 	else if (!strcasecmp( "CopyArrivalDate", cfile->cmd ))
@@ -228,8 +205,30 @@ getopt_helper( conffile_t *cfile, int *cops, channel_conf_t *conf )
 		conf->max_messages = parse_int( cfile );
 	else if (!strcasecmp( "ExpireUnread", cfile->cmd ))
 		conf->expire_unread = parse_bool( cfile );
-	else
+	else {
+		for (i = 0; i < as(boxOps); i++) {
+			if (!strcasecmp( boxOps[i].name, cfile->cmd )) {
+				int op = boxOps[i].op;
+				arg = cfile->val;
+				do {
+					if (!strcasecmp( "Both", arg )) {
+						*cops |= op;
+					} else if (!strcasecmp( "Master", arg )) {
+						conf->ops[M] |= op;
+					} else if (!strcasecmp( "Slave", arg )) {
+						conf->ops[S] |= op;
+					} else if (strcasecmp( "None", arg )) {
+						error( "%s:%d: invalid %s arg '%s'\n",
+						       cfile->file, cfile->line, boxOps[i].name, arg );
+						cfile->err = 1;
+					}
+				} while ((arg = get_arg( cfile, ARG_OPTIONAL, 0 )));
+				conf->ops[M] |= op * (XOP_HAVE_EXPUNGE / OP_EXPUNGE);
+				return 1;
+			}
+		}
 		return 0;
+	}
 	return 1;
 }
 
@@ -257,7 +256,8 @@ getcline( conffile_t *cfile )
 int
 merge_ops( int cops, int ops[] )
 {
-	int aops;
+	int aops, op;
+	uint i;
 
 	aops = ops[M] | ops[S];
 	if (ops[M] & XOP_HAVE_TYPE) {
@@ -290,21 +290,16 @@ merge_ops( int cops, int ops[] )
 				ops[M] |= cops & OP_MASK_TYPE;
 		}
 	}
-	if (ops[M] & XOP_HAVE_EXPUNGE) {
-		if (aops & cops & OP_EXPUNGE) {
-			error( "Conflicting Expunge args specified.\n" );
-			return 1;
-		}
-		ops[M] |= cops & OP_EXPUNGE;
-		ops[S] |= cops & OP_EXPUNGE;
-	}
-	if (ops[M] & XOP_HAVE_CREATE) {
-		if (aops & cops & OP_CREATE) {
-			error( "Conflicting Create args specified.\n" );
-			return 1;
+	for (i = 0; i < as(boxOps); i++) {
+		op = boxOps[i].op;
+		if (ops[M] & (op * (XOP_HAVE_EXPUNGE / OP_EXPUNGE))) {
+			if (aops & cops & op) {
+				error( "Conflicting %s args specified.\n", boxOps[i].name );
+				return 1;
+			}
+			ops[M] |= cops & op;
+			ops[S] |= cops & op;
 		}
-		ops[M] |= cops & OP_CREATE;
-		ops[S] |= cops & OP_CREATE;
 	}
 	return 0;
 }