File: lineconverter.cpp

package info (click to toggle)
qca2 2.3.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,888 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (161 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2003-2005  Justin Karneges <justin@affinix.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

#include "lineconverter.h"

namespace gpgQCAPlugin {

void LineConverter::setup(LineConverter::Mode m)
{
    state    = Normal;
    mode     = m;
    prebytes = 0;
    list.clear();
}

QByteArray LineConverter::update(const QByteArray &buf)
{
    if (mode == Read) {
        // Convert buf to UNIX line ending style
        // If buf ends with '\r' set state to Partival

        QByteArray out;

        if (state == Normal) {
            out = buf;
        } else {
            out.resize(buf.size() + 1);
            out[0] = '\r';
            memcpy(out.data() + 1, buf.data(), buf.size());
        }

        int n = 0;
        while (true) {
            n = out.indexOf('\r', n);
            // not found
            if (n == -1) {
                break;
            }
            // found, not last character
            if (n < (buf.size() - 1)) {
                // found windows line ending "\r\n"
                if (out[n + 1] == '\n') {
                    // clip out the '\r'
                    memmove(out.data() + n, out.data() + n + 1, out.size() - n - 1);
                    out.resize(out.size() - 1);
                }
            }
            // found, last character
            else {
                state = Partial;
                break;
            }
            ++n;
        }

        return out;
    } else {
        // On Windows use DOS line ending style.
        // On UNIX don't do any convertation. Return buf as is.
#ifdef Q_OS_WIN
        QByteArray out;
        int        prev = 0;
        int        at   = 0;

        while (1) {
            int n = buf.indexOf('\n', at);
            if (n == -1)
                break;

            int       chunksize = n - at;
            const int oldsize   = out.size();
            out.resize(oldsize + chunksize + 2);
            memcpy(out.data() + oldsize, buf.data() + at, chunksize);
            memcpy(out.data() + oldsize + chunksize, "\r\n", 2);

            list.append(prebytes + n + 1 - prev);
            prebytes = 0;
            prev     = n;

            at = n + 1;
        }
        if (at < buf.size()) {
            const int chunksize = buf.size() - at;
            const int oldsize   = out.size();
            out.resize(oldsize + chunksize);
            memcpy(out.data() + oldsize, buf.data() + at, chunksize);
        }

        prebytes += buf.size() - prev;
        return out;
#else
        return buf;
#endif
    }
}

QByteArray LineConverter::final()
{
    if (mode == Read) {
        QByteArray out;
        if (state == Partial) {
            out.resize(1);
            out[0] = '\n';
        }
        return out;
    } else {
        return QByteArray();
    }
}

QByteArray LineConverter::process(const QByteArray &buf)
{
    return update(buf) + final();
}

int LineConverter::writtenToActual(int bytes)
{
#ifdef Q_OS_WIN
    int n       = 0;
    int counter = bytes;
    while (counter > 0) {
        if (!list.isEmpty() && bytes >= list.first()) {
            ++n;
            counter -= list.takeFirst();
        } else {
            if (list.isEmpty())
                prebytes -= counter;
            else
                list.first() -= counter;

            if (prebytes < 0) {
                bytes += prebytes;
                prebytes = 0;
            }

            break;
        }
    }
    return bytes - n;
#else
    return bytes;
#endif
}

} // end namespace gpgQCAPlugin