std::string CPPCMS_API from_utf8(char const *encoding,std::string const &str) { if(is_utf8(encoding)) return str; return from_utf8(encoding,str.data(),str.data()+str.size()); }
int main(int argc, char *argv[]) { int opt_ret, main_ret = EXIT_FAILURE; pfx_tree_t substitutions; size_t longest_replacement = 0; substitutions = pfx_tree_init(); if (substitutions == NULL) { fprintf(stderr, "Failed to allocate the substitution tree\n"); goto main_cleanup; } while ((opt_ret = getopt_long(argc, argv, opts, long_opts, NULL)) != -1) { char *opt1, *opt2; switch (opt_ret) { case 'r': if (!get_two_subopts(argc, argv, &opt1, &opt2)) goto main_print_help; wchar_t *key_str = from_utf8(opt1); if (key_str == NULL) { perror("Error parsing arguments"); goto main_cleanup; } if (!pfx_tree_insert_safe(substitutions, key_str, wcslen(key_str), opt2)) { fprintf(stderr, "Failed to insert replacement, " "it probably shares a key with another.\n"); free(key_str); goto main_cleanup; } free(key_str); size_t tmp_replacement = strlen(opt2); if (tmp_replacement > longest_replacement) longest_replacement = tmp_replacement; break; case 'h': default: goto main_print_help; } } argv += optind; argc -= optind; if (argc != 2) { fprintf(stderr, "You must pass a SRC and DEST file\n"); goto main_print_help; } if (!substitute_file(argv[1], argv[0], substitutions, longest_replacement)) { perror("Error substituting"); goto main_cleanup; } main_ret = EXIT_SUCCESS; goto main_cleanup; main_print_help: fprintf(stderr, "Usage: substitute [OPTION] SRC DEST\n"); fprintf(stderr, "Example: substitute -r foo bar in.txt out.txt\n"); fprintf(stderr, "\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -h, --help " "Displays this help text\n"); fprintf(stderr, " -r, --replace=NEEDLE REPLACEMENT " "Replaces the NEEDLE in the source text with REPLACEMENT\n"); main_cleanup: pfx_tree_destroy(substitutions); return main_ret; }
static docstring asMathematicaName(docstring const & name) { return from_utf8(asMathematicaName(to_utf8(name))); }
string const doSubstitution(InsetExternalParams const & params, Buffer const & buffer, string const & s, bool use_latex_path, bool external_in_tmpdir, Substitute what) { Buffer const * masterBuffer = buffer.masterBuffer(); string const parentpath = external_in_tmpdir ? masterBuffer->temppath() : buffer.filePath(); string const filename = external_in_tmpdir ? params.filename.mangledFileName() : params.filename.outputFileName(parentpath); string const basename = changeExtension( onlyFileName(filename), string()); string const absname = makeAbsPath(filename, parentpath).absFileName(); string result = s; if (what != ALL_BUT_PATHS) { string const filepath = onlyPath(filename); string const abspath = onlyPath(absname); string const masterpath = external_in_tmpdir ? masterBuffer->temppath() : masterBuffer->filePath(); // FIXME UNICODE string relToMasterPath = onlyPath( to_utf8(makeRelPath(from_utf8(absname), from_utf8(masterpath)))); if (relToMasterPath == "./") relToMasterPath.clear(); // FIXME UNICODE string relToParentPath = onlyPath( to_utf8(makeRelPath(from_utf8(absname), from_utf8(parentpath)))); if (relToParentPath == "./") relToParentPath.clear(); result = subst_path(result, "$$FPath", filepath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$AbsPath", abspath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$RelPathMaster", relToMasterPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$RelPathParent", relToParentPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); if (FileName::isAbsolute(filename)) { result = subst_path(result, "$$AbsOrRelPathMaster", abspath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$AbsOrRelPathParent", abspath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); } else { result = subst_path(result, "$$AbsOrRelPathMaster", relToMasterPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$AbsOrRelPathParent", relToParentPath, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); } } if (what == PATHS) return result; result = subst_path(result, "$$FName", filename, use_latex_path, EXCLUDE_EXTENSION); result = subst_path(result, "$$Basename", basename, use_latex_path, PROTECT_EXTENSION, ESCAPE_DOTS); result = subst_path(result, "$$Extension", '.' + getExtension(filename), use_latex_path); result = subst_path(result, "$$Tempname", params.tempname().absFileName(), use_latex_path); result = subst_path(result, "$$Sysdir", package().system_support().absFileName(), use_latex_path); // Handle the $$Contents(filename) syntax if (contains(result, "$$Contents(\"")) { // Since use_latex_path may be true we must extract the file // name from s instead of result and do the substitutions // again, this time with use_latex_path false. size_t const spos = s.find("$$Contents(\""); size_t const send = s.find("\")", spos); string const file_template = s.substr(spos + 12, send - (spos + 12)); string const file = doSubstitution(params, buffer, file_template, false, external_in_tmpdir, what); string contents; FileName const absfile( makeAbsPath(file, masterBuffer->temppath())); if (absfile.isReadableFile()) // FIXME UNICODE contents = to_utf8(absfile.fileContents("UTF-8")); size_t const pos = result.find("$$Contents(\""); size_t const end = result.find("\")", pos); result.replace(pos, end + 2, contents); } return result; }
docstring InsetCaption::layoutName() const { if (type_.empty()) return from_ascii("Caption"); return from_utf8("Caption:" + type_); }
void write_utf8(ostream::base_ostream& out, boost::string_ref x) { out << from_utf8(x); }
fs::path generic_to_path(boost::string_ref x) { return fs::path(from_utf8(x)); }
static boost::shared_ptr<ustring> create() { return from_utf8(""); }
std::wstring torrent_details::to_wstring(size_t index) { switch (index) { case name_e: return name_; case state_e: return state_; case progress_e: return (wform(L"%1$.2f%%") % (completion_*100)).str(); case speed_down_e: return to_bytes_size(speed_.first, true); case speed_up_e: return to_bytes_size(speed_.second, true); case distributed_copies_e: { float copies = distributed_copies_; if (copies < 0) return L"Seeding"; else return (hal::wform(L"%1$.2f") % copies).str(); } case remaining_e: { return to_bytes_size(total_wanted_-total_wanted_done_, false); //(wform(L"%1$.2fMB") % (static_cast<float>()/(1024*1024))).str(); } case completed_e: return to_bytes_size(total_wanted_done_, false); //(wform(L"%1$.2fMB") % (static_cast<float>(total_wanted_done_)/(1024*1024))).str(); case total_wanted_e: { return to_bytes_size(total_wanted_, false); //(wform(L"%1$.2fMB") % (static_cast<float>(total_wanted_)/(1024*1024))).str(); } case uploaded_e: { return to_bytes_size(total_payload_uploaded_, false); //(wform(L"%1$.2fMB") % (static_cast<float>(total_payload_uploaded_)/(1024*1024))).str(); } case downloaded_e: { return to_bytes_size(total_payload_downloaded_, false); //(wform(L"%1$.2fMB") % (static_cast<float>(total_payload_downloaded_)/(1024*1024))).str(); } case peers_e: return (wform(L"%1% (%2%)") % connected_peers_ % peers_).str(); case seeds_e: return (wform(L"%1% (%2%)") % connected_seeds_ % seeds_).str(); case ratio_e: { float ratio = (total_payload_downloaded_) ? static_cast<float>(total_payload_uploaded_) / static_cast<float>(total_payload_downloaded_) : 0; return (wform(L"%1$.2f") % ratio).str(); } case eta_e: { if (!estimated_time_left_.is_special()) return hal::from_utf8( boost::posix_time::to_simple_string(estimated_time_left_)); else return app().res_wstr(HAL_INF); } case tracker: return currentTracker_; case update_tracker_in_e: { if (!update_tracker_in_.is_special()) return from_utf8( boost::posix_time::to_simple_string(update_tracker_in_)); else return app().res_wstr(HAL_INF); } case active_time_e: { if (!active_.is_special()) return from_utf8( boost::posix_time::to_simple_string(active_)); else return app().res_wstr(HAL_INF); } case seeding_time_e: { if (!seeding_.is_special()) return from_utf8( boost::posix_time::to_simple_string(seeding_)); else return app().res_wstr(HAL_INF); } case start_time_e: { if (!start_time_.is_special()) return from_utf8( boost::posix_time::to_simple_string(start_time_)); else return app().res_wstr(IDS_NA); } case finish_time_e: { if (!finish_time_.is_special()) return from_utf8( boost::posix_time::to_simple_string(finish_time_)); else return app().res_wstr(IDS_NA); } case queue_position_e: { if (queue_position_ != -1) return (wform(L"%1%") % queue_position_).str(); else return app().res_wstr(IDS_NA); } case managed_e: return managed_ ? L"Yes" : L"No"; case uuid_e: { std::wstringstream ss; ss << uuid_; return ss.str(); } case hash_e: return hash_; default: return L"(Undefined)"; // ??? }; }
void InsetInfo::updateInfo() { BufferParams const & bp = buffer().params(); switch (type_) { case UNKNOWN_INFO: error("Unknown Info: %1$s"); break; case SHORTCUT_INFO: case SHORTCUTS_INFO: { FuncRequest const func = lyxaction.lookupFunc(name_); if (func.action() == LFUN_UNKNOWN_ACTION) { error("Unknown action %1$s"); break; } KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func); if (bindings.empty()) { // It is impropriate to use error() for undefined shortcut setText(_("undefined")); break; } if (type_ == SHORTCUT_INFO) setText(bindings.begin()->print(KeySequence::Portable)); else setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable)); break; } case LYXRC_INFO: { ostringstream oss; if (name_.empty()) { setText(_("undefined")); break; } lyxrc.write(oss, true, name_); string result = oss.str(); if (result.size() < 2) { setText(_("undefined")); break; } string::size_type loc = result.rfind("\n", result.size() - 2); loc = loc == string::npos ? 0 : loc + 1; if (result.size() < loc + name_.size() + 1 || result.substr(loc + 1, name_.size()) != name_) { setText(_("undefined")); break; } // remove leading comments and \\name and space result = result.substr(loc + name_.size() + 2); // remove \n and "" result = rtrim(result, "\n"); result = trim(result, "\""); setText(from_utf8(result)); break; } case PACKAGE_INFO: // check in packages.lst setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no")); break; case TEXTCLASS_INFO: { // name_ is the class name LayoutFileList const & list = LayoutFileList::get(); bool available = false; if (list.haveClass(name_)) available = list[name_].isTeXClassAvailable(); setText(available ? _("yes") : _("no")); break; } case MENU_INFO: { docstring_list names; FuncRequest const func = lyxaction.lookupFunc(name_); if (func.action() == LFUN_UNKNOWN_ACTION) { error("Unknown action %1$s"); break; } // iterate through the menubackend to find it if (!theApp()->searchMenu(func, names)) { error("No menu entry for action %1$s"); break; } // if found, return its path. clear(); Paragraph & par = paragraphs().front(); Font const f(inherit_font, buffer().params().language); //Font fu = f; //fu.fontInfo().setUnderbar(FONT_ON); docstring_list::const_iterator beg = names.begin(); docstring_list::const_iterator end = names.end(); for (docstring_list::const_iterator it = beg ; it != end ; ++it) { // do not insert > for the top level menu item if (it != beg) par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR), Change(Change::UNCHANGED)); //FIXME: add proper underlines here. This // involves rewriting searchMenu used above to // return a vector of menus. If we do not do // that, we might as well use below // Paragraph::insert on each string (JMarc) for (size_type i = 0; i != it->length(); ++i) par.insertChar(par.size(), (*it)[i], f, Change(Change::UNCHANGED)); } break; } case ICON_INFO: { FuncRequest func = lyxaction.lookupFunc(name_); docstring icon_name = theApp()->iconName(func, true); //FIXME: We should use the icon directly instead of // going through FileName. The code below won't work // if the icon is embedded in the executable through // the Qt resource system. FileName file(to_utf8(icon_name)); if (!file.exists()) break; InsetGraphics * inset = new InsetGraphics(buffer_); InsetGraphicsParams igp; igp.filename = file; inset->setParams(igp); clear(); paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED)); break; } case BUFFER_INFO: { if (name_ == "name") { setText(from_utf8(buffer().fileName().onlyFileName())); break; } if (name_ == "path") { setText(from_utf8(buffer().filePath())); break; } if (name_ == "class") { setText(from_utf8(bp.documentClass().name())); break; } // everything that follows is for version control. // nothing that isn't version control should go below this line. if (!buffer().lyxvc().inUse()) { setText(_("No version control")); break; } LyXVC::RevisionInfo itype = LyXVC::Unknown; if (name_ == "vcs-revision") itype = LyXVC::File; else if (name_ == "vcs-tree-revision") itype = LyXVC::Tree; else if (name_ == "vcs-author") itype = LyXVC::Author; else if (name_ == "vcs-time") itype = LyXVC::Time; else if (name_ == "vcs-date") itype = LyXVC::Date; string binfo = buffer().lyxvc().revisionInfo(itype); if (binfo.empty()) setText(bformat(_("[[%1$s unknown]]"), from_utf8(name_))); else setText(from_utf8(binfo)); break; } case LYX_INFO: if (name_ == "version") setText(from_ascii(PACKAGE_VERSION)); break; } }
void InsetInfo::error(string const & err) { setText(bformat(_(err), from_utf8(name_)), Font(inherit_font, buffer().params().language), false); }
docstring InsetInfo::toolTip(BufferView const &, int, int) const { return bformat(_("Information regarding %1$s '%2$s'"), _(infoType()), from_utf8(name_)); }