//
//  main.cpp
//  Read GeoLiteCity csv files
//
//  Created by Robert Metcalfe on 29/05/13.
//  Copyright (c) 2013 Robert Metcalfe. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct my_block_struct {
    long startIpNum;
    long endIpNum;
    long locId;
};

struct my_location_struct {
    long locId;
    char country[3];
    char region[201];
    char city[201];
    double latitude;
    double longitude;
    char metroCode[201];
    char areaCode[201];
};


void read_block_record(struct my_block_struct *mblk, FILE *cReadp, bool summary) {
    char arec[201], *tok;
    long along, cnt=0;
    char first[201], last[201];
    first[0] = last[0] = 0;
    while (fgets(arec, 200, cReadp))
    {
      if (strstr(arec, ",") && (
          strstr(arec, "0") ||
          strstr(arec, "1") ||
          strstr(arec, "2") ||
          strstr(arec, "3") ||
          strstr(arec, "4") ||
          strstr(arec, "5") ||
          strstr(arec, "6") ||
          strstr(arec, "7") ||
          strstr(arec, "8") ||
          strstr(arec, "9"))) {
      tok = strtok(arec, ",");
      if (tok != NULL) {
            while (strstr(tok, "\"")) *(strstr(tok, "\"")) = ' ';
            sscanf(tok, "%ld", &along);
            mblk->startIpNum = along;
            tok = strtok(NULL, ",");
            while (strstr(tok, "\"")) *(strstr(tok, "\"")) = ' ';
            sscanf(tok, "%ld", &along);
            mblk->endIpNum = along;
            tok = strtok(NULL, ",");
            while (strstr(tok, "\"")) *(strstr(tok, "\"")) = ' ';
            sscanf(tok, "%ld", &along);
            mblk->locId = along;
            cnt++;
          if (first[0] == 0) {
              sprintf(first, "Block Record %ld has startIpNum,endIpNum,locId=%ld,%ld,%ld\n", cnt, mblk->startIpNum, mblk->endIpNum, mblk->locId);
              if (!summary) printf("%s", first);
          } else {
              sprintf(last, "Block Record %ld has startIpNum,endIpNum,locId=%ld,%ld,%ld\n", cnt, mblk->startIpNum, mblk->endIpNum, mblk->locId);
              if (!summary) printf("%s", last);
         }
      }
    }
    }
    if (summary) printf("%s%s", first, last);
}


void read_location_record(struct my_location_struct *mloc, FILE *cReadp, bool summary) {
    char arec[201], *tok, spare[201];
    long along, cnt=0, ioff = 0, i;
    double adouble;
    char first[201], last[201];
    first[0] = last[0] = 0;
    while (fgets(arec, 200, cReadp))
    {
        if (strstr(arec, ",") && (
                                  strstr(arec, "0") ||
                                  strstr(arec, "1") ||
                                  strstr(arec, "2") ||
                                  strstr(arec, "3") ||
                                  strstr(arec, "4") ||
                                  strstr(arec, "5") ||
                                  strstr(arec, "6") ||
                                  strstr(arec, "7") ||
                                  strstr(arec, "8") ||
                                  strstr(arec, "9"))) {
            tok = strtok(arec, ",");
            if (tok != NULL) {
                while (strstr(tok, "\"")) *(strstr(tok, "\"")) = ' ';
                sscanf(tok, "%ld", &along);
                mloc->locId = along;
                tok = strtok(NULL, ",");
                spare[0] = 0;
                for (i=0; i<strlen(tok); i++) {
                    if (*(tok+i) != '"') {
                        spare[strlen(spare) + 1] = 0;
                        spare[strlen(spare)] = *(tok+i);
                    }
                }
                strcpy(mloc->country, spare);
                tok = strtok(NULL, ",");
                spare[0] = 0;
                for (i=0; i<strlen(tok); i++) {
                    if (*(tok+i) != '"') {
                        spare[strlen(spare) + 1] = 0;
                        spare[strlen(spare)] = *(tok+i);
                    }
                }
                strcpy(mloc->region, spare);
                tok = strtok(NULL, ",");
                spare[0] = 0;
                for (i=0; i<strlen(tok); i++) {
                    if (*(tok+i) != '"') {
                        spare[strlen(spare) + 1] = 0;
                        spare[strlen(spare)] = *(tok+i);
                    }
                }
                strcpy(mloc->city, spare);
                
                tok = strtok(NULL, ",");
                while (strstr(tok, "\"")) *(strstr(tok, "\"")) = ' ';
                sscanf(tok, "%lf", &adouble);
                mloc->latitude = adouble;
                
                tok = strtok(NULL, ",");
                while (strstr(tok, "\"")) *(strstr(tok, "\"")) = ' ';
                sscanf(tok, "%lf", &adouble);
                mloc->longitude = adouble;
                tok = strtok(NULL, ",");
                spare[0] = 0;
                for (i=0; i<strlen(tok); i++) {
                    if (*(tok+i) != '"') {
                        spare[strlen(spare) + 1] = 0;
                        spare[strlen(spare)] = *(tok+i);
                    }
                }
                strcpy(mloc->metroCode, spare);
                tok = strtok(NULL, ",");
                spare[0] = 0;
                for (i=0; i<strlen(tok); i++) {
                    if (*(tok+i) != '"') {
                        spare[strlen(spare) + 1] = 0;
                        spare[strlen(spare)] = *(tok+i);
                    }
                }
                strcpy(mloc->areaCode, spare);
                cnt++;
                if (first[0] == 0) {
                    sprintf(first, "Location Record %ld has locId,country,region,city,metrocode,areacode=%ld,%s,%s,%s,%lf,%lf,%s,%s\n",
                            cnt, mloc->locId, mloc->country, mloc->region, mloc->city, mloc->latitude, mloc->longitude, mloc->metroCode, mloc->areaCode);
                    if (!summary) printf("%s", first);
                } else {
                    sprintf(last, "Location Record %ld has locId,country,region,city,metrocode,areacode=%ld,%s,%s,%s,%lf,%lf,%s,%s\n",
                            cnt, mloc->locId, mloc->country, mloc->region, mloc->city, mloc->latitude, mloc->longitude, mloc->metroCode, mloc->areaCode);
                    if (!summary) printf("%s", last);
                }
            }
        }
    }
    if (summary) printf("%s%s", first, last);
}



int main (void)
{
    FILE *csvReadp;
    
    csvReadp = fopen("/Users/robertmetcalfe/Downloads/GeoLiteCity_20121204/GeoLiteCity-Blocks.csv", "r");
    
    if (csvReadp == NULL ) {
        printf("\nFile /Users/robertmetcalfe/Downloads/GeoLiteCity_20121204/GeoLiteCity-Blocks.csv cannot be opened\n");    
    } else {        
        printf("\nContents of /Users/robertmetcalfe/Downloads/GeoLiteCity_20121204/GeoLiteCity-Blocks.csv\n\n");
    }
    struct my_block_struct block;
    read_block_record(&block, csvReadp, true);
    fclose(csvReadp);
    
    csvReadp = fopen("/Users/robertmetcalfe/Downloads/GeoLiteCity_20121204/GeoLiteCity-Location.csv", "r");
    
    if (csvReadp == NULL ) {
        printf("\nFile /Users/robertmetcalfe/Downloads/GeoLiteCity_20121204/GeoLiteCity-Location.csv cannot be opened\n");
    } else {
        printf("\nContents of /Users/robertmetcalfe/Downloads/GeoLiteCity_20121204/GeoLiteCity-Location.csv\n\n");
    }
    struct my_location_struct location;
    read_location_record(&location, csvReadp, true);
    fclose(csvReadp);
    return 0;
}
