summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/nsMsgLineBuffer.h
blob: 2ac579be3bb4727c7f98d0e39760a4d342867227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef _nsMsgLineBuffer_H
#define _nsMsgLineBuffer_H

#include "msgCore.h"  // precompiled header...

// I can't believe I have to have this stupid class, but I can't find
// anything suitable (nsStrImpl might be, when it's done). nsIByteBuffer
// would do, if I had a stream for input, which I don't.

class nsByteArray {
 public:
  nsByteArray();
  virtual ~nsByteArray();
  uint32_t GetSize() { return m_bufferSize; }
  uint32_t GetBufferPos() { return m_bufferPos; }
  nsresult GrowBuffer(uint64_t desired_size, uint32_t quantum = 1024);
  nsresult AppendString(const char* string);
  nsresult AppendBuffer(const char* buffer, uint32_t length);
  void ResetWritePos() { m_bufferPos = 0; }
  char* GetBuffer() { return m_buffer; }

 protected:
  char* m_buffer;
  uint32_t m_bufferSize;
  uint32_t
      m_bufferPos;  // write Pos in m_buffer - where the next byte should go.
};

/**
 * nsMsgLineBuffer breaks up incoming data into lines.
 * It accepts CRLF, CR or LF line endings.
 *
 * Data is fed in via BufferInput(). The virtual HandleLine() will be
 * invoked for each line. The data passed to HandleLine() is verbatim,
 * and will include whatever line endings were in the source data.
 *
 * Flush() should be called when the data is exhausted, to handle any
 * leftover bytes in the buffer (e.g. if the data doesn't end with an EOL).
 */
class nsMsgLineBuffer : private nsByteArray {
 public:
  nsMsgLineBuffer();
  virtual ~nsMsgLineBuffer();
  nsresult BufferInput(const char* net_buffer, int32_t net_buffer_size);

  /**
   * HandleLine should be implemented by derived classes, to handle a line.
   * The line will have whatever end-of-line characters were present in the
   * source data (potentially none, if the data ends mid-line).
   */
  virtual nsresult HandleLine(const char* line, uint32_t line_length) = 0;

  /**
   * Flush processes any unprocessed data currently in the buffer. Should
   * be called when the source data is exhausted.
   */
  nsresult Flush();
};

// I'm adding this utility class here for lack of a better place. This utility
// class is similar to nsMsgLineBuffer except it works from an input stream. It
// is geared towards efficiently parsing new lines out of a stream by storing
// read but unprocessed bytes in a buffer. I envision the primary use of this to
// be our mail protocols such as imap, news and pop which need to process line
// by line data being returned in the form of a proxied stream from the server.

class nsIInputStream;

class nsMsgLineStreamBuffer {
 public:
  NS_INLINE_DECL_REFCOUNTING(nsMsgLineStreamBuffer)

  // aBufferSize -- size of the buffer you want us to use for buffering stream
  //                data
  // aEndOfLinetoken -- The delimiter string to be used for determining the end
  //                of line. This allows us to parse platform specific end of
  //                line endings by making it a parameter.
  // aAllocateNewLines -- true if you want calls to ReadNextLine to allocate new
  //                memory for the line.
  //                if false, the char * returned is just a ptr into the buffer.
  //                Subsequent calls to ReadNextLine will alter the data so your
  //                ptr only has a life time of a per call.
  // aEatCRLFs --   true if you don't want to see the CRLFs on the lines
  //                returned by ReadNextLine.
  //                false if you do want to see them.
  // aLineToken --  Specify the line token to look for, by default is LF ('\n')
  //                which cover as well CRLF. If lines are terminated with a CR
  //                only, you need to set aLineToken to CR ('\r')
  nsMsgLineStreamBuffer(
      uint32_t aBufferSize, bool aAllocateNewLines, bool aEatCRLFs = true,
      char aLineToken = '\n');  // specify the size of the buffer you want the
                                // class to use....

  // Caller must free the line returned using PR_Free
  // aEndOfLinetoken   -- delimiter used to denote the end of a line.
  // aNumBytesInLine   -- The number of bytes in the line returned
  // aPauseForMoreData -- There is not enough data in the stream to make a line
  //                      at this time...
  char* ReadNextLine(nsIInputStream* aInputStream, uint32_t& aNumBytesInLine,
                     bool& aPauseForMoreData, nsresult* rv = nullptr,
                     bool addLineTerminator = false);
  nsresult GrowBuffer(uint32_t desiredSize);
  void ClearBuffer();
  bool NextLineAvailable();

 private:
  virtual ~nsMsgLineStreamBuffer();

 protected:
  bool m_eatCRLFs;
  bool m_allocateNewLines;
  char* m_dataBuffer;
  uint32_t m_dataBufferSize;
  uint32_t m_startPos;
  uint32_t m_numBytesInBuffer;
  char m_lineToken;
};

#endif