We had identical format_val and to_string; standardize on to_string

This commit is contained in:
ridiculousfish 2012-03-05 10:18:42 -08:00
parent 82a93d8406
commit 230fb921ec
3 changed files with 32 additions and 41 deletions

View file

@ -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<typename T>
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<long>(x));
}
/* Helper class for managing a null-terminated array of null-terminated strings (of some char type) */
template <typename CharType_t>
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<typename T>
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<long>(x));
}
/**
Return the number of seconds from the UNIX epoch, with subsecond
precision. This function uses the gettimeofday function, and will

View file

@ -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" )
{

View file

@ -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<int>(pid));
event.arguments->push_back(format_val<int>(status));
event.arguments->push_back(to_string<int>(pid));
event.arguments->push_back(to_string<int>(status));
event_fire( &event );
event.arguments->resize(0);
}