David Smart / NWSWeather
Revision:
11:98afc5abbfb8
Parent:
10:548533964124
Child:
12:d70de07ca914
diff -r 548533964124 -r 98afc5abbfb8 NWSWeather.cpp
--- a/NWSWeather.cpp	Mon Dec 22 17:21:50 2014 +0000
+++ b/NWSWeather.cpp	Thu Aug 06 11:12:15 2015 +0000
@@ -21,9 +21,9 @@
 // INFO("Stuff to show %d", var); // new-line is automatically appended
 //
 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
-#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define ERR(x, ...)  std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define INFO(x, ...) std::printf("[INF %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WRN %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...)  std::printf("[ERR %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
 #else
 #define INFO(x, ...)
 #define WARN(x, ...)
@@ -71,12 +71,15 @@
 
 NWSWeather::NWSReturnCode_T NWSWeather::setAlternateURL(const char * baseURL)
 {
+    int n = strlen(baseURL)+1;
+    
     if (m_baseurl)
         free(m_baseurl);
-    m_baseurl = (char *)malloc(strlen(baseURL)+1);
+    m_baseurl = (char *)malloc(n);
     if (m_baseurl)
-        strcpy(m_baseurl, baseURL);
+        strncpy(m_baseurl, baseURL, n);
     else {
+        ERR("failed to malloc(%d)", n);
         return nomemory;
     }
     ClearWeatherRecords();
@@ -97,9 +100,11 @@
 
     INFO("get(%s)", site);
     if (!message)
-        ERR("no memory");
+        ERR("failed to malloc(%d)", responseSize);
     while (message) {
-        url = (char *)malloc(strlen(m_baseurl) + strlen(site) + 5);
+        int n = strlen(m_baseurl) + strlen(site) + 5;
+        
+        url = (char *)malloc(n);
         if (url) {
             strcpy(url, m_baseurl);
             strcat(url, site);
@@ -115,6 +120,7 @@
                 } else {
                     INFO("wx get %d bytes.\r\n", strlen(message));
                     ParseWeatherXML(message);
+                    INFO("wx parse complete.\r\n");
                     retCode = noerror;
                     break;
                 }
@@ -124,7 +130,7 @@
                 break;
             }
         } else {
-            ERR("no memory");
+            ERR("failed to malloc(%d)", n);
             retCode = nomemory;
             break;
         }
@@ -256,9 +262,11 @@
     //  p1       p2   p3
     char *p1, *p2, *p3;
     int i;
+    int n;
     int count = WeatherItemCount;
 
-    p1 = strchr(p, '<');
+    INFO("ParseWeatherRecord(%s)", p);
+    p1 = strchr(p, '<');        // Pattern Matching <key>value</key>
     if (p1++) {
         p2 = strchr(p1+1, '>');
         if (p2) {
@@ -280,22 +288,29 @@
                             case isString:
                                 if (WeatherData[i].value.sValue)
                                     free(WeatherData[i].value.sValue);
-                                WeatherData[i].value.sValue = (char *)malloc(strlen(p2)+1);
+                                n = strlen(p2)+1;
+                                WeatherData[i].value.sValue = (char *)malloc(n);
                                 if (WeatherData[i].value.sValue) {
                                     strcpy(WeatherData[i].value.sValue, p2);
                                     WeatherData[i].updated = true;
+                                } else {
+                                    ERR("failed to malloc(%d)", n);
+                                    break;
                                 }
                                 break;
                             default:
+                                ERR("unknown type");
                                 return badparameter;
                                 //break;
                         }
+                        INFO("pw end");
                         return noerror;
                     }
                 }
             }
         }
     }
+    INFO("pw end");
     return noparamfound;
 }
 
@@ -303,7 +318,9 @@
 {
     char * p = message;
 
+    INFO("ParseWeatherXML: %s", p);
     ClearWeatherRecords();
+    INFO("cleared old");
     while (*p) {
         char * n = strchr(p, '\n');
         if (*n) {