summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/common.c58
-rw-r--r--source/q_shared.h21
2 files changed, 47 insertions, 32 deletions
diff --git a/source/common.c b/source/common.c
index a4d76f0..bcd197f 100644
--- a/source/common.c
+++ b/source/common.c
@@ -214,20 +214,34 @@ static void logfile_param_changed( cvar_t *self ) {
}
}
+static size_t format_local_time( char *buffer, size_t size, const char *fmt ) {
+ time_t t;
+ struct tm *tm;
+
+ if( !size ) {
+ return 0;
+ }
+
+ buffer[0] = 0;
+
+ t = time( NULL );
+ tm = localtime( &t );
+ if( !tm ) {
+ return 0;
+ }
+
+ return strftime( buffer, size, fmt, tm );
+}
+
static void logfile_write( const char *string ) {
char text[MAXPRINTMSG];
- char timebuf[MAX_QPATH];
+ char buf[MAX_QPATH];
char *p, *maxp;
size_t len;
- time_t clock;
- struct tm *tm;
int c;
if( logfile_prefix->string[0] ) {
- time( &clock );
- tm = localtime( &clock );
- len = strftime( timebuf, sizeof( timebuf ),
- logfile_prefix->string, tm );
+ len = format_local_time( buf, sizeof( buf ), logfile_prefix->string );
} else {
len = 0;
}
@@ -235,13 +249,9 @@ static void logfile_write( const char *string ) {
p = text;
maxp = text + sizeof( text ) - 1;
while( *string ) {
- if( Q_IsColorString( string ) ) {
- string += 2;
- continue;
- }
if( com_logNewline ) {
if( len > 0 && p + len < maxp ) {
- memcpy( p, timebuf, len );
+ memcpy( p, buf, len );
p += len;
}
com_logNewline = qfalse;
@@ -252,14 +262,10 @@ static void logfile_write( const char *string ) {
}
c = *string++;
- if( c & 128 ) {
- c &= 127;
- if( c < 32 ) {
- continue;
- }
- }
if( c == '\n' ) {
com_logNewline = qtrue;
+ } else {
+ c = Q_charascii( c );
}
*p++ = c;
@@ -1187,13 +1193,7 @@ Com_Time_m
=============
*/
size_t Com_Time_m( char *buffer, size_t size ) {
- time_t clock;
- struct tm *local;
-
- time( &clock );
- local = localtime( &clock );
-
- return strftime( buffer, size, com_time_format->string, local );
+ return format_local_time( buffer, size, com_time_format->string );
}
/*
@@ -1202,13 +1202,7 @@ Com_Date_m
=============
*/
static size_t Com_Date_m( char *buffer, size_t size ) {
- time_t clock;
- struct tm *local;
-
- time( &clock );
- local = localtime( &clock );
-
- return strftime( buffer, size, com_date_format->string, local );
+ return format_local_time( buffer, size, com_date_format->string );
}
size_t Com_FormatTime( char *buffer, size_t size, time_t t ) {
diff --git a/source/q_shared.h b/source/q_shared.h
index 8869d1d..93175b4 100644
--- a/source/q_shared.h
+++ b/source/q_shared.h
@@ -405,16 +405,23 @@ int Q_HighlightStr( char *out, const char *in, int bufsize );
//=============================================
+// fast "C" macros
#define Q_isupper( c ) ( (c) >= 'A' && (c) <= 'Z' )
#define Q_islower( c ) ( (c) >= 'a' && (c) <= 'z' )
#define Q_isdigit( c ) ( (c) >= '0' && (c) <= '9' )
#define Q_isalpha( c ) ( Q_isupper( c ) || Q_islower( c ) )
#define Q_isalnum( c ) ( Q_isalpha( c ) || Q_isdigit( c ) )
#define Q_isprint( c ) ( (c) >= 32 && (c) < 127 )
+#define Q_isgraph( c ) ( (c) > 32 && (c) < 127 )
+#define Q_isspace( c ) ( c == ' ' || c == '\f' || c == '\n' || \
+ c == '\r' || c == '\t' || c == '\v' )
// tests if specified character is valid quake path character
#define Q_ispath( c ) ( Q_isalnum( c ) || (c) == '_' || (c) == '-' )
+// tests if specified character has special meaning to quake console
+#define Q_isspecial( c ) ( (c) == '\r' || (c) == '\n' || (c) == 127 )
+
static inline int Q_tolower( int c ) {
if( Q_isupper( c ) ) {
c += ( 'a' - 'A' );
@@ -464,6 +471,20 @@ static inline int Q_charhex( int c ) {
return -1;
}
+// converts quake char to ASCII equivalent
+static inline int Q_charascii( int c ) {
+ c &= 127; // strip high bits
+ if( Q_isgraph( c ) || Q_isspace( c ) ) {
+ return c;
+ }
+ switch( c ) {
+ // handle bold brackets
+ case 16: return '[';
+ case 17: return ']';
+ }
+ return '.'; // don't output control chars, etc
+}
+
// portable case insensitive compare
int Q_strcasecmp( const char *s1, const char *s2 );
int Q_strncasecmp( const char *s1, const char *s2, size_t n );