summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjstebbins <[email protected]>2014-08-21 16:14:53 +0000
committerjstebbins <[email protected]>2014-08-21 16:14:53 +0000
commit7fe318c4215657e406e6f47406900f3031c861a8 (patch)
tree025e81463d7707fbc63d2f1f916087ba21c7290b
parent2839ca4c710c080467da7f9d485446400b1e4588 (diff)
CLI: make string split function aware of escaping and quoting
Fixes problem with audio track names that contain commas git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6331 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--test/test.c61
1 files changed, 53 insertions, 8 deletions
diff --git a/test/test.c b/test/test.c
index 482608e1b..44d9dd349 100644
--- a/test/test.c
+++ b/test/test.c
@@ -3448,14 +3448,54 @@ static void ShowPresets()
printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E ffaac,copy:ac3 -B 160,160 -6 dpl2,none -R Auto,Auto -D 0.0,0.0 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 -4 --decomb --loose-anamorphic --modulus 2 -m --x264-preset medium --h264-profile high --h264-level 4.1\n");
printf("\n>\n");
}
-static char * hb_strndup( char * str, int len )
+
+static char* strchr_quote(char *pos, char c, char q)
+{
+ if (pos == NULL)
+ return NULL;
+
+ while (*pos != 0 && *pos != c)
+ {
+ if (*pos == q)
+ {
+ pos = strchr_quote(pos+1, q, 0);
+ if (pos == NULL)
+ return NULL;
+ pos++;
+ }
+ else if (*pos == '\\' && *(pos+1) != 0)
+ pos += 2;
+ else
+ pos++;
+ }
+ if (*pos != c)
+ return NULL;
+ return pos;
+}
+
+static char *strndup_quote(char *str, char q, int len)
{
+ if (str == NULL)
+ return NULL;
+
char * res;
int str_len = strlen( str );
-
+ int src = 0, dst = 0;
res = malloc( len > str_len ? str_len + 1 : len + 1 );
- strncpy( res, str, len );
- res[len] = '\0';
+
+ while (str[src] != 0 && src < len)
+ {
+ if (str[src] == q)
+ src++;
+ else if (str[src] == '\\' && str[src+1] != 0)
+ {
+ res[dst++] = str[src+1];
+ src += 2;
+ }
+ else
+ res[dst++] = str[src++];
+ }
+ res[dst] = '\0';
return res;
}
@@ -3465,7 +3505,12 @@ static char** str_split( char *str, char delem )
char * end;
char ** ret;
int count, i;
+ char quote = '"';
+ if (delem == '"')
+ {
+ quote = '\'';
+ }
if ( str == NULL || str[0] == 0 )
{
ret = malloc( sizeof(char*) );
@@ -3476,7 +3521,7 @@ static char** str_split( char *str, char delem )
// Find number of elements in the string
count = 1;
pos = str;
- while ( ( pos = strchr( pos, delem ) ) != NULL )
+ while ( ( pos = strchr_quote( pos, delem, quote ) ) != NULL )
{
count++;
pos++;
@@ -3487,11 +3532,11 @@ static char** str_split( char *str, char delem )
pos = str;
for ( i = 0; i < count - 1; i++ )
{
- end = strchr( pos, delem );
- ret[i] = hb_strndup(pos, end - pos);
+ end = strchr_quote( pos, delem, quote );
+ ret[i] = strndup_quote(pos, quote, end - pos);
pos = end + 1;
}
- ret[i] = strdup(pos);
+ ret[i] = strndup_quote(pos, quote, strlen(pos));
return ret;
}