#include <stdio.h>
#include <stdlib.h>
#include "encode.h"
#include "read.h"
#include "pattern.h"

int encode_level(FILE* input_file, FILE* output_file)
{
   char character[1] = {' '};
   char* mode_string = encode_mode_string(input_file, output_file, character);
   if (!mode_string) return -1;
   if (!encode_sub_mode_string(input_file, output_file, character)) return -1;
   if (!encode_color_values(input_file, output_file, character)) return -1;
   if (!strcmp(mode_string, RAINBOW_MODE_STRING))
   {
      int ii = 0;
      char* beam_patterns[BEAM_PATTERN_COUNT];
      while (ii < BEAM_PATTERN_COUNT)
      {
         beam_patterns[ii++] = parse_pattern(input_file, character);
      }
      encode_course(beam_patterns, output_file);
   }
}

void encode_course(char** beam_patterns, FILE* output_file)
{
   int ii = 0, jj = 0, kk = 0, ll = 0;
   u8 color_index, position, length, gap;
   char* color_pattern = beam_patterns[0];
   char* position_pattern = beam_patterns[1];
   char* length_pattern = beam_patterns[2];
   char* gap_pattern = beam_patterns[3];
   while (color_pattern[ii])
   {
      if (!position_pattern[jj]) jj = 0;
      if (!length_pattern[kk]) kk = 0;
      if (!gap_pattern[ll]) ll = 0;
      printf(
         "%c%c%c%c ", color_pattern[ii], position_pattern[jj],
         length_pattern[kk], gap_pattern[ll]);
      color_index =  color_pattern[ii++] - 'A';
      position = position_pattern[jj++] - '0';
      length = length_pattern[kk++] - '0';
      gap = gap_pattern[ll++] - '0';
      fwrite(&color_index, sizeof(u8), 1, output_file);
      fwrite(&position, sizeof(u8), 1, output_file);
      fwrite(&length, sizeof(u8), 1, output_file);
      fwrite(&gap, sizeof(u8), 1, output_file);
   }
   printf("\n");
}

int encode_color_values(FILE* input_file, FILE* output_file, char* character)
{
   int ii;
   u32 byte_sequence;
   char* color_string;
   for (ii = 0; ii < COLOR_COUNT; ii++)
   {
      color_string = read_next_word(input_file, character);
      byte_sequence = strtol(color_string, NULL, 16);
      fwrite(&byte_sequence, sizeof(u8), 3, output_file);
      if (*character == '\n') break;
   }
   while (++ii < COLOR_COUNT)
   {
      byte_sequence = determine_color_from_index(output_file, ii);
      fwrite(&byte_sequence, sizeof(u8), 3, output_file);
   }
}

u32 determine_color_from_index(FILE* file, int ii)
{
   switch (ii)
   {
      case 0: return COLOR_RED;
      case 1: return COLOR_ORANGE;
      case 2: return COLOR_YELLOW;
      case 3: return COLOR_GREEN;
      case 4: return COLOR_BLUE;
      case 5: return COLOR_INDIGO;
      case 6: return COLOR_VIOLET;
   }
}
         

int encode_sub_mode_string(FILE* input_file, FILE* output_file, char* character)
{
   u8 byte_sequence;
   char* survival_sub_mode_string = read_next_word(input_file, character);
   if (!strcmp(survival_sub_mode_string, RACE_SUB_MODE_STRING))
   {
      byte_sequence = RACE_SUB_MODE_BYTE_SEQUENCE;
   }
   else if (!strcmp(survival_sub_mode_string, SURVIVAL_SUB_MODE_STRING))
   {
      byte_sequence = SURVIVAL_SUB_MODE_BYTE_SEQUENCE;
   }
   else
   {
      printf("Unrecognized sub-mode value in level definition\n");
      return -1;
   }
   fwrite(&byte_sequence, sizeof(u8), 1, output_file);
}

char* encode_mode_string(FILE* input_file, FILE* output_file, char* character)
{
   u8 byte_sequence;
   char* mode_string = read_next_word(input_file, character);
   if (!strcmp(mode_string, RAINBOW_MODE_STRING))
   {
      byte_sequence = RAINBOW_MODE_BYTE_SEQUENCE;
   }
   else
   {
      printf("Unrecognized mode value in level definition\n");
      return NULL;
   }
   fwrite(&byte_sequence, sizeof(u8), 1, output_file);
   return mode_string;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "initialization.h"

FILE* initialize_translator(
   char* argv[], int* decode_flag, FILE** output_file, FILE** javascript_file)
{
   char output_method[3], input_method[3];
   determine_methods(output_method, input_method, *decode_flag);

   FILE* input_file = NULL;
   char* input_file_path = parse_arguments(argv, decode_flag);
   char* output_file_path;
   char* javascript_file_path;
   if (input_file_path)
   {
      input_file = open_file(input_file_path, input_method);
      if (input_file)
      {
         output_file_path =
            build_output_file_name(input_file_path, *decode_flag);
         *output_file = open_file(output_file_path, output_method);
         javascript_file_path =
            replace_extension(
               input_file_path, LEVEL_EXTENSION, JAVASCRIPT_EXTENSION);
         *javascript_file = open_file(javascript_file_path, "w");
      }
   }

   return input_file;
}

void determine_methods(
   char* output_method, char* input_method, int decode_flag)
{
   if (decode_flag)
   {
      strcpy(input_method, "rb");
      strcpy(output_method, "w");
   }
   else
   {
      strcpy(input_method, "r");
      strcpy(output_method, "wb+");
   }
}

// I think you may be wrong there *wink*
FILE* open_file(char* file_name, char* mode)
{
   FILE* file = fopen(file_name, mode);
   if (file == NULL)
   {
      printf("File could not be opened: %s\n", file_name);
   }
   return file;
}

char* build_output_file_name(char* input_file_path, int decode_flag)
{
   if (input_file_path == NULL) return;
   char* output_file_path = decode_flag ?
      replace_extension(input_file_path, ENCODED_EXTENSION, LEVEL_EXTENSION) :
      replace_extension(input_file_path, LEVEL_EXTENSION, ENCODED_EXTENSION);

   return output_file_path;
}

char* replace_extension(char* path, char* extension, char* replacement)
{
   int new_length = strlen(path) + strlen(replacement) - strlen(extension);
   char* new_path = malloc(sizeof(char) * (new_length+1));
   strcpy(new_path, path);
   char* truncate_position = strstr(new_path, extension);
   *truncate_position = '\0';
   strcat(new_path, replacement);
   return new_path;
}

char* parse_arguments(char* argv[], int* decode_flag)
{
   int ii;
   for (ii = 1; argv[ii]; ii++)
   {
      if (argv[ii][0] == '-')
      {
         if (argv[ii][1] == DECODE_OPTION_INDICATOR) *decode_flag = TRUE;
      }
      else return argv[ii];
   }
   print_usage();
   return NULL;
}
216.73.216.157
216.73.216.157
216.73.216.157
 
November 10, 2013


Food Spring - Watermelon Stage

Getting the fruit as far as possible is the object of each level, collecting bigger, more valuable guns. The final result is determined by the size of the fruits' collection when the monkey arrives in North America and either survives or perishes in the fruits' attack.

Watermelon Peach
Pineapple Grapes