changeset 184:b5aa2b9c425a

[/trunk] Changeset 196 by mikael * Don't use a char* as prefix, but split it to timestamp + flags. * Don't use a boolean for timestamp, use a time_t (actually we always use a timestamp so the bool made no sense...).
author mikael
date Thu, 05 May 2005 19:38:44 +0000
parents c658c131ea10
children e8e447a07641
files mcabber/src/hbuf.c mcabber/src/hbuf.h mcabber/src/histolog.c mcabber/src/hooks.c mcabber/src/screen.c mcabber/src/screen.h
diffstat 6 files changed, 70 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/hbuf.c	Thu May 05 15:01:48 2005 +0000
+++ b/mcabber/src/hbuf.c	Thu May 05 19:38:44 2005 +0000
@@ -26,7 +26,6 @@
 
 /* This is a private structure type */
 
-#define PREFIX_LENGTH 32
 typedef struct {
   char *ptr;
   char *ptr_end;        // beginning of the block
@@ -36,20 +35,21 @@
   // XXX This should certainly be a pointer, and be allocated only when needed
   // (for ex. when HBB_FLAG_PERSISTENT is set).
   struct { // hbuf_line_info
-    char prefix[PREFIX_LENGTH];
-  } persist;
+    time_t timestamp;
+    guchar flags;
+  } prefix;
 } hbuf_block;
 
 
-//  hbuf_add_line(p_hbuf, text, prefix, width)
+//  hbuf_add_line(p_hbuf, text, prefix_flags, width)
 // Add a line to the given buffer.  If width is not null, then lines are
 // wrapped at this length.
 //
 // Note 1: Splitting according to width won't work if there are tabs; they
 //         should be expanded before.
 // Note 2: width does not include the ending \0.
-void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix,
-        unsigned int width)
+void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp,
+        guint prefix_flags, guint width)
 {
   GList *hbuf = *p_hbuf;
   char *line, *cr, *end;
@@ -58,8 +58,8 @@
   if (!text) return;
 
   hbuf_block_elt = g_new0(hbuf_block, 1);
-  if (prefix)
-    strncpy(hbuf_block_elt->persist.prefix, prefix, PREFIX_LENGTH-1);
+  hbuf_block_elt->prefix.timestamp  = timestamp;
+  hbuf_block_elt->prefix.flags      = prefix_flags;
   if (!hbuf) {
     do {
       hbuf_block_elt->ptr  = g_new(char, HBB_BLOCKSIZE);
@@ -217,28 +217,33 @@
   }
 }
 
-//  hbuf_get_lines(hbuf, n, where)
+//  hbuf_get_lines(hbuf, n, where)  FIXME bad comments XXX
 // Returns an array of 2*n pointers (for n prefixes + n lines from hbuf)
 // (prefix line 1, line 1, prefix line 2, line 2, etc.)
 // (The first line will be the line currently pointed by hbuf)
 // Note:The caller should free the array after use.
-char **hbuf_get_lines(GList *hbuf, unsigned int n)
+hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n)
 {
   unsigned int i;
 
-  char **array = g_new0(char*, n*2);
-  char **array_elt = array;
+  hbb_line **array = g_new0(hbb_line*, n);
+  hbb_line **array_elt = array;
 
   for (i=0 ; i < n ; i++) {
     if (hbuf) {
       hbuf_block *blk = (hbuf_block*)(hbuf->data);
       int maxlen;
       maxlen = blk->ptr_end - blk->ptr;
-      *array_elt++ = blk->persist.prefix;
-      *array_elt++ = g_strndup(blk->ptr, maxlen);
+      *array_elt = (hbb_line*)g_new(hbb_line, 1);
+      (*array_elt)->timestamp = blk->prefix.timestamp;
+      (*array_elt)->flags     = blk->prefix.flags;
+      (*array_elt)->text      = g_strndup(blk->ptr, maxlen);
+
       hbuf = g_list_next(hbuf);
     } else
-      *array_elt++ = NULL;
+      break;
+
+    array_elt++;
   }
 
   return array;
--- a/mcabber/src/hbuf.h	Thu May 05 15:01:48 2005 +0000
+++ b/mcabber/src/hbuf.h	Thu May 05 19:38:44 2005 +0000
@@ -1,6 +1,7 @@
 #ifndef __HBUF_H__
 #define __HBUF_H__ 1
 
+#include <time.h>
 #include <glib.h>
 
 // With current implementation a message must fit in a hbuf block,
@@ -12,13 +13,24 @@
 // - PERSISTENT: this is a new history line
 #define HBB_FLAG_ALLOC      1
 #define HBB_FLAG_PERSISTENT 2
-// #define HBB_FLAG_FREE       4
+
+#define HBB_PREFIX_IN       1
+#define HBB_PREFIX_OUT      2
+#define HBB_PREFIX_STATUS   4
+#define HBB_PREFIX_AUTH     8
+#define HBB_PREFIX_INFO    16
 
-void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix,
-        unsigned int width);
+typedef struct {
+  time_t timestamp;
+  guchar flags;
+  char *text;
+} hbb_line;
+
+void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp,
+        guint prefix_flags, guint width);
 void hbuf_free(GList **p_hbuf);
 void hbuf_rebuild(GList **p_hbuf, unsigned int width);
 
-char **hbuf_get_lines(GList *hbuf, unsigned int n);
+hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n);
 
 #endif /* __HBUF_H__ */
--- a/mcabber/src/histolog.c	Thu May 05 15:01:48 2005 +0000
+++ b/mcabber/src/histolog.c	Thu May 05 19:38:44 2005 +0000
@@ -101,12 +101,12 @@
 void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width)
 {
   char *filename;
-  time_t timestamp;
   guchar type, info;
   char *data, *tail;
+  time_t timestamp;
+  guint prefix_flags;
   guint len;
   FILE *fp;
-  char prefix[32];
 
   if (!FileLoadLogs) return;
 
@@ -155,12 +155,11 @@
       *(tail-1) = 0;
 
     if (type == 'M') {
-      strftime(prefix, 12, "[%H:%M] ", localtime(&timestamp));
       if (info == 'S')
-        strcat(prefix, "--> ");
+        prefix_flags = HBB_PREFIX_OUT;
       else
-        strcat(prefix, "<== ");
-      hbuf_add_line(p_buddyhbuf, &data[18], prefix, width);
+        prefix_flags = HBB_PREFIX_IN;
+      hbuf_add_line(p_buddyhbuf, &data[18], timestamp, prefix_flags, width);
     }
   }
   fclose(fp);
--- a/mcabber/src/hooks.c	Thu May 05 15:01:48 2005 +0000
+++ b/mcabber/src/hooks.c	Thu May 05 19:38:44 2005 +0000
@@ -44,7 +44,7 @@
   // Note: the hlog_write should not be called first, because in some
   // cases scr_WriteIncomingMessage() will load the history and we'd
   // have the message twice...
-  scr_WriteIncomingMessage(jid, buffer);
+  scr_WriteIncomingMessage(jid, buffer, timestamp);
   hlog_write_message(jid, timestamp, FALSE, buffer);
   hk_ext_cmd(jid, 'M', 'R', NULL);
   free(buffer);
--- a/mcabber/src/screen.c	Thu May 05 15:01:48 2005 +0000
+++ b/mcabber/src/screen.c	Thu May 05 19:38:44 2005 +0000
@@ -250,8 +250,9 @@
 {
   int n;
   int width;
-  char **lines;
+  hbb_line **lines, *line;
   GList *hbuf_head;
+  char date[32];
 
   width = scr_WindowWidth(win_entry->win);
 
@@ -286,14 +287,22 @@
   // Display these lines
   for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
     wmove(win_entry->win, n, 0);
-    if (*(lines+2*n)) {
-      if (**(lines+2*n))
-        wprintw(win_entry->win, "%s", *(lines+2*n));      // prefix
+    line = *(lines+n);
+    if (line) {
+      if (line->timestamp) {
+        strftime(date, 35, "%H:%M", localtime(&line->timestamp));
+      } else
+        strcpy(date, "     ");
+      if (line->flags & HBB_PREFIX_IN)
+        wprintw(win_entry->win, "[%.5s] <== ", date);
+      else if (line->flags & HBB_PREFIX_OUT)
+        wprintw(win_entry->win, "[%.5s] --> ", date);
       else {
         wprintw(win_entry->win, "            ");
       }
-      wprintw(win_entry->win, "%s", *(lines+2*n+1));      // line
+      wprintw(win_entry->win, "%s", line->text);      // line
       wclrtoeol(win_entry->win);
+      g_free(line->text);
     } else {
       wclrtobot(win_entry->win);
       break;
@@ -353,26 +362,12 @@
 // Write some text in the winId window (this usually is a jid).
 // Lines are splitted when they are too long to fit in the chat window.
 // If this window doesn't exist, it is created.
-void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp,
-        const char *prefix, int force_show)
+void scr_WriteInWindow(const char *winId, const char *text, time_t timestamp,
+        unsigned int prefix_flags, int force_show)
 {
-  char *fullprefix = NULL;
   window_entry_t *win_entry;
   int dont_show = FALSE;
 
-  // Prepare the prefix
-  if (prefix || TimeStamp) {
-    if (!prefix)  prefix = "";
-    fullprefix = calloc(1, strlen(prefix)+16);
-    if (TimeStamp) {
-      time_t now = time(NULL);
-      strftime(fullprefix, 12, "[%H:%M] ", localtime(&now));
-    } else {
-      strcpy(fullprefix, "            ");
-    }
-    strcat(fullprefix, prefix);
-  }
-
   // Look for the window entry.
   win_entry = scr_SearchWindow(winId);
 
@@ -387,9 +382,8 @@
     win_entry = scr_CreateBuddyPanel(winId, dont_show);
   }
 
-  hbuf_add_line(&win_entry->hbuf, text, fullprefix,
+  hbuf_add_line(&win_entry->hbuf, text, timestamp, prefix_flags,
                 maxX - scr_WindowWidth(rosterWnd) - 14);
-  free(fullprefix);
 
   if (win_entry->cleared) {
     win_entry->cleared = 0; // The message must be displayed
@@ -661,22 +655,26 @@
   doupdate();
 }
 
-void scr_WriteMessage(const char *jid, const char *text, char *prefix)
+void scr_WriteMessage(const char *jid, const char *text, time_t timestamp,
+        guint prefix_flags)
 {
-  scr_WriteInWindow(jid, text, TRUE, prefix, FALSE);
+  if (!timestamp) timestamp = time(NULL);
+
+  scr_WriteInWindow(jid, text, timestamp, prefix_flags, FALSE);
 }
 
-void scr_WriteIncomingMessage(const char *jidfrom, const char *text)
+void scr_WriteIncomingMessage(const char *jidfrom, const char *text,
+        time_t timestamp)
 {
   // FIXME expand tabs / filter out special chars...
-  scr_WriteMessage(jidfrom, text, "<== ");
+  scr_WriteMessage(jidfrom, text, timestamp, HBB_PREFIX_IN);
   update_panels();
   doupdate();
 }
 
 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
 {
-  scr_WriteMessage(jidto, text, "--> ");
+  scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT);
   scr_ShowWindow(jidto);
 }
 
--- a/mcabber/src/screen.h	Thu May 05 15:01:48 2005 +0000
+++ b/mcabber/src/screen.h	Thu May 05 19:38:44 2005 +0000
@@ -23,7 +23,8 @@
 void scr_DrawMainWindow(unsigned int fullinit);
 void scr_DrawRoster(void);
 void scr_TerminateCurses(void);
-void scr_WriteIncomingMessage(const char *jidfrom, const char *text);
+void scr_WriteIncomingMessage(const char *jidfrom, const char *text,
+        time_t timestamp);
 void scr_WriteOutgoingMessage(const char *jidto,   const char *text);
 void scr_ShowBuddyWindow(void);
 void scr_LogPrint(const char *fmt, ...);