1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-18 02:55:12 +00:00

spend some time with eve.{c,h}

* minimise core code hooks
* drop useless exporting of private functions (and make them static)
* reorder functions in eve.c so no prototypes are needed
* drop massive header include and add double include barrier in eve.h
This commit is contained in:
Phil Sutter 2009-10-04 18:39:28 +02:00
parent 700a32b2e7
commit ddca4aac68
4 changed files with 193 additions and 190 deletions

View File

@ -1828,8 +1828,7 @@ static void generate_text_internal(char *p, int p_max_size,
}
#ifdef EVE
OBJ(eve) {
char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
snprintf(p, p_max_size, "%s", skill);
print_eve(obj, p, p_max_size);
}
#endif
#ifdef HAVE_CURL

View File

@ -1445,17 +1445,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#endif
#ifdef EVE
END OBJ_ARG(eve, 0, "eve needs arguments: <userid> <apikey> <characterid>")
int argc;
char *userid = (char *) malloc(20 * sizeof(char));
char *apikey = (char *) malloc(64 * sizeof(char));
char *charid = (char *) malloc(20 * sizeof(char));
argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
obj->data.eve.charid = charid;
obj->data.eve.userid = userid;
obj->data.eve.apikey = apikey;
init_eve();
scan_eve(obj, arg);
#endif
#ifdef HAVE_CURL
END OBJ_ARG(curl, 0, "curl needs arguments: <uri> <interval in minutes>")

324
src/eve.c
View File

@ -24,6 +24,7 @@
#include "eve.h"
#include "config.h"
#include "text_object.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -42,6 +43,29 @@
#include <time.h>
#define MAXCHARS 4
#define EVE_UPDATE_DELAY 60
typedef struct {
char *charid;
char *skillname;
char *time;
char *lastOutput;
struct tm ends;
struct tm cache;
time_t delay;
int level;
int skill;
} Character;
struct xmlData {
char *data;
size_t size;
};
int num_chars = 0;
Character eveCharacters[MAXCHARS];
@ -108,7 +132,7 @@ int parseTrainingXml(char *data, Character * s)
return 0;
}
char *getXmlFromAPI(const char *userid, const char *apikey, const char *charid, const char *url)
static char *getXmlFromAPI(const char *userid, const char *apikey, const char *charid, const char *url)
{
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
@ -147,7 +171,7 @@ char *getXmlFromAPI(const char *userid, const char *apikey, const char *charid,
return content;
}
void init_eve(void)
static void init_eve(void)
{
int i;
@ -161,7 +185,151 @@ void init_eve(void)
}
}
char *eve(char *userid, char *apikey, char *charid)
static int isCacheValid(struct tm cached)
{
struct timeval tv;
struct timezone tz;
double offset = 0;
time_t now = 0;
time_t cache = 0;
double diff = 0;
gettimeofday(&tv, &tz);
offset = (double)(tz.tz_minuteswest * 60);
now = time(NULL);
cache = mktime(&cached);
diff = difftime(cache, now);
if (diff < offset)
return 0;
else
return 1;
}
static char *formatTime(struct tm *ends)
{
struct timeval tv;
struct timezone tz;
double offset = 0;
time_t now = 0;
time_t tEnds = 0;
long lin = 0;
long lie = 0;
long diff = 0;
gettimeofday(&tv, &tz);
offset = (double)(tz.tz_minuteswest * 60);
now = time(NULL);
tEnds = mktime(ends);
lin = (long)now;
lin += (long)offset;
lie = (long)tEnds;
diff = (lie - lin);
if (diff > 0) {
int days = (int)(diff / 60 / 60 / 24);
int hours = (int)((diff / 60 / 60) - (days * 24));
int minutes = (int)((diff / 60) - ((hours * 60) + (days * 60 * 24)));
int seconds = (int)(diff - ((minutes * 60) + (hours * 60 * 60) + (days * 60 * 60 * 24)));
char *output = malloc(100 * sizeof(char));
if (days > 0)
sprintf(output, "%dd, %dh, %02dm and %02ds", days, hours, minutes, seconds);
else if (hours > 0)
sprintf(output, "%dh, %02dm and %02ds", hours, minutes, seconds);
else
sprintf(output, "%02dm and %02ds", minutes, seconds);
return output;
} else {
char *output = strdup("Done");
return output;
}
}
static int file_exists(const char *filename)
{
struct stat fi;
if ((stat(filename, &fi)) == 0) {
if (fi.st_size > 0)
return 1;
else
return 0;
} else
return 0;
}
static void writeSkilltree(char *content, const char *filename)
{
FILE *fp = fopen(filename, "w");
fwrite(content, sizeof(char), strlen(content), fp);
fclose(fp);
}
static char *getSkillname(const char *file, int skillid)
{
char *skilltree;
char *skill = NULL;
xmlNodePtr n;
xmlDocPtr doc = 0;
xmlNodePtr root = 0;
if (!file_exists(file)) {
skilltree = getXmlFromAPI(NULL, NULL, NULL, EVEURL_SKILLTREE);
writeSkilltree(skilltree, file);
free(skilltree);
}
doc = xmlReadFile(file, NULL, 0);
if (!doc)
return NULL;
root = xmlDocGetRootElement(doc);
for (n = root->children; n; n = n->next) {
xmlNodePtr o;
for (o = n->children; o; o = o->next) {
xmlNodePtr p;
for (p = o->children; p; p = p->next) {
xmlNodePtr q;
for (q = p->children; q; q = q->next) {
xmlNodePtr r;
for (r = q->children; r; r = r->next) {
xmlElementPtr ele = (xmlElementPtr) r;
xmlAttrPtr attr = (xmlAttrPtr) ele->attributes;
char *mySkill;
int id, assigned = 0;
while (attr != NULL) {
if (!strcasecmp((const char *)attr->name, "typeName")) {
mySkill = strdup((const char *)attr->children->content);
assigned = 1;
} else if (!strcasecmp((const char *)attr->name, "typeID")) {
id = atoi((const char *)attr->children->content);
}
attr = attr->next;
}
if (id == skillid) {
skill = strdup(mySkill);
/* free(mySkill); */
goto END;
}
if (assigned)
free(mySkill);
}
}
}
}
}
END:
xmlFreeDoc(doc);
return skill;
}
static char *eve(char *userid, char *apikey, char *charid)
{
Character *chr = NULL;
const char *skillfile = "/tmp/.cesf";
@ -235,146 +403,24 @@ char *eve(char *userid, char *apikey, char *charid)
}
char *formatTime(struct tm *ends)
void scan_eve(struct text_object *obj, const char *arg)
{
struct timeval tv;
struct timezone tz;
double offset = 0;
time_t now = 0;
time_t tEnds = 0;
long lin = 0;
long lie = 0;
long diff = 0;
int argc;
char *userid = (char *) malloc(20 * sizeof(char));
char *apikey = (char *) malloc(64 * sizeof(char));
char *charid = (char *) malloc(20 * sizeof(char));
gettimeofday(&tv, &tz);
offset = (double)(tz.tz_minuteswest * 60);
now = time(NULL);
tEnds = mktime(ends);
lin = (long)now;
lin += (long)offset;
lie = (long)tEnds;
diff = (lie - lin);
argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
obj->data.eve.charid = charid;
obj->data.eve.userid = userid;
obj->data.eve.apikey = apikey;
if (diff > 0) {
int days = (int)(diff / 60 / 60 / 24);
int hours = (int)((diff / 60 / 60) - (days * 24));
int minutes = (int)((diff / 60) - ((hours * 60) + (days * 60 * 24)));
int seconds = (int)(diff - ((minutes * 60) + (hours * 60 * 60) + (days * 60 * 60 * 24)));
char *output = malloc(100 * sizeof(char));
if (days > 0)
sprintf(output, "%dd, %dh, %02dm and %02ds", days, hours, minutes, seconds);
else if (hours > 0)
sprintf(output, "%dh, %02dm and %02ds", hours, minutes, seconds);
else
sprintf(output, "%02dm and %02ds", minutes, seconds);
return output;
} else {
char *output = strdup("Done");
return output;
}
init_eve();
}
int isCacheValid(struct tm cached)
void print_eve(struct text_object *obj, char *p, int p_max_size)
{
struct timeval tv;
struct timezone tz;
double offset = 0;
time_t now = 0;
time_t cache = 0;
double diff = 0;
gettimeofday(&tv, &tz);
offset = (double)(tz.tz_minuteswest * 60);
now = time(NULL);
cache = mktime(&cached);
diff = difftime(cache, now);
if (diff < offset)
return 0;
else
return 1;
}
int file_exists(const char *filename)
{
struct stat fi;
if ((stat(filename, &fi)) == 0) {
if (fi.st_size > 0)
return 1;
else
return 0;
} else
return 0;
}
void writeSkilltree(char *content, const char *filename)
{
FILE *fp = fopen(filename, "w");
fwrite(content, sizeof(char), strlen(content), fp);
fclose(fp);
}
char *getSkillname(const char *file, int skillid)
{
char *skilltree;
char *skill = NULL;
xmlNodePtr n;
xmlDocPtr doc = 0;
xmlNodePtr root = 0;
if (!file_exists(file)) {
skilltree = getXmlFromAPI(NULL, NULL, NULL, EVEURL_SKILLTREE);
writeSkilltree(skilltree, file);
free(skilltree);
}
doc = xmlReadFile(file, NULL, 0);
if (!doc)
return NULL;
root = xmlDocGetRootElement(doc);
for (n = root->children; n; n = n->next) {
xmlNodePtr o;
for (o = n->children; o; o = o->next) {
xmlNodePtr p;
for (p = o->children; p; p = p->next) {
xmlNodePtr q;
for (q = p->children; q; q = q->next) {
xmlNodePtr r;
for (r = q->children; r; r = r->next) {
xmlElementPtr ele = (xmlElementPtr) r;
xmlAttrPtr attr = (xmlAttrPtr) ele->attributes;
char *mySkill;
int id, assigned = 0;
while (attr != NULL) {
if (!strcasecmp((const char *)attr->name, "typeName")) {
mySkill = strdup((const char *)attr->children->content);
assigned = 1;
} else if (!strcasecmp((const char *)attr->name, "typeID")) {
id = atoi((const char *)attr->children->content);
}
attr = attr->next;
}
if (id == skillid) {
skill = strdup(mySkill);
/* free(mySkill); */
goto END;
}
if (assigned)
free(mySkill);
}
}
}
}
}
END:
xmlFreeDoc(doc);
return skill;
snprintf(p, p_max_size, "%s",
eve(obj->data.eve.userid,
obj->data.eve.apikey, obj->data.eve.charid));
}

View File

@ -21,44 +21,12 @@
*
*/
#define _GNU_SOURCE
#define MAXCHARS 4
#define EVE_UPDATE_DELAY 60
#ifndef _EVE_H
#define _EVE_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct text_object;
#include <time.h>
void scan_eve(struct text_object *, const char *);
void print_eve(struct text_object *, char *, int);
typedef struct {
char *charid;
char *skillname;
char *time;
char *lastOutput;
struct tm ends;
struct tm cache;
time_t delay;
int level;
int skill;
} Character;
struct xmlData {
char *data;
size_t size;
};
char *eve(char *, char *, char *);
char *getXmlFromAPI(const char *, const char *, const char *, const char *);
char *getSkillname(const char *, int);
char *formatTime(struct tm *);
int parseTrainingXml(char *, Character *);
int parseSkilltreeXml(char *, char *);
int isCacheValid(struct tm);
int file_exists(const char *);
void writeSkilltree(char *, const char *);
void init_eve(void);
#endif /* _EVE_H */