99632623e83fd4272d57a871d8516a5259453820
[monky] / src / combine.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #include "core.h"
31 #include "logging.h"
32 #include "text_object.h"
33
34 void parse_combine_arg(struct text_object *obj, const char *arg, void *free_at_crash)
35 {
36         unsigned int i,j;
37         unsigned int indenting = 0;     //vars can be used as args for other vars
38         int startvar[2];
39         int endvar[2];
40         startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
41         j=0;
42         for (i=0; arg[i] != 0 && j < 2; i++) {
43                 if(startvar[j] == -1) {
44                         if(arg[i] == '$') {
45                                 startvar[j] = i;
46                         }
47                 }else if(endvar[j] == -1) {
48                         if(arg[i] == '{') {
49                                 indenting++;
50                         }else if(arg[i] == '}') {
51                                 indenting--;
52                         }
53                         if (indenting == 0 && arg[i+1] < 48) {  //<48 has 0, $, and the most used chars not used in varnames but not { or }
54                                 endvar[j]=i+1;
55                                 j++;
56                         }
57                 }
58         }
59         if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
60                 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
61                 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
62                 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
63
64                 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
65                 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
66
67                 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
68                 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
69
70                 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
71                 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
72
73                 obj->sub = malloc(sizeof(struct text_object));
74                 extract_variable_text_internal(obj->sub, obj->data.combine.left);
75                 obj->sub->sub = malloc(sizeof(struct text_object));
76                 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right);
77         } else {
78                 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
79         }
80 }
81
82 void print_combine(struct text_object *obj, char *p, struct information *cur)
83 {
84         char buf[2][max_user_text];
85         int i, j;
86         long longest=0;
87         int nextstart;
88         int nr_rows[2];
89         struct llrows {
90                 char* row;
91                 struct llrows* next;
92         };
93         struct llrows *ll_rows[2], *current[2];
94         struct text_object * objsub = obj->sub;
95
96         if (!cd)
97                 return;
98
99         p[0]=0;
100         for(i=0; i<2; i++) {
101                 nr_rows[i] = 1;
102                 nextstart = 0;
103                 ll_rows[i] = malloc(sizeof(struct llrows));
104                 current[i] = ll_rows[i];
105                 for(j=0; j<i; j++) objsub = objsub->sub;
106                 generate_text_internal(buf[i], max_user_text, *objsub, cur);
107                 for(j=0; buf[i][j] != 0; j++) {
108                         if(buf[i][j] == '\t') buf[i][j] = ' ';
109                         if(buf[i][j] == '\n') {
110                                 buf[i][j] = 0;
111                                 current[i]->row = strdup(buf[i]+nextstart);
112                                 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
113                                 current[i]->next = malloc(sizeof(struct llrows));
114                                 current[i] = current[i]->next;
115                                 nextstart = j + 1;
116                                 nr_rows[i]++;
117                         }
118                 }
119                 current[i]->row = strdup(buf[i]+nextstart);
120                 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
121                 current[i]->next = NULL;
122                 current[i] = ll_rows[i];
123         }
124         for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
125                 if(current[0]) {
126                         strcat(p, current[0]->row);
127                         i=strlen(current[0]->row);
128                 }else i = 0;
129                 while(i < longest) {
130                         strcat(p, " ");
131                         i++;
132                 }
133                 if(current[1]) {
134                         strcat(p, obj->data.combine.seperation);
135                         strcat(p, current[1]->row);
136                 }
137                 strcat(p, "\n");
138 #ifdef HAVE_OPENMP
139 #pragma omp parallel for schedule(dynamic,10)
140 #endif /* HAVE_OPENMP */
141                 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
142         }
143 #ifdef HAVE_OPENMP
144 #pragma omp parallel for schedule(dynamic,10)
145 #endif /* HAVE_OPENMP */
146         for(i=0; i<2; i++) {
147                 while(ll_rows[i] != NULL) {
148                         current[i]=ll_rows[i];
149                         free(current[i]->row);
150                         ll_rows[i]=current[i]->next;
151                         free(current[i]);
152                 }
153         }
154 }
155
156 void free_combine(struct text_object *obj)
157 {
158         free(obj->data.combine.left);
159         free(obj->data.combine.seperation);
160         free(obj->data.combine.right);
161         free_text_objects(obj->sub, 1);
162         free(obj->sub);
163 }