diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2014-11-10 00:06:25 -0800 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2014-11-10 00:06:43 -0800 |
commit | 38caa0d988c58a8acfee48ec157bd09fb62a4882 (patch) | |
tree | 9bfc80b0d9d35ccec3f513e912d49b0e897b138a | |
parent | 7c2a420e88e5c9375944b3b1c9d90af1c7daeb98 (diff) |
Replace some std::string with wcstring to reduce copying
-rw-r--r-- | env_universal_common.cpp | 66 | ||||
-rw-r--r-- | env_universal_common.h | 4 |
2 files changed, 28 insertions, 42 deletions
diff --git a/env_universal_common.cpp b/env_universal_common.cpp index 5da4c9dd..000c106e 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -66,17 +66,18 @@ #define SAVE_MSG "# This file is automatically generated by the fish.\n# Do NOT edit it directly, your changes will be overwritten.\n" static wcstring fishd_get_config(); -static std::string get_variables_file_path(const std::string &dir, const std::string &identifier); +static wcstring get_machine_identifier(); +static bool get_hostname_identifier(wcstring *result); static wcstring vars_filename_in_directory(const wcstring &wdir) { if (wdir.empty()) return L""; - const std::string dir = wcs2string(wdir); - const std::string machine_id = get_machine_identifier(); - const std::string machine_id_path = get_variables_file_path(dir, machine_id); - return str2wcstring(machine_id_path); + wcstring result = wdir; + result.append(L"/fishd."); + result.append(get_machine_identifier()); + return result; } static const wcstring &default_vars_path() @@ -122,13 +123,13 @@ static int check_runtime_path(const char * path) } /** Return the path of an appropriate runtime data directory */ -static std::string get_runtime_path() +static wcstring get_runtime_path() { - std::string path; + wcstring result; const char *dir = getenv("XDG_RUNTIME_DIR"); if (dir != NULL) { - path = dir; + result = str2wcstring(dir); } else { @@ -140,25 +141,26 @@ static std::string get_runtime_path() } // /tmp/fish.user - dir = "/tmp/fish."; - path.reserve(strlen(dir) + strlen(uname)); - path.append(dir); - path.append(uname); - if (check_runtime_path(path.c_str()) != 0) + std::string tmpdir = "/tmp/fish."; + tmpdir.append(uname); + if (check_runtime_path(tmpdir.c_str()) != 0) + { + debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", tmpdir.c_str()); + } + else { - debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", path.c_str()); - path.clear(); + result = str2wcstring(tmpdir); } } - return path; + return result; } /* Returns a "variables" file in the appropriate runtime directory. This is called infrequently and so does not need to be cached. */ static wcstring default_named_pipe_path() { - // Note that vars_filename_in_directory returns empty string wuhen passed the empty string - return vars_filename_in_directory(str2wcstring(get_runtime_path())); + // Note that vars_filename_in_directory returns empty string when passed the empty string + return vars_filename_in_directory(get_runtime_path()); } /** @@ -622,10 +624,10 @@ bool env_universal_t::load() { /* We failed to load, because the file was not found. Older fish used the hostname only. Try *moving* the filename based on the hostname into place; if that succeeds try again. Silently "upgraded." */ tried_renaming = true; - std::string hostname_id; + wcstring hostname_id; if (get_hostname_identifier(&hostname_id)) { - const wcstring hostname_path = wdirname(vars_path) + L'/' + str2wcstring(hostname_id); + const wcstring hostname_path = wdirname(vars_path) + L'/' + hostname_id; if (0 == wrename(hostname_path, vars_path)) { /* We renamed - try again */ @@ -962,16 +964,6 @@ void env_universal_t::parse_message_internal(const wcstring &msgstr, var_table_t } } -static std::string get_variables_file_path(const std::string &dir, const std::string &identifier) -{ - std::string name; - name.append(dir); - name.append("/"); - name.append("fishd."); - name.append(identifier); - return name; -} - /** Maximum length of hostname. Longer hostnames are truncated */ @@ -1054,31 +1046,29 @@ static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN]) #endif /* Function to get an identifier based on the hostname */ -bool get_hostname_identifier(std::string *result) +static bool get_hostname_identifier(wcstring *result) { bool success = false; char hostname[HOSTNAME_LEN + 1] = {}; if (gethostname(hostname, HOSTNAME_LEN) == 0) { - result->assign(hostname); + result->assign(str2wcstring(hostname)); success = true; } return success; } /* Get a sort of unique machine identifier. Prefer the MAC address; if that fails, fall back to the hostname; if that fails, pick something. */ -std::string get_machine_identifier(void) +wcstring get_machine_identifier() { - std::string result; + wcstring result; unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {}; if (get_mac_address(mac_addr)) { result.reserve(2 * MAC_ADDRESS_MAX_LEN); for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++) { - char buff[3]; - snprintf(buff, sizeof buff, "%02x", mac_addr[i]); - result.append(buff); + append_format(result, L"%02x", mac_addr[i]); } } else if (get_hostname_identifier(&result)) @@ -1088,7 +1078,7 @@ std::string get_machine_identifier(void) else { /* Fallback */ - result.assign("nohost"); + result.assign(L"nohost"); } return result; } diff --git a/env_universal_common.h b/env_universal_common.h index 207e4dba..16c06459 100644 --- a/env_universal_common.h +++ b/env_universal_common.h @@ -171,10 +171,6 @@ public: virtual bool notification_fd_became_readable(int fd); }; -std::string get_machine_identifier(); -bool get_hostname_identifier(std::string *result); - - bool universal_log_enabled(); #define UNIVERSAL_LOG(x) if (universal_log_enabled()) fprintf(stderr, "UNIVERSAL LOG: %s\n", x) |