FreeBASIC
0.91.0
Main Page
Data Structures
Files
File List
Globals
sys_parseargs.c
Go to the documentation of this file.
1
#include "
fb.h
"
2
3
/* dst - preallocated buffer to hold processed args
4
src - source string for arguments, may contain embedded null chars
5
length - length of src
6
returns -1 on error, or number of arguments */
7
int
fb_hParseArgs
(
char
*dst,
const
char
*src, ssize_t length )
8
{
9
int
in_quote = 0, argc = 0;
10
ssize_t bs_count = 0, i = 0;
11
const
char
*s = src;
12
char
*
p
= dst;
13
14
/* s - next char to read from src
15
p - next char to write in dst */
16
17
/* return -1 to indicate error */
18
if
( src ==
NULL
|| dst ==
NULL
|| length < 0 )
19
return
-1;
20
21
/* skip leading white space */
22
while
( i < length && (*s ==
' '
|| *s ==
'\0'
) ) {
23
i++;
24
s++;
25
}
26
27
/* scan for arguments. ' ' and '\0' are delimiters */
28
while
( i < length ) {
29
bs_count = 0;
30
31
do
{
32
if
( *s ==
'\\'
) {
33
*p++ = *s;
34
bs_count++;
35
}
else
{
36
if
( *s ==
'\"'
) {
37
if
( bs_count & 1 ) {
38
p -= ((bs_count - 1) >> 1) + 1;
39
*p++ = *s;
40
}
else
{
41
p -= ( bs_count >> 1 );
42
in_quote = !in_quote;
43
}
44
}
else
if
( *s ==
' '
|| *s ==
'\0'
) {
45
if
( in_quote ) {
46
*p++ =
' '
;
47
}
else
{
48
*p++ =
'\0'
;
49
break
;
50
}
51
}
else
{
52
*p++ = *s;
53
}
54
55
bs_count = 0;
56
}
57
58
i++;
59
s++;
60
61
}
while
( i < length );
62
63
argc++;
64
65
/* skip trailing white space */
66
while
( i < length && ( *s ==
' '
|| *s ==
'\0'
) ) {
67
i++;
68
s++;
69
}
70
}
71
72
*p =
'\0'
;
73
74
/* return arguments found */
75
return
argc;
76
}
rtlib
sys_parseargs.c
Generated on Thu Jan 23 2014 19:40:14 for FreeBASIC by
1.8.4