1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

lines, words: outsource code

This commit is contained in:
Phil Sutter 2009-11-19 23:59:50 +01:00
parent ea22f21252
commit 87873bceeb
3 changed files with 63 additions and 48 deletions

View 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) {

View 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);
}

View 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 */