Code

we are using a comparison with sizeof(long) to figure the size of time_t this is...
[rrdtool-all.git] / program / src / rrd_restore.c
index 962b64c47f4729c9e7cb7e2810759c2ca7431b75..cefdfaea1da699431e1d234256468a9cea04df4b 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3.7  Copyright by Tobi Oetiker, 1997-2009
+ * RRDtool 1.3.9  Copyright by Tobi Oetiker, 1997-2009
  * This file:     Copyright 2008 Florian octo Forster
  * Distributed under the GPL
  *****************************************************************************
@@ -32,6 +32,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <locale.h>
 
 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
 #include <math.h>
@@ -108,14 +109,14 @@ static int get_long_from_node(
 
     end_ptr = NULL;
     temp = strtol(str_ptr, &end_ptr, 0);
-    xmlFree(str_ptr);
 
     if (str_ptr == end_ptr) {
         rrd_set_error("get_long_from_node: Cannot parse buffer as long: %s",
                       str_ptr);
+        xmlFree(str_ptr);
         return (-1);
     }
-
+    xmlFree(str_ptr);
     *value = temp;
 
     return (0);
@@ -138,19 +139,60 @@ static int get_ulong_from_node(
 
     end_ptr = NULL;
     temp = strtoul(str_ptr, &end_ptr, 0);
-    xmlFree(str_ptr);
 
     if (str_ptr == end_ptr) {
         rrd_set_error("get_ulong_from_node: Cannot parse buffer as unsigned long: %s",
                       str_ptr);
+        xmlFree(str_ptr);
         return (-1);
     }
-
+    xmlFree(str_ptr);
     *value = temp;
 
     return (0);
 }                       /* int get_ulong_from_node */
 
+
+#ifdef WIN32
+/* Gross Hack Alert */
+#if _MSC_VER < 1300
+#define strtoll(p, e, b) ((*(e) = (char*)(p) + (((b) == 10) ? strspn((p), "0123456789") : 0)), _atoi64(p))
+#else
+#define strtoll(p, e, b) _strtoi64(p, e, b)
+#endif
+#endif
+
+static int get_llong_from_node(
+    xmlDoc * doc,
+    xmlNode * node,
+    long long *value)
+{
+    long long       temp;
+    char     *str_ptr;
+    char     *end_ptr;
+
+    str_ptr = (char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
+    if (str_ptr == NULL) {
+        rrd_set_error("get_llong_from_node: xmlNodeListGetString failed.");
+        return (-1);
+    }
+
+    end_ptr = NULL;
+    temp = strtoll(str_ptr, &end_ptr, 10);
+
+    if (str_ptr == end_ptr) {
+        rrd_set_error("get_llong_from_node: Cannot parse buffer as unsigned long long: %s",
+                      str_ptr);
+        xmlFree(str_ptr);
+        return (-1);
+    }
+    xmlFree(str_ptr);
+    *value = temp;
+
+    return (0);
+}                       /* int get_llong_from_node */
+
 static int get_double_from_node(
     xmlDoc * doc,
     xmlNode * node,
@@ -175,15 +217,15 @@ static int get_double_from_node(
 
     end_ptr = NULL;
     temp = strtod(str_ptr, &end_ptr);
-    xmlFree(str_ptr);
 
     if (str_ptr == end_ptr) {
         rrd_set_error
             ("get_double_from_node: Cannot parse buffer as double: %s",
              str_ptr);
+        xmlFree(str_ptr);
         return (-1);
     }
-
+    xmlFree(str_ptr);           
     *value = temp;
 
     return (0);
@@ -917,9 +959,19 @@ static int parse_tag_rrd(
         else if (xmlStrcmp(child->name, (const xmlChar *) "step") == 0)
             status = get_ulong_from_node(doc, child,
                                         &rrd->stat_head->pdp_step);
-        else if (xmlStrcmp(child->name, (const xmlChar *) "lastupdate") == 0)
-            status = get_long_from_node(doc, child,
-                                        &rrd->live_head->last_up);
+        else if (xmlStrcmp(child->name, (const xmlChar *) "lastupdate") == 0) {
+            if (sizeof(time_t) == sizeof(int)) {
+               status = get_long_from_node(doc, child, (long *)&rrd->live_head->last_up);
+            }
+            else { if (sizeof(time_t) == sizeof(long long)) {
+                       status = get_llong_from_node(doc, child, (long long *)&rrd->live_head->last_up);
+                    }
+                    else {
+                       rrd_set_error("can't convert to time_t ...", child->name);
+                       status = -1;    
+                    }
+            }
+        }
         else if (xmlStrcmp(child->name, (const xmlChar *) "ds") == 0)
             status = parse_tag_ds(doc, child, rrd);
         else if (xmlStrcmp(child->name, (const xmlChar *) "rra") == 0)
@@ -1087,6 +1139,7 @@ int rrd_restore(
     char **argv)
 {
     rrd_t    *rrd;
+    char*    old_locale;
 
 #ifdef WIN32
     srand((unsigned int) time(NULL));
@@ -1134,7 +1187,16 @@ int rrd_restore(
         return (-1);
     }
 
+#ifdef HAVE_SETLOCALE
+    old_locale = setlocale(LC_NUMERIC, "C");
+#endif
+
     rrd = parse_file(argv[optind]);
+
+#ifdef HAVE_SETLOCALE
+    setlocale(LC_NUMERIC, old_locale);
+#endif
+
     if (rrd == NULL)
         return (-1);