mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-17 18:45:10 +00:00
c++-ify variable_substitute()
This commit is contained in:
parent
dee932d83b
commit
df6db63972
@ -152,60 +152,47 @@ FILE *open_file(const char *file, int *reported)
|
||||
return fp;
|
||||
}
|
||||
|
||||
void variable_substitute(const char *s, char *dest, unsigned int n)
|
||||
std::string variable_substitute(std::string s)
|
||||
{
|
||||
while (*s && n > 1) {
|
||||
if (*s == '$') {
|
||||
s++;
|
||||
if (*s != '$') {
|
||||
char buf[256];
|
||||
const char *a, *var;
|
||||
unsigned int len;
|
||||
std::string::size_type pos = 0;
|
||||
while((pos = s.find('$', pos)) != std::string::npos) {
|
||||
if(pos + 1 >= s.size())
|
||||
break;
|
||||
|
||||
/* variable is either $foo or ${foo} */
|
||||
if (*s == '{') {
|
||||
s++;
|
||||
a = s;
|
||||
while (*s && *s != '}') {
|
||||
s++;
|
||||
}
|
||||
} else {
|
||||
a = s;
|
||||
while (*s && (isalnum((int) *s) || *s == '_')) {
|
||||
s++;
|
||||
}
|
||||
if(s[pos+1] == '$') {
|
||||
s.erase(pos, 1);
|
||||
++pos;
|
||||
} else {
|
||||
std::string var;
|
||||
std::string::size_type l = 0;
|
||||
|
||||
if(isalpha(s[pos+1])) {
|
||||
l = 1;
|
||||
while(pos+l < s.size() && isalnum(s[pos+l]))
|
||||
++l;
|
||||
var = s.substr(pos+1, l-1);
|
||||
} else if(s[pos+1] == '{') {
|
||||
l = s.find('}', pos);
|
||||
if(l == std::string::npos)
|
||||
break;
|
||||
l -= pos - 1;
|
||||
var = s.substr(pos+2, l-3);
|
||||
} else
|
||||
++pos;
|
||||
|
||||
if(l) {
|
||||
s.erase(pos, l);
|
||||
const char *val = getenv(var.c_str());
|
||||
if(val) {
|
||||
s.insert(pos, val);
|
||||
pos += strlen(val);
|
||||
}
|
||||
|
||||
/* copy variable to buffer and look it up */
|
||||
len = (s - a > 255) ? 255 : (s - a);
|
||||
strncpy(buf, a, len);
|
||||
buf[len] = '\0';
|
||||
|
||||
if (*s == '}') {
|
||||
s++;
|
||||
}
|
||||
|
||||
var = getenv(buf);
|
||||
|
||||
if (var) {
|
||||
/* add var to dest */
|
||||
len = strlen(var);
|
||||
if (len >= n) {
|
||||
len = n - 1;
|
||||
}
|
||||
strncpy(dest, var, len);
|
||||
dest += len;
|
||||
n -= len;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
*dest++ = *s++;
|
||||
n--;
|
||||
}
|
||||
|
||||
*dest = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
void format_seconds(char *buf, unsigned int n, long seconds)
|
||||
|
@ -67,7 +67,7 @@ double get_time(void);
|
||||
std::string to_real_path(const std::string &source);
|
||||
FILE *open_file(const char *file, int *reported);
|
||||
int open_fifo(const char *file, int *reported);
|
||||
void variable_substitute(const char *s, char *dest, unsigned int n);
|
||||
std::string variable_substitute(std::string s);
|
||||
|
||||
void format_seconds(char *buf, unsigned int n, long t);
|
||||
void format_seconds_short(char *buf, unsigned int n, long t);
|
||||
|
24
src/conky.cc
24
src/conky.cc
@ -2795,11 +2795,9 @@ static void set_default_configurations(void)
|
||||
|
||||
free(current_mail_spool);
|
||||
{
|
||||
char buf[256];
|
||||
|
||||
variable_substitute(MAIL_FILE, buf, 256);
|
||||
if (buf[0] != '\0') {
|
||||
current_mail_spool = strndup(buf, text_buffer_size);
|
||||
std::string buf = variable_substitute(MAIL_FILE);
|
||||
if (not buf.empty()) {
|
||||
current_mail_spool = strndup(buf.c_str(), text_buffer_size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3461,13 +3459,11 @@ char load_config_file(const char *f)
|
||||
#endif /* BUILD_X11 */
|
||||
CONF("mail_spool") {
|
||||
if (value) {
|
||||
char buffer[256];
|
||||
std::string buffer = variable_substitute(value);
|
||||
|
||||
variable_substitute(value, buffer, 256);
|
||||
|
||||
if (buffer[0] != '\0') {
|
||||
if (not buffer.empty()) {
|
||||
free_and_zero(current_mail_spool);
|
||||
current_mail_spool = strndup(buffer, text_buffer_size);
|
||||
current_mail_spool = strndup(buffer.c_str(), text_buffer_size);
|
||||
}
|
||||
} else {
|
||||
CONF_ERR;
|
||||
@ -4211,12 +4207,10 @@ void initialisation(int argc, char **argv) {
|
||||
|
||||
#ifdef MAIL_FILE
|
||||
if (current_mail_spool == NULL) {
|
||||
char buf[256];
|
||||
std::string buf = variable_substitute(MAIL_FILE);
|
||||
|
||||
variable_substitute(MAIL_FILE, buf, 256);
|
||||
|
||||
if (buf[0] != '\0') {
|
||||
current_mail_spool = strndup(buf, text_buffer_size);
|
||||
if (not buf.empty()) {
|
||||
current_mail_spool = strndup(buf.c_str(), text_buffer_size);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -352,7 +352,7 @@ static void update_mail_count(struct local_mail_s *mail)
|
||||
void parse_local_mail_args(struct text_object *obj, const char *arg)
|
||||
{
|
||||
float n1;
|
||||
char mbox[256], dst[256];
|
||||
char mbox[256];
|
||||
struct local_mail_s *locmail;
|
||||
|
||||
if (!arg) {
|
||||
@ -370,11 +370,11 @@ void parse_local_mail_args(struct text_object *obj, const char *arg)
|
||||
}
|
||||
}
|
||||
|
||||
variable_substitute(mbox, dst, sizeof(dst));
|
||||
std::string dst = variable_substitute(mbox);
|
||||
|
||||
locmail = (struct local_mail_s*)malloc(sizeof(struct local_mail_s));
|
||||
memset(locmail, 0, sizeof(struct local_mail_s));
|
||||
locmail->mbox = strndup(dst, text_buffer_size);
|
||||
locmail->mbox = strndup(dst.c_str(), text_buffer_size);
|
||||
locmail->interval = n1;
|
||||
obj->data.opaque = locmail;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user