From 9efa29be28c1c7fa769b68ae98a98d5d72a2c193 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sat, 17 Oct 2009 16:14:25 +0200 Subject: [PATCH] combine: outsource code --- src/Makefile.am | 14 ++--- src/combine.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++ src/combine.h | 37 +++++++++++ src/conky.c | 69 +------------------- src/core.c | 52 +-------------- 5 files changed, 212 insertions(+), 123 deletions(-) create mode 100644 src/combine.c create mode 100644 src/combine.h diff --git a/src/Makefile.am b/src/Makefile.am index a03232f9..f22eddae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -49,13 +49,13 @@ $(config_output): ${config_input} endif # BUILD_CONFIG_OUTPUT # source files always needed for compiling -mandatory_sources = colours.c colours.h common.c common.h conky.c conky.h \ - core.c core.h diskio.c diskio.h exec.c exec.h fs.c fs.h logging.h \ - mail.c mail.h mixer.c mixer.h template.c template.h timed_thread.c \ - timed_thread.h mboxscan.c mboxscan.h read_tcp.c read_tcp.h scroll.c \ - scroll.h specials.c specials.h tailhead.c tailhead.h temphelper.c \ - temphelper.h text_object.c text_object.h timeinfo.c timeinfo.h \ - algebra.c algebra.h +mandatory_sources = colours.c colours.h combine.c combine.h common.c common.h \ + conky.c conky.h core.c core.h diskio.c diskio.h exec.c exec.h fs.c \ + fs.h logging.h mail.c mail.h mixer.c mixer.h template.c template.h \ + timed_thread.c timed_thread.h mboxscan.c mboxscan.h read_tcp.c \ + read_tcp.h scroll.c scroll.h specials.c specials.h tailhead.c \ + tailhead.h temphelper.c temphelper.h text_object.c text_object.h \ + timeinfo.c timeinfo.h algebra.c algebra.h # source files only needed when the apropriate option is enabled audacious = audacious.c audacious.h diff --git a/src/combine.c b/src/combine.c new file mode 100644 index 00000000..99632623 --- /dev/null +++ b/src/combine.c @@ -0,0 +1,163 @@ +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * Conky, a system monitor, based on torsmo + * + * Any original torsmo code is licensed under the BSD license + * + * All code written since the fork of torsmo is licensed under the GPL + * + * Please see COPYING for details + * + * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen + * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#include "core.h" +#include "logging.h" +#include "text_object.h" + +void parse_combine_arg(struct text_object *obj, const char *arg, void *free_at_crash) +{ + unsigned int i,j; + unsigned int indenting = 0; //vars can be used as args for other vars + int startvar[2]; + int endvar[2]; + startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1; + j=0; + for (i=0; arg[i] != 0 && j < 2; i++) { + if(startvar[j] == -1) { + if(arg[i] == '$') { + startvar[j] = i; + } + }else if(endvar[j] == -1) { + if(arg[i] == '{') { + indenting++; + }else if(arg[i] == '}') { + indenting--; + } + if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or } + endvar[j]=i+1; + j++; + } + } + } + if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) { + obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1); + obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1); + obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1); + + strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]); + obj->data.combine.left[endvar[0] - startvar[0]] = 0; + + strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]); + obj->data.combine.seperation[startvar[1] - endvar[0]] = 0; + + strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]); + obj->data.combine.right[endvar[1] - startvar[1]] = 0; + + obj->sub = malloc(sizeof(struct text_object)); + extract_variable_text_internal(obj->sub, obj->data.combine.left); + obj->sub->sub = malloc(sizeof(struct text_object)); + extract_variable_text_internal(obj->sub->sub, obj->data.combine.right); + } else { + CRIT_ERR(obj, free_at_crash, "combine needs arguments: "); + } +} + +void print_combine(struct text_object *obj, char *p, struct information *cur) +{ + char buf[2][max_user_text]; + int i, j; + long longest=0; + int nextstart; + int nr_rows[2]; + struct llrows { + char* row; + struct llrows* next; + }; + struct llrows *ll_rows[2], *current[2]; + struct text_object * objsub = obj->sub; + + if (!cd) + return; + + p[0]=0; + for(i=0; i<2; i++) { + nr_rows[i] = 1; + nextstart = 0; + ll_rows[i] = malloc(sizeof(struct llrows)); + current[i] = ll_rows[i]; + for(j=0; jsub; + generate_text_internal(buf[i], max_user_text, *objsub, cur); + for(j=0; buf[i][j] != 0; j++) { + if(buf[i][j] == '\t') buf[i][j] = ' '; + if(buf[i][j] == '\n') { + buf[i][j] = 0; + current[i]->row = strdup(buf[i]+nextstart); + if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row); + current[i]->next = malloc(sizeof(struct llrows)); + current[i] = current[i]->next; + nextstart = j + 1; + nr_rows[i]++; + } + } + current[i]->row = strdup(buf[i]+nextstart); + if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row); + current[i]->next = NULL; + current[i] = ll_rows[i]; + } + for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) { + if(current[0]) { + strcat(p, current[0]->row); + i=strlen(current[0]->row); + }else i = 0; + while(i < longest) { + strcat(p, " "); + i++; + } + if(current[1]) { + strcat(p, obj->data.combine.seperation); + strcat(p, current[1]->row); + } + strcat(p, "\n"); +#ifdef HAVE_OPENMP +#pragma omp parallel for schedule(dynamic,10) +#endif /* HAVE_OPENMP */ + for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next; + } +#ifdef HAVE_OPENMP +#pragma omp parallel for schedule(dynamic,10) +#endif /* HAVE_OPENMP */ + for(i=0; i<2; i++) { + while(ll_rows[i] != NULL) { + current[i]=ll_rows[i]; + free(current[i]->row); + ll_rows[i]=current[i]->next; + free(current[i]); + } + } +} + +void free_combine(struct text_object *obj) +{ + free(obj->data.combine.left); + free(obj->data.combine.seperation); + free(obj->data.combine.right); + free_text_objects(obj->sub, 1); + free(obj->sub); +} diff --git a/src/combine.h b/src/combine.h new file mode 100644 index 00000000..22042c57 --- /dev/null +++ b/src/combine.h @@ -0,0 +1,37 @@ +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * Conky, a system monitor, based on torsmo + * + * Any original torsmo code is licensed under the BSD license + * + * All code written since the fork of torsmo is licensed under the GPL + * + * Please see COPYING for details + * + * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen + * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#ifndef _COMBINE_H +#define _COMBINE_H + +void parse_combine_arg(struct text_object *, const char *, void *); +void print_combine(struct text_object *, char *, struct information *); +void free_combine(struct text_object *); + +#endif /* _COMBINE_H */ diff --git a/src/conky.c b/src/conky.c index 4fcad2cb..fb235fa9 100644 --- a/src/conky.c +++ b/src/conky.c @@ -75,6 +75,7 @@ #include "algebra.h" #include "build.h" #include "colours.h" +#include "combine.h" #include "diskio.h" #include "exec.h" #ifdef X11 @@ -2530,73 +2531,7 @@ void generate_text_internal(char *p, int p_max_size, print_scroll(obj, p, p_max_size, cur); } OBJ(combine) { - char buf[2][max_user_text]; - int i, j; - long longest=0; - int nextstart; - int nr_rows[2]; - struct llrows { - char* row; - struct llrows* next; - }; - struct llrows *ll_rows[2], *current[2]; - struct text_object * objsub = obj->sub; - - p[0]=0; - for(i=0; i<2; i++) { - nr_rows[i] = 1; - nextstart = 0; - ll_rows[i] = malloc(sizeof(struct llrows)); - current[i] = ll_rows[i]; - for(j=0; jsub; - generate_text_internal(buf[i], max_user_text, *objsub, cur); - for(j=0; buf[i][j] != 0; j++) { - if(buf[i][j] == '\t') buf[i][j] = ' '; - if(buf[i][j] == '\n') { - buf[i][j] = 0; - current[i]->row = strdup(buf[i]+nextstart); - if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row); - current[i]->next = malloc(sizeof(struct llrows)); - current[i] = current[i]->next; - nextstart = j + 1; - nr_rows[i]++; - } - } - current[i]->row = strdup(buf[i]+nextstart); - if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row); - current[i]->next = NULL; - current[i] = ll_rows[i]; - } - for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) { - if(current[0]) { - strcat(p, current[0]->row); - i=strlen(current[0]->row); - }else i = 0; - while(i < longest) { - strcat(p, " "); - i++; - } - if(current[1]) { - strcat(p, obj->data.combine.seperation); - strcat(p, current[1]->row); - } - strcat(p, "\n"); - #ifdef HAVE_OPENMP - #pragma omp parallel for schedule(dynamic,10) - #endif /* HAVE_OPENMP */ - for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next; - } - #ifdef HAVE_OPENMP - #pragma omp parallel for schedule(dynamic,10) - #endif /* HAVE_OPENMP */ - for(i=0; i<2; i++) { - while(ll_rows[i] != NULL) { - current[i]=ll_rows[i]; - free(current[i]->row); - ll_rows[i]=current[i]->next; - free(current[i]); - } - } + print_combine(obj, p, cur); } #ifdef NVIDIA OBJ(nvidia) { diff --git a/src/core.c b/src/core.c index 51412c16..4db44eaa 100644 --- a/src/core.c +++ b/src/core.c @@ -34,6 +34,7 @@ #include "algebra.h" #include "build.h" #include "colours.h" +#include "combine.h" #include "diskio.h" #include "exec.h" #ifdef X11 @@ -1445,50 +1446,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ(scroll, 0) parse_scroll_arg(obj, arg, free_at_crash); END OBJ_ARG(combine, 0, "combine needs arguments: ") - unsigned int i,j; - unsigned int indenting = 0; //vars can be used as args for other vars - int startvar[2]; - int endvar[2]; - startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1; - j=0; - for (i=0; arg[i] != 0 && j < 2; i++) { - if(startvar[j] == -1) { - if(arg[i] == '$') { - startvar[j] = i; - } - }else if(endvar[j] == -1) { - if(arg[i] == '{') { - indenting++; - }else if(arg[i] == '}') { - indenting--; - } - if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or } - endvar[j]=i+1; - j++; - } - } - } - if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) { - obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1); - obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1); - obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1); - - strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]); - obj->data.combine.left[endvar[0] - startvar[0]] = 0; - - strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]); - obj->data.combine.seperation[startvar[1] - endvar[0]] = 0; - - strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]); - obj->data.combine.right[endvar[1] - startvar[1]] = 0; - - obj->sub = malloc(sizeof(struct text_object)); - extract_variable_text_internal(obj->sub, obj->data.combine.left); - obj->sub->sub = malloc(sizeof(struct text_object)); - extract_variable_text_internal(obj->sub->sub, obj->data.combine.right); - } else { - CRIT_ERR(obj, free_at_crash, "combine needs arguments: "); - } + parse_combine_arg(obj, arg, free_at_crash); #ifdef NVIDIA END OBJ_ARG(nvidia, 0, "nvidia needs an argument") if (set_nvidia_type(&obj->data.nvidia, arg)) { @@ -2128,11 +2086,7 @@ void free_text_objects(struct text_object *root, int internal) free_scroll(obj); break; case OBJ_combine: - free(data.combine.left); - free(data.combine.seperation); - free(data.combine.right); - free_text_objects(obj->sub, 1); - free(obj->sub); + free_combine(obj); break; #ifdef APCUPSD case OBJ_apcupsd: