Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
nsTextAddress Class Reference

#include <nsTextAddress.h>

Collaboration diagram for nsTextAddress:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsTextAddress ()
virtual ~nsTextAddress ()
nsresult ImportAddresses (PRBool *pAbort, const PRUnichar *pName, nsIFileSpec *pSrc, nsIAddrDatabase *pDb, nsIImportFieldMap *fieldMap, nsString &errors, PRUint32 *pProgress)
nsresult DetermineDelim (nsIFileSpec *pSrc)
char GetDelim (void)

Static Public Member Functions

static nsresult ReadRecordNumber (nsIFileSpec *pSrc, char *pLine, PRInt32 bufferSz, char delim, PRInt32 *pLineLen, PRInt32 rNum)
static nsresult ReadRecord (nsIFileSpec *pSrc, char *pLine, PRInt32 bufferSz, char delim, PRInt32 *pLineLen)
static PRBool GetField (const char *pLine, PRInt32 maxLen, PRInt32 index, nsCString &field, char delim)

Private Member Functions

nsresult ProcessLine (const char *pLine, PRInt32 len, nsString &errors)

Static Private Member Functions

static PRBool IsLineComplete (const char *pLine, PRInt32 len)
static PRInt32 CountFields (const char *pLine, PRInt32 maxLen, char delim)
static void SanitizeSingleLine (nsCString &val)

Private Attributes

char m_delim
PRInt32 m_LFCount
PRInt32 m_CRCount
nsIAddrDatabasem_database
nsIImportFieldMapm_fieldMap
nsCOMPtr< nsIImportServicem_pService

Detailed Description

Definition at line 54 of file nsTextAddress.h.


Constructor & Destructor Documentation

Definition at line 50 of file nsTextAddress.cpp.

Definition at line 58 of file nsTextAddress.cpp.


Member Function Documentation

PRInt32 nsTextAddress::CountFields ( const char *  pLine,
PRInt32  maxLen,
char  delim 
) [static, private]

Definition at line 220 of file nsTextAddress.cpp.

{
    const char *pChar = pLine;
    PRInt32        len = 0;
    PRInt32        count = 0;
    char        tab = 9;

    if (delim == tab)
        tab = 0;

    while (len < maxLen) {
        while (((*pChar == ' ') || (*pChar == tab)) && (len < maxLen)) {
            pChar++;
            len++;
        }
        if ((len < maxLen) && (*pChar == '"')) {
            pChar++;
            len++;
            while ((len < maxLen) && (*pChar != '"')) {
                len++;
                pChar++;
                if (((len + 1) < maxLen) && (*pChar == '"') && (*(pChar + 1) == '"')) {
                    len += 2;
                    pChar += 2;
                }
            }
            if (len < maxLen) {
                pChar++;
                len++;
            }
        }        
        while ((len < maxLen) && (*pChar != delim)) {
            len++;
            pChar++;
        }
        
        count++;
        pChar++;
        len++;
    }

    return( count);
}

Here is the caller graph for this function:

Definition at line 374 of file nsTextAddress.cpp.

{
    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);
}

Here is the call graph for this function:

Here is the caller graph for this function:

char nsTextAddress::GetDelim ( void  ) [inline]

Definition at line 62 of file nsTextAddress.h.

{ return( m_delim);}

Here is the caller graph for this function:

PRBool nsTextAddress::GetField ( const char *  pLine,
PRInt32  maxLen,
PRInt32  index,
nsCString field,
char  delim 
) [static]

Definition at line 264 of file nsTextAddress.cpp.

{
    PRBool result = PR_FALSE;
    const char *pChar = pLine;
    PRInt32        len = 0;
    char        tab = 9;

    field.Truncate();

    if (delim == tab)
        tab = 0;

    while (index && (len < maxLen)) {
        while (((*pChar == ' ') || (*pChar == tab)) && (len < maxLen)) {
            pChar++;
            len++;
        }
        if (len >= maxLen)
            break;
        if (*pChar == '"') {
            len = -1;
            do {
                len++;
                pChar++;
                if (((len + 1) < maxLen) && (*pChar == '"') && (*(pChar + 1) == '"')) {
                    len += 2;
                    pChar += 2;
                }
            } while ((len < maxLen) && (*pChar != '"'));
            if (len < maxLen) {
                pChar++;
                len++;
            }
        }
        if (len >= maxLen)
            break;
        
        while ((len < maxLen) && (*pChar != delim)) {
            len++;
            pChar++;
        }
        
        if (len >= maxLen)
            break;

        index--;
        pChar++;
        len++;
    }

    if (len >= maxLen) {
        return( result);
    }

    result = PR_TRUE;

    while ((len < maxLen) && ((*pChar == ' ') || (*pChar == tab))) {
        len++;
        pChar++;
    }

    const char *pStart = pChar;
    PRInt32        fLen = 0;
    PRBool        quoted = PR_FALSE;
    if (*pChar == '"') {
        pStart++;
        fLen = -1;
        do {
            pChar++;
            len++;
            fLen++;
            if (((len + 1) < maxLen) && (*pChar == '"') && (*(pChar + 1) == '"')) {
                quoted = PR_TRUE;
                len += 2;
                pChar += 2;
                fLen += 2;
            }
        } while ((len < maxLen) && (*pChar != '"'));
    }
    else {
        while ((len < maxLen) && (*pChar != delim)) {
            pChar++;
            len++;
            fLen++;
        }
    }

    if (!fLen) {
        return( result);
    }

    field.Append( pStart, fLen);
    field.Trim( kWhitespace);

    if (quoted) {
        field.ReplaceSubstring( "\"\"", "\"");
    }

    return( result);
}

Here is the caller graph for this function:

nsresult nsTextAddress::ImportAddresses ( PRBool pAbort,
const PRUnichar pName,
nsIFileSpec pSrc,
nsIAddrDatabase pDb,
nsIImportFieldMap fieldMap,
nsString errors,
PRUint32 pProgress 
)

Definition at line 64 of file nsTextAddress.cpp.

{
    // Open the source file for reading, read each line and process it!
    NS_IF_RELEASE( m_database);
    NS_IF_RELEASE( m_fieldMap);
    m_database = pDb;
    m_fieldMap = fieldMap;
    NS_ADDREF( m_fieldMap);
    NS_ADDREF( m_database);

    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);
    }
    
    PRInt32    loc;
    PRInt32    lineLen = 0;
    PRBool     skipRecord = PR_FALSE;

    rv = m_fieldMap->GetSkipFirstRecord(&skipRecord);
    if (NS_FAILED(rv)) {
      IMPORT_LOG0("*** Error checking to see if we should skip the first record\n");
      return rv;
    }

    // Skip the first record if the user has requested it.
    if (skipRecord)
      rv = ReadRecord( pSrc, pLine, kTextAddressBufferSz, m_delim, &lineLen);

    while (!(*pAbort) && !eof && NS_SUCCEEDED( rv)) {
        rv = pSrc->Tell( &loc);
        if (NS_SUCCEEDED( rv) && pProgress)
            *pProgress = (PRUint32)loc;
        rv = ReadRecord( pSrc, pLine, kTextAddressBufferSz, m_delim, &lineLen);
        if (NS_SUCCEEDED( rv)) {
            rv = ProcessLine( pLine, strlen( pLine), errors);
            if (NS_FAILED( rv)) {
                IMPORT_LOG0( "*** Error processing text record.\n");
            }
            else
                rv = pSrc->Eof( &eof);
        }
    }
    
    rv = pSrc->CloseStream();
    
    delete [] pLine;
    
    if (!eof) {
        IMPORT_LOG0( "*** Error reading the address book, didn't reach the end\n");
        return( NS_ERROR_FAILURE);
    }
    
    rv = pDb->Commit(nsAddrDBCommitType::kLargeCommit);
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsTextAddress::IsLineComplete ( const char *  pLine,
PRInt32  len 
) [static, private]

Definition at line 205 of file nsTextAddress.cpp.

{
    PRBool    quoted = PR_FALSE;

    while (len) {
      if ((*pLine == '"')) {
        quoted = !quoted;
      }
      pLine++;
      len--;
    }

    return !quoted;
}

Here is the caller graph for this function:

nsresult nsTextAddress::ProcessLine ( const char *  pLine,
PRInt32  len,
nsString errors 
) [private]

Definition at line 434 of file nsTextAddress.cpp.

{
    if (!m_fieldMap) {
        IMPORT_LOG0( "*** Error, text import needs a field map\n");
        return( NS_ERROR_FAILURE);
    }

    nsresult rv;
    
    // Wait until we get our first non-empty field, then create a new row,
    // fill in the data, then add the row to the database.
        

    nsIMdbRow *    newRow = nsnull;
    nsString    uVal;
    nsCString    fieldVal;
    PRInt32        fieldNum;
    PRInt32        numFields = 0;
    PRBool        active;
    rv = m_fieldMap->GetMapSize( &numFields);
    for (PRInt32 i = 0; (i < numFields) && NS_SUCCEEDED( rv); i++) {
        active = PR_FALSE;
        rv = m_fieldMap->GetFieldMap( i, &fieldNum);
        if (NS_SUCCEEDED( rv))
            rv = m_fieldMap->GetFieldActive( i, &active);
        if (NS_SUCCEEDED( rv) && active) {
            if (GetField( pLine, len, i, fieldVal, m_delim)) {
                if (!fieldVal.IsEmpty()) {
                    if (!newRow) {
                        rv = m_database->GetNewRow( &newRow);
                        if (NS_FAILED( rv)) {
                            IMPORT_LOG0( "*** Error getting new address database row\n");
                        }
                    }
                    if (newRow) {
                        NS_CopyNativeToUnicode( fieldVal, uVal);
                        rv = m_fieldMap->SetFieldValue( m_database, newRow, fieldNum, uVal.get());
                    }
                }
            }
            else
                break;
            
        }
        else {
            if (active) {
                IMPORT_LOG1( "*** Error getting field map for index %ld\n", (long) i);
            }
        }

    }
    
    if (NS_SUCCEEDED( rv)) {
        if (newRow) {
            rv = m_database->AddCardRowToDB( newRow);
            // Release newRow????
        }
    }
    else {
        // Release newRow??
    }

    return( rv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsTextAddress::ReadRecord ( nsIFileSpec pSrc,
char *  pLine,
PRInt32  bufferSz,
char  delim,
PRInt32 pLineLen 
) [static]

Definition at line 133 of file nsTextAddress.cpp.

{
    PRBool        wasTruncated;
    PRBool        isEof;
    char *        pRead;
    PRInt32        lineLen = 0;
    nsresult    rv;
    do {
        if (lineLen) {
            if ((lineLen + 2) < bufferSz) {
                memcpy( pLine + lineLen, "\x0D\x0A", 2);
                lineLen += 2;
                pLine[lineLen] = 0;
            }
        }
        wasTruncated = PR_FALSE;
        pRead = pLine;
        pRead += lineLen;
        pSrc->Eof(&isEof);
        if (isEof)
          // If we get an EOF here, then the line isn't complete
          // so we must have an incorrect file.
          rv = NS_ERROR_FAILURE;
        else
        {
          rv = pSrc->ReadLine( &pRead, bufferSz - lineLen, &wasTruncated);
          if (wasTruncated) {
            pLine[bufferSz - 1] = 0;
            IMPORT_LOG0( "Unable to read line from file, buffer too small\n");
            rv = NS_ERROR_FAILURE;
          }
          else if (NS_SUCCEEDED( rv)) {
            lineLen = strlen( pLine);
          }
        }
    } while (NS_SUCCEEDED( rv) && !IsLineComplete( pLine, lineLen));

    *pLineLen = lineLen;
    return( rv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsTextAddress::ReadRecordNumber ( nsIFileSpec pSrc,
char *  pLine,
PRInt32  bufferSz,
char  delim,
PRInt32 pLineLen,
PRInt32  rNum 
) [static]

Definition at line 175 of file nsTextAddress.cpp.

{
    PRInt32    rIndex = 0;
    nsresult rv = pSrc->Seek( 0);
    if (NS_FAILED( rv))
        return( rv);
    
    PRBool    eof = PR_FALSE;

    while (!eof && (rIndex <= rNum)) {
        if (NS_FAILED( rv = ReadRecord( pSrc, pLine, bufferSz, delim, pLineLen)))
            return( rv);
        if (rIndex == rNum)
            return( NS_OK);
        rIndex++;
        rv = pSrc->Eof( &eof);
        if (NS_FAILED( rv))
            return( rv);
    }

    return( NS_ERROR_FAILURE);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsTextAddress::SanitizeSingleLine ( nsCString val) [static, private]

Definition at line 366 of file nsTextAddress.cpp.

{
    val.ReplaceSubstring( "\x0D\x0A", ", ");
    val.ReplaceChar( 13, ' ');
    val.ReplaceChar( 10, ' ');
}

Member Data Documentation

Definition at line 78 of file nsTextAddress.h.

Definition at line 79 of file nsTextAddress.h.

char nsTextAddress::m_delim [private]

Definition at line 76 of file nsTextAddress.h.

Definition at line 80 of file nsTextAddress.h.

Definition at line 77 of file nsTextAddress.h.

Definition at line 81 of file nsTextAddress.h.


The documentation for this class was generated from the following files: