int main (int argc, char **argv) { FILE* stream = stdin; gchar* filename = NULL; GOptionEntry entries[] = { { "file", 'f', 0, G_OPTION_ARG_FILENAME, &filename, "file constaining metar observations", NULL }, { NULL } }; GOptionContext* context; GError* error = NULL; char buf[BUFLEN]; int len; GWeatherInfo *info; context = g_option_context_new ("- test libgweather metar parser"); g_option_context_add_main_entries (context, entries, NULL); g_option_context_parse (context, &argc, &argv, &error); if (error) { perror (error->message); return error->code; } if (filename) { stream = fopen (filename, "r"); if (!stream) { perror ("fopen"); return -1; } } else { fprintf (stderr, "Enter a METAR string...\n"); } while (fgets (buf, sizeof (buf), stream)) { len = strlen (buf); if (buf[len - 1] == '\n') { buf[--len] = '\0'; } printf ("\n%s\n", buf); /* a bit hackish... */ info = g_object_new (GWEATHER_TYPE_INFO, NULL); info->priv->valid = 1; metar_parse (buf, info); printf ("Returned info:\n"); printf (" update: %s", ctime (&info->priv->update)); printf (" sky: %s\n", gweather_info_get_sky (info)); printf (" cond: %s\n", gweather_info_get_conditions (info)); printf (" temp: %s\n", gweather_info_get_temp (info)); printf (" dewp: %s\n", gweather_info_get_dew (info)); printf (" wind: %s\n", gweather_info_get_wind (info)); printf (" pressure: %s\n", gweather_info_get_pressure (info)); printf (" vis: %s\n", gweather_info_get_visibility (info)); // TODO: retrieve location's lat/lon to display sunrise/set times } return 0; }
static void metar_finish (SoupSession *session, SoupMessage *msg, gpointer data) { GWeatherInfo *info = (GWeatherInfo *)data; GWeatherInfoPrivate *priv; WeatherLocation *loc; const gchar *p, *eoln; gchar *searchkey, *metar; gboolean success = FALSE; g_return_if_fail (info != NULL); priv = info->priv; if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) { if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) priv->network_error = TRUE; else { /* Translators: %d is an error code, and %s the error string */ g_warning (_("Failed to get METAR data: %d %s.\n"), msg->status_code, msg->reason_phrase); } _gweather_info_request_done (info); return; } loc = &priv->location; searchkey = g_strdup_printf ("\n%s", loc->code); p = strstr (msg->response_body->data, searchkey); g_free (searchkey); if (p) { p += WEATHER_LOCATION_CODE_LEN + 2; eoln = strchr(p, '\n'); if (eoln) metar = g_strndup (p, eoln - p); else metar = g_strdup (p); success = metar_parse (metar, info); g_free (metar); } else if (!strstr (msg->response_body->data, "National Weather Service")) { /* The response doesn't even seem to have come from NWS... * most likely it is a wifi hotspot login page. Call that a * network error. */ priv->network_error = TRUE; } priv->valid = success; _gweather_info_request_done (info); }