Back to index

lightning-sunbird  0.9+nobinonly
ImportOutFile.h
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 #ifndef ImportOutFile_h___
00038 #define ImportOutFile_h___
00039 
00040 #include "nsImportTranslator.h"
00041 #include "nsIFileSpec.h"
00042 
00043 #define kMaxMarkers         10
00044 
00045 class ImportOutFile;
00046 
00047 class ImportOutFile {
00048 public:
00049        ImportOutFile();
00050        ImportOutFile( nsIFileSpec *pFile, PRUint8 * pBuf, PRUint32 sz);
00051        ~ImportOutFile();
00052 
00053        PRBool InitOutFile( nsIFileSpec *pFile, PRUint32 bufSz = 4096);
00054        void   InitOutFile( nsIFileSpec *pFile, PRUint8 * pBuf, PRUint32 sz);
00055        inline PRBool WriteData( const PRUint8 * pSrc, PRUint32 len);
00056        inline PRBool WriteByte( PRUint8 byte);
00057        PRBool WriteStr( const char *pStr) {return( WriteU8NullTerm( (const PRUint8 *) pStr, PR_FALSE)); }
00058        PRBool WriteU8NullTerm( const PRUint8 * pSrc, PRBool includeNull);
00059        PRBool WriteEol( void) { return( WriteStr( "\x0D\x0A")); }
00060        PRBool Done( void) {return( Flush());}
00061 
00062        // Marker support
00063        PRBool SetMarker( int markerID);
00064        void   ClearMarker( int markerID);
00065        PRBool WriteStrAtMarker( int markerID, const char *pStr);
00066 
00067        // 8-bit to 7-bit translation
00068        PRBool Set8bitTranslator( nsImportTranslator *pTrans);
00069        PRBool End8bitTranslation( PRBool *pEngaged, nsCString& useCharset, nsCString& encoding);
00070 
00071 protected:
00072        PRBool Flush( void);
00073 
00074 protected:
00075        nsIFileSpec * m_pFile;
00076        PRUint8 *            m_pBuf;
00077        PRUint32             m_bufSz;
00078        PRUint32             m_pos;
00079        PRBool               m_ownsFileAndBuffer;
00080 
00081        // markers
00082        PRUint32             m_markers[kMaxMarkers];
00083 
00084        // 8 bit to 7 bit translations
00085        nsImportTranslator   *      m_pTrans;
00086        PRBool                             m_engaged;
00087        PRBool                             m_supports8to7;
00088        ImportOutFile *                    m_pTransOut;
00089        PRUint8 *                          m_pTransBuf;
00090 };
00091 
00092 inline PRBool ImportOutFile::WriteData( const PRUint8 * pSrc, PRUint32 len) {
00093        while ((len + m_pos) > m_bufSz) {
00094               if ((m_bufSz - m_pos)) {
00095                      memcpy( m_pBuf + m_pos, pSrc, m_bufSz - m_pos);
00096                      len -= (m_bufSz - m_pos);
00097                      pSrc += (m_bufSz - m_pos);
00098                      m_pos = m_bufSz;
00099               }
00100               if (!Flush())
00101                      return( PR_FALSE);
00102        }
00103        
00104        if (len) {
00105               memcpy( m_pBuf + m_pos, pSrc, len);
00106               m_pos += len;
00107        }
00108 
00109        return( PR_TRUE);
00110 }
00111 
00112 inline PRBool ImportOutFile::WriteByte( PRUint8 byte) {
00113        if (m_pos == m_bufSz) {
00114               if (!Flush())
00115                      return( PR_FALSE);
00116        }
00117        *(m_pBuf + m_pos) = byte;
00118        m_pos++;
00119        return( PR_TRUE);
00120 }
00121 
00122 #endif /* ImportOutFile_h__ */
00123 
00124