/* !VoteFile application.

   Version 2.01:

     * Written in C
     * Compatible with CastAWote 3

    1996 Richard Murray, 6th January 1996

   *** ACORN C RELEASE 4 VERSION ***

   This is the "ve_vf" version which outputs
   sysvars for VoteEdit to pick up on; and
   auto-scans/fixes.

*/



/* ANSI includes */

#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "swis.h"
#include "bbc.h"
#include "kernel.h"
#include "sprite.h"



/* Define a few variables... */

char creator[255];
char system_addr[255];
int  voted_for;
char question[255];
char software_options[255];
char additional_one[255];
char additional_two[255];
char additional_three[255];
char option_one_text[255];
int  option_one_result;
char option_two_text[255];
int  option_two_result;
char option_three_text[255];
int  option_three_result;
char option_four_text[255];
int  option_four_result;
char option_five_text[255];
int  option_five_result;
char option_six_text[255];
int  option_six_result;
char option_seven_text[255];
int  option_seven_result;
char option_eight_text[255];
int  option_eight_result;

FILE *file_handle;
FILE *fix_handle;
int  number_of_votes;
char junk[255];
int  length_of_string;
int  junk_variable;
int  file_length;
int  fastmode;
int  fixit;
int  easteregg;
int  currvote;
char oldnull[] = "{/}";
int  questionerror;
int  totalquestionerror;
_kernel_swi_regs r;



/* Procedure/function declarations */

void read_string(FILE *file, char *string);
int read_word(FILE *file);
void write_string(FILE *file, char *string);
void write_word(FILE *file, int num);
void loadquestion(void);
void examinequestion(void);
void updatequestion(void);



/* Main program code */

int main(int argc, char *argv[])
{
   fixit = TRUE;

   if (( file_handle = fopen("<CastAWoteA$Dir>.Data.Data","rb") ) == NULL)
      exit(EXIT_FAILURE);

   if (( fix_handle = fopen("<CastAWoteA$Dir>.Data.Fix_Data","wb") ) == NULL)
      exit(EXIT_FAILURE);

   file_length = 0;
   while ( !feof(file_handle) )
   {
      junk_variable = fgetc(file_handle);
      file_length++;
   }
   rewind(file_handle);

   if ( file_length < 55 )
   {
      os_cli("Set Vote$File$ErrorString FTS");
      exit(EXIT_FAILURE);
   }

   read_string(file_handle, junk);
   write_string(fix_handle,"*** < ...eliFetoV yb detaerc elif atad etoWAtsaC > ***");

   number_of_votes = read_word(file_handle);
   write_word(fix_handle,number_of_votes);


   currvote=1;
   while(currvote <= number_of_votes)
   {
      questionerror = 0;
      loadquestion();
      examinequestion();
      if (fixit == TRUE )
         updatequestion();
      currvote++;
   }

   write_string(fix_handle,"elif fo dnE");
   fprintf(fix_handle,"\n\n\nThis file was fixed and recreated by:\n");
   fprintf(fix_handle,"VoteFile version 2.01VE (6th January 1994)\n");
   fprintf(fix_handle,"(C) copyright 1996 Richard Murray\n\n\n");

   fclose(file_handle);
   fclose(fix_handle);
   sprintf(junk,"Set Vote$File$Errors %d",totalquestionerror);
   os_cli(junk);
   exit(totalquestionerror);
}


void read_string(FILE *file, char *string)
{
   int len, i;
   i = fgetc(file);
   if (i != 0x00)
   {
      os_cli("Set Vote$File$ErrorString ERS");
      fclose(file_handle);
      if ( fixit == TRUE )
         fclose(fix_handle);
      exit(2);
   }

   len = fgetc(file);
   if (len>0)
     for (i=len-1;i>=0;i--)
     {
        string[i] = fgetc(file);
     }
     string[len] = '\0';
}


int read_word(FILE *file)
{
   int num;
   num = fgetc(file);
   if (num != 0x40)
   {
      os_cli("Set Vote$File$ErrorString ERW");
      fclose(file_handle);
      if ( fixit == TRUE )
         fclose(fix_handle);
      exit(2);
   }

   num = fgetc(file);
   num = (num << 8) | fgetc(file);
   num = (num << 8) | fgetc(file);
   num = (num << 8) | fgetc(file);
   return num;
}


void write_string(FILE *file, char *string)
{
   int len = strlen(string);
   int i;
   fputc(0x00, file);
   fputc(len, file);
   for (i=len-1;i>=0;i--)
   {
      fputc(string[i], file);
   }
}


void write_word(FILE *file, int num)
{
   fputc(0x40, file);
   fputc((0xff & (num >> 24)), file);
   fputc((0xff & (num >> 16)), file);
   fputc((0xff & (num >> 8)), file);
   fputc((0xff & num), file);
}




/* Loader */

void loadquestion(void)
{
   read_string(file_handle,creator);
   read_string(file_handle,system_addr);
   voted_for = read_word(file_handle);
   read_string(file_handle,question);
   read_string(file_handle,software_options);
   read_string(file_handle,additional_one);
   read_string(file_handle,additional_two);
   read_string(file_handle,additional_three);
   read_string(file_handle,option_one_text);
   option_one_result = read_word(file_handle);
   read_string(file_handle,option_two_text);
   option_two_result = read_word(file_handle);
   read_string(file_handle,option_three_text);
   option_three_result = read_word(file_handle);
   read_string(file_handle,option_four_text);
   option_four_result = read_word(file_handle);
   read_string(file_handle,option_five_text);
   option_five_result = read_word(file_handle);
   read_string(file_handle,option_six_text);
   option_six_result = read_word(file_handle);
   read_string(file_handle,option_seven_text);
   option_seven_result = read_word(file_handle);
   read_string(file_handle,option_eight_text);
   option_eight_result = read_word(file_handle);
}

void updatequestion(void)
{
   write_string(fix_handle,creator);
   write_string(fix_handle,system_addr);
   write_word(fix_handle,voted_for);
   write_string(fix_handle,question);
   write_string(fix_handle,software_options);
   write_string(fix_handle,additional_one);
   write_string(fix_handle,additional_two);
   write_string(fix_handle,additional_three);
   write_string(fix_handle,option_one_text);
   write_word(fix_handle,option_one_result);
   write_string(fix_handle,option_two_text);
   write_word(fix_handle,option_two_result);
   write_string(fix_handle,option_three_text);
   write_word(fix_handle,option_three_result);
   write_string(fix_handle,option_four_text);
   write_word(fix_handle,option_four_result);
   write_string(fix_handle,option_five_text);
   write_word(fix_handle,option_five_result);
   write_string(fix_handle,option_six_text);
   write_word(fix_handle,option_six_result);
   write_string(fix_handle,option_seven_text);
   write_word(fix_handle,option_seven_result);
   write_string(fix_handle,option_eight_text);
   write_word(fix_handle,option_eight_result);
}



/* Examine code */

void examinequestion(void)
{
   char fixstring[]="<invalid data fixed by VoteFile>";

   junk_variable = strcmp(creator,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(creator,fixstring);
   }
   junk_variable = strcmp(system_addr,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(system_addr,fixstring);
   }
   junk_variable = strcmp(question,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(question,fixstring);
   }
   junk_variable = strcmp(software_options,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(software_options,fixstring);
   }
   junk_variable = strcmp(additional_one,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(additional_one,fixstring);
   }
   junk_variable = strcmp(additional_two,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(additional_two,fixstring);
   }
   junk_variable = strcmp(additional_three,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(additional_three,fixstring);
   }
   junk_variable = strcmp(option_one_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_one_text,fixstring);
   }
   junk_variable = strcmp(option_two_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_two_text,fixstring);
   }
   junk_variable = strcmp(option_three_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_three_text,fixstring);
   }
   junk_variable = strcmp(option_four_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_four_text,fixstring);
   }
   junk_variable = strcmp(option_five_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_five_text,fixstring);
   }
   junk_variable = strcmp(option_six_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_six_text,fixstring);
   }
   junk_variable = strcmp(option_seven_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_seven_text,fixstring);
   }
   junk_variable = strcmp(option_eight_text,oldnull);
   if ( junk_variable == NULL )
   {
      questionerror++;
      strcpy(option_eight_text,fixstring);
   }

   junk_variable = strlen(creator);
   if ( junk_variable < 2 )
   {
      questionerror++;
      strcpy(creator,fixstring);
   }
   if ( junk_variable > 60 )
   {
      questionerror++;
      creator[60]=0;
   }
   junk_variable = strlen(system_addr);
   if ( junk_variable < 3 )
   {
      questionerror++;
      strcpy(system_addr,fixstring);
   }
   if ( junk_variable > 60 )
   {
      questionerror++;
      system_addr[60]=0;
   }
   junk_variable = strlen(question);
   if ( junk_variable < 3 )
   {
      questionerror++;
      strcpy(question,fixstring);
   }
   if ( junk_variable > 60 )
   {
      questionerror++;
      question[60]=0;
   }
   junk_variable = strlen(software_options);
   if ( junk_variable < 3 )
   {
      questionerror++;
      strcpy(software_options,fixstring);
   }
   if ( junk_variable > 60 )
   {
      questionerror++;
      software_options[60]=0;
   }
   junk_variable = strlen(option_one_text);
   if ( junk_variable < 1 )
   {
      questionerror++;
      strcpy(option_one_text,fixstring);
   }
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_one_text[60]=0;
   }
   junk_variable = strlen(option_two_text);
   if ( junk_variable < 1 )
   {
      questionerror++;
      strcpy(option_two_text,fixstring);
   }
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_two_text[60]=0;
   }
   junk_variable = strlen(option_three_text);
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_three_text[60]=0;
   }
   junk_variable = strlen(option_four_text);
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_four_text[60]=0;
   }
   junk_variable = strlen(option_five_text);
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_five_text[60]=0;
   }
   junk_variable = strlen(option_six_text);
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_six_text[60]=0;
   }
   junk_variable = strlen(option_seven_text);
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_seven_text[60]=0;
   }
   junk_variable = strlen(option_eight_text);
   if ( junk_variable > 60 )
   {
      questionerror++;
      option_eight_text[60]=0;
   }

   junk_variable=strcmp(creator,"Anonymous");
   if ( junk_variable == NULL )
   {
      questionerror++;
   }

   if (strlen(additional_one) == 0 && strlen(additional_two) != 0 )
   {
      questionerror++;
   }
   if (strlen(additional_two) == 0 && strlen(additional_three) != 0 )
   {
      questionerror++;
   }
   if (strlen(additional_one) == 0 && strlen(additional_three) != 0 )
   {
      questionerror++;
   }

   if (strlen(option_three_text) == 0 )
   {
      if (strlen(option_four_text) != 0 )
      {
         questionerror++;
         strcpy(option_four_text,"");
         }
      if (strlen(option_five_text) != 0 )
      {
         questionerror++;
         strcpy(option_five_text,"");
         }
      if (strlen(option_six_text) != 0 )
      {
         questionerror++;
         strcpy(option_six_text,"");
         }
      if (strlen(option_seven_text) != 0 )
      {
         questionerror++;
         strcpy(option_seven_text,"");
         }
      if (strlen(option_eight_text) != 0 )
      {
         questionerror++;
         strcpy(option_eight_text,"");
         }
   }
   if (strlen(option_four_text) == 0 )
   {
      if (strlen(option_five_text) != 0 )
      {
         questionerror++;
         strcpy(option_five_text,"");
         }
      if (strlen(option_six_text) != 0 )
      {
         questionerror++;
         strcpy(option_six_text,"");
         }
      if (strlen(option_seven_text) != 0 )
      {
         questionerror++;
         strcpy(option_seven_text,"");
         }
      if (strlen(option_eight_text) != 0 )
      {
         questionerror++;
         strcpy(option_eight_text,"");
         }
   }

   if (strlen(option_five_text) == 0 )
   {
      if (strlen(option_six_text) != 0 )
      {
         questionerror++;
         strcpy(option_six_text,"");
         }
      if (strlen(option_seven_text) != 0 )
      {
         questionerror++;
         strcpy(option_seven_text,"");
         }
      if (strlen(option_eight_text) != 0 )
      {
         questionerror++;
         strcpy(option_eight_text,"");
         }
   }
   if (strlen(option_six_text) == 0 )
   {
      if (strlen(option_seven_text) != 0 )
      {
         questionerror++;
         strcpy(option_seven_text,"");
         }
      if (strlen(option_eight_text) != 0 )
      {
         questionerror++;
         strcpy(option_eight_text,"");
         }
   }
   if (strlen(option_seven_text) == 0 )
   {
      if (strlen(option_eight_text) != 0 )
      {
         questionerror++;
         strcpy(option_eight_text,"");
         }
   }

   if (strlen(option_three_text) == 0 && option_three_result != 0)
   {
      questionerror++;
      option_three_result = 0;
   }

   if (strlen(option_four_text) == 0 && option_four_result != 0)
   {
      questionerror++;
      option_four_result = 0;
   }
   if (strlen(option_five_text) == 0 && option_five_result != 0)
   {
      questionerror++;
      option_five_result = 0;
   }
   if (strlen(option_six_text) == 0 && option_six_result != 0)
   {
      questionerror++;
      option_six_result = 0;
   }
   if (strlen(option_seven_text) == 0 && option_seven_result != 0)
   {
      questionerror++;
      option_seven_result = 0;
   }
   if (strlen(option_eight_text) == 0 && option_eight_result != 0)
   {
      questionerror++;
      option_eight_result = 0;
   }

   /* See if there are any residual 'tagged' votes from version 2 code */
   if (option_seven_result == -1)
   {
      questionerror++;
      option_seven_result = 0;
   }
   if (option_eight_result == -1)
   {
      questionerror++;
      option_eight_result = 0;
   }

   junk_variable = option_one_result;
   junk_variable += option_two_result;
   junk_variable += option_three_result;
   junk_variable += option_four_result;
   junk_variable += option_five_result;
   junk_variable += option_six_result;
   junk_variable += option_seven_result;
   junk_variable += option_eight_result;
   if (junk_variable != voted_for)
   {
      questionerror++;
      voted_for = junk_variable;
   }

   junk_variable = strcspn(software_options,"");
   if ( junk_variable == strlen(software_options) )
   {
      questionerror++;
      strcpy(system_addr,"<bbsname> @ 0:0/0.0");
      strcpy(software_options,"CastAWote 3.00 alpha/- (ArcBBS)  0000000000000000");
   }

   totalquestionerror+=questionerror;
}
