summaryrefslogtreecommitdiffstats
path: root/tdefile-plugins/torrent/bint.h
blob: 7e5acd5fd0ce71cee2ccfa446b68820b028f00da (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
/*
 * 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 _BINT_H
#define _BINT_H

#include <tqcstring.h>
#include "bbase.h"
#include "bytetape.h"

/**
 * Class to represent a b-encoded integer.
 *
 * @author Michael Pyne <mpyne@grammarian.homelinux.net>
 * @see BBase
 */
class BInt : public BBase
{
    public:

    /**
     * Constructs a BInt by reading a b-encoded integer from @p dict.
     * You can start reading from a position other than the beginning
     * by passing that position to the constructor.
     *
     * @param dict the buffer to read from
     * @param start the position within the buffer of the beginning
     *        of the b-encoded integer.
     */
    BInt (TQByteArray &dict, int start = 0);

    /**
     * Constructs a BInt by reading a b-encoded integer from @p tape.
     * 
     * @param tape the ByteTape to read from.  It should already be
     *        positioned at the beginning of the b-encoded integer data.
     *        After construction, @p tape will point to the byte after
     *        the b-encoded integer on success.  If construction was
     *        not successful, @p tape will have an undefined position.
     */
    BInt (ByteTape &tape);

    /**
     * Destructor for this class.  No special action is taken.
     */
    virtual ~BInt ();

    /**
     * Returns the integer value of the data used to construct this
     * object.
     *
     * @return this object's integer value
     */
    TQ_LLONG get_value () const { return m_value; }

    /**
     * Returns the type of this class.
     *
     * @return bInt.  This value is only returned by this class.
     */
    virtual classID type_id() const { return bInt; }
    
    /**
     * This function should be called to determine whether the
     * integer was successfully created, since no exceptions
     * are thrown.
     *
     * @return true if this is a valid integer, 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);

    private:
    
    /**
     * Initialization function for the class, called to handle the
     * actual work of reading the b-encoded data from @p tape.
     *
     * @param tape the ByteTape to read from
     */
    void init(ByteTape &tape);

    TQ_LLONG m_value;
    bool m_valid;
};

#endif /* _BINT_H */