spend some time with eve.{c,h}
authorPhil Sutter <phil@nwl.cc>
Sun, 4 Oct 2009 16:39:28 +0000 (18:39 +0200)
committerPhil Sutter <phil@nwl.cc>
Mon, 12 Oct 2009 19:33:02 +0000 (21:33 +0200)
* 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

src/conky.c
src/core.c
src/eve.c
src/eve.h

index 73288dc..c819826 100644 (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
index 0e37afd..cf5396f 100644 (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>")
index c0635af..16446bf 100644 (file)
--- a/src/eve.c
+++ b/src/eve.c
@@ -24,6 +24,7 @@
 
 #include "eve.h"
 #include "config.h"
+#include "text_object.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
 #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,81 +185,28 @@ void init_eve(void)
        }
 }
 
-char *eve(char *userid, char *apikey, char *charid)
+static int isCacheValid(struct tm cached)
 {
-       Character *chr = NULL;
-       const char *skillfile = "/tmp/.cesf";
-       int i = 0;
-       char *output = 0;
-       char *timel = 0;
-       char *skill = 0;
-       char *content = 0;
+       struct timeval tv;
+       struct timezone tz;
+       double offset = 0;
        time_t now = 0;
-       char *error = 0;
-
-
-       for (i = 0; i < MAXCHARS; i++) {
-               if (eveCharacters[i].charid != NULL) {
-                       if (strcasecmp(eveCharacters[i].charid, charid) == 0) {
-                               chr = &eveCharacters[i];
-                               break;
-                       }
-               }
-       }
-
-       if (!chr) {
-               if (num_chars == MAXCHARS - 1)
-                       return NULL;
-               chr = &eveCharacters[num_chars];
-               chr->charid = strdup(charid);
-               num_chars++;
-       }
-
-       if (chr->delay > 0) {
-               now = time(NULL);
-               if (now < chr->delay) {
-                       output = strdup("Server error");
-                       return output;
-               } else
-                       chr->delay = 0;
-       }
-
-       if (isCacheValid(chr->cache)) {
-               output = (char *)malloc(200 * sizeof(char));
-               timel = strdup(formatTime(&chr->ends));
-               sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
-               free(timel);
-               return output;
-       } else {
-               content = getXmlFromAPI(userid, apikey, charid, EVEURL_TRAINING);
-               if (content == NULL) {
-                       error = strdup("Server error");
-                       now = time(NULL);
-                       now += (time_t) 1800;
-                       chr->delay = now;
-                       return error;
-               }
-
-               if (parseTrainingXml(content, chr)) {
-                       output = (char *)malloc(200 * sizeof(char));
-                       sprintf(output, "API error");
-                       return output;
-               }
-
-               output = (char *)malloc(200 * sizeof(char));
-               timel = formatTime(&chr->ends);
-               skill = getSkillname(skillfile, chr->skill);
-
-               chr->skillname = strdup(skill);
+       time_t cache = 0;
+       double diff = 0;
 
-               sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
-               free(skill);
-               return output;
-       }
+       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;
 }
 
-char *formatTime(struct tm *ends)
+static char *formatTime(struct tm *ends)
 {
        struct timeval tv;
        struct timezone tz;
@@ -276,28 +247,7 @@ char *formatTime(struct tm *ends)
        }
 }
 
-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;
-}
-
-int file_exists(const char *filename)
+static int file_exists(const char *filename)
 {
        struct stat fi;
 
@@ -310,14 +260,14 @@ int file_exists(const char *filename)
                return 0;
 }
 
-void writeSkilltree(char *content, const char *filename)
+static 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)
+static char *getSkillname(const char *file, int skillid)
 {
        char *skilltree;
        char *skill = NULL;
@@ -378,3 +328,99 @@ char *getSkillname(const char *file, int skillid)
 
        return skill;
 }
+
+static char *eve(char *userid, char *apikey, char *charid)
+{
+       Character *chr = NULL;
+       const char *skillfile = "/tmp/.cesf";
+       int i = 0;
+       char *output = 0;
+       char *timel = 0;
+       char *skill = 0;
+       char *content = 0;
+       time_t now = 0;
+       char *error = 0;
+
+
+       for (i = 0; i < MAXCHARS; i++) {
+               if (eveCharacters[i].charid != NULL) {
+                       if (strcasecmp(eveCharacters[i].charid, charid) == 0) {
+                               chr = &eveCharacters[i];
+                               break;
+                       }
+               }
+       }
+
+       if (!chr) {
+               if (num_chars == MAXCHARS - 1)
+                       return NULL;
+               chr = &eveCharacters[num_chars];
+               chr->charid = strdup(charid);
+               num_chars++;
+       }
+
+       if (chr->delay > 0) {
+               now = time(NULL);
+               if (now < chr->delay) {
+                       output = strdup("Server error");
+                       return output;
+               } else
+                       chr->delay = 0;
+       }
+
+       if (isCacheValid(chr->cache)) {
+               output = (char *)malloc(200 * sizeof(char));
+               timel = strdup(formatTime(&chr->ends));
+               sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
+               free(timel);
+               return output;
+       } else {
+               content = getXmlFromAPI(userid, apikey, charid, EVEURL_TRAINING);
+               if (content == NULL) {
+                       error = strdup("Server error");
+                       now = time(NULL);
+                       now += (time_t) 1800;
+                       chr->delay = now;
+                       return error;
+               }
+
+               if (parseTrainingXml(content, chr)) {
+                       output = (char *)malloc(200 * sizeof(char));
+                       sprintf(output, "API error");
+                       return output;
+               }
+
+               output = (char *)malloc(200 * sizeof(char));
+               timel = formatTime(&chr->ends);
+               skill = getSkillname(skillfile, chr->skill);
+
+               chr->skillname = strdup(skill);
+
+               sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
+               free(skill);
+               return output;
+       }
+
+}
+
+void scan_eve(struct text_object *obj, const char *arg)
+{
+       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();
+}
+
+void print_eve(struct text_object *obj, char *p, int p_max_size)
+{
+       snprintf(p, p_max_size, "%s",
+                       eve(obj->data.eve.userid,
+                               obj->data.eve.apikey, obj->data.eve.charid));
+}
index a124efa..8f144db 100644 (file)
--- a/src/eve.h
+++ b/src/eve.h
  *
  */
 
-#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 */