1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-28 13:00:45 +00:00

cpp-ify template.c

This commit is contained in:
Nikolas Garofil 2010-02-09 00:28:33 +01:00
parent a5ed45fc7b
commit b49ac8e6d3
3 changed files with 20 additions and 28 deletions

View File

@ -35,7 +35,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h) endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc
diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.c diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc
mboxscan.c read_tcp.c scroll.cc specials.cc tailhead.cc mboxscan.c read_tcp.c scroll.cc specials.cc tailhead.cc
temphelper.cc text_object.c timeinfo.c top.cc algebra.c prioqueue.c proc.c temphelper.cc text_object.c timeinfo.c top.cc algebra.c prioqueue.c proc.c
user.c) user.c)

View File

@ -1,5 +1,5 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- /* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c * vim: ts=4 sw=4 noet ai cindent syntax=cpp
* *
* Conky, a system monitor, based on torsmo * Conky, a system monitor, based on torsmo
* *
@ -31,11 +31,11 @@
#include "logging.h" #include "logging.h"
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string>
/* The templates defined by the user. /* The templates defined by the user.
* *
* This is a 1 to 1 mapping from templateN config option to template[N] field. */ * This is a 1 to 1 mapping from templateN config option to template[N] field. */
static char *template[MAX_TEMPLATES]; static char *_template[MAX_TEMPLATES]; //this is named _template because template is a reserved word in c++
/* free all templates /* free all templates
* *
@ -47,15 +47,15 @@ void free_templates(void)
static int initialised = 0; static int initialised = 0;
if (!initialised) { if (!initialised) {
memset(template, 0, MAX_TEMPLATES * sizeof(char *)); memset(_template, 0, MAX_TEMPLATES * sizeof(char *));
initialised = 1; initialised = 1;
return; return;
} }
for (i = 0; i < MAX_TEMPLATES; i++) { for (i = 0; i < MAX_TEMPLATES; i++) {
if (template[i]) { if (_template[i]) {
free(template[i]); free(_template[i]);
template[i] = NULL; _template[i] = NULL;
} }
} }
} }
@ -67,9 +67,9 @@ int set_template(int n, const char *val)
{ {
if (n < 0 || n >= MAX_TEMPLATES || !val) if (n < 0 || n >= MAX_TEMPLATES || !val)
return 1; return 1;
if (template[n]) if (_template[n])
free(template[n]); free(_template[n]);
template[n] = strdup(val); _template[n] = strdup(val);
return 0; return 0;
} }
@ -85,7 +85,7 @@ static char *backslash_escape(const char *src, char **templates, unsigned int te
unsigned int dup_idx = 0, dup_len; unsigned int dup_idx = 0, dup_len;
dup_len = strlen(src) + 1; dup_len = strlen(src) + 1;
src_dup = malloc(dup_len * sizeof(char)); src_dup = (char*) malloc(dup_len * sizeof(char));
p = src; p = src;
while (*p) { while (*p) {
@ -109,7 +109,7 @@ static char *backslash_escape(const char *src, char **templates, unsigned int te
(tmpl_num > template_count)) (tmpl_num > template_count))
break; break;
dup_len += strlen(templates[tmpl_num - 1]); dup_len += strlen(templates[tmpl_num - 1]);
src_dup = realloc(src_dup, dup_len * sizeof(char)); src_dup = (char*) realloc(src_dup, dup_len * sizeof(char));
sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]); sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
dup_idx += strlen(templates[tmpl_num - 1]); dup_idx += strlen(templates[tmpl_num - 1]);
p += digits; p += digits;
@ -122,7 +122,7 @@ static char *backslash_escape(const char *src, char **templates, unsigned int te
p++; p++;
} }
src_dup[dup_idx] = '\0'; src_dup[dup_idx] = '\0';
src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char)); src_dup = (char*) realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
return src_dup; return src_dup;
} }
@ -164,7 +164,7 @@ static char *handle_template(const char *tmpl, const char *args)
(*p) = '\0'; (*p) = '\0';
p++; p++;
} }
argsp = realloc(argsp, ++argcnt * sizeof(char *)); argsp = (char**) realloc(argsp, ++argcnt * sizeof(char *));
argsp[argcnt - 1] = p_old; argsp[argcnt - 1] = p_old;
} }
for (i = 0; i < argcnt; i++) { for (i = 0; i < argcnt; i++) {
@ -175,7 +175,7 @@ static char *handle_template(const char *tmpl, const char *args)
} }
} }
eval_text = backslash_escape(template[template_idx], argsp, argcnt); eval_text = backslash_escape(_template[template_idx], argsp, argcnt);
DBGP("substituted %s, output is '%s'", tmpl, eval_text); DBGP("substituted %s, output is '%s'", tmpl, eval_text);
free(args_dup); free(args_dup);
for (i = 0; i < argcnt; i++) for (i = 0; i < argcnt; i++)
@ -192,7 +192,7 @@ char *find_and_replace_templates(const char *inbuf)
int stack, outlen; int stack, outlen;
outlen = strlen(inbuf) + 1; outlen = strlen(inbuf) + 1;
o = outbuf = calloc(outlen, sizeof(char)); o = outbuf = (char*) calloc(outlen, sizeof(char));
memset(outbuf, 0, outlen * sizeof(char)); memset(outbuf, 0, outlen * sizeof(char));
p = indup = strdup(inbuf); p = indup = strdup(inbuf);
@ -244,7 +244,7 @@ char *find_and_replace_templates(const char *inbuf)
if (tmpl_out) { if (tmpl_out) {
outlen += strlen(tmpl_out); outlen += strlen(tmpl_out);
*o = '\0'; *o = '\0';
outbuf = realloc(outbuf, outlen * sizeof(char)); outbuf = (char*) realloc(outbuf, outlen * sizeof(char));
strcat (outbuf, tmpl_out); strcat (outbuf, tmpl_out);
free(tmpl_out); free(tmpl_out);
o = outbuf + strlen(outbuf); o = outbuf + strlen(outbuf);
@ -253,7 +253,7 @@ char *find_and_replace_templates(const char *inbuf)
} }
} }
*o = '\0'; *o = '\0';
outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char)); outbuf = (char*) realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
free(indup); free(indup);
return outbuf; return outbuf;
} }

View File

@ -31,18 +31,10 @@
#ifndef _TEMPLATE_H #ifndef _TEMPLATE_H
#define _TEMPLATE_H #define _TEMPLATE_H
#ifdef __cplusplus
extern "C" {
#endif
void free_templates(void); void free_templates(void);
int set_template(int, const char *); int set_template(int, const char *);
char *find_and_replace_templates(const char *); char *find_and_replace_templates(const char *);
int text_contains_templates(const char *); int text_contains_templates(const char *);
#ifdef __cplusplus
}
#endif
#endif /* _TEMPLATE_H */ #endif /* _TEMPLATE_H */