示例#1
0
TEST(verifyEquality, DoubleWord)
{
	unsigned char headerData[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0 };
	std::stringstream headerStream(reinterpret_cast<char*>(headerData));

	Binary::DoubleWord someHeaderValue = Binary::Word::readLittleEndian(headerStream);
	
	Binary::DoubleWord expected(0xBBAA);
	try
	{
		verifyEquality(expected, someHeaderValue, "expected");
		CHECK(true);
	}
	catch (const std::exception& exc)
	{
		std::cout << exc.what() << std::endl;
		CHECK(false);
	}

	Binary::DoubleWord notReallyExpected(0xAABB);
	try
	{
		verifyEquality(notReallyExpected, someHeaderValue, "notReallyExpected");
		CHECK(false);
	}
	catch (const std::exception& exc)
	{
		std::cout << exc.what() << std::endl;
		CHECK(true);
	}
}
示例#2
0
void SerialServer::processTransfer(QByteArray& header)
{
    QDataStream headerStream(&header, QIODevice::ReadOnly);
    
    quint16 startWord;
    quint16 packetCount;
    
    headerStream >> startWord;
    headerStream >> packetCount;
    
    if(startWord != SERIAL_START)
        return;
    qWarning("Got start...");
    qWarning("packetcount=%d", packetCount);
    
    QByteArray compressedData;
    
    for(quint16 i = 0;i < packetCount;i++) {
        QByteArray data;
        if(!readPacket(&data))
            return;
        compressedData += data;
    }
    
    
    QByteArray data = qUncompress(compressedData);
    compressedData.clear();
    compressedData.squeeze();
    
    processData(data);
}
示例#3
0
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase)
{
    QScopedPointer<Database> db(new Database());
    m_db = db.data();
    m_device = device;
    m_error = false;
    m_errorStr.clear();
    m_headerEnd = false;
    m_xmlData.clear();
    m_masterSeed.clear();
    m_transformSeed.clear();
    m_encryptionIV.clear();
    m_streamStartBytes.clear();
    m_protectedStreamKey.clear();

    StoreDataStream headerStream(m_device);
    headerStream.open(QIODevice::ReadOnly);
    m_headerStream = &headerStream;

    bool ok;

    quint32 signature1 = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok);
    if (!ok || signature1 != KeePass2::SIGNATURE_1) {
        raiseError(tr("Not a KeePass database."));
        return nullptr;
    }

    quint32 signature2 = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok);
    if (ok && signature2 == KeePass1::SIGNATURE_2) {
        raiseError(tr("The selected file is an old KeePass 1 database (.kdb).\n\n"
                      "You can import it by clicking on Database > 'Import KeePass 1 database'.\n"
                      "This is a one-way migration. You won't be able to open the imported "
                      "database with the old KeePassX 0.4 version."));
        return nullptr;
    }
    else if (!ok || signature2 != KeePass2::SIGNATURE_2) {
        raiseError(tr("Not a KeePass database."));
        return nullptr;
    }

    quint32 version = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok)
            & KeePass2::FILE_VERSION_CRITICAL_MASK;
    quint32 maxVersion = KeePass2::FILE_VERSION & KeePass2::FILE_VERSION_CRITICAL_MASK;
    if (!ok || (version < KeePass2::FILE_VERSION_MIN) || (version > maxVersion)) {
        raiseError(tr("Unsupported KeePass database version."));
        return nullptr;
    }

    while (readHeaderField() && !hasError()) {
    }

    headerStream.close();

    if (hasError()) {
        return nullptr;
    }

    // check if all required headers were present
    if (m_masterSeed.isEmpty() || m_transformSeed.isEmpty() || m_encryptionIV.isEmpty()
            || m_streamStartBytes.isEmpty() || m_protectedStreamKey.isEmpty()
            || m_db->cipher().isNull()) {
        raiseError("missing database headers");
        return nullptr;
    }

    if (!m_db->setKey(key, m_transformSeed, false)) {
        raiseError(tr("Unable to calculate master key"));
        return nullptr;
    }

    CryptoHash hash(CryptoHash::Sha256);
    hash.addData(m_masterSeed);
    hash.addData(m_db->transformedMasterKey());
    QByteArray finalKey = hash.result();

    SymmetricCipherStream cipherStream(m_device, SymmetricCipher::Aes256,
                                       SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
    if (!cipherStream.init(finalKey, m_encryptionIV)) {
        raiseError(cipherStream.errorString());
        return nullptr;
    }
    if (!cipherStream.open(QIODevice::ReadOnly)) {
        raiseError(cipherStream.errorString());
        return nullptr;
    }

    QByteArray realStart = cipherStream.read(32);

    if (realStart != m_streamStartBytes) {
        raiseError(tr("Wrong key or database file is corrupt."));
        return nullptr;
    }

    HashedBlockStream hashedStream(&cipherStream);
    if (!hashedStream.open(QIODevice::ReadOnly)) {
        raiseError(hashedStream.errorString());
        return nullptr;
    }

    QIODevice* xmlDevice;
    QScopedPointer<QtIOCompressor> ioCompressor;

    if (m_db->compressionAlgo() == Database::CompressionNone) {
        xmlDevice = &hashedStream;
    }
    else {
        ioCompressor.reset(new QtIOCompressor(&hashedStream));
        ioCompressor->setStreamFormat(QtIOCompressor::GzipFormat);
        if (!ioCompressor->open(QIODevice::ReadOnly)) {
            raiseError(ioCompressor->errorString());
            return nullptr;
        }
        xmlDevice = ioCompressor.data();
    }

    KeePass2RandomStream randomStream;
    if (!randomStream.init(m_protectedStreamKey)) {
        raiseError(randomStream.errorString());
        return nullptr;
    }

    QScopedPointer<QBuffer> buffer;

    if (m_saveXml) {
        m_xmlData = xmlDevice->readAll();
        buffer.reset(new QBuffer(&m_xmlData));
        buffer->open(QIODevice::ReadOnly);
        xmlDevice = buffer.data();
    }

    KeePass2XmlReader xmlReader;
    xmlReader.readDatabase(xmlDevice, m_db, &randomStream);

    if (xmlReader.hasError()) {
        raiseError(xmlReader.errorString());
        if (keepDatabase) {
            return db.take();
        }
        else {
            return nullptr;
        }
    }

    Q_ASSERT(version < 0x00030001 || !xmlReader.headerHash().isEmpty());

    if (!xmlReader.headerHash().isEmpty()) {
        QByteArray headerHash = CryptoHash::hash(headerStream.storedData(), CryptoHash::Sha256);
        if (headerHash != xmlReader.headerHash()) {
            raiseError("Header doesn't match hash");
            return nullptr;
        }
    }

    return db.take();
}
示例#4
0
void ChatWindowStyle::readStyleFiles()
{
    // load style info
    QString infoPlistFile = d->baseHref + QLatin1String("../Info.plist");
    ChatStylePlistFileReader plistReader(infoPlistFile);
    d->defaultVariantName = plistReader.defaultVariant();
    if (d->defaultVariantName.isEmpty()) {
        // older themes use this
        d->defaultVariantName = plistReader.displayNameForNoVariant();
    }
    if (d->defaultVariantName.isEmpty()) {
        // If name is still empty we use "Normal"
        d->defaultVariantName = i18nc("Normal style variant menu item", "Normal");
    }
    kDebug() << "defaultVariantName = " << d->defaultVariantName;
    d->defaultFontFamily  = plistReader.defaultFontFamily();
    d->defaultFontSize    = plistReader.defaultFontSize();
    d->disableCombineConsecutive = plistReader.disableCombineConsecutive();
    d->messageViewVersion = plistReader.messageViewVersion();

    // specify the files for the identifiers
    QHash<InternalIdentifier, QLatin1String> templateFiles;

    templateFiles.insert(Template, QLatin1String("Template.html"));

    templateFiles.insert(Header, QLatin1String("Header.html"));
    templateFiles.insert(Content, QLatin1String("Content.html"));
    templateFiles.insert(Footer, QLatin1String("Footer.html"));
    templateFiles.insert(Topic, QLatin1String("Topic.html"));

    templateFiles.insert(IncomingContent, QLatin1String("Incoming/Content.html"));
    templateFiles.insert(IncomingNextContent, QLatin1String("Incoming/NextContent.html"));
    templateFiles.insert(OutgoingContent, QLatin1String("Outgoing/Content.html"));
    templateFiles.insert(OutgoingNextContent, QLatin1String("Outgoing/NextContent.html"));
    templateFiles.insert(Status, QLatin1String("Status.html"));

    templateFiles.insert(IncomingHistory, QLatin1String("Incoming/Context.html"));
    templateFiles.insert(IncomingNextHistory, QLatin1String("Incoming/NextContext.html"));
    templateFiles.insert(OutgoingHistory, QLatin1String("Outgoing/Context.html"));
    templateFiles.insert(OutgoingNextHistory, QLatin1String("Outgoing/NextContext.html"));

    templateFiles.insert(ActionIncoming, QLatin1String("Incoming/Action.html"));
    templateFiles.insert(ActionOutgoing, QLatin1String("Outgoing/Action.html"));

    templateFiles.insert(FileTransferIncoming, QLatin1String("FileTransferRequest.html"));
    templateFiles.insert(VoiceClipIncoming, QLatin1String("voiceClipRequest.html"));

    templateFiles.insert(OutgoingStateUnknown, QLatin1String("Outgoing/StateUnknown.html"));
    templateFiles.insert(OutgoingStateSending, QLatin1String("Outgoing/StateSending.html"));
    templateFiles.insert(OutgoingStateSent, QLatin1String("Outgoing/StateSent.html"));
    templateFiles.insert(OutgoingStateError, QLatin1String("Outgoing/StateError.html"));


    // load all files
    QFile fileAccess;
    Q_FOREACH(const QLatin1String &fileName, templateFiles) {
        QString path = d->baseHref + fileName;
        // Load template file
        if (QFile::exists(path)) {
            fileAccess.setFileName(path);
            fileAccess.open(QIODevice::ReadOnly);
            QTextStream headerStream(&fileAccess);
            headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
            QString data = headerStream.readAll();
            if(!data.isEmpty()) {
                //kDebug() << fileName << "was found!";
                setContent( templateFiles.key(fileName), data);
            } else {
                kDebug() << fileName << "was not found!";
            }
            //kDebug() << fileName << content(templateFiles.key(fileName));
            fileAccess.close();
        }
    }
示例#5
0
void completeOutFiles(const IddFileFactoryDataVector& iddFiles,
                      GenerateIddFactoryOutFiles& outFiles) {


  std::vector<std::pair<std::string, std::string>> filetypes{ {"UserCustom", ""}, {"WholeFactory", ""} };
  for (const IddFileFactoryData& idd : iddFiles) {
    filetypes.emplace_back(idd.fileName(), "");
  }


  std::stringstream tempSS;
  writeDomain(tempSS, "IddFileType", filetypes, false);

  outFiles.iddEnumsHxx.tempFile
    << std::endl
    << "/** \\class IddFileType */" << std::endl
    << tempSS.str();
  tempSS.str("");

  std::vector<std::pair<std::string, std::string>> objtypes{ {"Catchall", ""}, {"UserCustom", ""} };

  // loop through each IDD file
  for (const IddFileFactoryData& idd : iddFiles) {
    // write out an IddObjectType enum value for each object in the IDD file
    for (const StringPair& objectName : idd.objectNames()) {
      objtypes.emplace_back(objectName.first, objectName.second);
    }
  }

  objtypes.emplace_back("CommentOnly", "");
  writeDomain(tempSS, "IddObjectType", objtypes, false);

  outFiles.iddEnumsHxx.tempFile
    << std::endl
    << "/** \\class IddObjectType */" << std::endl
    << tempSS.str()
    << std::endl
    << "}\n"
    << "#endif // UTILITIES_IDD_IDDENUMS_HXX" << std::endl;
  tempSS.str("");

  // complete and close IddFieldEnums.hxx
  outFiles.iddFieldEnumsHxx.tempFile
    << std::endl
    << "#endif // UTILITIES_IDD_IDDFIELDENUMS_HXX" << std::endl;

  // complete and close IddFieldEnums.ixx
  outFiles.iddFieldEnumsIxx.tempFile
    << std::endl
    << "#endif // UTILITIES_IDD_IDDFIELDENUMS_IXX" << std::endl;

  // complete and close IddFactory.cxx

  // create function for CommentOnly IddObject
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "IddObject createCommentOnlyIddObject() {" << std::endl
    << std::endl
    << "  static IddObject object;" << std::endl
    << std::endl
    << "  if (object.type() == IddObjectType::Catchall) {" << std::endl
    << "    std::stringstream ss;" << std::endl
    << "    ss << \"CommentOnly; ! Autogenerated comment only object.\" << std::endl;" << std::endl
    << std::endl
    << "    IddObjectType objType(IddObjectType::CommentOnly);" << std::endl
    << "    OptionalIddObject oObj = IddObject::load(\"CommentOnly\"," << std::endl
    << "                                             \"\"," << std::endl
    << "                                             ss.str()," << std::endl
    << "                                             objType);" << std::endl
    << "    OS_ASSERT(oObj);" << std::endl
    << "    object = *oObj;" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  return object;" << std::endl
    << "}" << std::endl;

  // constructor
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "IddFactorySingleton::IddFactorySingleton() {" << std::endl
    << std::endl
    << "  // initialize callback map" << std::endl
    << std::endl;
  // register create functions in the callback map
  // Catchall
  outFiles.iddFactoryCxx.tempFile
    << "  m_callbackMap.insert(IddObjectCallbackMap::value_type(IddObjectType::Catchall,"
    << "createCatchallIddObject));" << std::endl;
  // parsed objects
  for (const IddFileFactoryData& idd : iddFiles) {
    outFiles.iddFactoryCxx.tempFile
      << "  register" << idd.fileName() << "ObjectsInCallbackMap();" << std::endl;
  }
  // CommentOnly
  outFiles.iddFactoryCxx.tempFile
    << "  m_callbackMap.insert(IddObjectCallbackMap::value_type(IddObjectType::CommentOnly,"
    << "createCommentOnlyIddObject));" << std::endl
    << std::endl
    << "  // instantiate IddObjectType to IddFileType multimap" << std::endl
    << std::endl;
  // register IddObjectTypes with IddFileTypes
  for (const IddFileFactoryData& idd : iddFiles) {
    std::string fileName = idd.fileName();

    // register local objects
    for (const StringPair& objectName : idd.objectNames()) {
      outFiles.iddFactoryCxx.tempFile
        << "  m_sourceFileMap.insert(IddObjectSourceFileMap::value_type(IddObjectType::"
        << objectName.first << ",IddFileType::" << fileName << "));" << std::endl;
    }

    // register imported objects
    for (unsigned i = 0, ni = idd.numIncludedFiles(); i < ni; ++i) {
      /* Included files are identified by name. Optionally, there may be a list of removed objects
         that should not be inherited by the composite file. */
      IddFileFactoryData::FileNameRemovedObjectsPair includedFileData = idd.includedFile(i);
      // Get the included file by name. Will throw if there is no match.
      IddFileFactoryData includedFile = getFile(includedFileData.first,iddFiles);

      std::vector<std::string> excludedObjects;
      for (const StringPair& objectName : includedFile.objectNames()) {

        // If objectName is in list of removed objects, do not add it to m_sourceFileMap,
        if (std::find(includedFileData.second.begin(),includedFileData.second.end(),objectName.first)
            != includedFileData.second.end()) {
          // and keep its name in case we need to write a warning.
          excludedObjects.push_back(objectName.first);
          continue;
        }

        // objectName is not to be removed, so add it to the composite file.
        outFiles.iddFactoryCxx.tempFile
          << "  m_sourceFileMap.insert(IddObjectSourceFileMap::value_type(IddObjectType::"
          << objectName.first << ",IddFileType::" << fileName << "));" << std::endl;

      } // foreach

      // Write warning if we did not encounter all of the objects that were to be removed.
      if (excludedObjects.size() != includedFileData.second.size()) {
        std::cout << "Warning: Did not encounter all 'objects to remove' while including file " << std::endl
                  << "'" << includedFileData.first << "' in file '" << fileName << "'." << std::endl
                  << "\\remove-object Tags      Objects Actually Removed " << std::endl
                  << "------------------------- -------------------------" << std::endl;
        for (int x = 0, y = 0, nx = includedFileData.second.size(), ny = excludedObjects.size();
             (x < nx) || (y < ny); ++x, ++y) {
          if (x < nx) { std::cout << std::setw(25) << includedFileData.second[x] << " "; }
          else { std::cout << std::setw(25) << " " << " "; }
          if (y < ny) { std::cout << std::setw(25)<< excludedObjects[y] << std::endl; }
          else { std::cout << std::setw(25) << " " << std::endl; }
        } // for
      } // if

    } // for

    // Register CommentOnly object for all files.
    outFiles.iddFactoryCxx.tempFile
        << "  m_sourceFileMap.insert(IddObjectSourceFileMap::value_type(IddObjectType::CommentOnly"
        << ",IddFileType::" << fileName << "));" << std::endl;
  }
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "}" << std::endl;

  // version and header getters
  outFiles.iddFactoryCxx.tempFile
      << std::endl
      << "std::string IddFactorySingleton::getVersion(IddFileType fileType) const {" << std::endl
      << "  std::string result;" << std::endl
      << std::endl
      << "  switch (fileType.value()) {" << std::endl;
  for (const IddFileFactoryData& idd : iddFiles) {
    outFiles.iddFactoryCxx.tempFile
        << "    case IddFileType::" << idd.fileName() << " :" << std::endl
        << "      result = \"" << idd.version() << "\";" << std::endl
        << "      break;" << std::endl;
  }
  outFiles.iddFactoryCxx.tempFile
      << "    default :" << std::endl
      << "      LOG_AND_THROW(\"No version to return for IddFileType \" << fileType.valueDescription() << \".\");" << std::endl
      << "  } // switch" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::string IddFactorySingleton::getHeader(IddFileType fileType) const {" << std::endl
      << "  std::stringstream result;" << std::endl
      << "  switch (fileType.value()) {" << std::endl;
  for (const IddFileFactoryData& idd : iddFiles) {
    outFiles.iddFactoryCxx.tempFile
        << "    case IddFileType::" << idd.fileName() << " :" << std::endl
        << "      result";
    std::stringstream headerStream(idd.header());
    std::string line;
    while (std::getline(headerStream,line)) {
      outFiles.iddFactoryCxx.tempFile
          << std::endl
          << "        << \"" << line << "\" << std::endl";
    }
    // print information about included files
    for (unsigned i = 0, ni = idd.numIncludedFiles(); i < ni; ++i) {
      IddFileFactoryData::FileNameRemovedObjectsPair includedFileData = idd.includedFile(i);
      IddFileFactoryData includedFile = getFile(includedFileData.first,iddFiles);
      outFiles.iddFactoryCxx.tempFile
        << std::endl
        << "        << \"!\\n\"" << std::endl
        << "        << \"! **************************************************************************\\n\"" << std::endl
        << "        << \"! Includes File: '" << includedFile.fileName() << "'\\n\"" << std::endl
        << "        << \"!\\n\"" << std::endl
        << "        << \"! Contains all objects from " << includedFile.fileName() << " IDD_Version " << includedFile.version() << ", except: \\n\"" << std::endl;
      for (const std::string& objectName : includedFileData.second) {
        outFiles.iddFactoryCxx.tempFile
          << "        << \"!   " << objectName << "\\n\"" << std::endl;
      }
      outFiles.iddFactoryCxx.tempFile
        << "        << \"! **************************************************************************\\n\"";
    }
    outFiles.iddFactoryCxx.tempFile
      << ";" << std::endl
      << std::endl
      << "      break;" << std::endl;
  }
 outFiles.iddFactoryCxx.tempFile
     << "    default :" << std::endl
     << "      LOG_AND_THROW(\"No header to return for IddFileType \" << fileType.valueDescription() << \".\");" << std::endl
     << "  } // switch" << std::endl
     << "  return result.str();" << std::endl
     << "}" << std::endl;

  // object getters
  outFiles.iddFactoryCxx.tempFile
      << std::endl
      << "std::vector<IddObject> IddFactorySingleton::objects() const {" << std::endl
      << "  IddObjectVector result;" << std::endl
      << std::endl
      << "  for (IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
      << "       itEnd = m_callbackMap.end(); it != itEnd; ++it) {" << std::endl
      << "    QMutexLocker l(&m_callbackmutex);" << std::endl
      << "    result.push_back(it->second());" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<IddObject> IddFactorySingleton::getObjects(IddFileType fileType) const {" << std::endl
      << "  IddObjectVector result;" << std::endl
      << std::endl
      << "  for(IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
      << "      itend = m_callbackMap.end(); it != itend; ++it) {" << std::endl
      << "    if (isInFile(it->first,fileType)) { " << std::endl
      << "      // This lock is necessary to protect construction of the statics used in the callbacks " << std::endl
      << "      QMutexLocker l(&m_callbackmutex);" << std::endl
      << "      result.push_back(it->second()); " << std::endl
      << "    }" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<std::string> IddFactorySingleton::groups() const {" << std::endl
      << "  StringSet result;" << std::endl
      << "  for (const IddObject& object : objects()) {" << std::endl
      << "    result.insert(object.group());" << std::endl
      << "  }" << std::endl
      << "  return StringVector(result.begin(),result.end());" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<std::string> IddFactorySingleton::getGroups(IddFileType fileType) const {" << std::endl
      << "  StringSet result;" << std::endl
      << "  for (const IddObject& object : getObjects(fileType)) {" << std::endl
      << "    result.insert(object.group());" << std::endl
      << "  }" << std::endl
      << "  return StringVector(result.begin(),result.end());" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<IddObject> IddFactorySingleton::getObjectsInGroup(const std::string& group) const {" << std::endl
      << "  IddObjectVector result;" << std::endl
      << "  for (const IddObject& object : objects()) {" << std::endl
      << "    if (istringEqual(object.group(),group)) {" << std::endl
      << "      result.push_back(object);" << std::endl
      << "    }" << std::endl
      << "  }" << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<IddObject> IddFactorySingleton::getObjectsInGroup(const std::string& group, IddFileType fileType) const {" << std::endl
      << "  IddObjectVector result;" << std::endl
      << "  for (const IddObject& object : getObjects(fileType)) {" << std::endl
      << "    if (istringEqual(object.group(),group)) {" << std::endl
      << "      result.push_back(object);" << std::endl
      << "    }" << std::endl
      << "  }" << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<IddObject> IddFactorySingleton::getObjects(const boost::regex& objectRegex) const {" << std::endl
      << "  IddObjectVector result;" << std::endl
      << std::endl
      << "  for (int value : IddObjectType::getValues()) {" << std::endl
      << "    IddObjectType candidate(value);" << std::endl
      << "    if (boost::regex_match(candidate.valueName(),objectRegex) || " << std::endl
      << "        boost::regex_match(candidate.valueDescription(),objectRegex))" << std::endl
      << "    {" << std::endl
      << "      if (OptionalIddObject object = getObject(candidate)) {" << std::endl
      << "        result.push_back(*object);" << std::endl
      << "      }" << std::endl
      << "    }" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "std::vector<IddObject> IddFactorySingleton::getObjects(const boost::regex& objectRegex," << std::endl
      << "                                                       IddFileType fileType) const " << std::endl
      << "{" << std::endl
      << "  IddObjectVector result;" << std::endl
      << std::endl
      << "  for (int value : IddObjectType::getValues()) {" << std::endl
      << "    IddObjectType candidate(value);" << std::endl
      << "    if (isInFile(candidate,fileType) && " << std::endl
      << "        (boost::regex_match(candidate.valueName(),objectRegex) || " << std::endl
      << "         boost::regex_match(candidate.valueDescription(),objectRegex)))" << std::endl
      << "    {" << std::endl
      << "      if (OptionalIddObject object = getObject(candidate)) {" << std::endl
      << "        result.push_back(*object);" << std::endl
      << "      }" << std::endl
      << "    }" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "IddObject IddFactorySingleton::getVersionObject(IddFileType fileType) const {" << std::endl
      << "  if (fileType == IddFileType::EnergyPlus) {" << std::endl
      << "    return getObject(IddObjectType(IddObjectType::Version)).get();" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  if (fileType == IddFileType::OpenStudio) {" << std::endl
      << "    return getObject(IddObjectType(IddObjectType::OS_Version)).get();" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  LOG_AND_THROW(\"Unable to identify unique version object for IddFileType \" << fileType.valueName() << \".\");" << std::endl
      << "  return IddObject();" << std::endl
      << "}" << std::endl
      << std::endl
      << "boost::optional<IddObject> IddFactorySingleton::getObject(const std::string& objectName) const" << std::endl
      << "{" << std::endl
      << "  OptionalIddObject result;" << std::endl
      << std::endl
      << "  // let IddObjectType OPENSTUDIO_ENUM handle the string processing" << std::endl
      << "  try {" << std::endl
      << "    IddObjectType objectType(objectName);" << std::endl
      << "    result = getObject(objectType);" << std::endl
      << "  }" << std::endl
      << "  catch (...) {}" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl
      << std::endl
      << "boost::optional<IddObject> IddFactorySingleton::getObject(IddObjectType objectType) const" << std::endl
      << "{" << std::endl
      << "  OptionalIddObject result;" << std::endl
      << std::endl
      << "  IddObjectCallbackMap::const_iterator lookupPair;" << std::endl
      << "  lookupPair = m_callbackMap.find(objectType);" << std::endl
      << "  if (lookupPair != m_callbackMap.end()) { " << std::endl
      << "    QMutexLocker l(&m_callbackmutex);" << std::endl
      << "    result = lookupPair->second(); " << std::endl
      << "  }" << std::endl
      << "  else { " << std::endl
      << "    OS_ASSERT(objectType == IddObjectType::UserCustom); " << std::endl
      << "    LOG(Info,\"UserCustom objects are not available through the IddFactory. Please query your IddFile by IddObject.name().\");" << std::endl
      << "  }" << std::endl
      << std::endl
      << "  return result;" << std::endl
      << "}" << std::endl;

  // required, unique, and required or unique objects
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "std::vector<IddObject> IddFactorySingleton::requiredObjects() const {" << std::endl
    << std::endl
    << "  IddObjectVector result;" << std::endl
    << std::endl
    << "  for (IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
    << "    itEnd = m_callbackMap.end(); it != itEnd; ++it) {" << std::endl
    << "    // This lock is necessary to protect construction of the statics used in the callbacks " << std::endl
    << "    QMutexLocker l(&m_callbackmutex);" << std::endl
    << "    IddObject candidate = it->second();" << std::endl
    << "    l.unlock(); " << std::endl
    << "    if (candidate.properties().required) {" << std::endl
    << "      result.push_back(candidate);" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  return result;" << std::endl
    << "}" << std::endl
    << std::endl
    << "std::vector<IddObject> IddFactorySingleton::getRequiredObjects(IddFileType fileType) const {" << std::endl
    << std::endl
    << "  IddObjectVector result; " << std::endl
    << std::endl
    << "  for(IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
    << "      itEnd = m_callbackMap.end(); it != itEnd; ++it) {" << std::endl
    << "    if (isInFile(it->first,fileType)) {" << std::endl
    << "      OptionalIddObject candidate = getObject(it->first);" << std::endl
    << "      if (candidate->properties().required) {" << std::endl
    << "        result.push_back(*candidate);" << std::endl
    << "      }" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  return result;" << std::endl
    << "}" << std::endl
    << std::endl
    << "std::vector<IddObject> IddFactorySingleton::uniqueObjects() const {" << std::endl
    << std::endl
    << "  IddObjectVector result;" << std::endl
    << std::endl
    << "  for (IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
    << "    itEnd = m_callbackMap.end(); it != itEnd; ++it) {" << std::endl
    << "    QMutexLocker l(&m_callbackmutex);" << std::endl
    << "    IddObject candidate = it->second();" << std::endl
    << "    l.unlock(); " << std::endl
    << "    if (candidate.properties().unique) {" << std::endl
    << "      result.push_back(candidate);" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  return result;" << std::endl
    << "}" << std::endl
    << std::endl
    << "std::vector<IddObject> IddFactorySingleton::getUniqueObjects(IddFileType fileType) const {" << std::endl
    << std::endl
    << "  IddObjectVector result; " << std::endl
    << std::endl
    << "   for(IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
    << "      itEnd = m_callbackMap.end(); it != itEnd; ++it) {" << std::endl
    << "    if (isInFile(it->first,fileType)) {" << std::endl
    << "      OptionalIddObject candidate = getObject(it->first);" << std::endl
    << "      if (candidate->properties().unique) {" << std::endl
    << "        result.push_back(*candidate);" << std::endl
    << "      }" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  return result;" << std::endl
    << std::endl
    << "}" << std::endl;

  // iddFile getters
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "IddFile IddFactorySingleton::getIddFile(IddFileType fileType) const {" << std::endl
    << "  IddFile result;" << std::endl
    << std::endl
    << "  if (fileType == IddFileType::UserCustom) {" << std::endl
    << "    return result; " << std::endl
    << "  }" << std::endl
    << std::endl
    << "  // Add the IddObjects." << std::endl
    << "  for(IddObjectCallbackMap::const_iterator it = m_callbackMap.begin()," << std::endl
    << "      itend = m_callbackMap.end(); it != itend; ++it) {" << std::endl
    << "    if (isInFile(it->first,fileType)) {" << std::endl
    << "      // This lock is necessary to protect construction of the statics used in the callbacks " << std::endl
    << "      QMutexLocker l(&m_callbackmutex);" << std::endl
    << "      result.addObject(it->second());" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  // Set the file version and header." << std::endl
    << "  try {" << std::endl
    << "    result.setVersion(getVersion(fileType));" << std::endl
    << "    result.setHeader(getHeader(fileType));" << std::endl
    << "  }" << std::endl
    << "  catch (...) {}" << std::endl
    << std::endl
    << "  return result;" << std::endl
    << "}" << std::endl
    << std::endl
    << "boost::optional<IddFile> IddFactorySingleton::getIddFile(IddFileType fileType, const VersionString& version) const {" << std::endl
    << "  OptionalIddFile result;" << std::endl
    << std::endl
    << "  if (fileType == IddFileType::UserCustom) {" << std::endl
    << "    return result; " << std::endl
    << "  }" << std::endl
    << std::endl
    << "  if (fileType == IddFileType::WholeFactory) {" << std::endl
    << "    LOG(Warn,\"Cannot return the WholeFactory IddFile by version.\");" << std::endl
    << "    return result;" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  if (fileType == IddFileType::EnergyPlus) {" << std::endl
    << "    LOG(Warn,\"At this time, OpenStudio cannot return EnergyPlus IDD files by version.\");" << std::endl
    << "    return result;" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  VersionString currentVersion(openStudioVersion());" << std::endl
    << "  OS_ASSERT(fileType == IddFileType::OpenStudio);" << std::endl
    << "  if (version == currentVersion) {" << std::endl
    << "    return getIddFile(fileType);" << std::endl
    << "  }" << std::endl
    << "  else {" << std::endl
    << "    std::map<VersionString, IddFile>::const_iterator it = m_osIddFiles.find(version);" << std::endl
    << "    if (it != m_osIddFiles.end()) {" << std::endl
    << "      return it->second;" << std::endl
    << "    }" << std::endl
    << "    std::string iddPath = \":/idd/versions\";" << std::endl
    << "    std::stringstream folderString;" << std::endl
    << "    folderString << version.major() << \"_\" << version.minor() << \"_\" << version.patch().get();" << std::endl
    << "    iddPath += \"/\" + folderString.str() + \"/OpenStudio.idd\";" << std::endl
    << "    if (::openstudio::embedded_files::hasFile(iddPath) && (version < currentVersion)) {" << std::endl
    << "      std::stringstream ss;" << std::endl
    << "      ss << ::openstudio::embedded_files::getFileAsString(iddPath);" << std::endl
    << "      result = IddFile::load(ss);" << std::endl
    << "    }" << std::endl
    << "    if (result) {" << std::endl
    << "      QMutexLocker l(&m_callbackmutex);" << std::endl
    << "      m_osIddFiles[version] = *result;" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << "  return result;" << std::endl
    << "}" << std::endl;

  // query whether object is in file
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "bool IddFactorySingleton::isInFile(IddObjectType objectType, IddFileType fileType) const {" << std::endl
    << "  typedef IddObjectSourceFileMap::const_iterator const_iterator;" << std::endl
    << "  std::pair<const_iterator,const_iterator> range;" << std::endl
    << "  range = m_sourceFileMap.equal_range(objectType);" << std::endl
    << "  for (const_iterator it = range.first; it != range.second; ++it) {" << std::endl
    << "    if ((it->second == fileType) || (fileType == IddFileType::WholeFactory)) {" << std::endl
    << "      return true;" << std::endl
    << "    }" << std::endl
    << "  }" << std::endl
    << std::endl
    << "  return false;" << std::endl
    << "}" << std::endl;

  // Implementation for IddObjectType and IddFileType
  writeBuildStringVec(outFiles.iddFactoryCxx.tempFile, "IddObjectType", objtypes, false);
  writeBuildStringVec(outFiles.iddFactoryCxx.tempFile, "IddFileType", filetypes, false);

  // close out file
  outFiles.iddFactoryCxx.tempFile
    << std::endl
    << "} // openstudio" << std::endl;

  // close out other IddFactory cxx files
  for (std::shared_ptr<IddFactoryOutFile>& cxxFile : outFiles.iddFactoryIddFileCxxs) {
    cxxFile->tempFile
      << std::endl
      << "} // openstudio" << std::endl;
  }

  outFiles.finalize();

  std::cout << "IddFactory files generated." << std::endl;
}
示例#6
0
http::request* http::request::parse (std::string str)
{
	request* req = new request();
	req->raw_message = str;
	
	std::stringstream ss(str);
	std::string line;
	
	/**
	 * parsing the request line, e.g. "GET /index.html?name=hey HTTP/1.1"
	 */
	utils::ioutils::getline(ss, line);
	std::stringstream lineStream(line);
	std::getline (lineStream, req->request_method, ' ');
	std::getline (lineStream, req->request_uri, ' ');
	
	req->file_name = req->request_uri;
	
	unsigned pos;
	if (req->request_uri.length() > 1 && (pos = req->request_uri.find('?')) != std::string::npos && pos <= req->request_uri.length())
	{
		// name=hey
		req->request_query_string = req->request_uri.substr (pos + 1);
		// index.html
		req->file_name = req->request_uri.substr(0, pos);
		req->query_string_map = parse_query_string (req->request_query_string);
	}
	
	utils::ioutils::getline (lineStream, req->server_protocol);
	
	/**
	 * parsing additional request headers. Ends when no more lines can be fetched (i.e. an "\r\n" line).
	 */
	while (utils::ioutils::getline(ss, line) && line.size() > 0)
	{
		std::stringstream headerStream(line);
		std::string key, value;
		std::getline (headerStream, key, ':');
		std::getline (headerStream, value);
		
		if ( value[0] == ' ')
			value.erase (0, 1);
		
		req->header(key, value);
	}
	
	/**
	 * the next line should give us the query string, if method equals POST
	 */
	utils::ioutils::getline(ss, line);
	if (req->method() == "POST" && line.length() > 0)
	{
		req->request_query_string = line;
		req->query_string_map = parse_query_string (line);
	}
	
	std::string host = req->header("Host");
	
	if (host == "")
		return NULL;
	
	req->request_url = std::string("http://");
	req->request_url.append (host);
	
	return req;
}
void ChatWindowStyle::readStyleFiles()
{
	QString headerFile = d->baseHref + QString("Header.html");
	QString footerFile = d->baseHref + QString("Footer.html");
	QString incomingFile = d->baseHref + QString("Incoming/Content.html");
	QString nextIncomingFile = d->baseHref + QString("Incoming/NextContent.html");
	QString outgoingFile = d->baseHref + QString("Outgoing/Content.html");
	QString nextOutgoingFile = d->baseHref + QString("Outgoing/NextContent.html");
	QString statusFile = d->baseHref + QString("Status.html");
	QString actionIncomingFile = d->baseHref + QString("Incoming/Action.html");
	QString actionOutgoingFile = d->baseHref + QString("Outgoing/Action.html");
	QString fileTransferIncomingFile = d->baseHref + QString("Incoming/FileTransferRequest.html");
	QString voiceClipIncomingFile = d->baseHref + QString("Incoming/voiceClipRequest.html");
	QString outgoingStateUnknownFile = d->baseHref + QString("Outgoing/StateUnknown.html");
	QString outgoingStateSendingFile = d->baseHref + QString("Outgoing/StateSending.html");
	QString outgoingStateSentFile = d->baseHref + QString("Outgoing/StateSent.html");
	QString outgoingStateErrorFile = d->baseHref + QString("Outgoing/StateError.html");

	QFile fileAccess;
	// First load header file.
	if( QFile::exists(headerFile) )
	{
		fileAccess.setFileName(headerFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->headerHtml = headerStream.readAll();
		kDebug(14000) << "Header HTML: " << d->headerHtml;
		fileAccess.close();
	}
	// Load Footer file
	if( QFile::exists(footerFile) )
	{
		fileAccess.setFileName(footerFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->footerHtml = headerStream.readAll();
		kDebug(14000) << "Footer HTML: " << d->footerHtml;
		fileAccess.close();
	}
	// Load incoming file
	if( QFile::exists(incomingFile) )
	{
		fileAccess.setFileName(incomingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->incomingHtml = headerStream.readAll();
		kDebug(14000) << "Incoming HTML: " << d->incomingHtml;
		fileAccess.close();
	}
	// Load next Incoming file
	if( QFile::exists(nextIncomingFile) )
	{
		fileAccess.setFileName(nextIncomingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->nextIncomingHtml = headerStream.readAll();
		kDebug(14000) << "NextIncoming HTML: " << d->nextIncomingHtml;
		fileAccess.close();
	}
	// Load outgoing file
	if( QFile::exists(outgoingFile) )
	{
		fileAccess.setFileName(outgoingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->outgoingHtml = headerStream.readAll();
		kDebug(14000) << "Outgoing HTML: " << d->outgoingHtml;
		fileAccess.close();
	}
	// Load next outgoing file
	if( QFile::exists(nextOutgoingFile) )
	{
		fileAccess.setFileName(nextOutgoingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->nextOutgoingHtml = headerStream.readAll();
		kDebug(14000) << "NextOutgoing HTML: " << d->nextOutgoingHtml;
		fileAccess.close();
	}
	// Load status file
	if( QFile::exists(statusFile) )
	{
		fileAccess.setFileName(statusFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->statusHtml = headerStream.readAll();
		kDebug(14000) << "Status HTML: " << d->statusHtml;
		fileAccess.close();
	}
	
	// Load Action Incoming file
	if( QFile::exists(actionIncomingFile) )
	{
		fileAccess.setFileName(actionIncomingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->actionIncomingHtml = headerStream.readAll();
		kDebug(14000) << "ActionIncoming HTML: " << d->actionIncomingHtml;
		fileAccess.close();
	}
	// Load Action Outgoing file
	if( QFile::exists(actionOutgoingFile) )
	{
		fileAccess.setFileName(actionOutgoingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->actionOutgoingHtml = headerStream.readAll();
		kDebug(14000) << "ActionOutgoing HTML: " << d->actionOutgoingHtml;
		fileAccess.close();
	}
	// Load FileTransfer Incoming file
	if( QFile::exists(fileTransferIncomingFile) )
	{
		fileAccess.setFileName(fileTransferIncomingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->fileTransferIncomingHtml = headerStream.readAll();
		kDebug(14000) << "fileTransferIncoming HTML: " << d->fileTransferIncomingHtml;
		fileAccess.close();
	}
	
	if ( d->fileTransferIncomingHtml.isEmpty() ||
	     ( !d->fileTransferIncomingHtml.contains( "saveFileHandlerId" ) &&
	       !d->fileTransferIncomingHtml.contains( "saveFileAsHandlerId" ) ) )
	{	// Create default html
		d->fileTransferIncomingHtml = d->incomingHtml;
		QString message = QString( "%message%\n"
		                           "<div>\n"
		                           " <div style=\"width:37px; float:left;\">\n"
		                           "  <img src=\"%fileIconPath%\" style=\"width:32px; height:32px; vertical-align:middle;\" />\n"
		                           " </div>\n"
		                           " <div>\n"
		                           "  <span><b>%fileName%</b> (%fileSize%)</span><br>\n"
		                           "  <span>\n"
		                           "   <input id=\"%saveFileAsHandlerId%\" type=\"button\" value=\"%1\">\n"
		                           "   <input id=\"%cancelRequestHandlerId%\" type=\"button\" value=\"%2\">\n"
		                           "  </span>\n"
		                           " </div>\n"
		                           "</div>" )
		                           .arg( i18n( "Download" ), i18n( "Cancel" ) );
		d->fileTransferIncomingHtml.replace( QLatin1String("%message%"), message );
	}

	// Load VoiceClip Incoming file
	if( QFile::exists(voiceClipIncomingFile) )
	{
		fileAccess.setFileName(voiceClipIncomingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->voiceClipIncomingHtml = headerStream.readAll();
		kDebug(14000) << "voiceClipIncoming HTML: " << d->voiceClipIncomingHtml;
		fileAccess.close();
	}

	if ( d->voiceClipIncomingHtml.isEmpty() ||
	     ( !d->voiceClipIncomingHtml.contains( "playVoiceHandlerId" ) &&
	       !d->voiceClipIncomingHtml.contains( "saveAsVoiceHandlerId" ) ) )
	{	// Create default html
		d->voiceClipIncomingHtml = d->incomingHtml;
		QString message = QString( "%message%\n"
		                           "<div>\n"
		                           " <div style=\"width:37px; float:left;\">\n"
		                           "  <img src=\"%fileIconPath%\" style=\"width:32px; height:32px; vertical-align:middle;\" />\n"
		                           " </div>\n"
		                           " <div>\n"
		                           "  <span>\n"
		                           "   <input id=\"%playVoiceHandlerId%\" type=\"button\" value=\"%1\">\n"
		                           "   <input id=\"%saveAsVoiceHandlerId%\" type=\"button\" value=\"%2\">\n"
		                           "  </span>\n"
		                           " </div>\n"
		                           "</div>" )
		                           .arg( i18n( "Play" ), i18n( "Save as" ) );
		d->voiceClipIncomingHtml.replace( QLatin1String("%message%"), message );
	}

	// Load outgoing file
	if( QFile::exists(outgoingStateUnknownFile) )
	{
		fileAccess.setFileName(outgoingStateUnknownFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->outgoingStateUnknownHtml = headerStream.readAll();
		kDebug(14000) << "Outgoing StateUnknown HTML: " << d->outgoingStateUnknownHtml;
		fileAccess.close();
	}

	if( QFile::exists(outgoingStateSendingFile) )
	{
		fileAccess.setFileName(outgoingStateSendingFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->outgoingStateSendingHtml = headerStream.readAll();
		kDebug(14000) << "Outgoing StateSending HTML: " << d->outgoingStateSendingHtml;
		fileAccess.close();
	}

	if( QFile::exists(outgoingStateSentFile) )
	{
		fileAccess.setFileName(outgoingStateSentFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->outgoingStateSentHtml = headerStream.readAll();
		kDebug(14000) << "Outgoing StateSent HTML: " << d->outgoingStateSentHtml;
		fileAccess.close();
	}

	if( QFile::exists(outgoingStateErrorFile) )
	{
		fileAccess.setFileName(outgoingStateErrorFile);
		fileAccess.open(QIODevice::ReadOnly);
		QTextStream headerStream(&fileAccess);
		headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
		d->outgoingStateErrorHtml = headerStream.readAll();
		kDebug(14000) << "Outgoing StateError HTML: " << d->outgoingStateErrorHtml;
		fileAccess.close();
	}
}
示例#8
0
void indexTranscriptsSA(ParserT* parser,
            		std::string& outputDir,
                        std::mutex& iomutex) {
    // Seed with a real random value, if available
    std::random_device rd;

    // Create a random uniform distribution
    std::default_random_engine eng(rd());

    std::uniform_int_distribution<> dis(0, 3);

    uint32_t n{0};
    uint32_t k = rapmap::utils::my_mer::k();
    std::vector<std::string> transcriptNames;
    std::vector<uint32_t> transcriptStarts;
    //std::vector<uint32_t> positionIDs;
    constexpr char bases[] = {'A', 'C', 'G', 'T'};
    uint32_t polyAClipLength{10};
    uint32_t numPolyAsClipped{0};
    std::string polyA(polyAClipLength, 'A');

    using TranscriptList = std::vector<uint32_t>;
    using eager_iterator = MerMapT::array::eager_iterator;
    using KmerBinT = uint64_t;

    size_t numDistinctKmers{0};
    size_t numKmers{0};
    size_t currIndex{0};
    std::cerr << "\n[Step 1 of 4] : counting k-mers\n";

    //rsdic::RSDicBuilder rsdb;
    std::vector<uint32_t> onePos; // Positions in the bit array where we should write a '1'
    fmt::MemoryWriter txpSeqStream;
    {
        ScopedTimer timer;
        while(true) {
            typename ParserT::job j(*parser);
            if(j.is_empty()) break;
            for(size_t i = 0; i < j->nb_filled; ++i) { // For each sequence
                std::string& readStr = j->data[i].seq;
                readStr.erase(std::remove_if(readStr.begin(), readStr.end(),
                               [](const char a) -> bool {
                                    return !(isprint(a));
                                }), readStr.end());
                // Do Kallisto-esque clipping of polyA tails
                if (readStr.size() > polyAClipLength and
                        readStr.substr(readStr.length() - polyAClipLength) == polyA) {

                    auto newEndPos = readStr.find_last_not_of("Aa");
                    // If it was all As
                    if (newEndPos == std::string::npos) {
                        readStr.resize(0);
                    } else {
                        readStr.resize(newEndPos + 1);
                    }
                    ++numPolyAsClipped;
                }

                uint32_t readLen  = readStr.size();
                uint32_t txpIndex = n++;

		// The name of the current transcript
                transcriptNames.push_back(j->data[i].header);
		// The position at which this transcript starts
                transcriptStarts.push_back(currIndex);


                bool firstBase{true};
                rapmap::utils::my_mer mer;
                mer.polyT();
                for (size_t b = 0; b < readLen; ++b) {
                    readStr[b] = ::toupper(readStr[b]);
                    int c = jellyfish::mer_dna::code(readStr[b]);
                    // Replace non-ACGT bases with pseudo-random bases
                    if (jellyfish::mer_dna::not_dna(c)) {
                        char rbase = bases[dis(eng)];
                        c = jellyfish::mer_dna::code(rbase);
                        readStr[b] = rbase;
                    }
                    //positionIDs.push_back(txpIndex);
		    //rsdb.PushBack(0);
                }
                txpSeqStream << readStr;
                txpSeqStream << '$';
                //positionIDs.push_back(txpIndex);
		//rsdb.PushBack(1);
                currIndex += readLen + 1;
		onePos.push_back(currIndex - 1);
            }
            if (n % 10000 == 0) {
                std::cerr << "\r\rcounted k-mers for " << n << " transcripts";
            }
        }
    }
    std::cerr << "\n";

    std::cerr << "Clipped poly-A tails from " << numPolyAsClipped << " transcripts\n";

    // Put the concatenated text in a string
    std::string concatText = txpSeqStream.str();
    // And clear the stream
    txpSeqStream.clear();


    // Make our dense bit arrray
    BIT_ARRAY* bitArray = bit_array_create(concatText.length());
    for (auto p : onePos) {
	    bit_array_set_bit(bitArray, p);
    }

    /** SANITY CHECKS RELATED TO THE RANK structure **/
    /*
    uint64_t nextSetBit{0};
    uint64_t offset{0};
    auto numBits = bit_array_length(bitArray);
    while (offset < numBits and bit_array_find_next_set_bit(bitArray, offset, &nextSetBit)) {
	if (concatText[nextSetBit] != '$') {
		std::cerr << "Bit # " << nextSetBit << " is set to 1, but the "
			  << "corresponding character in the text is " << concatText[nextSetBit] << "\n";
	}
	offset = nextSetBit + 1;
    }

    if (bit_array_num_bits_set(bitArray) != onePos.size()) {
	std::cerr << "ERROR: Bit array has " << bit_array_num_bits_set(bitArray)
		  << " bits set, but this should be " << onePos.size() << "!\n";
	std::exit(1);
    }

    rank9b bitmap(bitArray->words, bitArray->num_of_bits);
    for (size_t i = 0; i < onePos.size() - 1; ++i) {
	    auto pos = onePos[i];
	    auto r = bitmap.rank(pos+1);

	    if (r != i+1) {
		std::cerr << "rank should be " << i+1 << " but it's " << r << "\n";
		std::cerr << "text is " << concatText[pos] < "\n\n";
		std::cerr << "bit vector says " << (bit_array_get_bit(bitArray, pos) ? '1' : '0') << "\n";
	    }
    }

    std::ofstream rsStream(outputDir + "rsdSafe.bin", std::ios::binary);
    {
        ScopedTimer timer;
        rsdic::RSDic rsd;
        rsdb.Build(rsd);
        rsd.Save(rsStream);
        std::cerr << "done\n";
    }
    rsStream.close();
    */
    /** END OF SANITY CHECK **/
    onePos.clear();
    onePos.shrink_to_fit();

    std::string rsFileName = outputDir + "rsd.bin";
    FILE* rsFile = fopen(rsFileName.c_str(), "w");
    {
        ScopedTimer timer;
        std::cerr << "Building rank-select dictionary and saving to disk ";
	bit_array_save(bitArray, rsFile);
        std::cerr << "done\n";
    }
    fclose(rsFile);
    bit_array_free(bitArray);


    std::ofstream seqStream(outputDir + "txpInfo.bin", std::ios::binary);
    {
        ScopedTimer timer;
        std::cerr << "Writing sequence data to file . . . ";
        cereal::BinaryOutputArchive seqArchive(seqStream);
        seqArchive(transcriptNames);
        seqArchive(transcriptStarts);
        //seqArchive(positionIDs);
        seqArchive(concatText);
        std::cerr << "done\n";
    }
    seqStream.close();

    // clear stuff we no longer need
    //positionIDs.clear();
    //positionIDs.shrink_to_fit();
    transcriptStarts.clear();
    transcriptStarts.shrink_to_fit();
    transcriptNames.clear();
    transcriptNames.shrink_to_fit();
    // done clearing


    // Build the suffix array
    size_t tlen = concatText.length();
    std::vector<int> SA(tlen, 0);
    std::ofstream saStream(outputDir + "sa.bin", std::ios::binary);
    {
        ScopedTimer timer;
        std::cerr << "Building suffix array . . . ";
        auto ret = sais(reinterpret_cast<unsigned char*>(
                        const_cast<char*>(concatText.c_str())),
                        SA.data(), tlen + 1);
        if (ret == 0) {
            std::cerr << "success\n";
            {
                ScopedTimer timer2;
                std::cerr << "saving to disk . . . ";
                cereal::BinaryOutputArchive saArchive(saStream);
                saArchive(SA);
		// don't actually need the LCP right now
                // saArchive(LCP);
                std::cerr << "done\n";
            }
        } else {
            std::cerr << "FAILURE: return code from sais() was " << ret << "\n";
	    std::exit(1);
        }
        std::cerr << "done\n";
    }
    saStream.close();

    // clear things we don't need
    //LCP.clear();
    // LCP.shrink_to_fit();
    // done clearing

    // Now, build the k-mer lookup table
    /*
    std::unordered_map<uint64_t,
                       rapmap::utils::SAInterval,
                       rapmap::utils::KmerKeyHasher> khash;
                       */
    google::dense_hash_map<uint64_t,
                        rapmap::utils::SAInterval,
                        rapmap::utils::KmerKeyHasher> khash;
    khash.set_empty_key(std::numeric_limits<uint64_t>::max());

    /*
    concatText.erase(std::remove_if(concatText.begin(),
                                    concatText.end(),
                                    [] (const char a) -> bool { return !isprint(a); }),
                     concatText.end());
                     */

    // The start and stop of the current interval
    uint32_t start = 0, stop = 0;
    // An iterator to the beginning of the text
    auto textB = concatText.begin();
    auto textE = concatText.end();
    // The current k-mer as a string
    rapmap::utils::my_mer mer;
    bool currentValid{false};
    std::string currentKmer;
    std::string nextKmer;
    while (stop < tlen) {
        // Check if the string starting at the
        // current position is valid (i.e. doesn't contain $)
        // and is <= k bases from the end of the string
        nextKmer = concatText.substr(SA[stop], k);
        if (nextKmer.length() == k and
            nextKmer.find_first_of('$') == std::string::npos) {
            // If this is a new k-mer, then hash the current k-mer
            if (nextKmer != currentKmer) {
                if (currentKmer.length() == k and
                    currentKmer.find_first_of('$') == std::string::npos) {
                    mer = rapmap::utils::my_mer(currentKmer);
                    auto bits = mer.get_bits(0, 2*k);
                    auto hashIt = khash.find(bits);
                    if (hashIt == khash.end()) {
                        if (start > 1) {
                            if (concatText.substr(SA[start-1], k) ==
                                concatText.substr(SA[start], k)) {
                                std::cerr << "T[SA["
                                          << start-1 << "]:" << k << "] = "
                                          << concatText.substr(SA[start-1], k)
                                          << " = T[SA[" << start << "]:" << k << "]\n";
                                std::cerr << "start = " << start << ", stop = " << stop << "\n";
                                std::cerr << "(1) THIS SHOULD NOT HAPPEN\n";
                                std::exit(1);
                            }
                        }
                        if (start == stop) {
                            std::cerr << "AHH (1) : Interval is empty! (start = " << start
                                      << ") = (stop =  " << stop << ")\n";
                        }
                        if (start == stop) {
                            std::cerr << "AHH (2) : Interval is empty! (start = " << start
                                << ") = (stop =  " << stop << ")\n";
                        }

                        khash[bits] = {start, stop};
                    } else {
                        std::cerr << "\nERROR (1): trying to add same suffix "
                                  << currentKmer << " (len = "
                                  << currentKmer.length() << ") multiple times!\n";
                        auto prevInt = hashIt->second;
                        std::cerr << "existing interval is ["
                                  << prevInt.begin << ", " << prevInt.end << ")\n";
                        for (auto x = prevInt.begin; x < prevInt.end; ++x) {
                            auto suff = concatText.substr(SA[x], k);
                            for (auto c : suff) {
                                std::cerr << "*" << c << "*";
                            }
                            std::cerr << " (len = " << suff.length() <<")\n";
                        }
                        std::cerr << "new interval is ["
                                  << start << ", " << stop << ")\n";
                        for (auto x = start; x < stop; ++x) {
                            auto suff = concatText.substr(SA[x], k);
                            for (auto c : suff) {
                                std::cerr << "*" << c << "*";
                            }
                            std::cerr << "\n";
                        }
                    }
                }
                currentKmer = nextKmer;
                start = stop;
            }
        } else {
            // If this isn't a valid suffix (contains a $)

            // If the previous interval was valid, put it
            // in the hash.
            if (currentKmer.length() == k and
                currentKmer.find_first_of('$') == std::string::npos) {
                mer = rapmap::utils::my_mer(currentKmer);
                auto bits = mer.get_bits(0, 2*k);
                auto hashIt = khash.find(bits);
                if (hashIt == khash.end()) {
                    if (start > 2) {
                        if (concatText.substr(SA[start-1], k) ==
                            concatText.substr(SA[start], k)) {
                            std::cerr << "T[SA["
                                << start-1 << "]:" << k << "] = "
                                << concatText.substr(SA[start-1], k)
                                << " = T[SA[" << start << "]:" << k << "]\n";
                            std::cerr << "start = " << start << ", stop = " << stop << "\n";
                            std::cerr << "(2) THIS SHOULD NOT HAPPEN\n";
                            std::exit(1);
                        }
                    }
                    khash[bits] = {start, stop};
                } else {
                    std::cerr << "\nERROR (2): trying to add same suffix "
                              << currentKmer << "multiple times!\n";
                    auto prevInt = hashIt->second;
                    std::cerr << "existing interval is ["
                        << prevInt.begin << ", " << prevInt.end << ")\n";
                    for (auto x = prevInt.begin; x < prevInt.end; ++x) {
                        std::cerr << concatText.substr(SA[x], k) << "\n";
                    }
                    std::cerr << "new interval is ["
                        << start << ", " << stop << ")\n";
                    for (auto x = start; x < stop; ++x) {
                        std::cerr << concatText.substr(SA[x], k) << "\n";
                    }
                }

            }
            // The current interval is invalid and empty
            currentKmer = nextKmer;
            start = stop;
        }
        if (stop % 1000000 == 0) {
            std::cerr << "\r\rprocessed " << stop << " positions";
        }
        // We always update the end position
        ++stop;
    }
    if (start < tlen) {
        if (currentKmer.length() == k and
            currentKmer.find_first_of('$') != std::string::npos) {
            mer = rapmap::utils::my_mer(currentKmer);
            khash[mer.get_bits(0, 2*k)] = {start, stop};
        }
    }
    std::cerr << "\nkhash had " << khash.size() << " keys\n";
    std::ofstream hashStream(outputDir + "hash.bin", std::ios::binary);
    {
        ScopedTimer timer;
        std::cerr << "saving hash to disk . . . ";
        cereal::BinaryOutputArchive hashArchive(hashStream);
        hashArchive(k);
        khash.serialize(google::dense_hash_map<uint64_t,
                        rapmap::utils::SAInterval,
                        rapmap::utils::KmerKeyHasher>::NopointerSerializer(), &hashStream);
        //hashArchive(khash);
        std::cerr << "done\n";
    }
    hashStream.close();


    std::string indexVersion = "q0";
    IndexHeader header(IndexType::QUASI, indexVersion, true, k);
    // Finally (since everything presumably succeeded) write the header
    std::ofstream headerStream(outputDir + "header.json");
    {
	cereal::JSONOutputArchive archive(headerStream);
	archive(header);
    }
    headerStream.close();

}
示例#9
0
    /*------------------------------------------------------------------------------*

     *------------------------------------------------------------------------------*/
    void WebProxy::processQuery() {
        QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
        QByteArray requestData = socket->readAll();

        int pos = requestData.indexOf("\r\n");
        QByteArray requestLine = requestData.left(pos);
        requestData.remove(0, pos + 2);

        QList<QByteArray> entries = requestLine.split(' ');
        QByteArray method = entries.value(0);
        QByteArray address = entries.value(1);
        QByteArray version = entries.value(2);

        qDebug( )  << __FILE__ << __FUNCTION__ << "Processing " << address;

        QUrl url = QUrl::fromEncoded(address);
        if (!url.isValid()) {
            //qWarning() << "Invalid URL:" << url;
            socket->disconnectFromHost();
            return;
        }

        //Act as server is request are for local server
        if ((url.host() == "") && (QFile(address).exists())) {
            //qDebug( )  << __FILE__ << __FUNCTION__ << "Sending " << address;
            QByteArray header;
            QTextStream headerStream(&header, QIODevice::WriteOnly);
            //Construct response header
            headerStream << "HTTP/1.0 200 OK" << endl;
            headerStream << "Server: gpsbook/" << qApp->applicationVersion() << endl;
            headerStream << "Date: " << QDateTime::currentDateTime().toUTC().toString("ddd, dd MMM yyyy hh:mm:ss") << "GMT" << endl;
            headerStream << "Content-Type: text/html; charset=utf-8" << endl;
            headerStream << "Connection: close" << endl;
            headerStream << "Pragma: no-cache" << endl;
            headerStream << "Cache-Control: no-cache" << endl;
            QFile file(address);
            if (!file.open(QFile::ReadOnly | QFile::Text))
            {
                 qWarning() << "Cannot open:" << address;
                 socket->disconnectFromHost();
                 return ;
            }

            QByteArray content;
            QTextStream contentStream(&content, QIODevice::WriteOnly);

            while (!file.atEnd()) {
                contentStream << file.readLine() << endl;
            }

            headerStream << "Content-Length:" << content.size() << endl;
            headerStream << "" << endl;

            socket->write(header);
            socket->write(content);
            //qDebug( )  << __FILE__ << __FUNCTION__ << "File sent (" << content.size() << "bytes) :-)";
            socket->disconnectFromHost();
        return;
        }


#if ( QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) )
        // Some finction of QUrl have been deprecated
        // This code is require for the internet browser and should be reviewed.
#else


#ifdef Q_OS_LINUX
        //Remove advert to speedup development ;-)
        if (url.toString().contains("googlesyndication") ||
            url.toString().contains("yieldmanager.com")) {
            socket->disconnectFromHost();
            return;
        }
#endif

        qDebug( )  << __FILE__ << __FUNCTION__ << "URL: " << url.toString();

        QString host = url.host();
        int port = (url.port() < 0) ? 80 : url.port();
        QByteArray req = url.encodedPath();
        if (url.hasQuery())
            req.append('?').append(url.encodedQuery());
        requestLine = method + " " + req + " " + version + "\r\n";
        requestData.prepend(requestLine);

        QString key = host + ':' + QString::number(port);
        QTcpSocket *proxySocket = socket->findChild<QTcpSocket*>(key);
        if (proxySocket) {
            proxySocket->setObjectName(key);
            proxySocket->setProperty("url", url);
            proxySocket->setProperty("requestData", requestData);
            proxySocket->write(requestData);
        } else {
            proxySocket = new QTcpSocket(socket);
            proxySocket->setObjectName(key);
            proxySocket->setProperty("url", url);
            proxySocket->setProperty("requestData", requestData);
            connect(proxySocket, SIGNAL(connected()), this, SLOT(sendRequest()));
            connect(proxySocket, SIGNAL(readyRead()), this, SLOT(transferData()));
            connect(proxySocket, SIGNAL(disconnected()), this, SLOT(closeConnection()));
            connect(proxySocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(closeConnection()));
            proxySocket->connectToHost(host, port);
        }
#endif
    } //WebProxy::processQuery
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key)
{
    QScopedPointer<Database> db(new Database());
    m_db = db.data();
    QPointer<QIODevice> m_device = device;
    m_error = false;
    m_errorStr = QString();
    m_headerEnd = false;
    m_xmlData.clear();
    m_masterSeed.clear();
    m_transformSeed.clear();
    m_encryptionIV.clear();
    m_streamStartBytes.clear();
    m_protectedStreamKey.clear();

    StoreDataStream headerStream(m_device);
    headerStream.open(QIODevice::ReadOnly);
    m_headerStream = &headerStream;

    bool ok;

    quint32 signature1 = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok);
    if (!ok || signature1 != KeePass2::SIGNATURE_1) {
        raiseError(tr("Not a KeePass database."));
        return Q_NULLPTR;
    }

    quint32 signature2 = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok);
    if (!ok || signature2 != KeePass2::SIGNATURE_2) {
        raiseError(tr("Not a KeePass database."));
        return Q_NULLPTR;
    }

    quint32 version = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok)
            & KeePass2::FILE_VERSION_CRITICAL_MASK;
    quint32 maxVersion = KeePass2::FILE_VERSION & KeePass2::FILE_VERSION_CRITICAL_MASK;
    if (!ok || (version < KeePass2::FILE_VERSION_MIN) || (version > maxVersion)) {
        raiseError(tr("Unsupported KeePass database version."));
        return Q_NULLPTR;
    }

    while (readHeaderField() && !hasError()) {
    }

    headerStream.close();

    // check if all required headers were present
    if (m_masterSeed.isEmpty() || m_transformSeed.isEmpty() || m_encryptionIV.isEmpty()
            || m_streamStartBytes.isEmpty() || m_protectedStreamKey.isEmpty()
            || m_db->cipher().isNull()) {
        raiseError("");
        return Q_NULLPTR;
    }

    m_db->setKey(key, m_transformSeed, false);

    CryptoHash hash(CryptoHash::Sha256);
    hash.addData(m_masterSeed);
    hash.addData(m_db->transformedMasterKey());
    QByteArray finalKey = hash.result();

    SymmetricCipherStream cipherStream(m_device, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
                                       SymmetricCipher::Decrypt, finalKey, m_encryptionIV);
    cipherStream.open(QIODevice::ReadOnly);

    QByteArray realStart = cipherStream.read(32);

    if (realStart != m_streamStartBytes) {
        raiseError(tr("Wrong key or database file is corrupt."));
        return Q_NULLPTR;
    }

    HashedBlockStream hashedStream(&cipherStream);
    hashedStream.open(QIODevice::ReadOnly);

    QIODevice* xmlDevice;
    QScopedPointer<QtIOCompressor> ioCompressor;

    if (m_db->compressionAlgo() == Database::CompressionNone) {
        xmlDevice = &hashedStream;
    }
    else {
        ioCompressor.reset(new QtIOCompressor(&hashedStream));
        ioCompressor->setStreamFormat(QtIOCompressor::GzipFormat);
        ioCompressor->open(QIODevice::ReadOnly);
        xmlDevice = ioCompressor.data();
    }

    KeePass2RandomStream randomStream(m_protectedStreamKey);

    QScopedPointer<QBuffer> buffer;

    if (m_saveXml) {
        m_xmlData = xmlDevice->readAll();
        buffer.reset(new QBuffer(&m_xmlData));
        buffer->open(QIODevice::ReadOnly);
        xmlDevice = buffer.data();
    }

    KeePass2XmlReader xmlReader;
    xmlReader.readDatabase(xmlDevice, m_db, &randomStream);

    if (xmlReader.hasError()) {
        raiseError(xmlReader.errorString());
        return Q_NULLPTR;
    }

    Q_ASSERT(version < 0x00030001 || !xmlReader.headerHash().isEmpty());

    if (!xmlReader.headerHash().isEmpty()) {
        QByteArray headerHash = CryptoHash::hash(headerStream.storedData(), CryptoHash::Sha256);
        if (headerHash != xmlReader.headerHash()) {
            raiseError("");
            return Q_NULLPTR;
        }
    }

    return db.take();
}
bool SpriteState::exportSprite(const QString& filename, const QString& outputDir_,
                               const QString &postpScript, SpriteStateError* err) const
{
    setError(err, ErrNone);

    QFileInfo fileInfo(filename);

    QString outputDir;
    if (!outputDir_.isEmpty()) {
        outputDir = outputDir_;
    } else {
        outputDir = fileInfo.path();
    }

    QString binFileName  = outputDir + QDir::separator() + fileInfo.baseName() + ".lkob";
    QString textFileName = outputDir + QDir::separator() + fileInfo.baseName() + ".lkot";
    QString headerFileName = outputDir + QDir::separator() + "AnimNameDef_" + fileInfo.baseName() + ".h";

    QFile binOutput(binFileName);
    QFile textOutput(textFileName);
    QFile headerOutput(headerFileName);

    if (binOutput.exists()) {
        if (!binOutput.remove()) {
            qDebug() <<  "Error: SpriteState::exportSprite():"
                     << binOutput.fileName() << "already exists and cannot be removed";
            setError(err, ErrCantOpenReadWriteMode);
            return false;
        }
    }

    if (!binOutput.open(QFile::WriteOnly | QFile::Append)) {
        qDebug() <<  "Error: SpriteState::exportSprite(): could not open "
                 << binOutput.fileName() << " in WriteOnly mode";
        setError(err, ErrCantOpenReadWriteMode);
        return false;
    }

    if (!textOutput.open(QFile::WriteOnly | QFile::Text)) {
        qDebug() <<  "Error: SpriteState::exportSprite(): could not open "
                 << textOutput.fileName() << " in WriteOnly mode";
        setError(err, ErrCantOpenReadWriteMode);
        return false;
    }

    if (!headerOutput.open(QFile::WriteOnly | QFile::Text)) {
        qDebug() <<  "Error: SpriteState::exportSprite(): could not open "
                 << headerOutput.fileName() << " in WriteOnly mode";
        setError(err, ErrCantOpenReadWriteMode);
        return false;
    }

    /////////////////////////////////////////////////////////////////////////////////
    // Export bin and text file

    QTextStream textStream(&textOutput);
    textStream << "### Exported LvkSprite ################################\n";
    textStream << "Exported LvkSprite version 0.1\n\n";

    textStream << "# Frame Pixmaps\n";
    textStream << "# format: frameId,offset(bytes),length(bytes)\n";
    textStream << "fpixmaps(\n";

    qint64 prevOffset = 0; /* previous offset */
    qint64 offset = 0;

    for (QMapIterator<Id, LvkFrame> it(_frames); it.hasNext();) {
        LvkFrame frame = it.next().value();

        // export only those frames that are used at least in one animation
        if (!isFrameUnused(frame.id)) {
            prevOffset = offset;
            writeImageWithPostprocessing(binOutput, frame, postpScript);
            offset = binOutput.size();
            textStream << "\t" <<  frame.id << "," <<  prevOffset << "," << (offset - prevOffset) << "\n";
        }
        else
        {
            qDebug() << "Omitting unused frame " << frame.id;
        }
    }
    textStream << ")\n\n";

    binOutput.close();

    textStream << "# Animations\n";
    textStream << "# format: animationId,name\n";
    textStream << "# Animation frames\n";
    textStream << "# format: aframeId,frameId,delay,ox,oy,sticky\n";
    textStream << "animations(\n";
    for (QMapIterator<Id, LvkAnimation> it(_animations); it.hasNext();) {
        it.next();
        textStream << "\t" << it.value().toString() << "\n";
        textStream << "\taframes(\n";
        for (QListIterator<LvkAframe> it2(it.value()._aframes); it2.hasNext();) {
            textStream << "\t\t" << it2.next().toString() << "\n";
        }
        textStream << "\t)\n\n";
    }
    textStream << ")\n\n";

    textStream << "### End Exported LvkSprite ############################\n";

    textOutput.close();

    /////////////////////////////////////////////////////////////////////////////////
    // Export header file

    QString headerFileMacroName = "__" + getMacroName(headerFileName) + "__";

    QTextStream headerStream(&headerOutput);
    headerStream << "// File autogenerated by " HEADER_LATEST "\n";
    headerStream << "// -- DO NOT EDIT OR MODIFY THIS FILE --\n\n";
    headerStream << "#ifndef " << headerFileMacroName << "\n";
    headerStream << "#define " << headerFileMacroName << "\n\n";

    for (QMapIterator<Id, LvkAnimation> it(_animations); it.hasNext();) {
        it.next();
        headerStream << "#define ANIM_" << getMacroName(it.value().name) << "\t\t\t\"" << it.value().name << "\"\n";
        headerStream << "#define ANIM_" << getMacroName(it.value().name) << "_FLAGS\t\t\t 0x" << QString::number(it.value().flags, 16) << "\n";
    }
    headerStream << "\n";

    if (!_customHeader.isEmpty()) {
        headerStream << "////////////////////////////////////////////////\n";
        headerStream << "// starting custom header data\n\n";
        headerStream << _customHeader << "\n";
        headerStream << "// end custom header data\n";
        headerStream << "////////////////////////////////////////////////\n";
        headerStream << "\n";
        headerStream << "\n";
    }

    headerStream << "#endif //" << headerFileMacroName << "\n";

    headerOutput.close();

    return true;
}
示例#12
0
void ChatWindowStyle::readStyleFiles()
{
    // load style info
    QString infoPlistFile = d->baseHref + QLatin1String("../Info.plist");
    ChatStylePlistFileReader plistReader(infoPlistFile);
    d->defaultVariantName = plistReader.defaultVariant();
    if (d->defaultVariantName.isEmpty()) {
        // older themes use this
        d->defaultVariantName = plistReader.displayNameForNoVariant();
    }
    if (d->defaultVariantName.isEmpty()) {
        // If name is still empty we use "Normal"
        d->defaultVariantName = i18nc("Normal style variant menu item", "Normal");
    }
    qCDebug(KTP_TEXTUI_LIB) << "defaultVariantName = " << d->defaultVariantName;
    d->defaultFontFamily  = plistReader.defaultFontFamily().isEmpty() ? QFontDatabase::systemFont(QFontDatabase::GeneralFont).family()
                                                                      : plistReader.defaultFontFamily();

    // If the theme has no default font size, use the system font size, but since that is in points (pt), we need to convert
    // it to pixel size (and using pixelSize() does not work if the QFont was not set up using setPixelSize), so we use the
    // rough conversion ratio 4/3 and floor the number
    d->defaultFontSize    = plistReader.defaultFontSize() == 0 ? qFloor(QFontDatabase::systemFont(QFontDatabase::GeneralFont).pointSizeF() * (4.0/3.0))
                                                               : plistReader.defaultFontSize();
    d->disableCombineConsecutive = plistReader.disableCombineConsecutive();
    d->messageViewVersion = plistReader.messageViewVersion();

    // specify the files for the identifiers
    QHash<InternalIdentifier, QLatin1String> templateFiles;

    templateFiles.insert(Template, QLatin1String("Template.html"));

    templateFiles.insert(Header, QLatin1String("Header.html"));
    templateFiles.insert(Content, QLatin1String("Content.html"));
    templateFiles.insert(Footer, QLatin1String("Footer.html"));
    templateFiles.insert(Topic, QLatin1String("Topic.html"));

    templateFiles.insert(IncomingContent, QLatin1String("Incoming/Content.html"));
    templateFiles.insert(IncomingNextContent, QLatin1String("Incoming/NextContent.html"));
    templateFiles.insert(OutgoingContent, QLatin1String("Outgoing/Content.html"));
    templateFiles.insert(OutgoingNextContent, QLatin1String("Outgoing/NextContent.html"));
    templateFiles.insert(Status, QLatin1String("Status.html"));

    templateFiles.insert(IncomingHistory, QLatin1String("Incoming/Context.html"));
    templateFiles.insert(IncomingNextHistory, QLatin1String("Incoming/NextContext.html"));
    templateFiles.insert(OutgoingHistory, QLatin1String("Outgoing/Context.html"));
    templateFiles.insert(OutgoingNextHistory, QLatin1String("Outgoing/NextContext.html"));

    templateFiles.insert(ActionIncoming, QLatin1String("Incoming/Action.html"));
    templateFiles.insert(ActionOutgoing, QLatin1String("Outgoing/Action.html"));

    templateFiles.insert(FileTransferIncoming, QLatin1String("FileTransferRequest.html"));
    templateFiles.insert(VoiceClipIncoming, QLatin1String("voiceClipRequest.html"));

    templateFiles.insert(OutgoingStateUnknown, QLatin1String("Outgoing/StateUnknown.html"));
    templateFiles.insert(OutgoingStateSending, QLatin1String("Outgoing/StateSending.html"));
    templateFiles.insert(OutgoingStateSent, QLatin1String("Outgoing/StateSent.html"));
    templateFiles.insert(OutgoingStateError, QLatin1String("Outgoing/StateError.html"));


    // load all files
    QFile fileAccess;
    Q_FOREACH(const QLatin1String &fileName, templateFiles) {
        QString path = d->baseHref + fileName;
        // Load template file
        if (QFile::exists(path)) {
            fileAccess.setFileName(path);
            fileAccess.open(QIODevice::ReadOnly);
            QTextStream headerStream(&fileAccess);
            headerStream.setCodec(QTextCodec::codecForName("UTF-8"));
            QString data = headerStream.readAll();
            if(!data.isEmpty()) {
                //qCDebug(KTP_TEXTUI_LIB) << fileName << "was found!";
                setContent( templateFiles.key(fileName), data);
            } else {
                qCDebug(KTP_TEXTUI_LIB) << fileName << "was not found!";
            }
            //qCDebug(KTP_TEXTUI_LIB) << fileName << content(templateFiles.key(fileName));
            fileAccess.close();
        }
    }