From 87873bceeb1d01823ca36d9c770db13ebdd99202 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 19 Nov 2009 23:59:50 +0100 Subject: [PATCH] lines, words: outsource code --- src/conky.c | 50 ++----------------------------------------- src/tailhead.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/tailhead.h | 3 +++ 3 files changed, 63 insertions(+), 48 deletions(-) diff --git a/src/conky.c b/src/conky.c index 2377b266..3e3e7de7 100644 --- a/src/conky.c +++ b/src/conky.c @@ -2286,57 +2286,11 @@ void generate_text_internal(char *p, int p_max_size, print_tailhead("head", obj, p, p_max_size); } OBJ(lines) { - static int rep = 0; - FILE *fp = open_file(obj->data.s, &rep); - - if(fp != NULL) { -/* FIXME: use something more general (see also tail.c, head.c */ -#define BUFSZ 0x1000 - char buf[BUFSZ]; - int j, lines; - - lines = 0; - while(fgets(buf, BUFSZ, fp) != NULL){ - for(j = 0; buf[j] != 0; j++) { - if(buf[j] == '\n') { - lines++; - } - } - } - sprintf(p, "%d", lines); - fclose(fp); - } else { - sprintf(p, "File Unreadable"); - } + print_lines(obj, p, p_max_size); } OBJ(words) { - static int rep = 0; - FILE *fp = open_file(obj->data.s, &rep); - - if(fp != NULL) { - char buf[BUFSZ]; - int j, words; - char inword = FALSE; - - words = 0; - while(fgets(buf, BUFSZ, fp) != NULL){ - for(j = 0; buf[j] != 0; j++) { - if(!isspace(buf[j])) { - if(inword == FALSE) { - words++; - inword = TRUE; - } - } else { - inword = FALSE; - } - } - } - sprintf(p, "%d", words); - fclose(fp); - } else { - sprintf(p, "File Unreadable"); - } + print_words(obj, p, p_max_size); } #ifdef TCP_PORT_MONITOR OBJ(tcp_portmon) { diff --git a/src/tailhead.c b/src/tailhead.c index c9eff2a7..1c15be1b 100644 --- a/src/tailhead.c +++ b/src/tailhead.c @@ -33,6 +33,7 @@ #include "text_object.h" #include "logging.h" #include +#include #include #include #include @@ -178,3 +179,60 @@ void print_tailhead(const char* type, struct text_object *obj, char *p, int p_ma } return; } + +/* FIXME: use something more general (see also tail.c, head.c */ +#define BUFSZ 0x1000 + +void print_lines(struct text_object *obj, char *p, int p_max_size) +{ + static int rep = 0; + FILE *fp = open_file(obj->data.s, &rep); + char buf[BUFSZ]; + int j, lines; + + if (!fp) { + snprintf(p, p_max_size, "File Unreadable"); + return; + } + + lines = 0; + while(fgets(buf, BUFSZ, fp) != NULL){ + for(j = 0; buf[j] != 0; j++) { + if(buf[j] == '\n') { + lines++; + } + } + } + snprintf(p, p_max_size, "%d", lines); + fclose(fp); +} + +void print_words(struct text_object *obj, char *p, int p_max_size) +{ + static int rep = 0; + FILE *fp = open_file(obj->data.s, &rep); + char buf[BUFSZ]; + int j, words; + char inword = 0; + + if(!fp) { + snprintf(p, p_max_size, "File Unreadable"); + return; + } + + words = 0; + while(fgets(buf, BUFSZ, fp) != NULL){ + for(j = 0; buf[j] != 0; j++) { + if(!isspace(buf[j])) { + if(!inword) { + words++; + inword = 1; + } + } else { + inword = 0; + } + } + } + snprintf(p, p_max_size, "%d", words); + fclose(fp); +} diff --git a/src/tailhead.h b/src/tailhead.h index ded4f1c8..6bd3dff2 100644 --- a/src/tailhead.h +++ b/src/tailhead.h @@ -34,4 +34,7 @@ void free_tailhead(struct text_object *); void init_tailhead(const char *, const char *, struct text_object *, void *); void print_tailhead(const char *, struct text_object *, char *, int); +void print_lines(struct text_object *, char *, int); +void print_words(struct text_object *, char *, int); + #endif /* _TAILHEAD_H */