#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define NUM_MATCHES 5

typedef struct 
  {
  long lon;
  long lat;
  long address;
  char name[30];
  } street_type;

/* ============= */
char *strip_space(char *s)
    {
    char *c = s;
    while(*c == ' ')
        c++;
    strcpy(s,c);
     while(*c++) /* clean off trailing cr*/
       if(*c==13 || *c == 10)
         *c = NULL;
    return s;
    }



CString GpsToStreet(char* truck)
{
   FILE *db_file; /* needed */
   FILE *truck_file;
   unsigned long differance, differance_array[NUM_MATCHES]; /* needed */
   street_type db_record, db_array[NUM_MATCHES];    /* needed */
   long lon,lat;                         /* needed */
   unsigned int sec,hr, min,seconds;     /* needed */
   int id, index,i;                      /* needed */ 
   char truck[256], truck_file_name[81], db_file_name[81], *truck_field_ptr[10],*stp;
	CString str;

	str = "";

   strcpy(db_file_name,"gvan.dli");

      
      if(truck[0]!='\n')
      {
/*=== here is the code the looks up one truck and write the record ==*/         
         stp = truck;
         strip_space(stp);
         truck_field_ptr[0]=stp;
         index = 1;
         while(*stp)
         {
         if(*stp=='|')
            {
            *stp= NULL;
            truck_field_ptr[index++] = stp+1;
            }
         stp++;
         }
         lon = atol(truck_field_ptr[2]);
         lat = atol(truck_field_ptr[3]);
         
         /*--------search the file------------*/
         memset(&db_array,0,sizeof(street_type)*NUM_MATCHES);
         for(i=0; i<NUM_MATCHES; i++)
            differance_array[i] = LONG_MAX;
         db_file=fopen(db_file_name,"rb");
         if (!db_file)
            {
            MessageBox("db_file_name failed opening");
            return str;
            }
         do
            {
            fread(&db_record,sizeof(street_type),1,db_file);
            if(feof(db_file))
               break;
            /* This is a very crude in inacrute method of calculting the */
            /* distance between two points the error increases with lattatude */
            /* If this prove to be a problem better method are available */
            
            differance = labs(db_record.lon-lon) + labs(db_record.lat-lat);

            for(i=0; i<NUM_MATCHES; i++)       /* if it closer copy it once*/
            if(differance<differance_array[i])
               {
               memcpy(&db_array[i],&db_record,sizeof(street_type));
               differance_array[i] = differance;
               break;
               }
            }while(!feof(db_file));
         fclose(db_file);
        
        seconds =atoi(truck_field_ptr[4]);
        hr = seconds/60/60;
        seconds = seconds-hr*60*60;
        min = seconds/60;
        sec = seconds-min*60;
        
        /*unit1|15|3616205|-9706713|225853|0|0 */
        /*      id, time(zulu),street1, address1, street2, address2  lat, lon, speed, status, data*/
        
        wsprintf(LPCTSTR(str), "%s, %02d:%02d:%02d, %ld, %s, %ld, %s, %f, %f, %s, %s, %s;\n",
                truck_field_ptr[0],hr,min,sec, 
                db_array[0].address,db_array[0].name, db_array[1].address,db_array[1].name, 
                (float)lat*0.00001, (float) lon*0.00001,
                truck_field_ptr[1], truck_field_ptr[5],truck_field_ptr[6]);                 
                

/*============= down to here ===============================*/           
      }
	  return str;
}
 
