SilentEye 0.4.1

sef/data.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye. If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "data.h"
00017 #include "silenteyeexception.h"
00018 
00019 namespace SilentEyeFramework {
00020         
00021         Data::Data(const QByteArray& bytes, const DataFormat format)
00022         {
00023             this->setObjectName("Data");
00024             m_logger = new Logger(this);
00025             if (format == UINT32)
00026             {
00027                 m_format = format;
00028                 m_data = bytes;
00029             }
00030             else if(bytes.isEmpty())
00031             {
00032                 m_format = F_UNDEF;
00033                 m_data = bytes;
00034             }
00035             else
00036             {
00037                 QByteArray rawbytes = bytes;
00038                 m_format = (DataFormat) ((int)rawbytes.at(0) - (int)'0');
00039                 rawbytes.remove(0, 1);
00040                 if (m_format < 0 || m_format > 7)
00041                     throw SilentEyeException("Loaded informations are invalid! Check your options...",
00042                                              "Data format unknown (" + QString::number(m_format) + ")");
00043                 if (format != F_UNDEF && format != m_format)
00044                     throw SilentEyeException("Requested format doesn't match loaded informations! Check your options...",
00045                                              "Data format dismatch (" + QString::number(m_format) + "!=" + QString::number(format) + ")");
00046                 
00047                 if (m_format == FILE)
00048                 {
00049                     int index = rawbytes.indexOf('<');
00050                     m_data = rawbytes.right(rawbytes.size() - (index+1));
00051                     rawbytes.truncate(index);
00052                     m_name = QString::fromUtf8(rawbytes.data());
00053                 }
00054                 else
00055                 {
00056                     m_name = "";
00057                     m_data = rawbytes;
00058                 }
00059             }
00060         }
00061 
00062         Data::Data(const DataFormat format)
00063             : m_format(format)
00064         {
00065             this->setObjectName("Data");
00066             m_logger = new Logger(this);
00067         }
00068         
00069         Data::Data(const DataFormat format, const QByteArray& data, const QString name)
00070             : m_format(format), m_name(name), m_data(data)
00071         {
00072             this->setObjectName("Data");
00073             m_logger = new Logger(this);
00074         }
00075 
00076         Data::~Data()
00077         {
00078             delete m_logger;
00079         }
00080         
00081         Data::DataFormat Data::format() const
00082         {
00083             return m_format;
00084         }
00085         
00086         QString Data::name() const
00087         {
00088             return m_name;
00089         }
00090         
00091         QByteArray Data::data() const
00092         {
00093             return m_data;
00094         }
00095         
00096         QByteArray Data::toByteArray() const
00097         {
00098             QByteArray bytes = m_data;
00099             switch(m_format)
00100             {
00101             case BYTES:
00102             case UTF8:
00103             case LATIN1:
00104             case ASCII:
00105                 bytes.prepend((char)((int)'0' + (int)m_format));
00106                 break;
00107             case FILE:
00108                 bytes.prepend((m_name+'<').toUtf8());
00109                 bytes.prepend((char)((int)'0' + (int)m_format));
00110                 break;
00111             case UINT32:
00112             case F_UNDEF:
00113                 break;
00114             }
00115             
00116             return bytes;
00117         }
00118 }