Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GzFileBuf.cpp « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d61a2252561c8422c15fa0885f775d7d5418be8d (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
#include "GzFileBuf.h"

#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>

GzFileBuf::GzFileBuf(const char* filename)
{
  m_gz_file = gzopen(filename, "rb");
  if (m_gz_file == NULL) {
    std::cerr << "ERROR: Failed to open " << filename << std::endl;
    std::exit(1);
  }
  setg(m_buf + sizeof(int),     // beginning of putback area
       m_buf + sizeof(int),     // read position
       m_buf + sizeof(int));    // end position
}

GzFileBuf::~GzFileBuf()
{
  gzclose(m_gz_file);
}

int GzFileBuf::overflow(int_type c)
{
  throw;
}

// read one character
int GzFileBuf::underflow()
{
  // is read position before end of m_buf?
  if (gptr() < egptr()) {
    return traits_type::to_int_type(*gptr());
  }

  /* process size of putback area
   * - use number of characters read
   * - but at most four
   */
  unsigned int num_put_back = static_cast<unsigned int>(gptr() - eback());
  if (num_put_back > sizeof(int)) {
    num_put_back = sizeof(int);
  }

  /* copy up to four characters previously read into
   * the putback m_buf (area of first four characters)
   */
  std::memmove(m_buf + (sizeof(int) - num_put_back),
               gptr() - num_put_back, num_put_back);

  // read new characters
  const int num = gzread(m_gz_file, m_buf + sizeof(int),
                         kBufSize - sizeof(int));
  if (num <= 0) {
    // ERROR or EOF
    return EOF;           // NOTE: the macro EOF defined in stdio.h
  }

  // reset m_buf pointers
  setg(m_buf + (sizeof(int) - num_put_back),   // beginning of putback area
       m_buf + sizeof(int),                // read position
       m_buf + sizeof(int) + num);           // end of buffer

  // return next character
  return traits_type::to_int_type(*gptr());
}

std::streampos GzFileBuf::seekpos(
  std::streampos sp,
  std::ios_base::openmode which)
{
  throw;
}

std::streamsize GzFileBuf::xsgetn(char* s,
                                  std::streamsize num)
{
  return static_cast<std::streamsize>(gzread(m_gz_file,s,num));
}

std::streamsize GzFileBuf::xsputn(const char* s,
                                  std::streamsize num)
{
  throw;
}