/soc/2012/sanket/statscollector-2.x.y: 385c3bad8b10: Extract sta...

sanket sanket at soc.pidgin.im
Wed Jul 18 06:07:09 EDT 2012


Changeset: 385c3bad8b10c7f8d3d600338bb0993ed2ed4baf
Author:	 sanket <sanket at soc.pidgin.im>
Date:	 2012-07-18 03:06 +0530
Branch:	 soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/385c3bad8b10

Description:

Extract status code in a simpler way

Though my earlier code did the same work which I currently
achieve in 2 lines, in a much fancy way. It apparently crashed
for no-connection.

The current code makes assumption about how the First line of
HTTP header looks like, which is reasonable.

diffstat:

 libpurple/plugins/statscollector.c |  42 +++++++++++--------------------------
 1 files changed, 13 insertions(+), 29 deletions(-)

diffs (60 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -789,43 +789,27 @@
 
 }
 
-static int
-elem(gchar **s){
-
-  /* Returns the lenght of a NULL terminated string array */
-
-  int len;
-
-  len = 0;
-  while(s[len]) len++;
-  return len;
-
-}
-
 static void
 url_callback(PurpleUtilFetchUrlData *url_data, gpointer user_data, \
     const gchar *url_text, gsize len, const gchar *error_message){
 
-  /* Check if the header has HTTP/1.1 200 ... */
+  /*
+   * Check if the header has HTTP/1.1 200 ...
+   * I am assuming that the first few characters will always follow
+   * the following format:
+   * HTTP/1.x xyz reason
+   */
 
-  char **lines, **parts;
-  int code;
-  gboolean success;
+  int code = -1;
+  char *header = url_text;
 
-  success = FALSE;
-
-  lines = g_strsplit(url_text, "\n", 3);
-  if(elem(lines) >= 1){
-    purple_debug_info("STATS", "%s", lines[0]);
-    parts = g_strsplit(lines[0]," ",3);
-    if(elem(parts) >= 2){
-      purple_debug_info("STATS", "%s", parts[1]);
-      code = atoi(parts[1]);
-      if(code==200) success = TRUE;
-    }
+  if(header && strlen(header) >= 14) {
+    header += 9;
+    header[3] = '\0';
+    code = atoi(header);
   }
 
-  if(success){
+  if(code == 200){
     purple_prefs_set_int("/plugins/core/statscollector/last-sent",\
         get_time_sec());
   }



More information about the Commits mailing list