You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.3 KiB
139 lines
4.3 KiB
15 years ago
|
/*
|
||
|
* 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.,
|
||
15 years ago
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
15 years ago
|
*/
|
||
|
#ifndef _BSTRING_H
|
||
|
#define _BSTRING_H
|
||
|
|
||
15 years ago
|
#include <tqstring.h>
|
||
|
#include <tqcstring.h>
|
||
15 years ago
|
|
||
|
#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
|
||
|
*/
|
||
15 years ago
|
BString (TQByteArray &dict, int start = 0);
|
||
15 years ago
|
|
||
|
/**
|
||
|
* 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 ();
|
||
|
|
||
|
/**
|
||
15 years ago
|
* Returns a TQString representation of the data in the
|
||
15 years ago
|
* BString. It is the responsibility of the caller to ensure
|
||
15 years ago
|
* that the data is convertible to a TQString. More specifically,
|
||
15 years ago
|
* the data should not contain any embedded NULLs.
|
||
|
*
|
||
15 years ago
|
* @return TQString containing the data from this BString.
|
||
15 years ago
|
*/
|
||
15 years ago
|
TQString get_string() const { return TQString::fromUtf8(m_data.data()); }
|
||
15 years ago
|
|
||
|
/**
|
||
|
* 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
|
||
15 years ago
|
* TQIODevice.
|
||
|
* @param device the TQIODevice to write to
|
||
15 years ago
|
* @return true on a successful write, false otherwise
|
||
|
*/
|
||
15 years ago
|
virtual bool writeToDevice (TQIODevice &device);
|
||
15 years ago
|
|
||
|
/**
|
||
15 years ago
|
* Changes the value of the string to the given TQString.
|
||
15 years ago
|
*
|
||
15 years ago
|
* @param str the TQString containing the new value
|
||
15 years ago
|
* @return true if the value was successfully changed,
|
||
|
* false otherwise.
|
||
|
*/
|
||
15 years ago
|
bool setValue (const TQString &str);
|
||
15 years ago
|
|
||
|
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);
|
||
|
|
||
15 years ago
|
TQByteArray m_data;
|
||
15 years ago
|
bool m_valid;
|
||
|
};
|
||
|
|
||
|
#endif /* _BSTRING_H */
|
||
|
|
||
|
// vim: set et ts=4 sw=4:
|