[Solved] stdin with QIODevice



  • Hi i have a class named StandardInputReader inherited QIODevice. It gets input from stdin. All iodevice functionality works except readAll function. When i call readAll, it waits forever. But if i call @device.read(device.bytesAvailable());@ all works fine. I redefined
    @
    bool isSequential() const;
    qint64 bytesAvailable() const;
    bool waitForReadyRead(int msecs);

    protected:
    qint64 readData(char *data, qint64 maxSize);
    qint64 writeData(const char data, qint64 maxSize); / Does nothing */
    @

    these functions. I even looked at QIODevice source code and didn't figure out what causing this. Any suggestions?



  • Hi,

    if you look at the source of QIODevive::read all, you find:

    @
    QByteArray QIODevice::readAll()
    {
    ....
    do {
    result.resize(result.size() + QIODEVICE_BUFFERSIZE);
    readResult = read(result.data() + readBytes, result.size() - readBytes);
    if (readResult > 0 || readBytes == 0)
    readBytes += readResult;
    } while (readResult > 0);
    ...
    }
    @

    This means, unless StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...


  • Moderators

    [quote author="Gerolf" date="1318504064"]
    This means, unless StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...[/quote]
    The other way around, or?

    You mean, when StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...



  • I think i figured it out. QIODevice::read function as Gerolf calls my readData function. i used standard c read function(man 2 read) to read from stdin. when readData calls read, process is blocked until new data available for reading. Because of this, QIODevice::read enters infinite loop and never returns. I just added @
    if (bytesAvailable() == 0)
    return 0;
    @ before read and problem solved. Thanks for replies.



  • [quote author="koahnig" date="1318507175"]
    [quote author="Gerolf" date="1318504064"]
    This means, unless StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...[/quote]
    The other way around, or?

    You mean, when StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...[/quote]

    right,....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.