combine: outsource code
authorPhil Sutter <phil@nwl.cc>
Sat, 17 Oct 2009 14:14:25 +0000 (16:14 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 00:50:28 +0000 (01:50 +0100)
src/Makefile.am
src/combine.c [new file with mode: 0644]
src/combine.h [new file with mode: 0644]
src/conky.c
src/core.c

index a03232f..f22edda 100644 (file)
@@ -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 (file)
index 0000000..9963262
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+#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: <text1> <text2>");
+       }
+}
+
+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; j<i; j++) objsub = objsub->sub;
+               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 (file)
index 0000000..22042c5
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+#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 */
index 4fcad2c..fb235fa 100644 (file)
@@ -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; j<i; j++) objsub = objsub->sub;
-                                       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) {
index 51412c1..4db44ea 100644 (file)
@@ -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: <text1> <text2>")
-               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: <text1> <text2>");
-               }
+               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: