QString Stringify::JO(Object *o) { if (stack.contains(o)) { ctx->throwTypeError(); return QString(); } Scope scope(ctx); QString result; stack.push(o); QString stepback = indent; indent += gap; QStringList partial; if (propertyList.isEmpty()) { ObjectIterator it(scope, o, ObjectIterator::EnumerableOnly); ScopedValue name(scope); ScopedValue val(scope); while (1) { name = it.nextPropertyNameAsString(val); if (name->isNull()) break; QString key = name->toQString(); QString member = makeMember(key, val); if (!member.isEmpty()) partial += member; } } else { ScopedString s(scope); for (int i = 0; i < propertyList.size(); ++i) { bool exists; s = propertyList.at(i); ScopedValue v(scope, o->get(s.getPointer(), &exists)); if (!exists) continue; QString member = makeMember(s->toQString(), v); if (!member.isEmpty()) partial += member; } } if (partial.isEmpty()) { result = QStringLiteral("{}"); } else if (gap.isEmpty()) { result = QStringLiteral("{") + partial.join(QLatin1Char(',')) + QStringLiteral("}"); } else { QString separator = QStringLiteral(",\n") + indent; result = QStringLiteral("{\n") + indent + partial.join(separator) + QStringLiteral("\n") + stepback + QStringLiteral("}"); } indent = stepback; stack.pop(); return result; }
QString Stringify::JA(ArrayObject *a) { if (stack.contains(a)) { ctx->throwTypeError(); return QString(); } Scope scope(a->engine()); QString result; stack.push(a); QString stepback = indent; indent += gap; QStringList partial; uint len = a->getLength(); ScopedValue v(scope); for (uint i = 0; i < len; ++i) { bool exists; v = a->getIndexed(i, &exists); if (!exists) { partial += QStringLiteral("null"); continue; } QString strP = Str(QString::number(i), v); if (!strP.isEmpty()) partial += strP; else partial += QStringLiteral("null"); } if (partial.isEmpty()) { result = QStringLiteral("[]"); } else if (gap.isEmpty()) { result = QStringLiteral("[") + partial.join(QLatin1Char(',')) + QStringLiteral("]"); } else { QString separator = QStringLiteral(",\n") + indent; result = QStringLiteral("[\n") + indent + partial.join(separator) + QStringLiteral("\n") + stepback + QStringLiteral("]"); } indent = stepback; stack.pop(); return result; }
Profile::Ptr ProfileManager::loadProfile(const QString& shortPath) { // the fallback profile has a 'special' path name, "FALLBACK/" if (shortPath == _fallbackProfile->path()) return _fallbackProfile; QString path = shortPath; // add a suggested suffix and relative prefix if missing QFileInfo fileInfo(path); if (fileInfo.isDir()) return Profile::Ptr(); if (fileInfo.suffix() != QLatin1String("profile")) path.append(".profile"); if (fileInfo.path().isEmpty() || fileInfo.path() == QLatin1String(".")) path.prepend(QLatin1String("konsole") + QDir::separator()); // if the file is not an absolute path, look it up if (!fileInfo.isAbsolute()) path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, path); // if the file is not found, return immediately if (path.isEmpty()) { return Profile::Ptr(); } // check that we have not already loaded this profile foreach(const Profile::Ptr& profile, _profiles) { if (profile->path() == path) return profile; } // guard to prevent problems if a profile specifies itself as its parent // or if there is recursion in the "inheritance" chain // (eg. two profiles, A and B, specifying each other as their parents) static QStack<QString> recursionGuard; PopStackOnExit<QString> popGuardOnExit(recursionGuard); if (recursionGuard.contains(path)) { qWarning() << "Ignoring attempt to load profile recursively from" << path; return _fallbackProfile; } else { recursionGuard.push(path); } // load the profile ProfileReader* reader = new KDE4ProfileReader; Profile::Ptr newProfile = Profile::Ptr(new Profile(fallbackProfile())); newProfile->setProperty(Profile::Path, path); QString parentProfilePath; bool result = reader->readProfile(path, newProfile, parentProfilePath); if (!parentProfilePath.isEmpty()) { Profile::Ptr parentProfile = loadProfile(parentProfilePath); newProfile->setParent(parentProfile); } delete reader; if (!result) { qWarning() << "Could not load profile from " << path; return Profile::Ptr(); } else { addProfile(newProfile); return newProfile; } }
// taken from qstardict QString WordBrowser::parseData(const char *data, int dictIndex, bool htmlSpaces, bool reformatLists) { QString result; quint32 dataSize = *reinterpret_cast<const quint32*>(data); const char *dataEnd = data + dataSize; const char *ptr = data + sizeof(quint32); while (ptr < dataEnd) { switch (*ptr++) { case 'm': case 'l': case 'g': { QString str = QString::fromUtf8(ptr); ptr += str.toUtf8().length() + 1; result += str; break; } case 'x': { QString str = QString::fromUtf8(ptr); ptr += str.toUtf8().length() + 1; xdxf2html(str); result += str; break; } case 't': { QString str = QString::fromUtf8(ptr); ptr += str.toUtf8().length() + 1; result += "<font class=\"example\">"; result += str; result += "</font>"; break; } case 'y': { ptr += strlen(ptr) + 1; break; } case 'W': case 'P': { ptr += *reinterpret_cast<const quint32*>(ptr) + sizeof(quint32); break; } default: ; // nothing } } if (reformatLists) { int pos = 0; QStack<QChar> openedLists; while (pos < result.length()) { if (result[pos].isDigit()) { int n = 0; while (result[pos + n].isDigit()) ++n; pos += n; if (result[pos] == '&' && result.mid(pos + 1, 3) == "gt;") result.replace(pos, 4, ">"); QChar marker = result[pos]; QString replacement; if (marker == '>' || marker == '.' || marker == ')') { if (n == 1 && result[pos - 1] == '1') // open new list { if (openedLists.contains(marker)) { replacement = "</li></ol>"; while (openedLists.size() && openedLists.top() != marker) { replacement += "</li></ol>"; openedLists.pop(); } } openedLists.push(marker); replacement += "<ol>"; } else { while (openedLists.size() && openedLists.top() != marker) { replacement += "</li></ol>"; openedLists.pop(); } replacement += "</li>"; } replacement += "<li>"; pos -= n; n += pos; while (result[pos - 1].isSpace()) --pos; while (result[n + 1].isSpace()) ++n; result.replace(pos, n - pos + 1, replacement); pos += replacement.length(); } else ++pos; } else ++pos; } while (openedLists.size()) { result += "</li></ol>"; openedLists.pop(); } } if (htmlSpaces) { int n = 0; while (result[n].isSpace()) ++n; result.remove(0, n); n = 0; while (result[result.length() - 1 - n].isSpace()) ++n; result.remove(result.length() - n, n); for (int pos = 0; pos < result.length();) { switch (result[pos].toAscii()) { case '[': result.insert(pos, "<font class=\"transcription\">"); pos += 28 + 1; // sizeof "<font class=\"transcription\">" + 1 break; case ']': result.insert(pos + 1, "</font>"); pos += 7 + 1; // sizeof "</font>" + 1 break; case '\t': result.insert(pos, " "); pos += 24 + 1; // sizeof " " + 1 break; case '\n': { int count = 1; n = 1; while (result[pos + n].isSpace()) { if (result[pos + n] == '\n') ++count; ++n; } if (count > 1) result.replace(pos, n, "</p><p>"); else result.replace(pos, n, "<br>"); break; } default: ++pos; } } } return result; }