VendorCode.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "VendorCode.h"
00025 
00026 using Gnutella::Packets::VendorCode;
00027 
00028 VendorCode::VendorCode()
00029 {
00030     p.vendorCode = "UNKN";
00031     p.isValid = false;
00032 }
00033 
00034 VendorCode::VendorCode (const QByteArray &vendorCode)
00035 {
00036     if (vendorCode.length() == VendorCodeLength) {
00037         p.vendorCode = vendorCode;
00038         p.isValid = true;
00039     } else {
00040         p.vendorCode = "UNKN";  // Unknown
00041         p.isValid = false;
00042     }
00043 }
00044 
00045 VendorCode::~VendorCode()
00046 {
00047 }
00048 
00049 QString VendorCode::toString() const
00050 {
00051     // \todo Make a map with all known codes - vendor names and return a
00052     // string from there.
00053     return QString::fromLatin1 (p.vendorCode.data(), VendorCodeLength);
00054 }
00055 
00056 QDataStream & Gnutella::Packets::operator>> (QDataStream &stream, VendorCode &vendorCode)
00057 {
00058     // \todo After reading the code assign the byte array from a set of values
00059     // so that all copies share the same values (sth like a flyweight.
00060     uchar tmp;
00061     vendorCode.p.vendorCode.resize (VendorCode::VendorCodeLength);
00062     for (int i = 0; i < VendorCode::VendorCodeLength; i++) {
00063         stream >> tmp;
00064         vendorCode.p.vendorCode[i] = tmp;
00065     }
00066     return stream;
00067 }
00068 
00069 QDataStream & Gnutella::Packets::operator<< (QDataStream &stream, const VendorCode &vendorCode)
00070 {
00071     for (int i = 0; i < VendorCode::VendorCodeLength; i++) {
00072         stream << static_cast <uchar> (vendorCode.p.vendorCode.at (i));
00073     }
00074     return stream;
00075 }