[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
libspeechd SPDVoice cleanup api
From: |
Bohdan R . Rau |
Subject: |
libspeechd SPDVoice cleanup api |
Date: |
Sat, 25 Oct 2014 10:11:31 +0200 |
W dniu 2014-10-24 11:29, Bohdan R. Rau napisa?(a):
> On library level: we need only one malloc (no reallocs) in
> spd_execute_command_with_list_reply function
Code below is not tested - it only shows possibility of single malloc
instead of several strndups and reallocs ;)
char **spd_execute_command_with_list_reply(SPDConnection * connection,
char *command)
{
char *reply = NULL;
char *line;
int err;
char **result;
int i, count, linelen;
char *string_block;
size_t result_size=sizeof(char *);
spd_execute_command_with_reply(connection, command, &reply);
if (!ret_ok(reply)) {
if (reply != NULL)
free(reply);
return NULL;
}
for (count=0 ;; count++) {
line = get_param_str(reply, i + 1, &linelen, &err);
if ((err) || (line == NULL))
break;
result_size += linelen + 1 + sizeof(char *);
}
result=malloc(result_size);
string_block=(char *)(result + count + 1);
for (i=0 ; i < count; i++) {
result[i]=string_block;
line = get_param_str_len(reply, i + 1, &linelen, &err);
memcpy(string_block,line,linelen);
string_block += linelen;
*string_block++ = 0;
}
result[i]=NULL;
free(reply);
return result;
}
static char *get_param_str_len(char *reply, int num, int *len, int
*err)
{
int i;
char *tptr;
char *pos;
char *pos_begin;
char *pos_end;
assert(err != NULL);
if (num < 1) {
*err = -1;
return NULL;
}
pos = reply;
for (i = 0; i <= num - 2; i++) {
pos = strstr(pos, "\r\n");
if (pos == NULL) {
*err = -2;
return NULL;
}
pos += 2;
}
if (strlen(pos) < 4)
return NULL;
*err = strtol(pos, &tptr, 10);
if (*err >= 300 && *err <= 399)
return NULL;
if ((*tptr != '-') || (tptr != pos + 3)) {
*err = -3;
return NULL;
}
pos_begin = pos + 4;
pos_end = strstr(pos_begin, "\r\n");
if (pos_end == NULL) {
*err = -4;
return NULL;
}
*len = (pos_end - pos_begin);
*err = 0;
return pos_begin;
}
static char *get_param_str(char *reply, int num, int *err)
{
char *rc;
int len;
rc=get_param_str_len(reply,num,&len,err);
if (rc) {
rc = strndup(rc,len);
}
return rc;
}
ethanak
--
http://milena.polip.com/ - Pa pa, Ivonko!
libspeechd SPDVoice cleanup api, Jeremy Whiting, 2014/10/24
libspeechd SPDVoice cleanup api, Luke Yelavich, 2014/10/24