Patchwork [07,of,14,RFC,c-hglib:level0] hg_rawcommannd: additional functions (free_data and cmd_prepare)

login
register
mail settings
Submitter Iulian Stana
Date Sept. 3, 2013, 8:30 p.m.
Message ID <a2462b15fca8923a2306.1378240246@doppler>
Download mbox | patch
Permalink /patch/2312/
State Deferred, archived
Headers show

Comments

Iulian Stana - Sept. 3, 2013, 8:30 p.m.
# HG changeset patch
# User Iulian Stana <julian.stana@gmail.com>
# Date 1378235460 -10800
#      Tue Sep 03 22:11:00 2013 +0300
# Node ID a2462b15fca8923a230647f906de40f6ebd585f0
# Parent  bc8721af130cb444e6a0c5351c6ea56f44f283ab
hg_rawcommannd: additional functions (free_data and cmd_prepare)

free_data: release the used memory from out_data, hg_handle field
cmd_prepare: prepare the command for sending process

Patch

diff --git a/client.c b/client.c
--- a/client.c
+++ b/client.c
@@ -152,6 +152,7 @@ 
 	return handle;
 }
 
+
 /*
  * Close the connection for the given handle.
  * */
@@ -175,6 +176,55 @@ 
 	return 0;
 }
 
+/* Release data from handle pointers. */
+void free_data(hg_handle *handle){
+	if(handle->out_data){
+		free(handle->out_data);
+		handle->out_data = NULL;
+		handle->out_data_size = 0;
+	}
+}
+
+/**
+ * \brief Prepare the command for sending process. 
+ * 
+ * Replace all the blank space with the '\0' character.
+ * \param command an array that will contain the mercurial command
+ * \param cmd_size - array size
+ * \retval string representing the command that will be send to cmdsrv
+ * \retval *cmd_size will be set on string size
+ * 
+ * \code
+ *    char *command[] = {"tip", "-p", NULL};
+ *    char *cmd_str = cmd_prepare(command, 0);
+ *    prinf("==%s==", cmd_str);
+ *    ---> ==tip\0-p==
+ * \endcode
+ * */
+char *cmd_prepare(char *const command[], int *cmd_size)
+{
+	size_t cmd_length = 0;
+	char *new_cmd;
+	int i = 0;
+
+	while(command[i]){
+		cmd_length += strlen(command[i]) + 1;
+		++i;
+	}
+
+	new_cmd = malloc(cmd_length + 1);
+	i = 0;
+	while(command[i]){
+		strcpy(new_cmd, command[i]);
+		new_cmd += strlen(command[i]) + 1; 
+		++i;
+	}
+	new_cmd -= cmd_length;
+
+	*cmd_size = cmd_length - 1;
+	return new_cmd;
+}
+
 /*
  * Sending a command to the mercurial command server, through the given handle.
  * */