// replaces ${X} by the value of the key X // The function accept the format ${X} or $${X} depending on the value of twoDollarSigns // the $${} is used by the eval_string function, allowing lazy string parsing QString ICISettingsPrivate::replace_in_string(QString string, bool twoDollarSigns) { bool escaped = false; int in = 0; int begin = 0; int offset = (twoDollarSigns ? 3 : 2); for(int i = 0; i < string.size() && i>=0; i++) { QChar c = string.at(i); if (c == '\\') { escaped = !escaped ; continue; } else if(c == '$' && !escaped) { if(i+2 >= string.size()) return string; QChar next = string.at(i+1); if(next == '$' && !escaped) { if(twoDollarSigns) next = string.at(i+2); else { i++; //skip 2 elems continue; } } if(next != '{') continue; if(in == 0) begin = i; in++; if(twoDollarSigns) ++i; continue; } else if( c == '}' && !escaped && --in == 0) { QString substring = replace_in_string(string.mid(begin+offset, (i-begin)-offset), twoDollarSigns); QStringList keys = substring.split('.'); if(this->hasKey(keys)) { QVariant value = this->value(keys, QVariant()); if(value.canConvert<QString>()) { QString replacement = value.toString(); string.replace(begin, (i - begin)+1, replacement); i = (begin + replacement.size()) - 1; } } else if(twoDollarSigns) { i+=2; } } else { escaped = false; } } return string; }
QStringList ICISettingsPrivate::identifier_keys(ICI::IdentifierNode* node) { QStringList keys; ICI::IdentifierNode* n = node; while(n) { if(n->type == ICI::Node::Type_Identifier) keys << n->name; else keys << replace_in_string(n->name); n = n->next; } return keys; }
bool ICISettingsPrivate::evaluate(ICI::IncludeStatementNode* node, ICI::StatementListNode* parent){ currentNode = node; if(node->executed) return true; bool required = true; QString path = replace_in_string(node->path); if(path.at(0) == '?') { required = false; path = path.mid(1); } if(path.isEmpty()){ errorString = formatError("include() requires a file", node); return false; } QDir dir(QFileInfo(node->file).absolutePath()); path = dir.absoluteFilePath(path); QStringList paths; if(QFileInfo(path).isDir()){ Q_FOREACH(const QFileInfo & file, QDir(path).entryInfoList(QDir::NoDotAndDotDot|QDir::Files)){ //qDebug() << file.absoluteFilePath(); paths << file.absoluteFilePath(); } }
TEST(StrUtilsGroup, replace_in_string) { Glib::ustring test_replaces_str = "one two threetwo"; CHECK(replace_in_string(test_replaces_str, "two", "four") == "one four threefour"); }
void Doc::LaunchInstalledBrowser_ALLOS(Glib::ustring path, Gtk::Window* parent, Glib::ustring default_browser) { std::vector<Glib::ustring> browsers ; //> default browser will be tried first if (!default_browser.empty()) browsers.insert(browsers.end(), default_browser) ; #ifdef __APPLE__ browsers.insert(browsers.end(), "/Applications/Safari.app/Contents/MacOS/Safari"); #endif #ifdef WIN32 browsers.insert(browsers.end(), "C:\\Program Files\\Internet Explorer\\iexplore.exe"); browsers.insert(browsers.end(), "C:\\Program Files\\Mozilla Firefox\\firefox.exe"); #endif browsers.insert(browsers.end(), "firefox") ; browsers.insert(browsers.end(), "seamonkey") ; browsers.insert(browsers.end(), "opera") ; browsers.insert(browsers.end(), "mozilla") ; browsers.insert(browsers.end(), "konqueror") ; browsers.insert(browsers.end(), "netscape") ; browsers.insert(browsers.end(), "epiphany") ; //> 1 - Find available browser in path Glib::ustring browser = "" ; for (guint i = 0; i<browsers.size() && browser.compare("")==0 ; i++) { char* brow = g_find_program_in_path( browsers[i].c_str() ) ; if (brow) browser = browsers[i] ; } //> 2 - Launch Glib::ustring error = "" ; Glib::ustring command = "" ; if (browser.compare("")!=0) { #ifndef WIN32 command = browser + " " + path ; #else command = "\"" + browser + "\"" ; command = command + " \"" + replace_in_string(path, "/", "\\") + "\"" ; #endif try { Glib::spawn_command_line_async( command ); } catch (Glib::SpawnError e) { Log::err() << TRANSAG_DISPLAY_NAME << " --> Glibmm SpawnError : " << e.what() << std::endl ; Glib::ustring s1 = _("An error occured while launching documentation browser") ; Glib::ustring s2 = _("Please check your default browser in preferences") ; s1 = s1 + "\n" + s2 ; if (parent) Explorer_dialog::msg_dialog_error(s1, parent, true) ; } } else error = _("No browser found") ; if (error.compare("")!=0 && parent) Explorer_dialog::msg_dialog_error(error, parent, true) ; }
string percent_url_encode(const string& seq) { return replace_in_string(seq, "%", "%25"); }