1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-29 05:12:41 +00:00
conky/src/misc.cc

137 lines
3.8 KiB
C++
Raw Normal View History

2018-05-19 14:27:10 +00:00
/* -*- 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-2010 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/>.
*
*/
2018-12-21 02:20:43 +00:00
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
2018-05-19 14:27:10 +00:00
#include "conky.h"
#include "core.h"
#include "logging.h"
#include "specials.h"
#include "text_object.h"
2018-12-21 02:20:43 +00:00
static inline void read_file(const char *data, char *buf, const int size) {
FILE *fp;
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
memset(buf, 0, size);
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
if (!data) return;
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
fp = fopen(data, "r");
if (fp) {
int length;
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
length = fread(buf, 1, size - 1, fp);
fclose(fp);
buf[length] = '\0';
if (length > 0 && buf[length - 1] == '\n') { buf[length - 1] = '\0'; }
} else {
buf[0] = '\0';
}
2018-05-19 14:27:10 +00:00
}
2018-12-21 02:20:43 +00:00
static inline unsigned int file_buffer_size(const char *data,
const unsigned int maxsize) {
struct stat buf;
if (stat(data, &buf)) return maxsize;
if (buf.st_size < 0 || buf.st_size > maxsize) return maxsize;
if (buf.st_size < 10) return 10;
return buf.st_size + 1;
2018-05-19 14:27:10 +00:00
}
2018-12-21 02:20:43 +00:00
void print_cat(struct text_object *obj, char *p, unsigned int p_max_size) {
read_file(obj->data.s, p, p_max_size);
2018-05-19 14:27:10 +00:00
}
2018-12-21 02:20:43 +00:00
void print_catp(struct text_object *obj, char *p, unsigned int p_max_size) {
const unsigned int sz =
file_buffer_size(obj->data.s, text_buffer_size.get(*state));
char *buf = new char[sz];
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
read_file(obj->data.s, buf, sz);
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
evaluate(buf, p, p_max_size);
2018-05-19 14:27:10 +00:00
2018-12-21 02:20:43 +00:00
delete[] buf;
2018-05-19 14:27:10 +00:00
}
2020-01-16 23:56:45 +00:00
void print_startcase(struct text_object *obj, char *p,
unsigned int p_max_size) {
evaluate(obj->data.s, p, p_max_size);
for (unsigned int x = 0, z = 0; x < p_max_size - 1 && p[x]; x++) {
if (isspace(p[x])) {
z = 0;
} else {
p[x] = z ? tolower(p[x]) : toupper(p[x]);
z++;
}
}
}
2020-01-16 23:56:45 +00:00
void print_lowercase(struct text_object *obj, char *p,
unsigned int p_max_size) {
evaluate(obj->data.s, p, p_max_size);
for (unsigned int x = 0; x < p_max_size - 1 && p[x]; x++)
p[x] = tolower(p[x]);
}
2020-01-16 23:56:45 +00:00
void print_uppercase(struct text_object *obj, char *p,
unsigned int p_max_size) {
evaluate(obj->data.s, p, p_max_size);
for (unsigned int x = 0; x < p_max_size - 1 && p[x]; x++)
p[x] = toupper(p[x]);
}
void strip_trailing_whitespace(struct text_object *obj, char *p,
unsigned int p_max_size) {
evaluate(obj->data.s, p, p_max_size);
for (unsigned int x = p_max_size - 2;; x--) {
if (p[x] && !isspace(p[x])) {
p[x + 1] = '\0';
break;
} else if (x == 0) {
p[x] = '\0';
break;
}
}
}
long long int apply_base_multiplier(const char *s, long long int num) {
long long int base = 1024LL;
2018-12-21 02:20:43 +00:00
if (*s && (0 == (strcmp(s, "si")))) { base = 1000LL; }
return (num * base);
}