From patchwork Fri Sep 13 20:31:57 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [07, of, 11, c-hglib:level0, V2] hg_rawcommannd: additional functions (free_data and cmd_prepare) From: Iulian Stana X-Patchwork-Id: 2438 Message-Id: <4a3e55c42250a8efa86c.1379104317@doppler> To: mercurial-devel@selenic.com Date: Fri, 13 Sep 2013 23:31:57 +0300 # HG changeset patch # User Iulian Stana # Date 1379103560 -10800 # Fri Sep 13 23:19:20 2013 +0300 # Node ID 4a3e55c42250a8efa86cd1ce83bfee48fbc10378 # Parent f590a6dd6fe4e6402ba645b7a59ce009780d6fef 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 diff --git a/utils.c b/utils.c --- a/utils.c +++ b/utils.c @@ -15,3 +15,29 @@ return (val << 16) | (val >> 16); } +/* + * Prepare the command for sending process. + * */ +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; +} diff --git a/utils.h b/utils.h --- a/utils.h +++ b/utils.h @@ -10,4 +10,23 @@ * */ uint32_t swap_uint32( uint32_t val ); +/** + * \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); + + #endif