1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +02:00

Fix embedded quotation.

This commit is contained in:
Alain Magloire 2003-02-25 16:05:46 +00:00
parent 7e0b8b204c
commit 203cb53ced
2 changed files with 38 additions and 12 deletions

View file

@ -739,19 +739,32 @@ int copyTo(char * target, const char * source, int cpyLength, int availSpace)
BOOL bSlash = FALSE; BOOL bSlash = FALSE;
int i = 0, j = 0; int i = 0, j = 0;
int totCpyLength = cpyLength; int totCpyLength = cpyLength;
BOOL bQoutedTerm = FALSE;
#define QUOTATION_DO 0
#define QUOTATION_DONE 1
#define QUOTATION_NONE 2
int nQuotationMode = 0;
if(availSpace <= cpyLength) // = to reserve space for final '\0' if(availSpace <= cpyLength) // = to reserve space for final '\0'
return -1; return -1;
if(('\"' == *source) && ('\"' == *(source + cpyLength - 1))) if(('\"' == *source) && ('\"' == *(source + cpyLength - 1)))
bQoutedTerm = TRUE; // Already quoted {
nQuotationMode = QUOTATION_DONE;
}
else else
if(strchr(source, ' ') == NULL) if(strchr(source, ' ') == NULL)
bQoutedTerm = TRUE; // No reason to quotate term becase it doesn't have embedded spaces {
// No reason to quotate term becase it doesn't have embedded spaces
nQuotationMode = QUOTATION_NONE;
}
else else
{ {
// Needs to be quotated
nQuotationMode = QUOTATION_DO;
*target = '\"'; *target = '\"';
++j; ++j;
} }
@ -763,7 +776,8 @@ int copyTo(char * target, const char * source, int cpyLength, int availSpace)
bSlash = TRUE; bSlash = TRUE;
else else
{ {
if(source[i] == '\"' && (!bQoutedTerm || ((i != 0) && (i != (cpyLength - 1))) ) ) // Don't escape embracing quotation marks
if((source[i] == '\"') && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))) ) )
{ {
if(!bSlash) // If still not escaped if(!bSlash) // If still not escaped
{ {
@ -781,7 +795,7 @@ int copyTo(char * target, const char * source, int cpyLength, int availSpace)
target[j] = source[i]; target[j] = source[i];
} }
if(!bQoutedTerm) if(nQuotationMode == QUOTATION_DO)
{ {
if(j == availSpace) if(j == availSpace)
return -1; return -1;

View file

@ -187,29 +187,41 @@ int copyTo(LPTSTR target, LPCTSTR source, int cpyLength, int availSpace)
BOOL bSlash = FALSE; BOOL bSlash = FALSE;
int i = 0, j = 0; int i = 0, j = 0;
int totCpyLength = cpyLength; int totCpyLength = cpyLength;
BOOL bQoutedTerm = FALSE;
#define QUOTATION_DO 0
#define QUOTATION_DONE 1
#define QUOTATION_NONE 2
int nQuotationMode = 0;
if(availSpace <= cpyLength) // = to reserve space for '\0' if(availSpace <= cpyLength) // = to reserve space for '\0'
return -1; return -1;
if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1)))
bQoutedTerm = TRUE; // Already quoted {
// Already done
nQuotationMode = QUOTATION_DONE;
}
else else
if(_tcschr(source, _T(' ')) == NULL) if(_tcschr(source, _T(' ')) == NULL)
bQoutedTerm = TRUE; // No reason to quotate term becase it doesn't have embedded spaces {
// No reason to quotate term becase it doesn't have embedded spaces
nQuotationMode = QUOTATION_NONE;
}
else else
{ {
// Needs to be quotated
nQuotationMode = QUOTATION_DO;
*target = _T('\"'); *target = _T('\"');
++j; ++j;
} }
for(; i < cpyLength; ++i, ++j) for(; i < cpyLength; ++i, ++j)
{ {
if(source[i] == _T('\\')) if(source[i] == _T('\\'))
bSlash = TRUE; bSlash = TRUE;
else else
// Escape double quote only if quotation mark is not start or end character // Don't escape embracing quotation marks
if((source[i] == _T('\"')) && (!bQoutedTerm || ((i != 0) && (i != (cpyLength - 1)))) ) if((source[i] == _T('\"')) && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))) ) )
{ {
if(!bSlash) if(!bSlash)
{ {
@ -228,7 +240,7 @@ int copyTo(LPTSTR target, LPCTSTR source, int cpyLength, int availSpace)
target[j] = source[i]; target[j] = source[i];
} }
if(!bQoutedTerm) if(nQuotationMode == QUOTATION_DO)
{ {
if(j == availSpace) if(j == availSpace)
return -1; return -1;