void GasInfoSettings::setValue(const QString & key, const QVariant & value) { Hash* h = reg(); if (h->contains(key)) { (*h)[key] = value; } QSettings::setValue(key, value); }
uint GasInfoSettings::archivePeriodF() { Hash* h = reg(); if (!h->contains(archivePeriodKey)) { (*h)[archivePeriodKey] = GasInfoSettings().archivePeriod(); } return (*h)[archivePeriodKey].toInt(); }
void SDLApp::loadConfiguration(int argc, char** argv) { const char* config_file = "config/config.txt"; if (argc > 1) { config_file = argv[1]; } std::ifstream fin(config_file, std::ios::in); Tokenizer tok(&fin); Parser parser(&tok); tok.nextToken(); Hash *h = parser.readValue()->getHash(); int width = h->getInteger("width"); int height = h->getInteger("height"); int threadCount = h->getInteger("threads"); int boxw = h->getInteger("boxWidth"); int boxh = h->getInteger("boxHeight"); surface = SDL_SetVideoMode(width, height, 24, SDL_HWSURFACE | SDL_DOUBLEBUF); if (surface == NULL) { fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); SDL_Quit(); exit(2); } setupCamera(h->getString("camera"), width, height); camera->setSurface(surface); camera->setThreadParameters(threadCount, boxw, boxh); if(h->contains("mesh")) { MeshManager::instance().loadMeshes(h->getString("mesh")); } LightManager::instance().loadLights(h->getString("lights")); GeometryManager::instance().loadObjects(h->getString("objects")); if(h->contains("animation")) { animation = new Animation(camera, surface); animation->setup(h->getString("animation")); } }
double GasInfoSettings::h2sAlarmThresF() { Hash* h = reg(); if (!h->contains(h2sAlarmThresKey)) { GasInfoSettings settings; (*h)[h2sAlarmThresKey] = settings.h2sAlarmThres(); } return (*h)[h2sAlarmThresKey].toDouble(); }
int GasInfoSettings::activeIntervalF() { Hash* h = reg(); if (!h->contains(activeIntervalKey)) { GasInfoSettings settings; (*h)[activeIntervalKey] = settings.activeInterval(); } return (*h)[activeIntervalKey].toInt(); }
bool GasInfoSettings::terminalAlarmWindowOpenF(int id) { QString key = terminalAlarmWindowOpenKey(id); Hash* h = reg(); if (!h->contains(key)) { (*h)[key] = false; } return (*h)[key].toBool(); }
QString RpmBuildPlugin::replaceVariables(const QString &str, const Hash &variables) const { const int size = str.size(); QString result; for (int i = 0; i < size; i++) { QString toAppend(str[i]); if ( i < (size - static_cast<int>(sizeof("__a__"))) ) // enought chars to create shortest variable? { if (str[i] == '_' && str[i + 1] == '_') //prefix matches? (__) { const int j = i + 2; i = j; //jump over prefix toAppend += '_'; while ( (i + 1) < size && (str[i] != '_' || str[i + 1] != '_') ) //wait for suffix (__) toAppend += str[i++]; if (i + 1 < size && str[i] == '_' && str[i + 1] == '_') { toAppend += "__"; i++; //we got it! const int k = i - 2; //name without suffix const QString varName = str.mid(j, k - j + 1); if (variables.contains(varName)) { toAppend = variables[varName]; debug(DebugLevel::Info) << "Variable \"" << varName << "\" used in spec file. (value = \"" << variables[varName] << "\")"; } else debug(DebugLevel::Warning) << "Unknown variable \"" << varName << "\" used in spec file."; } } } result.append(toAppend); } return result; }
void FW::printMemStats(void) { #if FW_MEM_DEBUG // Create snapshot of the alloc list. s_lock.enter(); AllocHeader* first = NULL; for (AllocHeader* src = s_memAllocs.next; src != &s_memAllocs; src = src->next) { AllocHeader* alloc = (AllocHeader*)::malloc(sizeof(AllocHeader)); *alloc = *src; alloc->next = first; first = alloc; } s_lock.leave(); // Calculate total size per owner. Hash<String, S64> owners; for (AllocHeader* alloc = first; alloc;) { if (!owners.contains(alloc->ownerID)) owners.add(alloc->ownerID, 0); owners[alloc->ownerID] += alloc->size; AllocHeader* next = alloc->next; ::free(alloc); alloc = next; } // Print. printf("\n"); printf("%-32s%.2f\n", "Memory usage / megs", (F32)s_memoryUsed * exp2(-20)); for (int slot = owners.firstSlot(); slot != -1; slot = owners.nextSlot(slot)) { const HashEntry<String, S64>& entry = owners.getSlot(slot); printf(" %-30s%-12.2f%.0f%%\n", entry.key.getPtr(), (F32)entry.value * exp2(-20), (F32)entry.value / (F32)s_memoryUsed * 100.0f); } printf("\n"); #endif }
void App::downscaleTextures(MeshBase* mesh) { FW_ASSERT(mesh); Hash<const Image*, Texture> hash; for (int submeshIdx = 0; submeshIdx < mesh->numSubmeshes(); submeshIdx++) for (int textureIdx = 0; textureIdx < MeshBase::TextureType_Max; textureIdx++) { Texture& tex = mesh->material(submeshIdx).textures[textureIdx]; if (tex.exists()) { const Image* orig = tex.getImage(); if (!hash.contains(orig)) { Image* scaled = orig->downscale2x(); hash.add(orig, (scaled) ? Texture(scaled, tex.getID()) : tex); } tex = hash.get(orig); } } }
void* FW::malloc(size_t size) { FW_ASSERT(size >= 0); #if FW_MEM_DEBUG s_lock.enter(); AllocHeader* alloc = (AllocHeader*)::malloc(sizeof(AllocHeader) + size); if (!alloc) fail("Out of memory!"); void* ptr = alloc + 1; alloc->prev = s_memAllocs.prev; alloc->next = &s_memAllocs; alloc->prev->next = alloc; alloc->next->prev = alloc; alloc->size = size; alloc->ownerID = "Uncategorized"; s_memoryUsed += size; if (!s_memPushingOwner) { U32 threadID = Thread::getID(); if (s_memOwnerStacks.contains(threadID) && s_memOwnerStacks[threadID].getSize()) alloc->ownerID = s_memOwnerStacks[threadID].getLast(); } s_lock.leave(); #else void* ptr = ::malloc(size); if (!ptr) fail("Out of memory!"); s_lock.enter(); s_memoryUsed += _msize(ptr); s_lock.leave(); #endif return ptr; }
void FW::exportWavefrontMesh(BufferedOutputStream& stream, const MeshBase* mesh, const String& fileName) { FW_ASSERT(mesh); Mesh<VertexPNT> pnt(*mesh); // Extract base name. String dirName = fileName.getDirName(); String baseName = fileName.getFileName(); int idx = baseName.indexOf('.'); if (idx != -1) baseName = baseName.substring(0, idx); // Write OBJ file. if (baseName.getLength()) { stream.writef("mtllib %s.mtl\n", baseName.getPtr()); stream.writef("\n"); } for (int i = 0; i < pnt.numVertices(); i++) { const VertexPNT& v = pnt.vertex(i); stream.writef("v %g %g %g\n", v.p.x, v.p.y, v.p.z); } stream.writef("\n"); for (int i = 0; i < pnt.numVertices(); i++) { const VertexPNT& v = pnt.vertex(i); stream.writef("vt %g %g\n", v.t.x, 1.0f - v.t.y); } stream.writef("\n"); for (int i = 0; i < pnt.numVertices(); i++) { const VertexPNT& v = pnt.vertex(i); stream.writef("vn %g %g %g\n", v.n.x, v.n.y, v.n.z); } for (int i = 0; i < pnt.numSubmeshes(); i++) { stream.writef("\n"); if (baseName.getLength()) stream.writef("usemtl %d\n", i); const Array<Vec3i>& tris = pnt.indices(i); for (int j = 0; j < tris.getSize(); j++) { Vec3i v = tris[j] + 1; stream.writef("f %d/%d/%d %d/%d/%d %d/%d/%d\n", v.x, v.x, v.x, v.y, v.y, v.y, v.z, v.z, v.z); } } // No base name => do not write materials. if (!baseName.getLength()) return; // Hash textures and determine file names. Hash<const Image*, String> texImageHash; Set<String> texNameSet; for (int i = 0; i < pnt.numSubmeshes(); i++) { const MeshBase::Material& mat = pnt.material(i); for (int j = 0; j < MeshBase::TextureType_Max; j++) { const Texture& tex = mat.textures[j]; if (!tex.exists() || texImageHash.contains(tex.getImage())) continue; // Extract name from ID. String name = tex.getID().getFileName(); int idx = name.indexOf('.'); if (idx != -1) name = name.substring(0, idx); // No name => generate one. if (!name.getLength()) name = sprintf("tex%d", texImageHash.getSize()); // Ensure that the name is unique. String oldName = name; for (int k = 0; texNameSet.contains(name); k++) name = sprintf("%s_%d", oldName.getPtr(), k); // Append format postfix. name += ".png"; // Record. texImageHash.add(tex.getImage(), name); texNameSet.add(name); } } // Write MTL file. File mtlFile(dirName + '/' + baseName + ".mtl", File::Create); BufferedOutputStream mtlOut(mtlFile); for (int i = 0; i < pnt.numSubmeshes(); i++) { if (i) mtlOut.writef("\n"); const MeshBase::Material& mat = pnt.material(i); mtlOut.writef("newmtl %d\n", i); mtlOut.writef("Ka 0 0 0\n"); mtlOut.writef("Kd %g %g %g\n", mat.diffuse.x, mat.diffuse.y, mat.diffuse.z); mtlOut.writef("d %g\n", mat.diffuse.w); mtlOut.writef("Ks %g %g %g\n", mat.specular.x, mat.specular.y, mat.specular.z); mtlOut.writef("Ns %g\n", mat.glossiness); if (texImageHash.contains(mat.textures[MeshBase::TextureType_Diffuse].getImage())) mtlOut.writef("map_Kd %s\n", texImageHash[mat.textures[MeshBase::TextureType_Diffuse].getImage()].getPtr()); if (texImageHash.contains(mat.textures[MeshBase::TextureType_Alpha].getImage())) mtlOut.writef("map_d %s\n", texImageHash[mat.textures[MeshBase::TextureType_Alpha].getImage()].getPtr()); if (texImageHash.contains(mat.textures[MeshBase::TextureType_Displacement].getImage())) mtlOut.writef("disp -mm %g %g %s\n", mat.displacementBias / mat.displacementCoef, mat.displacementCoef, texImageHash[mat.textures[MeshBase::TextureType_Displacement].getImage()].getPtr()); if (texImageHash.contains(mat.textures[MeshBase::TextureType_Normal].getImage())) mtlOut.writef("bump %s\n", texImageHash[mat.textures[MeshBase::TextureType_Normal].getImage()].getPtr()); if (texImageHash.contains(mat.textures[MeshBase::TextureType_Environment].getImage())) mtlOut.writef("refl -type sphere %s\n", texImageHash[mat.textures[MeshBase::TextureType_Environment].getImage()].getPtr()); } mtlOut.flush(); // Write textures. for (int i = texImageHash.firstSlot(); i != -1; i = texImageHash.nextSlot(i)) { const Image* texImage = texImageHash.getSlot(i).key; const String& texName = texImageHash.getSlot(i).value; exportImage(dirName + '/' + texName, texImage); } }
void tst_QHash::insert1() { const char *hello = "hello"; const char *world = "world"; const char *allo = "allo"; const char *monde = "monde"; { typedef QHash<QString, QString> Hash; Hash hash; QString key; for (int i = 0; i < 10; ++i) { key[0] = i + '0'; for (int j = 0; j < 10; ++j) { key[1] = j + '0'; hash.insert(key, "V" + key); } } for (int i = 0; i < 10; ++i) { key[0] = i + '0'; for (int j = 0; j < 10; ++j) { key[1] = j + '0'; hash.remove(key); } } } { typedef QHash<int, const char *> Hash; Hash hash; hash.insert(1, hello); hash.insert(2, world); QVERIFY(hash.size() == 2); QVERIFY(!hash.isEmpty()); { Hash hash2 = hash; hash2 = hash; hash = hash2; hash2 = hash2; hash = hash; hash2.clear(); hash2 = hash2; QVERIFY(hash2.size() == 0); QVERIFY(hash2.isEmpty()); } QVERIFY(hash.size() == 2); { Hash hash2 = hash; hash2[1] = allo; hash2[2] = monde; QVERIFY(hash2[1] == allo); QVERIFY(hash2[2] == monde); QVERIFY(hash[1] == hello); QVERIFY(hash[2] == world); hash2[1] = hash[1]; hash2[2] = hash[2]; QVERIFY(hash2[1] == hello); QVERIFY(hash2[2] == world); hash[1] = hash[1]; QVERIFY(hash[1] == hello); } { Hash hash2 = hash; hash2.detach(); hash2.remove(1); QVERIFY(hash2.size() == 1); hash2.remove(1); QVERIFY(hash2.size() == 1); hash2.remove(0); QVERIFY(hash2.size() == 1); hash2.remove(2); QVERIFY(hash2.size() == 0); QVERIFY(hash.size() == 2); } hash.detach(); { Hash::iterator it1 = hash.find(1); QVERIFY(it1 != hash.end()); Hash::iterator it2 = hash.find(0); QVERIFY(it2 != hash.begin()); QVERIFY(it2 == hash.end()); *it1 = monde; QVERIFY(*it1 == monde); QVERIFY(hash[1] == monde); *it1 = hello; QVERIFY(*it1 == hello); QVERIFY(hash[1] == hello); hash[1] = monde; QVERIFY(it1.key() == 1); QVERIFY(it1.value() == monde); QVERIFY(*it1 == monde); QVERIFY(hash[1] == monde); hash[1] = hello; QVERIFY(*it1 == hello); QVERIFY(hash[1] == hello); } { const Hash hash2 = hash; Hash::const_iterator it1 = hash2.find(1); QVERIFY(it1 != hash2.end()); QVERIFY(it1.key() == 1); QVERIFY(it1.value() == hello); QVERIFY(*it1 == hello); Hash::const_iterator it2 = hash2.find(2); QVERIFY(it1 != it2); QVERIFY(it1 != hash2.end()); QVERIFY(it2 != hash2.end()); int count = 0; it1 = hash2.begin(); while (it1 != hash2.end()) { count++; ++it1; } QVERIFY(count == 2); count = 0; it1 = hash.begin(); while (it1 != hash.end()) { count++; ++it1; } QVERIFY(count == 2); } { QVERIFY(hash.contains(1)); QVERIFY(hash.contains(2)); QVERIFY(!hash.contains(0)); QVERIFY(!hash.contains(3)); } { QVERIFY(hash.value(1) == hello); QVERIFY(hash.value(2) == world); QVERIFY(hash.value(3) == 0); QVERIFY(hash.value(1, allo) == hello); QVERIFY(hash.value(2, allo) == world); QVERIFY(hash.value(3, allo) == allo); QVERIFY(hash.value(0, monde) == monde); } { QHash<int,Foo> hash; for (int i = 0; i < 10; i++) hash.insert(i, Foo()); QVERIFY(Foo::count == 10); hash.remove(7); QVERIFY(Foo::count == 9); } QVERIFY(Foo::count == 0); { QHash<int, int*> hash; QVERIFY(((const QHash<int,int*>*) &hash)->operator[](7) == 0); } } }
RpmBuildPlugin::Hash RpmBuildPlugin::solveVariables(const List &variables, //variables to solve const List &constants //constants (solved) ) const { Hash hash; append(hash, constants); //fill list of solved constants/variables with constants std::function<bool(const Pair &var)> resolveVariable; resolveVariable = [&](const Pair &var) -> bool { auto error = [&](const QString &variable, const QString &value, const QString &message) { debug(DebugLevel::Error) << "Could not resolve variable \"" << variable << "\"" << "with value \"" << value << "\"" << ". Error message: " << message; }; assert(hash.contains(var.first) == false); //check first char of value if (var.second.size() == 0) hash[var.first] = var.second; else { const char operation = var.second[0].toAscii() ; switch (operation) { case '=': //just use variable's value hash[var.first] = replaceVariables(var.second.mid(1), hash); //do not use first char which is '=' break; case 'r': //regexp { //Format for this operation: // "r:expression:regexp:result" // ie: "r:abc:a(.*):_%1_" will return "_bc_". (%1 is equivalent of \1) // This operation is +- equivalent of: // echo $expression | sed -e "s/$regexp/$result/" // Instead of ":" as separator, any other char may be used. const QString value = var.second.mid(1); //all chars except 1. char const size_t size = value.size(); if (size >= sizeof(":::")) //minimal regexp is: ":::" { const QString solvedValue = replaceVariables(value, hash); //replace all variables with theirs values const char splitter = solvedValue[0].toAscii(); //use first char as splitter //do splitting const QStringList regexpParts = solvedValue.mid(1).split(splitter); //ommit first ':' //we are expecting 3 parts if (regexpParts.size() == 3) { QRegExp regexp(regexpParts[1]); //second part is a pattern if (regexp.exactMatch(regexpParts[0])) //enter string to be matched (first part) { //now try to commit matched expression to result const QStringList captured = regexp.capturedTexts(); //list of captured texts "(.*)" QString result = regexpParts[2]; for (int i = 1; i < captured.size(); i++) { //this operation will replace each "%n" in result part of regexp with corresponding captured text. //This is why we use %n instead of \n, as %n is used by QString algorithms to replace args result = result.arg(captured[i]); } hash[var.first] = result; } else error(var.first, var.second, "Could not match regular expression."); } else error(var.first, var.second, QString("Variable's value should consist of 3 parts, but %1 were found.") .arg(regexpParts.size())); } else error(var.first, var.second, "Variable's value is too short even for simples regular expression."); } break; default: debug(DebugLevel::Error) << "unknown variable operator: \"" << operation << "\""; break; } } return true; }; for (const Pair &var: variables) { //try to resolve value if (hash.contains(var.first) == false) //not resolved yet? resolveVariable(var); } return hash; }