changeset 156:0a9fe14882dc

Introduce utility function getTimeStamp() and use it to lessen code duplication.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 03 Nov 2010 12:26:10 +0200
parents 0720ca51673e
children 3287901f9b78
files nnchat.c
diffstat 1 files changed, 18 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Wed Nov 03 01:33:39 2010 +0200
+++ b/nnchat.c	Wed Nov 03 12:26:10 2010 +0200
@@ -149,19 +149,26 @@
     return TRUE;
 }
 
+BOOL getTimeStamp(char *str, size_t len, const char *fmt)
+{
+    time_t stamp = time(NULL);
+    struct tm *stamp_tm;
+    if ((stamp_tm = localtime(&stamp)) != NULL) {
+        strftime(str, len, fmt, stamp_tm);
+        return TRUE;
+    } else {
+        str[0] = 0;
+        return FALSE;
+    }
+}
 
 void updateStatus(BOOL insertMode)
 {
-    char tmpStr[128] = "";
-    time_t timeStamp;
-    struct tm *tmpTime;;
+    char tmpStr[128];
     
     if (statusWin == NULL) return;
     
-    timeStamp = time(NULL);
-    if ((tmpTime = localtime(&timeStamp)) != NULL) {
-        strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
-    }
+    getTimeStamp(tmpStr, sizeof(tmpStr), "%H:%M:%S");
 
     wbkgdset(statusWin, COLOR_PAIR(10));
     werase(statusWin);
@@ -285,14 +292,9 @@
 
 void printMsgV(BOOL addStamp, BOOL logOnly, const char *fmt, va_list ap)
 {
-    char tmpStr[128] = "", buf[8192];
-    time_t timeStamp;
-    struct tm *tmpTime;;
+    char tmpStr[128], buf[8192];
     
-    timeStamp = time(NULL);
-    if ((tmpTime = localtime(&timeStamp)) != NULL) {
-        strftime(tmpStr, sizeof(tmpStr), "˝17˝[˝11˝%H:%M:%S˝17˝]˝0˝ ", tmpTime);
-    }
+    getTimeStamp(tmpStr, sizeof(tmpStr), "˝17˝[˝11˝%H:%M:%S˝17˝]˝0˝ ");
     
     vsnprintf(buf, sizeof(buf), fmt, ap);
     
@@ -421,14 +423,9 @@
 
 int handleLogin(int sock, const char *str)
 {
-    char tmpStr[256] = "";
-    time_t timeStamp;
-    struct tm *tmpTime;;
+    char tmpStr[256];
     
-    timeStamp = time(NULL);
-    if ((tmpTime = localtime(&timeStamp)) != NULL) {
-        strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime);
-    }
+    getTimeStamp(tmpStr, sizeof(tmpStr), "%c");
     
     if (!strncmp(str, "FAILURE", 7)) {
         printMsg("˝1˝Login failure˝0˝ - ˝3˝%s˝0˝\n", tmpStr);