|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 Alberto Mardegan <info@mardy.it> |
| 3 | + * |
| 4 | + * This file is part of QtRaw. |
| 5 | + * |
| 6 | + * QtRaw is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * QtRaw is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with QtRaw. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "datastream.h" |
| 21 | + |
| 22 | +#include <QIODevice> |
| 23 | +#include <QTextStream> |
| 24 | + |
| 25 | +Datastream::Datastream(QIODevice *device): |
| 26 | + m_device(device) |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +Datastream::~Datastream() |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +int Datastream::valid() |
| 35 | +{ |
| 36 | + return m_device->isReadable(); |
| 37 | +} |
| 38 | + |
| 39 | +int Datastream::read(void *ptr, size_t size, size_t nmemb) |
| 40 | +{ |
| 41 | + return int(m_device->read((char *)ptr, size * nmemb)); |
| 42 | +} |
| 43 | + |
| 44 | +int Datastream::seek(INT64 offset, int whence) |
| 45 | +{ |
| 46 | + qint64 pos; |
| 47 | + switch (whence) { |
| 48 | + case SEEK_SET: |
| 49 | + pos = offset; |
| 50 | + break; |
| 51 | + case SEEK_CUR: |
| 52 | + pos = m_device->pos() + offset; |
| 53 | + break; |
| 54 | + case SEEK_END: |
| 55 | + pos = m_device->size(); |
| 56 | + break; |
| 57 | + default: |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + if (pos < 0) pos = 0; |
| 62 | + return m_device->seek(pos) ? 0 : -1; |
| 63 | +} |
| 64 | + |
| 65 | +INT64 Datastream::tell() |
| 66 | +{ |
| 67 | + return m_device->pos(); |
| 68 | +} |
| 69 | + |
| 70 | +int Datastream::get_char() |
| 71 | +{ |
| 72 | + char c; |
| 73 | + return m_device->getChar(&c) ? c : -1; |
| 74 | +} |
| 75 | + |
| 76 | +char *Datastream::gets(char *s, int n) |
| 77 | +{ |
| 78 | + return m_device->readLine(s, n) >= 0 ? s : NULL; |
| 79 | +} |
| 80 | + |
| 81 | +int Datastream::scanf_one(const char *fmt, void *val) |
| 82 | +{ |
| 83 | + QTextStream stream(m_device); |
| 84 | + /* This is only used for %d or %f */ |
| 85 | + if (qstrcmp(fmt, "%d") == 0) { |
| 86 | + int d; |
| 87 | + stream >> d; |
| 88 | + *(static_cast<int*>(val)) = d; |
| 89 | + } else if (qstrcmp(fmt, "%f") == 0) { |
| 90 | + float f; |
| 91 | + stream >> f; |
| 92 | + *(static_cast<float*>(val)) = f; |
| 93 | + } else { |
| 94 | + return 0; |
| 95 | + } |
| 96 | + return (stream.status() == QTextStream::Ok) ? 1 : EOF; |
| 97 | +} |
| 98 | + |
| 99 | +int Datastream::eof() |
| 100 | +{ |
| 101 | + return m_device->atEnd(); |
| 102 | +} |
| 103 | + |
| 104 | +void *Datastream::make_jas_stream() |
| 105 | +{ |
| 106 | + return NULL; |
| 107 | +} |
0 commit comments