From c1cb65ff6dfa462394eff45135ecd5134092c5b1 Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 22 Mar 2008 18:08:47 +0000 Subject: [PATCH] smapi review * rewrote some functions in src/smapi.c for more simplicity and less malloc/free hell * added object cleanup in free_text_objects(), valgrind said it was missing * fixed indenting in construct_text_object() * fixed use of spaced_print() in generate_text_internal() git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@1028 7f574dfc-610e-0410-a909-a81674777703 --- src/conky.c | 28 +++++++++++++++++++--------- src/smapi.c | 30 +++++++++++------------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/conky.c b/src/conky.c index 2c2ac74d..60bbc84c 100644 --- a/src/conky.c +++ b/src/conky.c @@ -2189,6 +2189,16 @@ static void free_text_objects(unsigned int count, struct text_object *objs) info.users.times = 0; } break; +#ifdef SMAPI + case OBJ_smapi: + case OBJ_smapi_bat_perc: + free(objs[i].data.s); + break; + case OBJ_if_smapi_bat_installed: + free(objs[i].data.ifblock.s); + free(objs[i].data.ifblock.str); + break; +#endif #ifdef MPD case OBJ_mpd_title: case OBJ_mpd_artist: @@ -3497,14 +3507,14 @@ static struct text_object *construct_text_object(const char *s, if (blockdepth >= MAX_IF_BLOCK_DEPTH) { CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded"); } - if (!arg) { - ERR("if_smapi_bat_installed needs an argument"); - obj->data.ifblock.s = 0; - } else - obj->data.ifblock.s = strdup(arg); - blockstart[blockdepth] = object_count; - obj->data.ifblock.pos = object_count + 2; - blockdepth++; + if (!arg) { + ERR("if_smapi_bat_installed needs an argument"); + obj->data.ifblock.s = 0; + } else + obj->data.ifblock.s = strdup(arg); + blockstart[blockdepth] = object_count; + obj->data.ifblock.pos = object_count + 2; + blockdepth++; END OBJ(smapi_bat_perc, 0) if (arg) obj->data.s = strdup(arg); @@ -6004,7 +6014,7 @@ head: if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { val = smapi_bat_installed(idx) ? smapi_get_bat_int(idx, "remaining_percent") : 0; - spaced_print(p, p_max_size, "%*d", pad_percents, "smapi_bat_perc", val); + spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val); } else ERR("argument to smapi_bat_perc must be an integer"); } diff --git a/src/smapi.c b/src/smapi.c index 25f3e455..bfeaee78 100644 --- a/src/smapi.c +++ b/src/smapi.c @@ -30,44 +30,36 @@ int smapi_bat_installed(int idx) { char path[128]; struct stat sb; - char *str; + int ret = 0; snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx); if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) { snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx); - str = smapi_read_str(path); - if(str && ! strcmp(str, "1")) - return 1; + ret = (smapi_read_int(path) == 1) ? 1 : 0; } - return 0; + return ret; } char *smapi_read_str(char *path) { FILE *fp; - char *str; - if ((fp = fopen(path, "r")) == NULL) - return NULL; - if (fscanf(fp, "%as\n", &str) != 1) { + char *str = NULL; + if ((fp = fopen(path, "r")) != NULL) { + fscanf(fp, "%as\n", &str); fclose(fp); - return NULL; } - fclose(fp); return str; } int smapi_read_int(char *path) { FILE *fp; - int i; - if ((fp = fopen(path, "r")) == NULL) - return 0; - if (fscanf(fp, "%i\n", &i) != 1) { + int i = 0; + if ((fp = fopen(path, "r")) != NULL) { + fscanf(fp, "%i\n", &i); fclose(fp); - return 0; } - fclose(fp); return i; } @@ -115,9 +107,9 @@ char *smapi_get_bat_val(char *args) char *smapi_get_val(char *args) { - char *str; + char str[128]; - if(!args || sscanf(args, "%as", &str) <= 0) + if(!args || sscanf(args, "%127s", str) <= 0) return NULL; if(!strcmp(str, "bat"))