Back to index

lightning-sunbird  0.9+nobinonly
nsMsgLineBuffer.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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) 1999
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 _nsMsgLineBuffer_H
00038 #define _nsMsgLineBuffer_H
00039 
00040 #include "msgCore.h"    // precompiled header...
00041 
00042 // I can't believe I have to have this stupid class, but I can't find
00043 // anything suitable (nsStrImpl might be, when its done). nsIByteBuffer
00044 // would do, if I had a stream for input, which I don't.
00045 
00046 class NS_MSG_BASE nsByteArray 
00047 {
00048 public:
00049   nsByteArray();
00050   virtual ~nsByteArray();
00051   PRUint32    GetSize() {return m_bufferSize;}
00052   PRUint32    GetBufferPos() {return m_bufferPos;}
00053   nsresult    GrowBuffer(PRUint32 desired_size, PRUint32 quantum = 1024);
00054   nsresult    AppendString(const char *string);
00055   nsresult    AppendBuffer(const char *buffer, PRUint32 length);
00056   void        ResetWritePos() {m_bufferPos = 0;}
00057   char        *GetBuffer() {return m_buffer;}
00058 protected:
00059   char        *m_buffer;
00060   PRUint32    m_bufferSize;
00061   PRUint32    m_bufferPos;  // write Pos in m_buffer - where the next byte should go.
00062 };
00063 
00064 
00065 class NS_MSG_BASE nsMsgLineBufferHandler : public nsByteArray
00066 {
00067 public:
00068   virtual PRInt32 HandleLine(char *line, PRUint32 line_length) = 0;
00069 };
00070 
00071 class NS_MSG_BASE nsMsgLineBuffer : public nsMsgLineBufferHandler
00072 {
00073 public:
00074   nsMsgLineBuffer(nsMsgLineBufferHandler *handler, PRBool convertNewlinesP);
00075   
00076   virtual    ~nsMsgLineBuffer();
00077   PRInt32     BufferInput(const char *net_buffer, PRInt32 net_buffer_size);
00078   // Not sure why anyone cares, by NNTPHost seems to want to know the buf pos.
00079   PRUint32    GetBufferPos() {return m_bufferPos;}
00080   
00081   virtual PRInt32 HandleLine(char *line, PRUint32 line_length);
00082   // flush last line, though it won't be CRLF terminated.
00083   virtual PRInt32 FlushLastLine();
00084 protected:
00085   nsMsgLineBuffer(PRBool convertNewlinesP);
00086   
00087   PRInt32 ConvertAndSendBuffer();
00088   void SetLookingForCRLF(PRBool b);
00089   
00090   nsMsgLineBufferHandler *m_handler;
00091   PRBool      m_convertNewlinesP;
00092   PRBool      m_lookingForCRLF; 
00093 };
00094 
00095 // I'm adding this utility class here for lack of a better place. This utility class is similar to nsMsgLineBuffer
00096 // except it works from an input stream. It is geared towards efficiently parsing new lines out of a stream by storing
00097 // read but unprocessed bytes in a buffer. I envision the primary use of this to be our mail protocols such as imap, news and
00098 // pop which need to process line by line data being returned in the form of a proxied stream from the server.
00099 
00100 class nsIInputStream;
00101 
00102 class NS_MSG_BASE nsMsgLineStreamBuffer
00103 {
00104 public:
00105   // aBufferSize -- size of the buffer you want us to use for buffering stream data
00106   // aEndOfLinetoken -- The delimiter string to be used for determining the end of line. This 
00107   //                              allows us to parse platform specific end of line endings by making it
00108   //                                 a parameter.
00109   // aAllocateNewLines -- PR_TRUE if you want calls to ReadNextLine to allocate new memory for the line. 
00110   //                                      if false, the char * returned is just a ptr into the buffer. Subsequent calls to
00111   //                                      ReadNextLine will alter the data so your ptr only has a life time of a per call.
00112   // aEatCRLFs  -- PR_TRUE if you don't want to see the CRLFs on the lines returned by ReadNextLine. 
00113   //                         PR_FALSE if you do want to see them.
00114   // aLineToken -- Specify the line token to look for, by default is LF ('\n') which cover as well CRLF. If
00115   //            lines are terminated with a CR only, you need to set aLineToken to CR ('\r')
00116   nsMsgLineStreamBuffer(PRUint32 aBufferSize, PRBool aAllocateNewLines, 
00117                         PRBool aEatCRLFs = PR_TRUE, char aLineToken = '\n'); // specify the size of the buffer you want the class to use....
00118   virtual ~nsMsgLineStreamBuffer();
00119   
00120   // Caller must free the line returned using PR_Free
00121   // aEndOfLinetoken -- delimiter used to denote the end of a line.
00122   // aNumBytesInLine -- The number of bytes in the line returned
00123   // aPauseForMoreData -- There is not enough data in the stream to make a line at this time...
00124   char * ReadNextLine(nsIInputStream * aInputStream, PRUint32 &anumBytesInLine, PRBool &aPauseForMoreData, nsresult *rv = nsnull, PRBool addLineTerminator = PR_FALSE);
00125   nsresult GrowBuffer(PRInt32 desiredSize);
00126   void ClearBuffer();
00127   PRBool NextLineAvailable();
00128 protected:
00129   PRBool m_eatCRLFs;
00130   PRBool m_allocateNewLines;
00131   char * m_dataBuffer;
00132   PRUint32 m_dataBufferSize;
00133   PRUint32 m_startPos;
00134   PRUint32 m_numBytesInBuffer;
00135   char m_lineToken;
00136 };
00137 
00138 
00139 #endif