Blame libgweather/test_metar.c

rpm-build ca2b01
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
rpm-build ca2b01
/*
rpm-build ca2b01
 * Simple program to reproduce METAR parsing results from command line
rpm-build ca2b01
 */
rpm-build ca2b01
rpm-build ca2b01
#ifdef HAVE_CONFIG_H
rpm-build ca2b01
#include <config.h>
rpm-build ca2b01
#endif
rpm-build ca2b01
rpm-build ca2b01
#include <glib.h>
rpm-build ca2b01
#include <string.h>
rpm-build ca2b01
#include <stdio.h>
rpm-build ca2b01
#include "gweather-private.h"
rpm-build ca2b01
rpm-build ca2b01
#ifndef BUFLEN
rpm-build ca2b01
#define BUFLEN 4096
rpm-build ca2b01
#endif /* BUFLEN */
rpm-build ca2b01
rpm-build ca2b01
int
rpm-build ca2b01
main (int argc, char **argv)
rpm-build ca2b01
{
rpm-build ca2b01
    FILE *stream = stdin;
rpm-build ca2b01
    gchar* filename = NULL;
rpm-build ca2b01
    GOptionEntry entries[] = {
rpm-build ca2b01
	{ "file", 'f', 0, G_OPTION_ARG_FILENAME, &filename,
rpm-build ca2b01
	  "file containing METAR observations", NULL },
rpm-build ca2b01
	{ NULL }
rpm-build ca2b01
    };
rpm-build ca2b01
    GOptionContext* context;
rpm-build ca2b01
    GError* error = NULL;
rpm-build ca2b01
    char buf[BUFLEN];
rpm-build ca2b01
    int len;
rpm-build ca2b01
    GWeatherInfo *info;
rpm-build ca2b01
rpm-build ca2b01
    context = g_option_context_new ("- test libgweather metar parser");
rpm-build ca2b01
    g_option_context_add_main_entries (context, entries, NULL);
rpm-build ca2b01
    g_option_context_parse (context, &argc, &argv, &error);
rpm-build ca2b01
rpm-build ca2b01
    if (error) {
rpm-build ca2b01
	perror (error->message);
rpm-build ca2b01
	return error->code;
rpm-build ca2b01
    }
rpm-build ca2b01
    if (filename) {
rpm-build ca2b01
	stream = fopen (filename, "r");
rpm-build ca2b01
	if (!stream) {
rpm-build ca2b01
	    perror ("fopen");
rpm-build ca2b01
	    return -1;
rpm-build ca2b01
	}
rpm-build ca2b01
    } else {
rpm-build ca2b01
	fprintf (stderr, "Enter a METAR string...\n");
rpm-build ca2b01
    }
rpm-build ca2b01
rpm-build ca2b01
    while (fgets (buf, sizeof (buf), stream)) {
rpm-build ca2b01
	len = strlen (buf);
rpm-build ca2b01
	if (buf[len - 1] == '\n') {
rpm-build ca2b01
	    buf[--len] = '\0';
rpm-build ca2b01
	}
rpm-build ca2b01
	printf ("\n%s\n", buf);
rpm-build ca2b01
rpm-build ca2b01
	/* a bit hackish... */
rpm-build ca2b01
	info = g_object_new (GWEATHER_TYPE_INFO, NULL);
rpm-build ca2b01
	info->priv->valid = 1;
rpm-build ca2b01
	metar_parse (buf, info);
rpm-build ca2b01
	printf ("Returned info:\n");
rpm-build ca2b01
	printf ("  update:   %s", ctime (&info->priv->update));
rpm-build ca2b01
	printf ("  sky:      %s\n", gweather_info_get_sky (info));
rpm-build ca2b01
	printf ("  cond:     %s\n", gweather_info_get_conditions (info));
rpm-build ca2b01
	printf ("  temp:     %s\n", gweather_info_get_temp (info));
rpm-build ca2b01
	printf ("  dewp:     %s\n", gweather_info_get_dew (info));
rpm-build ca2b01
	printf ("  wind:     %s\n", gweather_info_get_wind (info));
rpm-build ca2b01
	printf ("  pressure: %s\n", gweather_info_get_pressure (info));
rpm-build ca2b01
	printf ("  vis:      %s\n", gweather_info_get_visibility (info));
rpm-build ca2b01
rpm-build ca2b01
	// TODO: retrieve location's lat/lon to display sunrise/set times
rpm-build ca2b01
    }
rpm-build ca2b01
    return 0;
rpm-build ca2b01
}