Clean up DirReadParam a bit, reworked so that the code for adding a filename parameter doesn't need to be duplicated 3 times. Should handle quotes properly now (bug 4177)

svn path=/trunk/; revision=39699
This commit is contained in:
Jeffrey Morlan 2009-02-21 18:12:16 +00:00
parent a98eb8d135
commit a4da9945e8

View file

@ -250,18 +250,13 @@ DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
LPTSTR temp; LPTSTR temp;
/* Initialize parameter array */ /* Initialize parameter array */
if(!params)
return FALSE;
*params = NULL; *params = NULL;
*entries = 0; *entries = 0;
ptrStart = NULL;
ptrEnd = NULL;
/* Initialize variables; */ /* Initialize variables; */
cCurSwitch = _T(' '); cCurSwitch = _T(' ');
bNegative = FALSE; bNegative = FALSE;
bPNegative = FALSE; bPNegative = FALSE;
bIntoQuotes = FALSE;
/* We suppose that switch parameters /* We suppose that switch parameters
were given to avoid setting them to default were given to avoid setting them to default
@ -348,75 +343,45 @@ DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
cCurSwitch = _T(' '); cCurSwitch = _T(' ');
} }
else if ((cCurSwitch == _T(' ')) || (cCurSwitch == _T('P'))) else if (cCurSwitch == _T(' '))
{ {
/* 2nd section (see README_DIR.txt) */ /* 2nd section (see README_DIR.txt) */
/* We are expecting parameter or the unknown */ /* We are expecting parameter or the unknown */
if (cCurChar == _T('/')) if (cCurChar == _T('/'))
cCurSwitch = _T('/'); cCurSwitch = _T('/');
/* Process a spacer */
else if (cCurChar == _T(' ')) else if (cCurChar == _T(' '))
{ /* do nothing */;
if (!bIntoQuotes)
{
cCurSwitch = _T(' ');
if(ptrStart && ptrEnd)
{
temp = cmd_alloc(((ptrEnd - ptrStart) + 2) * sizeof (TCHAR));
if(!temp)
return FALSE;
memcpy(temp, ptrStart, ((ptrEnd - ptrStart) + 2) * sizeof (TCHAR));
temp[(ptrEnd - ptrStart + 1)] = _T('\0');
if(!add_entry(entries, params, temp))
{
cmd_free(temp);
freep(*params);
return FALSE;
}
cmd_free(temp);
ptrStart = NULL;
ptrEnd = NULL;
}
}
}
else if (cCurChar == _T('\"'))
{
/* Process a quote */
bIntoQuotes = !bIntoQuotes;
if(!bIntoQuotes)
ptrEnd = Line;
}
else else
{ {
/* Process a character for parameter */ /* This is a file/directory name parameter. Find its end */
if ((cCurSwitch == _T(' ')) && ptrStart && ptrEnd) ptrStart = Line;
bIntoQuotes = FALSE;
while (*Line)
{ {
temp = cmd_alloc(((ptrEnd - ptrStart) + 2) * sizeof (TCHAR)); if (!bIntoQuotes && (*Line == _T('/') || *Line == _T(' ')))
if(!temp) break;
return FALSE; bIntoQuotes ^= (*Line == _T('"'));
memcpy(temp, ptrStart, ((ptrEnd - ptrStart) + 2) * sizeof (TCHAR)); Line++;
temp[(ptrEnd - ptrStart + 1)] = _T('\0');
if(!add_entry(entries, params, temp))
{
cmd_free(temp);
freep(*params);
return FALSE;
}
cmd_free(temp);
ptrStart = NULL;
ptrEnd = NULL;
} }
cCurSwitch = _T('P');
if(!ptrStart)
ptrStart = ptrEnd = Line;
ptrEnd = Line; ptrEnd = Line;
/* Copy it to the entries list */
temp = cmd_alloc((ptrEnd - ptrStart + 1) * sizeof (TCHAR));
if(!temp)
return FALSE;
memcpy(temp, ptrStart, (ptrEnd - ptrStart) * sizeof (TCHAR));
temp[ptrEnd - ptrStart] = _T('\0');
StripQuotes(temp);
if(!add_entry(entries, params, temp))
{
cmd_free(temp);
freep(*params);
return FALSE;
}
cmd_free(temp);
continue;
} }
} }
else else
@ -564,26 +529,6 @@ DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
Line++; Line++;
} }
/* Terminate the parameters */
if(ptrStart && ptrEnd)
{
temp = cmd_alloc((ptrEnd - ptrStart + 2) * sizeof(TCHAR));
if(!temp)
return FALSE;
memcpy(temp, ptrStart, (ptrEnd - ptrStart + 1) * sizeof(TCHAR));
temp[(ptrEnd - ptrStart + 1)] = _T('\0');
if(!add_entry(entries, params, temp))
{
cmd_free(temp);
freep(*params);
return FALSE;
}
cmd_free(temp);
ptrStart = NULL;
ptrEnd = NULL;
}
/* Calculate the switches with no switch paramater */ /* Calculate the switches with no switch paramater */
if (!(lpFlags->stAttribs.bParSetted)) if (!(lpFlags->stAttribs.bParSetted))
@ -597,7 +542,7 @@ DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
lpFlags->stOrderBy.eCriteria[0] = ORDER_NAME; lpFlags->stOrderBy.eCriteria[0] = ORDER_NAME;
lpFlags->stOrderBy.bCriteriaRev[0] = FALSE; lpFlags->stOrderBy.bCriteriaRev[0] = FALSE;
} }
if (!(lpFlags->stOrderBy.bParSetted)) if (!(lpFlags->stTimeField.bParSetted))
lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE ; lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE ;
/* Calculate the unsetted switches (the "-" prefixed)*/ /* Calculate the unsetted switches (the "-" prefixed)*/