nsresult nsTextAddress::DetermineDelim(nsIFile *aSrc) { nsCOMPtr<nsIInputStream> inputStream; nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), aSrc); if (NS_FAILED(rv)) { IMPORT_LOG0( "*** Error opening address file for reading\n"); return rv; } char *pLine = new char[kTextAddressBufferSz]; if (!pLine) return NS_ERROR_OUT_OF_MEMORY; PRUint32 bytesLeft = 0; rv = inputStream->Available(&bytesLeft); if (NS_FAILED(rv)) { IMPORT_LOG0( "*** Error checking address file for eof\n"); inputStream->Close(); delete [] pLine; return rv; } PRUint32 left; PRInt32 lineLen = 0; PRInt32 lineCount = 0; PRInt32 tabCount = 0; PRInt32 commaCount = 0; PRInt32 tabLines = 0; PRInt32 commaLines = 0; while (bytesLeft && NS_SUCCEEDED(rv) && (lineCount < 100)) { left = 0; rv = inputStream->Read(pLine, kTextAddressBufferSz, &left); if (left) pLine[kTextAddressBufferSz - 1] = 0; if (NS_SUCCEEDED(rv)) { lineLen = strlen(pLine); tabCount = CountFields(pLine, lineLen, 9); commaCount = CountFields(pLine, lineLen, ','); if (tabCount > commaCount) tabLines++; else if (commaCount) commaLines++; rv = inputStream->Available(&bytesLeft); } lineCount++; } rv = inputStream->Close(); delete [] pLine; if (tabLines > commaLines) m_delim = 9; else m_delim = ','; return rv; }
nsresult nsTextAddress::DetermineDelim( nsIFileSpec *pSrc) { nsresult rv = pSrc->OpenStreamForReading(); if (NS_FAILED( rv)) { IMPORT_LOG0( "*** Error opening address file for reading\n"); return( rv); } char *pLine = new char[kTextAddressBufferSz]; PRBool eof = PR_FALSE; rv = pSrc->Eof( &eof); if (NS_FAILED( rv)) { IMPORT_LOG0( "*** Error checking address file for eof\n"); pSrc->CloseStream(); return( rv); } PRBool wasTruncated = PR_FALSE; PRInt32 lineLen = 0; PRInt32 lineCount = 0; PRInt32 tabCount = 0; PRInt32 commaCount = 0; PRInt32 tabLines = 0; PRInt32 commaLines = 0; while (!eof && NS_SUCCEEDED( rv) && (lineCount < 100)) { wasTruncated = PR_FALSE; rv = pSrc->ReadLine( &pLine, kTextAddressBufferSz, &wasTruncated); if (wasTruncated) pLine[kTextAddressBufferSz - 1] = 0; if (NS_SUCCEEDED( rv)) { lineLen = strlen( pLine); tabCount = CountFields( pLine, lineLen, 9); commaCount = CountFields( pLine, lineLen, ','); if (tabCount > commaCount) tabLines++; else if (commaCount) commaLines++; rv = pSrc->Eof( &eof); } lineCount++; } rv = pSrc->CloseStream(); delete [] pLine; if (tabLines > commaLines) m_delim = 9; else m_delim = ','; return( NS_OK); }
nsresult nsTextAddress::DetermineDelim(nsIFile *aSrc) { nsCOMPtr<nsIInputStream> inputStream; nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), aSrc); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Error opening address file for reading\n"); return rv; } int32_t lineCount = 0; int32_t tabCount = 0; int32_t commaCount = 0; int32_t tabLines = 0; int32_t commaLines = 0; nsAutoString line; bool more = true; nsCOMPtr<nsIUnicharLineInputStream> lineStream; rv = GetUnicharLineStreamForFile(aSrc, inputStream, getter_AddRefs(lineStream)); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Error opening converter stream for importer\n"); return rv; } while (more && NS_SUCCEEDED(rv) && (lineCount < 100)) { rv = lineStream->ReadLine(line, &more); if (NS_SUCCEEDED(rv)) { tabCount = CountFields(line, char16_t('\t')); commaCount = CountFields(line, char16_t(',')); if (tabCount > commaCount) tabLines++; else if (commaCount) commaLines++; } lineCount++; } rv = inputStream->Close(); if (tabLines > commaLines) m_delim = char16_t('\t'); else m_delim = char16_t(','); IMPORT_LOG2("Tab count = %d, Comma count = %d\n", tabLines, commaLines); return rv; }
int32 BContact::CountFields(field_type type) const { if (fInitCheck != B_OK) return 0; int32 count = CountFields(); int32 fieldsCount = 0; for (int i = 0; i < count; i++) { printf("%d\n", i); BContactField* ret = fList.ItemAt(count); if (ret != NULL && ret->FieldType() == type) { printf("++\n"); fieldsCount++; } } return fieldsCount; }
// TODO improve performances sorting // fList's objects by type BContactField* BContact::FieldByType(field_type type, int32 index) { if (fInitCheck != B_OK) return NULL; int32 count = CountFields(); int32 fieldsCount = 0; for (int i = 0; i < count; i++) { BContactField* ret = fList.ItemAt(count); if (ret->TypeCode() == type) { fieldsCount++; if (fieldsCount == index) return ret; } } return NULL; }