Patchwork [03,of,55,RFC,c-hglib:level1] utils: additional functions (cmdbuilder and append_data) used for model 2

login
register
mail settings
Submitter Iulian Stana
Date Sept. 14, 2013, 12:35 a.m.
Message ID <25faae6c272acbdb530b.1379118915@doppler>
Download mbox | patch
Permalink /patch/2447/
State Deferred, archived
Headers show

Comments

Iulian Stana - Sept. 14, 2013, 12:35 a.m.
# HG changeset patch
# User Iulian Stana <julian.stana@gmail.com>
# Date 1379108169 -10800
#      Sat Sep 14 00:36:09 2013 +0300
# Node ID 25faae6c272acbdb530b7cefa5e16521f01851f9
# Parent  5277ca53b1457bfd051f2493f1bfde149cedc57e
utils: additional functions (cmdbuilder and append_data) used for model 2

Model (2):

(2) Wait for the commandserv to complete *all* his actions in response to
    the issued command, which is cache the commandserv response somewhere,
    and return the actual exitcode (channel 'r' from commandserv)
    as the result of the chglib API call"


cmdbuilder: A helper for building the command arguments
append_data: Append a pointer to a destination pointer

Patch

diff --git a/utils.c b/utils.c
--- a/utils.c
+++ b/utils.c
@@ -41,3 +41,44 @@ 
 	*cmd_size = cmd_length - 1;
 	return new_cmd;
 }
+
+/* 
+ * A helper for building the command arguments.
+ * */
+char **cmdbuilder(char *command, char *options[], ...)
+{
+	int cmd_size = 0, size = 0;
+	char **new_cmd;
+	char *argument;
+
+	va_list vl;
+	va_start (vl, NULL);
+
+	if(options)
+		for(size = 0; options[size] != NULL; size++);
+
+	cmd_size = size + 1;
+	new_cmd = malloc(sizeof(char *) * (cmd_size + 10));
+
+	new_cmd[0] = command;
+	if(options)
+		memcpy(new_cmd + 1, options, (cmd_size - 1)  * sizeof(char *));
+
+	while(argument = va_arg(vl, char *), argument != NULL){
+		new_cmd[cmd_size ++] = argument;
+	}
+
+	new_cmd[cmd_size] = NULL;
+	va_end(vl);
+	return new_cmd;
+}
+
+/* 
+ * Adding to the destination pointer the source pointer. 
+ * */
+int append_data(char **dest, char *source, int dsize, int ssize)
+{
+	*dest = realloc(*dest, dsize + ssize + 2);
+	memcpy(*dest + dsize, source, ssize + 1);
+	return 0;
+}
diff --git a/utils.h b/utils.h
--- a/utils.h
+++ b/utils.h
@@ -28,5 +28,27 @@ 
  * */
 char *cmd_prepare(char *const command[], int *cmd_size);
 
+/**
+ * \brief A helper for building the command arguments.
+ * \param command The name for mercurial command.
+ * \param options An array pointer to command option list.
+ * \param template Template to be used for this command.
+ * \retval new_string An array of pointers, where the command it's almost ready
+ *                    to be send to mercurial cmdserver. 
+ * */
+char **cmdbuilder(char *command, char *options[], ...);
+
+/**
+ * \brief Append a pointer to a destination pointer.
+ *
+ * Create space and concatenate the dest pointer with source pointer.
+ *
+ * \param dest The destination pointer where data will be stored.
+ * \param source The pointer that must be append.
+ * \param dsize Size of destination pointer.
+ * \param ssize Size of source pointer.
+ * \retval 0 To indicate success.
+ * */
+int append_data(char **dest, char *source, int dsize, int ssize);
 
 #endif