/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2010 Stephane Bisaro, aka Iri <iri@irizone.net>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SAFEfree(p)      { if (p) { free (p); (p) = NULL; } }

typedef struct
{
    char *oaci;
    char *date;
    char *time;
    char *wind_dir;
    char *wind_speed;
    char *wind_unit;
} OutputMetar; /*output = {NULL, NULL, NULL, NULL, NULL, NULL}*/


/* function from http://nicolasj.developpez.com/articles/libc/string/ */
char *str_sub (const char *s, unsigned int start, unsigned int end)
{
   char *new_s = NULL;

   if (s != NULL && start < end)
   {
      new_s = malloc (sizeof (*new_s) * (end - start + 2));
      if (new_s != NULL)
      {
         unsigned int i;
         for (i = start; i <= end; i++)
         {
            new_s[i-start] = s[i];
         }
         new_s[i-start+1] = '\0';
         new_s[i-start] = '\0';
      }
      else
      {
         fprintf (stderr, "Memoire insuffisante\n");
         exit (EXIT_FAILURE);
      }
   }
   return new_s;
}


void emptyBuffer ()
{
    int c = 0;
    while (c!= '\n' && c!= EOF)
        c = getchar ();
}

int myfgets (char * s, int len)
{
    char * posEnter = NULL;

    if (fgets (s, len, stdin) != NULL)
    {
        posEnter = strchr (s, '\n');
        if (posEnter != NULL)
            *posEnter = '\0';
        else
            emptyBuffer;
        return 1;
    }
    emptyBuffer;
    return 0;
}




int searchWordWind (char * word)
{
    int len = 0;
    char * end;

    len = strlen (word);
    end = str_sub (word, len-2, len);
    return 0;
}




int parser (char * metar)
{
    char * c;
    char * tmp;
    char * separateur = { " " };
    OutputMetar output;

    char * cr;
    char * cr2;

    /* OACI code */
    c = strtok (metar, separateur);
    output.oaci = (char *) c;

    /* Date & Time */
    c = strtok (NULL, separateur); c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    /*tmp = str_sub (c, 0, 1);*/
    cr = (char *) malloc(sizeof(char)*3);
    tmp = strndup (c+0, 2); printf ("\t000DATE >>> %s\n", tmp);
    /*output.date = tmp;*/
    cr = (char *) &output.date;
   /* memcpy (cr, tmp, 2);*/
   /* strncpy (cr, tmp, strlen (tmp));*/
    sprintf (cr, tmp, "%s");
    SAFEfree (tmp);
    /*tmp = str_sub (c, 2, 5);*/
    cr2 = (char *) malloc(sizeof(char)*5);
    tmp = strndup (c+2, 4);
    /*output.time = tmp;*/
    /*cr2 = (char *) &output.time;*/
    /*strncpy ((char *) &output.time, tmp, strlen (tmp));*/
    cr2 = (char *) &output.time;
    sprintf (cr2, tmp, "%s");
    /*memcpy ((char *) &output.time, tmp, 4);*/
    SAFEfree (tmp);

    /* Wind */
     c = strtok (NULL, separateur);
     /*strncasecmp*/




    while (c != NULL)
    {
        c = strtok (NULL, separateur);
        tmp = str_sub (c, 0, 1);
        /*printf ("\tTEST >>> %s\n", tmp);*/
        SAFEfree (tmp);
    }
    printf ("\tOACI >>> %s\n", output.oaci);
    printf ("\tDATE >>> %s\n", &output.date);
    printf ("\tTIME >>> %s\n", &output.time);
    return 0;
}



int main()
{
    char metar[1024] = "LFMV 241230Z AUTO 24003KT 190V280 9999NDV SCT023 BKN043 OVC064 16/11 Q1010";

    printf ("Decode Metar console\n");
    printf ("Enter a OACI code : ");
   /* myfgets (metar, 1024);*/
    printf ("\nMETAR : %s\n", metar);
    parser (metar);
    return 0;
}
