summaryrefslogtreecommitdiffstats
path: root/mailnews/import/src/nsImportScanFile.cpp
blob: c4eefef3b27734271d500a3061f7b1684744947d (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */

#include "nscore.h"
#include "nsIFile.h"
#include "nsImportScanFile.h"
#include "ImportCharSet.h"

nsImportScanFile::nsImportScanFile()
{
  m_allocated = false;
  m_eof = false;
  m_pBuf = nullptr;
}

nsImportScanFile::~nsImportScanFile()
{
  if (m_allocated)
    CleanUpScan();
}

void nsImportScanFile::InitScan(nsIInputStream *pInputStream, uint8_t * pBuf, uint32_t sz)
{
  m_pInputStream = pInputStream;
  m_pBuf = pBuf;
  m_bufSz = sz;
  m_bytesInBuf = 0;
  m_pos = 0;
}

void nsImportScanFile::CleanUpScan(void)
{
  m_pInputStream = nullptr;
  if (m_allocated) {
    delete [] m_pBuf;
    m_pBuf = NULL;
  }
}

void nsImportScanFile::ShiftBuffer(void)
{
  uint8_t *  pTop;
  uint8_t *  pCurrent;

  if (m_pos < m_bytesInBuf) {
    pTop = m_pBuf;
    pCurrent = pTop + m_pos;
    uint32_t    cnt = m_bytesInBuf - m_pos;
    while (cnt) {
      *pTop = *pCurrent;
      pTop++; pCurrent++;
      cnt--;
    }
  }

  m_bytesInBuf -= m_pos;
  m_pos = 0;
}

bool nsImportScanFile::FillBufferFromFile(void)
{
  uint64_t available;
  nsresult rv = m_pInputStream->Available(&available);
  if (NS_FAILED(rv))
    return false;

  // Fill up a buffer and scan it
  ShiftBuffer();

  // Read in some more bytes
  uint32_t  cnt = m_bufSz - m_bytesInBuf;
  // To distinguish from disk errors
  // Check first for end of file?
  // Set a done flag if true...
  uint32_t read;
  char *pBuf = (char *)m_pBuf;
  pBuf += m_bytesInBuf;
  rv = m_pInputStream->Read(pBuf, (int32_t) cnt, &read);

  if (NS_FAILED(rv))
    return false;
  rv = m_pInputStream->Available(&available);
  if (NS_FAILED(rv))
          m_eof = true;

  m_bytesInBuf += cnt;
  return true;
}

bool nsImportScanFile::Scan(bool *pDone)
{
  uint64_t available;
  nsresult rv = m_pInputStream->Available(&available);
  if (NS_FAILED(rv))
        {
    if (m_pos < m_bytesInBuf)
      ScanBuffer(pDone);
    *pDone = true;
    return true;
  }

  // Fill up a buffer and scan it
  if (!FillBufferFromFile())
    return false;

  return ScanBuffer(pDone);
}

bool nsImportScanFile::ScanBuffer(bool *)
{
  return true;
}


bool nsImportScanFileLines::ScanBuffer(bool *pDone)
{
  // m_pos, m_bytesInBuf, m_eof, m_pBuf are relevant

  uint32_t    pos = m_pos;
  uint32_t    max = m_bytesInBuf;
  uint8_t *    pChar = m_pBuf + pos;
  uint32_t    startPos;

  while (pos < max) {
    if (m_needEol) {
      // Find the next eol...
      while ((pos < max) && (*pChar != ImportCharSet::cCRChar) && (*pChar != ImportCharSet::cLinefeedChar)) {
        pos++;
        pChar++;
      }
      m_pos = pos;
      if (pos < max)
        m_needEol = false;
      if (pos == max) // need more buffer for an end of line
        break;
    }
    // Skip past any eol characters
    while ((pos < max) && ((*pChar == ImportCharSet::cCRChar) || (*pChar == ImportCharSet::cLinefeedChar))) {
      pos++;
      pChar++;
    }
    m_pos = pos;
    if (pos == max)
      break;
    // Make sure we can find either the eof or the
    // next end of line
    startPos = pos;
    while ((pos < max) && (*pChar != ImportCharSet::cCRChar) && (*pChar != ImportCharSet::cLinefeedChar)) {
      pos++;
      pChar++;
    }

    // Is line too big for our buffer?
    if ((pos == max) && !m_eof) {
      if (!m_pos) { // line too big for our buffer
        m_pos = pos;
        m_needEol = true;
      }
      break;
    }

    if (!ProcessLine(m_pBuf + startPos, pos - startPos, pDone)) {
      return false;
    }
    m_pos = pos;
  }

  return true;
}