Fix for bug 191052. Cleaned up IsLDIFFile() and made it so that if the average leagl ldif field is 3 or more then it's a valid ldif file. r=nhotta, sr=sspitzer,a=asa.

git-svn-id: svn://10.0.0.236/trunk@137314 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
cavin%netscape.com 2003-02-03 07:16:02 +00:00
parent c9ccb23d24
commit 7d227f00cd

View File

@ -549,6 +549,8 @@ static const char *const sLDIFFields[] = {
};
#define kMaxLDIFLen 14
// Count total number of legal ldif fields and records in the first 100 lines of the
// file and if the average legal ldif field is 3 or higher than it's a valid ldif file.
nsresult nsTextAddress::IsLDIFFile( nsIFileSpec *pSrc, PRBool *pIsLDIF)
{
*pIsLDIF = PR_FALSE;
@ -571,15 +573,13 @@ nsresult nsTextAddress::IsLDIFFile( nsIFileSpec *pSrc, PRBool *pIsLDIF)
PRBool wasTruncated = PR_FALSE;
PRInt32 lineLen = 0;
PRInt32 lineCount = 0;
PRInt32 ldifFields = 0;
PRInt32 ldifFields = 0; // total number of legal ldif fields.
char field[kMaxLDIFLen];
PRInt32 fLen = 0;
char * pChar;
PRInt32 rCount = 1;
PRInt32 recCount = 0; // total number of records.
PRInt32 i;
PRBool gotLDIF = PR_FALSE;
PRInt32 commaCount = 0;
PRInt32 tabCount = 0;
while (!eof && NS_SUCCEEDED( rv) && (lineCount < 100)) {
wasTruncated = PR_FALSE;
@ -588,24 +588,12 @@ nsresult nsTextAddress::IsLDIFFile( nsIFileSpec *pSrc, PRBool *pIsLDIF)
pLine[kTextAddressBufferSz - 1] = 0;
if (NS_SUCCEEDED( rv)) {
lineLen = strlen( pLine);
tabCount += CountFields( pLine, lineLen, 9);
commaCount += CountFields( pLine, lineLen, ',');
pChar = pLine;
if (!lineLen && gotLDIF) {
rCount++;
recCount++;
gotLDIF = PR_FALSE;
}
/*
There should be no leading whitespace in front
of an ldif field!
*//*
while (lineLen && ((*pChar == ' ') || (*pChar == 9))) {
lineLen--;
pChar++;
}
*/
if (lineLen && (*pChar != ' ') && (*pChar != 9)) {
fLen = 0;
while (lineLen && (fLen < (kMaxLDIFLen - 1)) && (*pChar != ':')) {
@ -617,10 +605,10 @@ nsresult nsTextAddress::IsLDIFFile( nsIFileSpec *pSrc, PRBool *pIsLDIF)
field[fLen] = 0;
if (lineLen && (*pChar == ':') && (fLen < (kMaxLDIFLen - 1))) {
// see if this is an ldif field?
// see if this is an ldif field (case insensitive)?
i = 0;
while (sLDIFFields[i]) {
if (!nsCRT::strcmp( sLDIFFields[i], field)) {
if (!nsCRT::strcasecmp( sLDIFFields[i], field)) {
ldifFields++;
gotLDIF = PR_TRUE;
break;
@ -635,28 +623,21 @@ nsresult nsTextAddress::IsLDIFFile( nsIFileSpec *pSrc, PRBool *pIsLDIF)
}
lineCount++;
}
// If we just saw ldif address, increment recCount.
if (gotLDIF)
recCount++;
rv = pSrc->CloseStream();
delete [] pLine;
ldifFields /= rCount;
tabCount /= lineCount;
commaCount /= lineCount;
if (recCount > 1)
ldifFields /= recCount;
/*
if ((tabCount <= ldifFields) && (commaCount <= ldifFields) && (ldifFields > 1)) {
*pIsLDIF = PR_TRUE;
}
*/
if (rCount == 1) {
if ((ldifFields >= 3) && (lineCount < 500))
*pIsLDIF = PR_TRUE;
}
else {
if (ldifFields >= 3)
*pIsLDIF = PR_TRUE;
}
// If the average field number >= 3 then it's a good ldif file.
if (ldifFields >= 3)
*pIsLDIF = PR_TRUE;
return( NS_OK);
}