Replace some std::string with wcstring to reduce copying

This commit is contained in:
ridiculousfish 2014-11-10 00:06:25 -08:00
parent 7c2a420e88
commit 38caa0d988
2 changed files with 28 additions and 42 deletions

View file

@ -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" #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 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) static wcstring vars_filename_in_directory(const wcstring &wdir)
{ {
if (wdir.empty()) if (wdir.empty())
return L""; return L"";
const std::string dir = wcs2string(wdir); wcstring result = wdir;
const std::string machine_id = get_machine_identifier(); result.append(L"/fishd.");
const std::string machine_id_path = get_variables_file_path(dir, machine_id); result.append(get_machine_identifier());
return str2wcstring(machine_id_path); return result;
} }
static const wcstring &default_vars_path() 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 */ /** 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"); const char *dir = getenv("XDG_RUNTIME_DIR");
if (dir != NULL) if (dir != NULL)
{ {
path = dir; result = str2wcstring(dir);
} }
else else
{ {
@ -140,25 +141,26 @@ static std::string get_runtime_path()
} }
// /tmp/fish.user // /tmp/fish.user
dir = "/tmp/fish."; std::string tmpdir = "/tmp/fish.";
path.reserve(strlen(dir) + strlen(uname)); tmpdir.append(uname);
path.append(dir); if (check_runtime_path(tmpdir.c_str()) != 0)
path.append(uname);
if (check_runtime_path(path.c_str()) != 0)
{ {
debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", path.c_str()); debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", tmpdir.c_str());
path.clear(); }
else
{
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. */ /* 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() static wcstring default_named_pipe_path()
{ {
// Note that vars_filename_in_directory returns empty string wuhen passed the empty string // Note that vars_filename_in_directory returns empty string when passed the empty string
return vars_filename_in_directory(str2wcstring(get_runtime_path())); 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." */ /* 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; tried_renaming = true;
std::string hostname_id; wcstring hostname_id;
if (get_hostname_identifier(&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)) if (0 == wrename(hostname_path, vars_path))
{ {
/* We renamed - try again */ /* 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 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 #endif
/* Function to get an identifier based on the hostname */ /* 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; bool success = false;
char hostname[HOSTNAME_LEN + 1] = {}; char hostname[HOSTNAME_LEN + 1] = {};
if (gethostname(hostname, HOSTNAME_LEN) == 0) if (gethostname(hostname, HOSTNAME_LEN) == 0)
{ {
result->assign(hostname); result->assign(str2wcstring(hostname));
success = true; success = true;
} }
return success; 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. */ /* 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] = {}; unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {};
if (get_mac_address(mac_addr)) if (get_mac_address(mac_addr))
{ {
result.reserve(2 * MAC_ADDRESS_MAX_LEN); result.reserve(2 * MAC_ADDRESS_MAX_LEN);
for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++) for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++)
{ {
char buff[3]; append_format(result, L"%02x", mac_addr[i]);
snprintf(buff, sizeof buff, "%02x", mac_addr[i]);
result.append(buff);
} }
} }
else if (get_hostname_identifier(&result)) else if (get_hostname_identifier(&result))
@ -1088,7 +1078,7 @@ std::string get_machine_identifier(void)
else else
{ {
/* Fallback */ /* Fallback */
result.assign("nohost"); result.assign(L"nohost");
} }
return result; return result;
} }

View file

@ -171,10 +171,6 @@ public:
virtual bool notification_fd_became_readable(int fd); virtual bool notification_fd_became_readable(int fd);
}; };
std::string get_machine_identifier();
bool get_hostname_identifier(std::string *result);
bool universal_log_enabled(); bool universal_log_enabled();
#define UNIVERSAL_LOG(x) if (universal_log_enabled()) fprintf(stderr, "UNIVERSAL LOG: %s\n", x) #define UNIVERSAL_LOG(x) if (universal_log_enabled()) fprintf(stderr, "UNIVERSAL LOG: %s\n", x)