summaryrefslogtreecommitdiffstats
path: root/tdefile-plugins/torrent/bytetape.cpp
blob: 2eaf8e015300fdf7cab399dd928b9d056a2bfa40 (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
/*
 * Copyright (c) 2003, 2004 Michael Pyne <michael.pyne@kdemail.net>
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This software 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; see the file COPYING.
 * If not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <kdebug.h>

#include "bytetape.h"

ByteTape::ByteTape (TQByteArray &array, int pos)
    : m_array(array), m_shared(new ByteTapeShared)
{
    m_shared->pos = pos;
}

ByteTape::ByteTape (const ByteTape &tape)
    : m_array(tape.m_array), m_shared(tape.m_shared)
{
}

ByteTape& ByteTape::operator += (const unsigned int i)
{
    m_shared->pos += i;
    if (m_array.size() <= m_shared->pos)
    {
        kdDebug(7034) << "Whoops.  Advanced too far." << endl;
        m_shared->pos = m_array.size() - 1;
    }
    
    return *this;
}

ByteTape& ByteTape::operator -= (const unsigned int i)
{
    if (i > m_shared->pos)
    {
        kdDebug(7034) << "Whoops, tried to back up too far." << endl;
        m_shared->pos = 0;
    }
    else
        m_shared->pos -= i;
    
    return *this;
}

char ByteTape::operator [] (const unsigned int i)
{
    if (i < m_array.size())
        return m_array[i];
    else
    {
        kdWarning() << "Can't dereference tape at " << i
                    << ", size is " << m_array.size() << endl;
        return 0;
    }
}

char &ByteTape::operator * ()
{
    return m_array[m_shared->pos];
}

// Postfix increment
ByteTape ByteTape::operator ++ (int)
{
    // Can't use copy ctor, as we'll be copying shared data, which defeats
    // the semantics of the postfix operator.
    ByteTape temp(m_array, m_shared->pos);
    m_shared->pos ++;

    if (m_shared->pos >= m_array.size())
    {
        m_shared->pos = m_array.size() - 1;
        kdDebug(7034) << "Tape already at end!" << endl;
        kdDebug(7034) << "Tape size is " << m_array.size() << endl;
    }
    
    return temp;
}

// Prefix increment
ByteTape & ByteTape::operator ++()
{
    m_shared->pos ++;
    if (m_shared->pos >= m_array.size())
    {
        m_shared->pos = m_array.size() - 1;
        kdDebug(7034) << "Tape already at end!" << endl;
        kdDebug(7034) << "Tape size is " << m_array.size() << endl;
    }
    
    return *this;
}

// Postfix decrement
ByteTape ByteTape::operator -- (int)
{
    // Can't use copy ctor, as we'll be copying shared data, which defeats
    // the semantics of the postfix operator.
    ByteTape temp(m_array, m_shared->pos);

    if (m_shared->pos != 0)
        m_shared->pos --;
    else
        kdDebug(7034) << "Tape already at beginning!" << endl;
    
    return temp;
}

// Prefix decrement
ByteTape & ByteTape::operator -- ()
{
    if (m_shared->pos != 0)
        m_shared->pos --;
    else
        kdDebug(7034) << "Tape already at beginning!" << endl;
    
    return *this;
}

bool ByteTape::setPos (unsigned int pos)
{
    if (pos >= m_array.size())
    {
        kdDebug(7034) << "Can't set tape to " << pos << endl;
        return false;
    }

    m_shared->pos = pos;
    return true;
}

char* ByteTape::at (const unsigned int i)
{
    if (i >= m_array.size())
    {
        kdDebug(7034) << "Access to tape at " << i << " out-of-range." << endl;
        return 0;
    }
        
    return m_array.data() + i;
}