Back to index

lightning-sunbird  0.9+nobinonly
nsImportScanFile.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nscore.h"
00039 #include "nsIFileSpec.h"
00040 #include "nsCRT.h"
00041 #include "nsImportScanFile.h"
00042 #include "ImportCharSet.h"
00043 
00044 nsImportScanFile::nsImportScanFile()
00045 {
00046        m_allocated = PR_FALSE;
00047        m_eof = PR_FALSE;
00048        m_pFile = nsnull;
00049        m_pBuf = nsnull;
00050 }
00051 
00052 nsImportScanFile::nsImportScanFile( nsIFileSpec *pSpec, PRUint8 * pBuf, PRUint32 sz)
00053 {
00054        m_allocated = PR_FALSE;
00055        m_eof = PR_FALSE;
00056        InitScan( pSpec, pBuf, sz);
00057 }
00058 
00059 nsImportScanFile::~nsImportScanFile()
00060 {
00061        if (m_allocated) {
00062               CleanUpScan();
00063        }
00064        
00065        NS_IF_RELEASE( m_pFile);
00066 }
00067 
00068 PRBool nsImportScanFile::OpenScan( nsIFileSpec *pSpec, PRUint32 bufSz)
00069 {
00070        if (!bufSz)
00071               bufSz = 32 * 1024;
00072        if (!m_pBuf) {
00073               m_pBuf = new PRUint8[bufSz];
00074        }
00075        
00076        PRBool open = PR_FALSE;
00077        nsresult rv = pSpec->IsStreamOpen( &open);
00078        if (NS_FAILED( rv) || !open) {
00079               rv = pSpec->OpenStreamForReading();
00080               if (NS_FAILED( rv)) {
00081                      delete [] m_pBuf;
00082                      m_pBuf = nsnull;
00083                      return( PR_FALSE);
00084               }
00085        }
00086        m_pFile = pSpec;
00087        NS_IF_ADDREF( m_pFile);
00088        m_allocated = PR_TRUE;
00089        m_bytesInBuf = 0;
00090        m_pos = 0;
00091        m_bufSz = bufSz;
00092        return( PR_TRUE);
00093 }
00094 
00095 
00096 void nsImportScanFile::InitScan( nsIFileSpec *pSpec, PRUint8 * pBuf, PRUint32 sz)
00097 {
00098        m_pFile = pSpec;
00099        NS_IF_ADDREF( pSpec);
00100        m_pBuf = pBuf;
00101        m_bufSz = sz;
00102        m_bytesInBuf = 0;
00103        m_pos = 0;
00104 }
00105 
00106 void nsImportScanFile::CleanUpScan( void)
00107 {
00108        NS_IF_RELEASE( m_pFile);
00109        m_pFile = nsnull;
00110        if (m_allocated) {
00111               if (m_pBuf)
00112                      delete [] m_pBuf;
00113               m_pBuf = NULL;
00114        }
00115 }
00116 
00117 void nsImportScanFile::ShiftBuffer( void)
00118 {
00119        PRUint8 *     pTop;
00120        PRUint8 *     pCurrent;
00121 
00122        if (m_pos < m_bytesInBuf) {
00123               pTop = m_pBuf;
00124               pCurrent = pTop + m_pos;
00125               PRUint32             cnt = m_bytesInBuf - m_pos;
00126               while (cnt) {
00127                      *pTop = *pCurrent;
00128                      pTop++; pCurrent++;
00129                      cnt--;
00130               }
00131        }
00132 
00133        m_bytesInBuf -= m_pos;
00134        m_pos = 0;
00135 }
00136 
00137 PRBool nsImportScanFile::FillBufferFromFile( void)
00138 {
00139        PRBool eof = PR_FALSE;
00140        nsresult rv = m_pFile->Eof( &eof);
00141        if (eof) {
00142               return( PR_FALSE);
00143        }
00144 
00145        // Fill up a buffer and scan it
00146        ShiftBuffer();
00147 
00148        // Read in some more bytes
00149        PRUint32      cnt = m_bufSz - m_bytesInBuf;
00150        // To distinguish from disk errors
00151        // Check first for end of file?
00152        // Set a done flag if true...
00153        PRInt32 read;
00154        char *pBuf = (char *)m_pBuf;
00155        pBuf += m_bytesInBuf;
00156        rv = m_pFile->Read( &pBuf, (PRInt32) cnt, &read);
00157 
00158        if (NS_FAILED( rv))
00159               return( PR_FALSE);
00160        eof = PR_FALSE;
00161        rv = m_pFile->Eof( &eof);
00162        if (eof)
00163               m_eof = PR_TRUE;
00164 
00165        m_bytesInBuf += cnt;
00166        return( PR_TRUE);
00167 }
00168 
00169 PRBool nsImportScanFile::Scan( PRBool *pDone)
00170 {
00171        PRBool eof = PR_FALSE;
00172        m_pFile->Eof( &eof);
00173 
00174        if (eof) {
00175               if (m_pos < m_bytesInBuf) {
00176                      ScanBuffer( pDone);
00177               }
00178               *pDone = PR_TRUE;
00179               return( PR_TRUE);
00180        }
00181 
00182        // Fill up a buffer and scan it
00183        if (!FillBufferFromFile())
00184               return( PR_FALSE);
00185 
00186        return( ScanBuffer( pDone));
00187 }
00188 
00189 PRBool nsImportScanFile::ScanBuffer( PRBool *)
00190 {
00191        return( PR_TRUE);
00192 }
00193 
00194 
00195 PRBool nsImportScanFileLines::ScanBuffer( PRBool *pDone)
00196 {
00197        // m_pos, m_bytesInBuf, m_eof, m_pBuf are relevant
00198 
00199        PRUint32             pos = m_pos;
00200        PRUint32             max = m_bytesInBuf;
00201        PRUint8 *            pChar = m_pBuf + pos;
00202        PRUint32             startPos;
00203        
00204        while (pos < max) {
00205               if (m_needEol) {
00206                      // Find the next eol...
00207                      while ((pos < max) && (*pChar != ImportCharSet::cCRChar) && (*pChar != ImportCharSet::cLinefeedChar)) {
00208                             pos++;
00209                             pChar++;
00210                      }
00211                      m_pos = pos;
00212                      if (pos < max)
00213                             m_needEol = PR_FALSE;
00214                      if (pos == max) // need more buffer for an end of line
00215                             break;
00216               }
00217               // Skip past any eol characters
00218               while ((pos < max) && ((*pChar == ImportCharSet::cCRChar) || (*pChar == ImportCharSet::cLinefeedChar))) {
00219                      pos++;
00220                      pChar++;
00221               }
00222               m_pos = pos;
00223               if (pos == max)
00224                      break;
00225               // Make sure we can find either the eof or the 
00226               // next end of line
00227               startPos = pos;
00228               while ((pos < max) && (*pChar != ImportCharSet::cCRChar) && (*pChar != ImportCharSet::cLinefeedChar)) {
00229                      pos++;
00230                      pChar++;
00231               }
00232               
00233               // Is line too big for our buffer?
00234               if ((pos == max) && !m_eof) {
00235                      if (!m_pos) { // line too big for our buffer
00236                             m_pos = pos;
00237                             m_needEol = PR_TRUE;
00238                      }
00239                      break;
00240               }
00241               
00242               if (!ProcessLine( m_pBuf + startPos, pos - startPos, pDone)) {
00243                      return( PR_FALSE);
00244               }
00245               m_pos = pos;
00246        }
00247 
00248        return( PR_TRUE);
00249 }
00250