File: streams.cpp

package info (click to toggle)
einstein 2.0.dfsg.2-10
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 2,548 kB
  • sloc: cpp: 10,437; makefile: 78; sh: 1
file content (57 lines) | stat: -rw-r--r-- 1,298 bytes parent folder | download | duplicates (10)
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
#include "streams.h"
#include "exceptions.h"
#include "unicode.h"


UtfStreamReader::UtfStreamReader(std::ifstream *s)
{
    stream = s;
}

UtfStreamReader::~UtfStreamReader()
{
}

// This function is very slow because of poor fromUtf8 function design
wchar_t UtfStreamReader::getNextChar()
{
    unsigned char buf[10];

    if (0 < backBuf.size()) {
        wchar_t wc = backBuf.front();
        backBuf.pop_front();
        return wc;
    }
    
    if (! stream->good())
        throw Exception(L"Error reading from stream 1");

    int sz = stream->readsome((char*)buf, 1);
    if (1 != sz)
        throw Exception(L"Error reading from stream 2");
    int size = getUtf8Length(buf[0]);
    if (size > 1) {
        sz = stream->readsome((char*)buf + 1, size - 1);
        if (size - 1 != sz)
            throw Exception(L"Error reading from stream 3");
    }
    buf[size] = 0;
    std::string s((char*)buf);
    std::wstring ws(fromUtf8(s));
    if (1 != ws.length())
        throw Exception(L"Error converting UTF-8 character to wide character");
    return ws[0];
}

void UtfStreamReader::ungetChar(wchar_t ch)
{
    backBuf.push_back(ch);
}

bool UtfStreamReader::isEof()
{
    if (stream->eof())
        return true;     // FIXME: it doesn't work. why?
    return EOF == stream->peek();
}