summaryrefslogtreecommitdiffstats
path: root/tdefile-plugins/torrent/bstring.h
blob: 22b23686fee674d712db5bcd8f40ba255f925eb0 (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
/*
 * 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.
 */
#ifndef _BSTRING_H
#define _BSTRING_H

#include <tqstring.h>
#include <tqcstring.h>

#include "bytetape.h"
#include "bbase.h"

/**
 * A class to handle the b-encoded strings used by BitTorrent.
 * It implements BBase, and although the class is referred to
 * as a string, it can hold arbitrary data, since b-encoded strings
 * are stored with a length, instead of using a terminator character.
 *
 * @author Michael Pyne <mpyne@grammarian.homelinux.net>
 * @see BBase
 */
class BString : public BBase
{
    public:

    /**
     * Construct a BString from @p dict.
     *
     * @param dict the buffer to read from
     * @param start the position in the buffer to start
     *        reading from
     */
    BString (TQByteArray &dict, int start = 0);

    /**
     * Construct a BString from @p tape.  Any changes made to
     * @p tape, such as its current position and data, will be
     * shared with the object that called this constructor.  @p tape
     * should already be positioned at the position to read from.
     * If construction was successful, @p tape will point to the
     * byte after the string data.  If construction was unsuccessful,
     * the position of the tape is undefined.
     *
     * @param tape the ByteTape to read from.
     */
    BString (ByteTape &tape);
    
    /**
     * Destroys the BString, and deallocates all memory that had
     * been used.
     */
    virtual ~BString ();

    /**
     * Returns a TQString representation of the data in the
     * BString.  It is the responsibility of the caller to ensure
     * that the data is convertible to a TQString.  More specifically,
     * the data should not contain any embedded NULLs.
     *
     * @return TQString containing the data from this BString.
     */
    TQString get_string() const { return TQString::fromUtf8(m_data.data()); }

    /**
     * Returns the amount of data held by the string.  It would be
     * perhaps more appropriate to call this size(), since this is
     * a buffer, not a true text string.
     *
     * @return the size of the string, not including the NULL
     *         terminator.
     */
    const int get_len() const { return m_data.size() - 1; }

    /**
     * Returns the type of this class.
     *
     * @return bString.  This value is only returned by this class.
     */
    virtual classID type_id() const { return bString; }

    /**
     * This function should be called to determine whether the
     * string was successfully created, since no exceptions
     * are thrown.
     *
     * @return true if this is a valid string, false otherwise
     */
    virtual bool isValid() const { return m_valid; }

    /**
     * Outputs the b-encoded representation of the object to the given
     * TQIODevice.
     * @param device the TQIODevice to write to
     * @return true on a successful write, false otherwise
     */
    virtual bool writeToDevice (TQIODevice &device);
    
    /**
     * Changes the value of the string to the given TQString.
     *
     * @param str the TQString containing the new value
     * @return true if the value was successfully changed,
     *         false otherwise.
     */
    bool setValue (const TQString &str);
    
    private:

    /**
     * This function handles the actual initialization of the object upon
     * construction, and set the m_valid flag if successful.
     *
     * @param tape the ByteTape to read from
     */
    void init (ByteTape &tape);

    TQByteArray m_data;
    bool m_valid;
};

#endif /* _BSTRING_H */