File: | weather-met.c |
Warning: | line 111, column 8 Dereference of null pointer (loaded from variable 'o') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ | |||
2 | /* weather-met.c - UK Met Office forecast source | |||
3 | * | |||
4 | * This program is free software; you can redistribute it and/or | |||
5 | * modify it under the terms of the GNU General Public License as | |||
6 | * published by the Free Software Foundation; either version 2 of the | |||
7 | * License, or (at your option) any later version. | |||
8 | * | |||
9 | * This program is distributed in the hope that it will be useful, but | |||
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
12 | * General Public License for more details. | |||
13 | * | |||
14 | * You should have received a copy of the GNU General Public License | |||
15 | * along with this program; if not, see | |||
16 | * <http://www.gnu.org/licenses/>. | |||
17 | */ | |||
18 | ||||
19 | #ifdef HAVE_CONFIG_H1 | |||
20 | #include <config.h> | |||
21 | #endif | |||
22 | ||||
23 | #include <ctype.h> | |||
24 | #include <stdlib.h> | |||
25 | #include <string.h> | |||
26 | ||||
27 | #define CAFEWEATHER_I_KNOW_THIS_IS_UNSTABLE | |||
28 | #include "weather.h" | |||
29 | #include "weather-priv.h" | |||
30 | ||||
31 | static char * | |||
32 | met_reprocess (char *x, int len) | |||
33 | { | |||
34 | char *p = x; | |||
35 | char *o; | |||
36 | int spacing = 0; | |||
37 | static gchar *buf; | |||
38 | static gint buflen = 0; | |||
39 | gchar *lastspace = NULL((void*)0); | |||
40 | int count = 0; | |||
41 | ||||
42 | if (buflen < len) | |||
43 | { | |||
44 | if (buf) | |||
45 | g_free (buf); | |||
46 | buf = g_malloc (len + 1); | |||
47 | buflen = len; | |||
48 | } | |||
49 | ||||
50 | o = buf; | |||
51 | x += len; /* End mark */ | |||
52 | ||||
53 | while (*p && p < x) { | |||
54 | if (g_ascii_isspace (*p)((g_ascii_table[(guchar) (*p)] & G_ASCII_SPACE) != 0)) { | |||
55 | if (!spacing) { | |||
56 | spacing = 1; | |||
57 | lastspace = o; | |||
58 | count++; | |||
59 | *o++ = ' '; | |||
60 | } | |||
61 | p++; | |||
62 | continue; | |||
63 | } | |||
64 | spacing = 0; | |||
65 | if (count > 75 && lastspace) { | |||
66 | count = o - lastspace - 1; | |||
67 | *lastspace = '\n'; | |||
68 | lastspace = NULL((void*)0); | |||
69 | } | |||
70 | ||||
71 | if (*p == '&') { | |||
72 | if (g_ascii_strncasecmp (p, "&", 5) == 0) { | |||
73 | *o++ = '&'; | |||
74 | count++; | |||
75 | p += 5; | |||
76 | continue; | |||
77 | } | |||
78 | if (g_ascii_strncasecmp (p, "<", 4) == 0) { | |||
79 | *o++ = '<'; | |||
80 | count++; | |||
81 | p += 4; | |||
82 | continue; | |||
83 | } | |||
84 | if (g_ascii_strncasecmp (p, ">", 4) == 0) { | |||
85 | *o++ = '>'; | |||
86 | count++; | |||
87 | p += 4; | |||
88 | continue; | |||
89 | } | |||
90 | } | |||
91 | if (*p == '<') { | |||
92 | if (g_ascii_strncasecmp (p, "<BR>", 4) == 0) { | |||
93 | *o++ = '\n'; | |||
94 | count = 0; | |||
95 | } | |||
96 | if (g_ascii_strncasecmp (p, "<B>", 3) == 0) { | |||
97 | *o++ = '\n'; | |||
98 | *o++ = '\n'; | |||
99 | count = 0; | |||
100 | } | |||
101 | p++; | |||
102 | while (*p && *p != '>') | |||
103 | p++; | |||
104 | if (*p) | |||
105 | p++; | |||
106 | continue; | |||
107 | } | |||
108 | *o++ = *p++; | |||
109 | count++; | |||
110 | } | |||
111 | *o = 0; | |||
| ||||
112 | return buf; | |||
113 | } | |||
114 | ||||
115 | ||||
116 | /* | |||
117 | * Parse the metoffice forecast info. | |||
118 | * For cafe 3.0 we want to just embed an HTML cafecomponent component and | |||
119 | * be done with this ;) | |||
120 | */ | |||
121 | ||||
122 | static gchar * | |||
123 | met_parse (const gchar *meto) | |||
124 | { | |||
125 | gchar *p; | |||
126 | gchar *rp; | |||
127 | gchar *r = g_strdup ("Met Office Forecast\n")g_strdup_inline ("Met Office Forecast\n"); | |||
128 | gchar *t; | |||
129 | ||||
130 | g_return_val_if_fail (meto != NULL, r)do { if ((meto != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "meto != NULL"); return (r); } } while (0); | |||
131 | ||||
132 | p = strstr (meto, "Summary: </b>"); | |||
133 | g_return_val_if_fail (p != NULL, r)do { if ((p != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "p != NULL"); return (r); } } while (0); | |||
134 | ||||
135 | rp = strstr (p, "Text issued at:"); | |||
136 | g_return_val_if_fail (rp != NULL, r)do { if ((rp != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "rp != NULL"); return (r); } } while (0); | |||
137 | ||||
138 | p += 13; | |||
139 | /* p to rp is the text block we want but in HTML malformat */ | |||
140 | t = g_strconcat (r, met_reprocess (p, rp - p), NULL((void*)0)); | |||
141 | g_free (r); | |||
142 | ||||
143 | return t; | |||
144 | } | |||
145 | ||||
146 | static void | |||
147 | met_finish (GObject *object, GAsyncResult *result, gpointer data) | |||
148 | { | |||
149 | SoupSession *session = SOUP_SESSION (object); | |||
150 | SoupMessage *msg = soup_session_get_async_result_message (session, result); | |||
151 | WeatherInfo *info = (WeatherInfo *)data; | |||
152 | GBytes *response_body = NULL((void*)0); | |||
153 | const gchar *msgdata; | |||
154 | ||||
155 | g_return_if_fail (info != NULL)do { if ((info != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "info != NULL"); return; } } while (0); | |||
| ||||
156 | ||||
157 | response_body = soup_session_send_and_read_finish (session, result, NULL((void*)0)); | |||
158 | ||||
159 | if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))((soup_message_get_status (msg)) >= 200 && (soup_message_get_status (msg)) < 300)) { | |||
160 | g_warning ("Failed to get Met Office forecast data: %d %s.\n", | |||
161 | soup_message_get_status (msg), soup_message_get_reason_phrase (msg)); | |||
162 | request_done (info, FALSE(0)); | |||
163 | g_bytes_unref (response_body); | |||
164 | return; | |||
165 | } | |||
166 | ||||
167 | msgdata = g_bytes_get_data(response_body, NULL((void*)0)); | |||
168 | ||||
169 | info->forecast = met_parse (msgdata); | |||
170 | request_done (info, TRUE(!(0))); | |||
171 | g_bytes_unref (response_body); | |||
172 | } | |||
173 | ||||
174 | void | |||
175 | metoffice_start_open (WeatherInfo *info) | |||
176 | { | |||
177 | gchar *url; | |||
178 | SoupMessage *msg; | |||
179 | WeatherLocation *loc; | |||
180 | ||||
181 | loc = info->location; | |||
182 | url = g_strdup_printf ("http://www.metoffice.gov.uk/weather/europe/uk/%s.html", loc->zone + 1); | |||
183 | ||||
184 | msg = soup_message_new ("GET", url); | |||
185 | soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT0, NULL((void*)0), met_finish, info); | |||
186 | g_free (url); | |||
187 | ||||
188 | info->requests_pending++; | |||
189 | } |