Exemplo n.º 1
0
__attribute__((unused)) static void replace_all(wcstring &str, const wchar_t *needle,
                                                const wchar_t *replacement) {
    size_t needle_len = wcslen(needle);
    size_t offset = 0;
    while ((offset = str.find(needle, offset)) != wcstring::npos) {
        str.replace(offset, needle_len, replacement);
        offset += needle_len;
    }
}
Exemplo n.º 2
0
bool unescape_string(wcstring &str, int escape_special)
{
    bool success = false;
    wchar_t *result = unescape(str.c_str(), escape_special);
    if (result) {
        str.replace(str.begin(), str.end(), result);
        free(result);
        success = true;
    }
    return success;
}
Exemplo n.º 3
0
/**
   Attempts tilde expansion of the string specified, modifying it in place.
*/
static void expand_home_directory(wcstring &input)
{
    const wchar_t * const in = input.c_str();
    if (in[0] == HOME_DIRECTORY)
    {
        int tilde_error = 0;
        size_t tail_idx;
        wcstring home;

        if (in[1] == '/' || in[1] == '\0')
        {
            /* Current users home directory */

            home = env_get_string(L"HOME");
            tail_idx = 1;
        }
        else
        {
            /* Some other users home directory */
            const wchar_t *name_end = wcschr(in, L'/');
            if (name_end)
            {
                tail_idx = name_end - in;
            }
            else
            {
                tail_idx = wcslen(in);
            }
            wcstring name_str = input.substr(1, tail_idx - 1);
            std::string name_cstr = wcs2string(name_str);
            struct passwd *userinfo = getpwnam(name_cstr.c_str());

            if (userinfo == NULL)
            {
                tilde_error = 1;
                input[0] = L'~';
            }
            else
            {
                home = str2wcstring(userinfo->pw_dir);
            }
        }

        if (! tilde_error)
        {
            input.replace(input.begin(), input.begin() + tail_idx, home);
        }
    }
}