예제 #1
0
int EventStream::load(QIODevice &source)
{
    uint8_t sig[4];
    int32_t size;
    int offset;
    struct evt_file_header file_header;

    if (source.read((char *)&file_header, sizeof(file_header)) != sizeof(file_header)) {
        qDebug() << "Unable to read file header: " << source.errorString();
        return -1;
    }

    if (memcmp(&file_header.magic1, EVENT_HDR_1, sizeof(file_header.magic1))) {
        qDebug() << "Error: File signature doesn't match";
        qDebug("%x%x%x%x vs %x%x%x%x\n", 
                file_header.magic1[0], file_header.magic1[1],
                file_header.magic1[2], file_header.magic1[3],
                EVENT_HDR_1[0], EVENT_HDR_1[1],
                EVENT_HDR_1[2], EVENT_HDR_1[3]);
        return -1;
    }

    size = _ntohl(file_header.count);

	for (offset=0; offset<size; offset++) {
        uint32_t addr;
        if (source.read((char *)&addr, sizeof(addr)) != sizeof(addr)) {
            qDebug() << "Unable to read addr table: " << source.errorString();
            return -1;
        }
        addr = _ntohl(addr);
        _offsets.append(addr);
    }
    
	if (source.read((char *)sig, sizeof(sig)) != sizeof(sig)) {
        qDebug() << "Unable to read stream: " << source.errorString();
        return -1;
    }

    if (memcmp(sig, EVENT_HDR_2, sizeof(sig))) {
        qDebug() << "Error: Main signature doesn't match";
        qDebug("%x%x%x%x vs %x%x%x%x\n", sig[0], sig[1], sig[2], sig[3],
                EVENT_HDR_2[0], EVENT_HDR_2[1], EVENT_HDR_2[2], EVENT_HDR_2[3]);
        return -1;
    }

    for (offset=0; offset<size; offset++) {
        Event e(source, this);
        _events.append(e);
    }

    _currentEvents = _events;

    return 0;
}
QString QIODeviceProto::errorString() const
{
  QIODevice *item = qscriptvalue_cast<QIODevice*>(thisObject());
  if (item)
    return item->errorString();
  return QString();
}
예제 #3
0
bool LoadFromJSON(QVariantMap &data, QIODevice &f, const char *magicIdentifier, uint32_t magicVersion)
{
  QByteArray json = f.readAll();

  if(json.isEmpty())
  {
    qCritical() << "Read invalid empty JSON data from file " << f.errorString();
    return false;
  }

  QJsonDocument doc = QJsonDocument::fromJson(json);

  if(doc.isEmpty() || doc.isNull())
  {
    qCritical() << "Failed to convert file to JSON document";
    return false;
  }

  data = doc.toVariant().toMap();

  if(data.isEmpty() || !data.contains(magicIdentifier))
  {
    qCritical() << "Converted config data is invalid or unrecognised";
    return false;
  }

  if(data[magicIdentifier].toUInt() != magicVersion)
  {
    qCritical() << "Converted config data is not the right version";
    return false;
  }

  return true;
}
예제 #4
0
bool SaveToJSON(QVariantMap &data, QIODevice &f, const char *magicIdentifier, uint32_t magicVersion)
{
  // marker that this data is valid
  data[magicIdentifier] = magicVersion;

  QJsonDocument doc = QJsonDocument::fromVariant(data);

  if(doc.isEmpty() || doc.isNull())
  {
    qCritical() << "Failed to convert data to JSON document";
    return false;
  }

  QByteArray jsontext = doc.toJson(QJsonDocument::Indented);

  qint64 ret = f.write(jsontext);

  if(ret != jsontext.size())
  {
    qCritical() << "Failed to write JSON data: " << ret << " " << f.errorString();
    return false;
  }

  return true;
}
예제 #5
0
파일: packet.cpp 프로젝트: xobs/sd-capture
static qint64 streamReadData(QIODevice &source, char *data, qint64 bytesTotal) {
	qint64 bytesRead = 0;
	qint64 bytesLeft = bytesTotal;
	int result;

	while (bytesLeft > 0) {
		result = source.read(data, bytesLeft);
		if (result < 0) {
			qDebug() << "Source had error:" << source.errorString();
			return result;
		}

		if (result == 0) {
			if (!source.waitForReadyRead(-1)) {
				qDebug() << "Source is done, closing";
				break;
			}
		}

		if (result > bytesTotal) {
			qDebug() << "Very bad.  Wanted " << bytesTotal << ", but got " << result;
		}
		bytesRead += result;
		data += result;
		if (bytesLeft - result < 0) {
			qDebug() << "That was a really strange read";
		}
		bytesLeft -= result;
	}
	if (bytesLeft < 0) {
		qDebug() << "How could bytesLeft (" << bytesLeft << ") be less than zero?";
	}
	return bytesRead;
}
예제 #6
0
/*!
 * Loads AWS endpoint data from \a device.
 *
 * This function parses AWS endpoint data in XML format.  The XML data is
 * expected to match the same format as the file provided by Amazon at
 * http://aws-sdk-configurations.amazonwebservices.com/endpoints.xml
 *
 * If \e any data has been loaded previously, this function will return
 * immediately with no parsing performed.
 */
void AwsEndpointPrivate::loadEndpointData(QIODevice &device)
{
    QMutexLocker locker(&mutex);
    if (!hosts.empty()) {
        return; // Already loaded.
    }

    // Open the device, if not already open.
    if ((!device.isOpen()) && (!device.open(QIODevice::ReadOnly))) {
        qWarning() << "AwsEndpoint:" << device.errorString();
        return;
    }

    // Parse the data.
    QXmlStreamReader xml(&device);
    loadEndpointData(xml);
}
예제 #7
0
int QuaZIODevicePrivate::doFlush(QString &error)
{
  int flushed = 0;
  while (outBufPos < outBufSize) {
    int more = io->write(outBuf + outBufPos, outBufSize - outBufPos);
    if (more == -1) {
      error = io->errorString();
      return -1;
    }
    if (more == 0)
      break;
    outBufPos += more;
    flushed += more;
  }
  if (outBufPos == outBufSize) {
    outBufPos = outBufSize = 0;
  }
  return flushed;
}
예제 #8
0
QXmlStreamReader::TokenType Parser::Private::blockingReadNext()
{
    QXmlStreamReader::TokenType token = QXmlStreamReader::Invalid;

    forever {
        token = reader.readNext();

        if (reader.error() == QXmlStreamReader::PrematureEndOfDocumentError) {
            if (reader.device()->waitForReadyRead(1000)) {
                // let's try again
                continue;
            } else {
                // device error, e.g. remote host closed connection, or timeout
                // ### we have no way to know if waitForReadyRead() timed out or failed with a real
                //     error, and sensible heuristics based on QIODevice fail.
                //     - error strings are translated and in any case not guaranteed to stay the same,
                //       so we can't use them.
                //     - errorString().isEmpty() does not work because errorString() is
                //       "network timeout error" if the waitFor... timed out.
                //     - isSequential() [for socket] && isOpen() doesn't work because isOpen()
                //       returns true if the remote host closed the connection.
                // ...so we fall back to knowing it might be a QAbstractSocket.

                QIODevice *dev = reader.device();
                QAbstractSocket *sock = qobject_cast<QAbstractSocket *>(dev);

                if (!sock || sock->state() != QAbstractSocket::ConnectedState) {
                    throw ParserException(dev->errorString());
                }
            }
        } else if (reader.hasError()) {
            throw ParserException(reader.errorString()); //TODO add line, column?
            break;
        } else {
            // read a valid next token
            break;
        }
    }

    return token;
}
예제 #9
0
/*!
 * \internal
 */
void QxtWebContent::errorReceived(QAbstractSocket::SocketError)
{
    QIODevice *device = qobject_cast<QIODevice*>(sender());
    if(device)
	setErrorString(device->errorString());
}