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.
129 lines
3.2 KiB
129 lines
3.2 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
|
*/
|
||
15 years ago
|
#include <tqcstring.h>
|
||
|
#include <tqiodevice.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include "bstring.h"
|
||
|
#include "bytetape.h"
|
||
|
|
||
15 years ago
|
BString::BString (TQByteArray &dict, int start) :
|
||
15 years ago
|
m_data(), m_valid(false)
|
||
|
{
|
||
|
ByteTape tape (dict, start);
|
||
|
init (tape);
|
||
|
}
|
||
|
|
||
|
BString::BString (ByteTape &tape)
|
||
|
: m_data(), m_valid(false)
|
||
|
{
|
||
|
init (tape);
|
||
|
}
|
||
|
|
||
15 years ago
|
// The reason we don't store stuff in a TQString is because BitTorrent
|
||
15 years ago
|
// b-encoded strings may contain zeroes within the string, which makes
|
||
|
// a BString more of a buffer than a true string.
|
||
|
void BString::init (ByteTape &tape)
|
||
|
{
|
||
15 years ago
|
TQByteArray &dict(tape.data());
|
||
15 years ago
|
|
||
13 years ago
|
if (dict.find(':', tape.pos()) == -1)
|
||
15 years ago
|
{
|
||
13 years ago
|
kdDebug(7034) << "Can't find : for string!" << endl;
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
// Copy the part from start to :, as it will be a number
|
||
|
// That number is the number of characters to read
|
||
13 years ago
|
int length = dict.find(':', tape.pos()) - tape.pos();
|
||
15 years ago
|
char *ptr = dict.data();
|
||
|
|
||
|
ptr += tape.pos();
|
||
|
|
||
15 years ago
|
TQByteArray buffer (length + 1);
|
||
14 years ago
|
tqmemmove (buffer.data(), ptr, length);
|
||
15 years ago
|
buffer[length] = 0;
|
||
|
|
||
15 years ago
|
TQString numberString (buffer);
|
||
15 years ago
|
bool a_isValid;
|
||
|
ulong len = numberString.toULong (&a_isValid);
|
||
|
|
||
|
if (!a_isValid)
|
||
|
{
|
||
|
kdDebug(7034) << "Invalid string length!" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Now that we have the length, we need to advance the tape
|
||
|
// past the colon
|
||
|
tape += length; // Move to colon
|
||
|
if (*tape != ':')
|
||
|
{
|
||
|
// Sanity check
|
||
|
kdError(7034) << "SANITY CHECK FAILED. *tape != ':'!" << endl;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
tape++; // Move past colon
|
||
|
|
||
|
// Time to copy the data
|
||
|
char *textBuffer = tape.at(tape.pos());
|
||
13 years ago
|
if (!m_data.resize(len + 1))
|
||
15 years ago
|
return;
|
||
|
|
||
14 years ago
|
tqmemmove (m_data.data(), textBuffer, len);
|
||
15 years ago
|
m_data[len] = 0; // Null terminate for convienience
|
||
|
|
||
|
tape += len;
|
||
|
m_valid = true;
|
||
|
}
|
||
|
|
||
|
BString::~BString ()
|
||
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
bool BString::writeToDevice(TQIODevice &device)
|
||
15 years ago
|
{
|
||
|
if (!m_valid)
|
||
|
return false;
|
||
|
|
||
15 years ago
|
TQString str = TQString("%1:").
|
||
15 years ago
|
arg(get_len());
|
||
|
|
||
15 years ago
|
TQCString utfString = str.utf8();
|
||
15 years ago
|
|
||
|
/* Don't write null terminator */
|
||
|
device.writeBlock (utfString.data(), utfString.size() - 1);
|
||
|
|
||
|
// Output the actual data
|
||
|
device.writeBlock (m_data.data(), m_data.size() - 1);
|
||
|
|
||
|
// Done
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool BString::setValue (const TQString &str)
|
||
15 years ago
|
{
|
||
|
m_data = str.utf8();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// vim: set et ts=4 sw=4:
|