Update versions of AIM dlls this allows AIM to send us whole directory.
[kdenetwork.git] / kfile-plugins / torrent / blist.cpp
blobe2438b4745f61d902c952903ac5ae2ae9d6c4b66
1 /*
2 * Copyright (c) 2003, 2004 Michael Pyne <michael.pyne@kdemail.net>
4 * This software is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this library; see the file COPYING.
16 * If not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <qiodevice.h>
21 #include "bytetape.h"
22 #include "blist.h"
23 #include "bdict.h"
24 #include "bstring.h"
25 #include "bint.h"
27 BList::BList (ByteTape &tape)
28 : m_valid(false), m_array()
30 init (tape);
33 BList::BList (QByteArray &dict, unsigned int start)
34 : m_valid(false), m_array()
36 ByteTape tape (dict, start);
38 init (tape);
41 void BList::init (ByteTape &tape)
43 BBase *temp;
45 if (*tape != 'l')
46 return;
48 tape++;
50 /* Repeat circling over the string until the list is over */
51 while (*tape != 'e')
53 switch (*tape)
55 case 'd':
56 temp = new BDict (tape);
57 break;
59 case 'l': /* This element is a list */
60 temp = new BList (tape);
61 break;
63 case 'i': /* This element is an int */
64 temp = new BInt (tape);
65 break;
67 default: /* Maybe a string? */
68 temp = new BString (tape);
71 if (!temp || !temp->isValid())
72 return; // Invalid list element
74 m_array.append (temp);
77 m_valid = true;
79 // Only way out is to detect 'e', so we need to increment tape past that.
80 tape++;
83 BList::~BList()
85 BBaseVectorIterator iter;
87 for (iter = begin(); iter != end(); ++iter)
88 delete *iter;
91 BBase* BList::index (unsigned int i)
93 if (i >= count())
94 return 0;
95 else
96 return m_array[i];
99 BList * BList::indexList (unsigned int i)
101 BBase *base = index(i);
103 if (base && base->type_id() == bList)
104 return dynamic_cast<BList*>(base);
106 return 0;
109 BInt * BList::indexInt (unsigned int i)
111 BBase *base = index(i);
113 if (base && base->type_id() == bInt)
114 return dynamic_cast<BInt*>(base);
116 return 0;
119 BDict * BList::indexDict (unsigned int i)
121 BBase *base = index(i);
123 if (base && base->type_id() == bDict)
124 return dynamic_cast<BDict*>(base);
126 return 0;
129 BString * BList::indexStr (unsigned int i)
131 BBase *base = index(i);
133 if (base && base->type_id() == bString)
134 return dynamic_cast<BString*>(base);
136 return 0;
139 bool BList::writeToDevice(QIODevice &device)
141 if (!m_valid)
142 return false;
144 const char *l_str = "l";
145 const char *e_str = "e";
146 Q_LONG written = 0, result = 0;
148 written = device.write (l_str, 1);
149 while (written < 1)
151 if (written < 0 || result < 0)
152 return false;
154 result = device.write (l_str, 1);
155 written += result;
158 BBaseVectorIterator iter;
159 for (iter = begin(); iter != end(); ++iter)
161 if (!((*iter)->writeToDevice (device)))
162 return false;
165 written = device.write (e_str, 1);
166 while (written < 1)
168 if (written < 0 || result < 0)
169 return false;
171 result = device.write (e_str, 1);
172 written += result;
175 return true;
178 // vim: set et sw=4 ts=4: