BItem.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-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 "BItem.h"
00025 #include "BDictionary.h"
00026 #include "BErrorItem.h"
00027 #include "BList.h"
00028 #include "BInt.h"
00029 #include "BString.h"
00030 #include "Imports.cpp"
00031 
00033 
00037 bool BItem::tryReadInt(BinaryReader &reader,
00038                        qint64 &outValue, char terminator) const
00039 {
00040     QByteArray rawInt = reader.readString (terminator);
00041     bool ok = true;
00042     outValue = rawInt.toLongLong(&ok); // qlonglong is the same as qint64
00043     return ok;
00044 }
00045 
00047 
00051 auto_ptr <BItem> BItem::readItem (BinaryReader &reader)
00052 {
00053     if (!reader.canRead (1))
00054         return auto_ptr <BItem> (new BErrorItem);
00055 
00056     auto_ptr <BItem> item;
00057     char ch = reader.lookAhead (1);
00058 
00059     switch (ch)
00060     {
00061     case 'i':
00062         item = auto_ptr <BItem> (new BInt);
00063         break;
00064     case 'l':
00065         item = auto_ptr <BItem> (new BList);
00066         break;
00067     case 'd':
00068         item = auto_ptr <BItem> (new BDictionary);
00069         break;
00070     default:
00071         if (ch > '0' && ch <= '9') {
00072             item = auto_ptr <BItem> (new BString);
00073         }
00074         break;
00075     }
00076     if (item.get() == 0 || !item->read (reader))
00077         return auto_ptr <BItem> (new BErrorItem);
00078     return item;
00079 }