diff --git a/common.h b/common.h index 0e7bc09ab..7eaed8665 100644 --- a/common.h +++ b/common.h @@ -281,6 +281,19 @@ void assert_is_locked(void *mutex, const char *who); */ char *wcs2str_internal( const wchar_t *in, char *out ); +/** Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer. */ +wcstring format_size(long long sz); + +/** Version of format_size that does not allocate memory. */ +void format_size_safe(char buff[128], unsigned long long sz); + +/** Our crappier versions of debug which is guaranteed to not allocate any memory, or do anything other than call write(). This is useful after a call to fork() with threads. */ +void debug_safe(int level, const char *msg, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL); + +/** Writes out a long safely */ +void format_long_safe(char buff[128], long val); +void format_long_safe(wchar_t buff[128], long val); + template T from_string(const wcstring &x) { @@ -297,6 +310,20 @@ wcstring to_string(const T &x) { return stream.str(); } +/* wstringstream is a huge memory pig. Let's provide some specializations where we can. */ +template<> +inline wcstring to_string(const long &x) { + wchar_t buff[128]; + format_long_safe(buff, x); + return wcstring(buff); +} + +template<> +inline wcstring to_string(const int &x) { + return to_string(static_cast(x)); +} + + /* Helper class for managing a null-terminated array of null-terminated strings (of some char type) */ template class null_terminated_array_t { @@ -658,42 +685,6 @@ int create_directory( const wcstring &d ); */ void bugreport(); -/** Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer. */ -wcstring format_size(long long sz); - -/** Version of format_size that does not allocate memory. */ -void format_size_safe(char buff[128], unsigned long long sz); - -/** Our crappier versions of debug which is guaranteed to not allocate any memory, or do anything other than call write(). This is useful after a call to fork() with threads. */ -void debug_safe(int level, const char *msg, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL); - -/** Writes out a long safely */ -void format_long_safe(char buff[128], long val); -void format_long_safe(wchar_t buff[128], long val); - - -/** Converts some type to a wstring. */ -template -inline wcstring format_val(T x) { - std::wstringstream stream; - stream << x; - return stream.str(); -} - -/* wstringstream is a huge memory pig. Let's provide some specializations where we can. */ -template<> -inline wcstring format_val(long x) { - wchar_t buff[128]; - format_long_safe(buff, x); - return wcstring(buff); -} - -template<> -inline wcstring format_val(int x) { - return format_val(static_cast(x)); -} - - /** Return the number of seconds from the UNIX epoch, with subsecond precision. This function uses the gettimeofday function, and will diff --git a/env.cpp b/env.cpp index 3795c896c..389b04508 100644 --- a/env.cpp +++ b/env.cpp @@ -1103,15 +1103,15 @@ env_var_t env_get_string( const wcstring &key ) } else if( key == L"COLUMNS" ) { - return format_val((long)common_get_width()); + return to_string((long)common_get_width()); } else if( key == L"LINES" ) { - return format_val((long)common_get_width()); + return to_string((long)common_get_width()); } else if( key == L"status" ) { - return format_val((long)proc_get_last_status()); + return to_string((long)proc_get_last_status()); } else if( key == L"umask" ) { diff --git a/proc.cpp b/proc.cpp index fe0f5bb31..7ad35bd4b 100644 --- a/proc.cpp +++ b/proc.cpp @@ -541,8 +541,8 @@ void proc_fire_event( const wchar_t *msg, int type, pid_t pid, int status ) event.param1.pid = pid; event.arguments->push_back(msg); - event.arguments->push_back(format_val(pid)); - event.arguments->push_back(format_val(status)); + event.arguments->push_back(to_string(pid)); + event.arguments->push_back(to_string(status)); event_fire( &event ); event.arguments->resize(0); }