iodeviceseq_qt.cpp

Go to the documentation of this file.
00001 /*
00002  !concept {:name => "QIODevice input device composing sub-QIODevices"}
00003 */
00004 
00005 #include "iodeviceseq_qt.hpp"
00006 
00007 #include "common/assertions.h"
00008 
00009 QIODeviceSeq::QIODeviceSeq(const QList<QIODevice*>& aList) :
00010   iList(aList), iIterator(iList), iDevice(NULL), iAtEnd(false)
00011 {
00012   // This device is open to begin with, and it need not and cannot be
00013   // closed.
00014   setOpenMode(QIODevice::ReadOnly);
00015 }
00016 
00017 QIODeviceSeq::~QIODeviceSeq()
00018 {
00019 }
00020 
00021 bool QIODeviceSeq::isSequential() const
00022 {
00023   return true;
00024 }
00025 
00026 bool QIODeviceSeq::atEnd() const
00027 {
00028   return iAtEnd;
00029 }
00030 
00031 // Note that we never emit readyRead() as we have all the data to
00032 // begin with, and therefore new data will not become available.
00033 // 
00034 // The documentation for this interface is confusing wrt to what to
00035 // return if there is no more data available; is it 0 or -1. How to
00036 // find out if an error has occurred? readData is not a public API,
00037 // sure, but it is pure virtual and so must be implemented by anyone
00038 // creating a QIODevice.
00039 // 
00040 // The best bet is to look at the source code of QIODevice::read() and
00041 // the implementations of QBuffer and QFile to make sure we get this
00042 // right enough for our use case. QBuffer never gives a read error.
00043 // QFile returns -1 only for actual errors, not for EOF, so we do the
00044 // same.
00045 qint64 QIODeviceSeq::readData(char *data, qint64 maxlen)
00046 {
00047   if (iAtEnd) {
00048     setErrorString("read past end");
00049     return -1;
00050   }
00051 
00052   forever {
00053     if (!iDevice) {
00054       if (!iIterator.hasNext()) {
00055   iAtEnd = true;
00056   return 0;
00057       }
00058       iDevice = iIterator.next();
00059       assert(iDevice);
00060     }
00061 
00062     qint64 r = iDevice->read(data, maxlen);
00063     switch (r)
00064       {
00065       case -1:
00066         {
00067           setErrorString(iDevice->errorString());
00068     return -1;
00069         }
00070       case 0:
00071         {
00072     // No more data from this device.
00073     iDevice = NULL;
00074           break;
00075         }
00076       default:
00077         {
00078     // Got some data
00079     return r;
00080         }
00081       }
00082   }
00083 
00084   assert(0 && "logic error");
00085   return -1;
00086 }
00087 
00088 // pure virtual, so must implement even though not supported
00089 qint64 QIODeviceSeq::writeData(const char *data, qint64 len)
00090 {
00091   return -1;
00092 }
00093 
00094 /**
00095 
00096 Copyright 2010 Helsinki Institute for Information Technology (HIIT)
00097 and the authors. All rights reserved.
00098 
00099 Authors: Tero Hasu <tero.hasu@hut.fi>
00100 
00101 Permission is hereby granted, free of charge, to any person
00102 obtaining a copy of this software and associated documentation files
00103 (the "Software"), to deal in the Software without restriction,
00104 including without limitation the rights to use, copy, modify, merge,
00105 publish, distribute, sublicense, and/or sell copies of the Software,
00106 and to permit persons to whom the Software is furnished to do so,
00107 subject to the following conditions:
00108 
00109 The above copyright notice and this permission notice shall be
00110 included in all copies or substantial portions of the Software.
00111 
00112 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00113 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00114 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00115 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00116 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00117 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00118 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00119 SOFTWARE.
00120 
00121  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1