summaryrefslogtreecommitdiffstats
path: root/mailnews/import/src/ImportOutFile.h
blob: 461728c2284625cffe90ab07089f7c4d4b564607 (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
/* -*- 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/. */
#ifndef ImportOutFile_h___
#define ImportOutFile_h___

#include "nsImportTranslator.h"
#include "nsIOutputStream.h"
#include "nsIFile.h"

#define kMaxMarkers    10

class ImportOutFile;

class ImportOutFile {
public:
  ImportOutFile();
  ImportOutFile(nsIFile *pFile, uint8_t * pBuf, uint32_t sz);
  ~ImportOutFile();

  bool    InitOutFile(nsIFile *pFile, uint32_t bufSz = 4096);
  void  InitOutFile(nsIFile *pFile, uint8_t * pBuf, uint32_t sz);
  inline bool    WriteData(const uint8_t * pSrc, uint32_t len);
  inline bool    WriteByte(uint8_t byte);
  bool    WriteStr(const char *pStr) {return WriteU8NullTerm((const uint8_t *) pStr, false); }
  bool    WriteU8NullTerm(const uint8_t * pSrc, bool includeNull);
  bool    WriteEol(void) { return WriteStr("\x0D\x0A"); }
  bool    Done(void) {return Flush();}

  // Marker support
  bool    SetMarker(int markerID);
  void  ClearMarker(int markerID);
  bool    WriteStrAtMarker(int markerID, const char *pStr);

  // 8-bit to 7-bit translation
  bool    Set8bitTranslator(nsImportTranslator *pTrans);
  bool    End8bitTranslation(bool *pEngaged, nsCString& useCharset, nsCString& encoding);

protected:
  bool    Flush(void);

protected:
  nsCOMPtr <nsIFile>      m_pFile;
        nsCOMPtr <nsIOutputStream> m_outputStream;
  uint8_t *    m_pBuf;
  uint32_t    m_bufSz;
  uint32_t    m_pos;
  bool        m_ownsFileAndBuffer;

  // markers
  uint32_t    m_markers[kMaxMarkers];

  // 8 bit to 7 bit translations
  nsImportTranslator  *  m_pTrans;
  bool            m_engaged;
  bool            m_supports8to7;
  ImportOutFile *      m_pTransOut;
  uint8_t *        m_pTransBuf;
};

inline bool    ImportOutFile::WriteData(const uint8_t * pSrc, uint32_t len) {
  while ((len + m_pos) > m_bufSz) {
    if ((m_bufSz - m_pos)) {
      memcpy(m_pBuf + m_pos, pSrc, m_bufSz - m_pos);
      len -= (m_bufSz - m_pos);
      pSrc += (m_bufSz - m_pos);
      m_pos = m_bufSz;
    }
    if (!Flush())
      return false;
  }

  if (len) {
    memcpy(m_pBuf + m_pos, pSrc, len);
    m_pos += len;
  }

  return true;
}

inline bool    ImportOutFile::WriteByte(uint8_t byte) {
  if (m_pos == m_bufSz) {
    if (!Flush())
      return false;
  }
  *(m_pBuf + m_pos) = byte;
  m_pos++;
  return true;
}

#endif /* ImportOutFile_h__ */