[Devel] [PATCH 3/4] add_dist_action(): shorter re-write w/o copy-paste

Igor Podlesny openvz at poige.ru
Tue May 14 22:22:07 PDT 2013


---
 src/lib/dist.c |   57 +++++++++++++++++++-------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)

diff --git a/src/lib/dist.c b/src/lib/dist.c
index 1ab263d..3ed7347 100644
--- a/src/lib/dist.c
+++ b/src/lib/dist.c
@@ -51,10 +51,25 @@ static int get_action_id(char *name)
 	return -1;
 }
 
+static inline char **action_id2ptr(int const id, dist_actions *const d_actions)
+{
+	switch (id) {
+		case ADD_IP:		return &d_actions->add_ip;
+		case DEL_IP:		return &d_actions->del_ip;
+		case SET_HOSTNAME:	return &d_actions->set_hostname;
+		case SET_DNS:		return &d_actions->set_dns;
+		case SET_USERPASS:	return &d_actions->set_userpass;
+		case SET_UGID_QUOTA:	return &d_actions->set_ugid_quota;
+		case POST_CREATE:	return &d_actions->post_create;
+		default:		return NULL;
+	}
+}
+
 static int add_dist_action(dist_actions *d_actions, char *name, char *action,
 	char *dir)
 {
-	char file[256];
+	char file[256],
+		**act = NULL;
 	int id;
 
 	if (!action[0])
@@ -66,43 +81,9 @@ static int add_dist_action(dist_actions *d_actions, char *name, char *action,
 		logger(-1, 0, "Action script %s not found", file);
 		return 0;
 	}
-	switch (id) {
-		case ADD_IP:
-			if (d_actions->add_ip != NULL)
-				break;
-			d_actions->add_ip = strdup(file);
-			break;
-		case DEL_IP:
-			if (d_actions->del_ip != NULL)
-				break;
-			d_actions->del_ip = strdup(file);
-			break;
-		case SET_HOSTNAME:
-			if (d_actions->set_hostname != NULL)
-				break;
-			d_actions->set_hostname = strdup(file);
-			break;
-		case SET_DNS:
-			if (d_actions->set_dns != NULL)
-				break;
-			d_actions->set_dns = strdup(file);
-			break;
-		case SET_USERPASS:
-			if (d_actions->set_userpass != NULL)
-				break;
-			d_actions->set_userpass = strdup(file);
-			break;
-		case SET_UGID_QUOTA:
-			if (d_actions->set_ugid_quota != NULL)
-				break;
-			d_actions->set_ugid_quota = strdup(file);
-			break;
-		case POST_CREATE:
-			if (d_actions->post_create != NULL)
-				break;
-			d_actions->post_create = strdup(file);
-			break;
-	}
+	if ((act = action_id2ptr(id, d_actions)) != NULL &&
+		*act == NULL)
+		*act = strdup(file);
 	return 0;
 }
 
-- 
1.7.9.5




More information about the Devel mailing list