std::vector <std::pair <size_t, size_t> > PcRe::match (const std::wstring &str) const { // We are not interested in subpatterns // pcre_exec requires an extra 50% space in ovector int ovector[3]; std::string utf8 = wstring_to_utf8 (str); std::vector <std::pair <size_t, size_t> > ret; int rc; size_t offset = 0; while ((rc = pcre_exec ((const pcre *)re, (const pcre_extra *)re_ex, utf8.data (), utf8.length (), offset, PCRE_NOTEMPTY, ovector, 3)) >= 0) { // rc==0 means too many substrings. // We do not distinguish these situations // Values in ovector are in bytes, not in UTF-8 characters size_t wchar_offset = mbs_to_wstring (utf8.data (), ovector[0]).length (); size_t wchar_len = mbs_to_wstring (utf8.data () + ovector[0], ovector[1]-ovector[0]).length (); ret.push_back (std::make_pair (wchar_offset, wchar_len)); offset = ovector[1]; } return ret; }
DirEntList list_dir ( const std::wstring &directory, const UnaryCallback <const DirEnt &, bool> &filter, const BinaryCallback <const DirEnt &, const DirEnt &, bool> &comp ) { DirEntList filelist; std::string dirname = wstring_to_mbs (directory); if (DIR *dir = ::opendir (dirname.c_str ())) { DirEnt tmp_ent; if (*c(dirname).rbegin() != '/') { dirname += '/'; } while (struct dirent *ent = readdir (dir)) { tmp_ent.name = mbs_to_wstring (ent->d_name); tmp_ent.attr = get_file_attr (dirname + ent->d_name); if (!filter (tmp_ent)) { filelist.push_back (std::move (tmp_ent)); } } closedir (dir); // Sort them filelist.sort (wrap (comp)); } return filelist; }
template <> const std::basic_string<wchar_t> &get_home_dir <wchar_t> () { static std::wstring dir = mbs_to_wstring (get_home_dir <char> ()); return dir; }
template <> std::basic_string<wchar_t> get_current_dir <wchar_t> () { return mbs_to_wstring (get_current_dir<char>()); }
std::wstring get_home_dir (const std::wstring &user) { return mbs_to_wstring (get_home_dir (wstring_to_mbs (user).c_str())); }