* cleanup, improve, refactor message summary

This commit is contained in:
Dirk-Jan C. Binnema
2010-09-11 11:19:58 +03:00
parent aa1c7e9831
commit c09cb1303b
4 changed files with 64 additions and 45 deletions

View File

@ -18,10 +18,12 @@
*/
#include <glib.h>
#include <string.h>
#include "mu-msg-str.h"
#include "mu-msg-flags.h"
const char*
mu_msg_str_date_s (time_t t)
{
@ -106,3 +108,44 @@ mu_msg_str_prio (MuMsgPrio prio)
}
char*
mu_msg_str_summarize (const char* str, size_t max_lines)
{
char *summary;
size_t nl_seen;
unsigned i,j;
gboolean last_was_blank;
g_return_val_if_fail (str, NULL);
g_return_val_if_fail (max_lines > 0, NULL);
/* len for summary <= original len */
summary = g_new (gchar, strlen(str) + 1);
/* copy the string up to max_lines lines, replace CR/LF/tab with
* single space */
for (i = j = 0, nl_seen = 0, last_was_blank = TRUE;
nl_seen < max_lines && str[i] != '\0'; ++i) {
if (str[i] == '\n' || str[i] == '\r' ||
str[i] == '\t' || str[i] == ' ' ) {
if (str[i] == '\n')
++nl_seen;
/* no double-blanks or blank at end of str */
if (!last_was_blank && str[i+1] != '\0')
summary[j++] = ' ';
last_was_blank = TRUE;
} else {
summary[j++] = str[i];
last_was_blank = FALSE;
}
}
summary[j] = '\0';
return summary;
}