/*
    Module : CSVloader

    Purpose: Loads the scoring data from the CSV file.

    Version: 0.05

    Date   : 5th June 2008

    Started: 11th July 2005

    Author : Rick Murray
             http://www.heyrick.co.uk/eurovision/

*/

#include "euroscore.h"
#include <conio.h>




void csvloader_loadfile(char *filename)
{
   /* Loads the data from the CSV file...

      This code is fairly "unrolled" for maximum clarity (so it may look repetitive).
   */

   char *posn = NULL;
   char *tail = NULL;
   int  resloop = 0;

   // For the first time...
   if (countrycount == countrymax)
      country_extendarray();

   in = fopen(filename, "rb");
   if (in == NULL)
   {
      fprintf(stderr, "Unable to open CSV file \"%s\".\n", filename);
      exit(EXIT_FAILURE);
   }

   /* read from file */
   while ( !feof(in) )
   {
      // get line
      strcpy(workspace, "");
      fgets(workspace, 1024, in);

      if ( strlen(workspace) > 0 )
      {
         // Clip off linefeed(s)
         while ( !isprint(workspace[strlen(workspace) - 1]) )
            workspace[strlen(workspace) - 1] = '\0';
      }

      // Check for blank entry
      if ( (workspace[0] == ',') )
         strcpy(workspace, ""); // blank it

      if ( strlen(workspace) > 0 )
      {
         // The format is:
         //   "<country>",<score>,"<img>","<1pt>"..."<12pts>","<comnt>","<ttlname>","<refname>"
         //
         // Example:
         //   "Bosnia",79,"21bosherz","Hungary","Romania","Norway","Moldova",  [ cont'd <--' ]
         //   "Albania","Greece","Macedonia","Turkey","Serbia","Croatia",      [ cont'd <--' ]
         //   "but 1<sup>st</sup> in my heart","Bosnia &amp; Herzegovina","Bosnia &amp; H."

         // Get country name
         if (workspace[0] != '\"')
         {
            fclose(in);
            fprintf(stderr, "CSV decode error: +%d missing \" in %s.\n\n", \
                    countrycount, "country");
            exit(EXIT_FAILURE);
         }
         posn = workspace + 1;
         tail = strchr(posn, ',');
         if (tail == NULL)
         {
            fclose(in);
            fprintf(stderr, "CSV decode error: +%d missing comma in %s.\n\n", \
                    countrycount, "country");
            exit(EXIT_FAILURE);
         }
         tail--;
         tail[0] = '\0';
         strncpy(country[countrycount].name, posn, (STRLEN - 1));

         // Get score
         posn = tail + 2;
         if (posn[0] == ',')
         {
            // The score data is ",,", so this one didn't complete the semi
            // we can't use "0" to mark this as it'd interfere with "nul pwah"!
            country[countrycount].score = -1;
         }
         else
         {
            country[countrycount].score = atoi(posn);
            popularity += country[countrycount].score;
         }

         // Get image
         tail = strchr(posn, ',');
         posn = tail + 1;
         if (posn[0] != ',')
         {
            // we have a picture [and we have to detect if it is a semi-final referece]
            posn = tail + 2;
            if (posn[-1] != '\"')
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing \" in %s.\n\n", \
                       countrycount, "image");
               exit(EXIT_FAILURE);
            }
            tail = strchr(posn, ',');
            if (tail == NULL)
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing comma in %s.\n\n", \
                       countrycount, "image");
               exit(EXIT_FAILURE);
            }
            tail--;
            tail[0] = '\0';
            // image is at posn; format is "<semi>!<image>"
            if (posn[1] == '!')
            {
               country[countrycount].semi = posn[0]; // semi is "<num>!"...
               posn += 2;
            }
            else
            {
               country[countrycount].semi = 0; // no semi
            }
            strncpy(country[countrycount].image, posn, (STRLEN - 1));
         }
         else
         {
            // we don't have a picture
            strcpy(country[countrycount].image, "");
            country[countrycount].semi = 0; // no picture? assume no semi as well...
         }

         // Get scores (MUST be supplied)
         for (resloop = 0; resloop < 10; resloop++)
         {
            // Read the string
            posn = tail + 2;
            // fprintf(stderr, "Score decode: \n%s\n", posn);

            if (posn[0] != '\"')
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing \" in score %d.\n%s\n", \
                       countrycount, (resloop + 1), posn);
               exit(EXIT_FAILURE);
            }
            tail = strchr(posn, ',');
            if (tail == NULL)
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing comma in score %d.\n\n", \
                       countrycount, (resloop + 1));
               exit(EXIT_FAILURE);
            }
            tail--;
            tail[0] = '\0';
            posn++;
            strncpy(country[countrycount].pointsto[scorelist[resloop]], posn, (STRLEN - 1));
         }

         // Get comment
         posn = tail + 2;
         if (posn[0] != ',')
         {
            // we have a comment
            if (posn[0] != '\"')
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing \" in %s.\n\n", \
                       countrycount, "comment");
               exit(EXIT_FAILURE);
            }
            tail = strchr(posn, ',');
            if (tail == NULL)
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing comma in %s.\n\n", \
                       countrycount, "comment");
               exit(EXIT_FAILURE);
            }
            tail--;
            tail[0] = '\0';
            posn++;
            strncpy(country[countrycount].comment, posn, (STRLEN - 1));
         }
         else
         {
            // we don't have a comment
            strcpy(country[countrycount].comment, "");
            tail = posn - 1;
         }

         // Get title name
         posn = tail + 2;
         if (posn[0] != ',')
         {
            // we have a title name
            if (posn[0] != '\"')
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing \" in %s.\n\n", \
                       countrycount, "titlename");
               exit(EXIT_FAILURE);
            }
            tail = strchr(posn, ',');
            if (tail == NULL)
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing comma in %s.\n\n", \
                       countrycount, "titlename");
               exit(EXIT_FAILURE);
            }
            tail--;
            tail[0] = '\0';
            posn++;
            strncpy(country[countrycount].titlename, posn, (STRLEN - 1));
         }
         else
         {
            // we don't have a title name
            strcpy(country[countrycount].titlename, "");
            tail = posn - 1;
         }

         // Get reference name
         posn = tail + 2;
         if (posn[0] != ',')
         {
            // we have a reference name
            if (posn[0] != '\"')
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing \" in %s.\n\n", \
                       countrycount, "refname");
               exit(EXIT_FAILURE);
            }
            tail = strchr(posn, ',');
            if (tail == NULL)
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing comma in %s.\n\n", \
                       countrycount, "refname");
               exit(EXIT_FAILURE);
            }
            tail--;
            tail[0] = '\0';
            posn++;
            strncpy(country[countrycount].refname, posn, (STRLEN - 1));
         }
         else
         {
            // we don't have a reference name
            strcpy(country[countrycount].refname, "");
            tail = posn - 1;
         }
         
         // Get song title
         posn = tail + 2;
         if ( (strlen(posn) > 0) && (posn[0] != ',') )
         {
            // we have a song
            if (posn[0] != '\"')
            {
               fclose(in);
               fprintf(stderr, "CSV decode error: +%d missing \" in %s.\n\n", \
                       countrycount, "song");
               exit(EXIT_FAILURE);
            }
            posn++;
            tail = strchr(posn, '\"');
            tail[0] = '\0';
            strncpy(country[countrycount].song, posn, (STRLEN - 1));
         }
         else
         {
            // we don't have a song
            strcpy(country[countrycount].song, "");
         }
         // done!

         // increase countrycount, add more if exceeds current array size
         countrycount++;
         if (countrycount == countrymax)
            country_extendarray();

      } // end of "if strlen > 0" loop
   } // end of "read until EOF" loop
   fclose(in);
   in = NULL;

   return;
}