lines, words: outsource code
authorPhil Sutter <phil@nwl.cc>
Thu, 19 Nov 2009 22:59:50 +0000 (23:59 +0100)
committerPhil Sutter <phil@nwl.cc>
Thu, 19 Nov 2009 22:59:50 +0000 (23:59 +0100)
src/conky.c
src/tailhead.c
src/tailhead.h

index 2377b26..3e3e7de 100644 (file)
@@ -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) {
index c9eff2a..1c15be1 100644 (file)
@@ -33,6 +33,7 @@
 #include "text_object.h"
 #include "logging.h"
 #include <sys/stat.h>
+#include <ctype.h>
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
@@ -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);
+}
index ded4f1c..6bd3dff 100644 (file)
@@ -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 */