/***********************************************************************************
 * Chaga string arithmetic library                                                 *
 * Copyright (C) 2026 Robert James Bruce                                           *
 *                                                                                 *
 * This file is part of the Chaga string arithmetic library.                       *
 *                                                                                 *
 * The Chaga string arithmetic library is free software: you can redistribute it   * 
 * and/or modify it under the terms of the GNU General Public License as published *
 * by the Free Software Foundation, either version 3 of the License, or (at your   *
 * option) any later version.                                                      *
 *                                                                                 *
 * The Chaga string arithmetic library is distributed in the hope that it will be  *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public *
 * License for more details.                                                       *
 *                                                                                 *
 * You should have received a copy of the GNU General Public License               *
 * along with the Chaga compiler. If not, see <https://www.gnu.org/licenses/>.     *
 ***********************************************************************************/
#include "include/libchaga-boolean_functions.h"




// ****************************************************************************
// * boolean_release_memory                                                   *
// *                                                                          *
// * This function releases memory formerly allocated for a boolean variable. *
// *                                                                          *
// * Input:  pointer to a boolean variable                                    *
// * Output: NULL                                                             *
// ****************************************************************************
unsigned char * boolean_release_memory (unsigned char *boolean_value)
{
  if (boolean_value != NULL)
  {
    free (boolean_value);
    boolean_value = NULL;
  }
  return boolean_value;
}




void boolean_display (unsigned char *boolean_value)
{
  if (boolean_value != NULL)
  {
    switch (*boolean_value)
    {
      case 0:
        printf ("FALSE");
      break;
      case 1:
        printf ("TRUE");
      break;
      default:
      break;
    }
  }
  else
  {
    printf ("UNDEFINED");
  }
}



unsigned char * boolean_xor (unsigned char *boolean_value1, unsigned char *boolean_value2)
{
  unsigned char *boolean_xor_result;

  boolean_xor_result = NULL;
  if ((boolean_value1 != NULL) && (boolean_value2 != NULL))
  {
    switch (*boolean_value1)
    {
      case TRUE:
        switch (*boolean_value2)
        {
          case TRUE:
            boolean_xor_result = malloc(sizeof(unsigned char));
            if (boolean_xor_result != NULL)
            {
              *boolean_xor_result = FALSE;
            }
          break;
          case FALSE:
            boolean_xor_result = malloc(sizeof(unsigned char));
            if (boolean_xor_result != NULL)
            {
              *boolean_xor_result = TRUE;
            }
          break;
          default:
          break;
        }
      break;
      case FALSE:
        switch (*boolean_value2)
        {
          case TRUE:
            boolean_xor_result = malloc(sizeof(unsigned char));
            if (boolean_xor_result != NULL)
            {
              *boolean_xor_result = TRUE;
            }
          break;
          case FALSE:
            boolean_xor_result = malloc(sizeof(unsigned char));
            if (boolean_xor_result != NULL)
            {
              *boolean_xor_result = FALSE;
            }
          break;
          default:
          break;
        }
      break;
      default:
      break;
    }
  }
  return boolean_xor_result;
}




unsigned char * convert_boolean_string_to_boolean (unsigned char *boolean_string)
{
  unsigned char *boolean_result;
  size_t boolean_string_length;

  boolean_result = NULL;
  if (boolean_string != NULL)
  {
    boolean_string_length = strnlen (boolean_string, 5);
    switch (boolean_string_length)
    {
      case 1:
        if (*boolean_string == 'F')
        {
          boolean_result = (unsigned char *) malloc(sizeof(unsigned char));
          if (boolean_result != NULL)
          {
            *boolean_result = FALSE;
          }
        }
        else
        {
          if (*boolean_string == 'T')
          {
            boolean_result = (unsigned char *) malloc(sizeof(unsigned char));
            if (boolean_result != NULL)
            {
              *boolean_result = TRUE;
            }
          }
        }
      break;
      case 4:
        if ((strncmp (boolean_string, "TRUE", 4) == 0) || 
            (strncmp (boolean_string, "true", 4) == 0))
        {
          boolean_result = (unsigned char *) malloc(sizeof(unsigned char));
          if (boolean_result != NULL)
          {
            *boolean_result = TRUE;
          }
        }
      break; 
      case 5:
        if ((strncmp (boolean_string, "FALSE", 5) == 0) || 
            (strncmp (boolean_string, "false", 5) == 0))
        {
          boolean_result = (unsigned char *) malloc(sizeof(unsigned char));
          if (boolean_result != NULL)
          {
            *boolean_result = FALSE;
          }
        }
      break;
      default:
      break;
    }
  }
  return boolean_result;
}




psint * convert_boolean_string_to_packed_string_integer (unsigned char *boolean_string)
{
  psint *packed_string_integer_result;
  size_t boolean_string_length;

  packed_string_integer_result = NULL;
  if (boolean_string != NULL)
  {
    boolean_string_length = strnlen (boolean_string, 5);
    switch (boolean_string_length)
    {
      case 1:
        if (*boolean_string == 'F')
        {
          packed_string_integer_result = packed_string_integer_initialize_to_zero (packed_string_integer_result);
        }
        else
        {
          if (*boolean_string == 'T')
          {
            packed_string_integer_result = packed_string_integer_set_value (packed_string_integer_result, "1");
          }
        }
      break;
      case 4:
        if ((strncmp (boolean_string, "TRUE", 4) == 0) || 
            (strncmp (boolean_string, "true", 4) == 0))
        {
          packed_string_integer_result = packed_string_integer_set_value (packed_string_integer_result, "1");
        }
      break; 
      case 5:
        if ((strncmp (boolean_string, "FALSE", 5) == 0) || 
            (strncmp (boolean_string, "false", 5) == 0))
        {
          packed_string_integer_result = packed_string_integer_initialize_to_zero (packed_string_integer_result);
        }
      break;
      default:
      break;
    }
  }
  return packed_string_integer_result;
}




psflt * convert_boolean_string_to_packed_string_float (unsigned char *boolean_string)
{
  psflt *packed_string_float_result;
  size_t boolean_string_length;

  packed_string_float_result = NULL;
  if (boolean_string != NULL)
  {
    boolean_string_length = strnlen (boolean_string, 5);
    switch (boolean_string_length)
    {
      case 1:
        if (*boolean_string == 'F')
        {
          packed_string_float_result = packed_string_float_initialize_to_zero (packed_string_float_result);
        }
        else
        {
          if (*boolean_string == 'T')
          {
            packed_string_float_result = packed_string_float_set_value (packed_string_float_result, "1");
          }
        }
      break;
      case 4:
        if ((strncmp (boolean_string, "TRUE", 4) == 0) || 
            (strncmp (boolean_string, "true", 4) == 0))
        {
          packed_string_float_result = packed_string_float_set_value (packed_string_float_result, "1");
        }
      break; 
      case 5:
        if ((strncmp (boolean_string, "FALSE", 5) == 0) || 
            (strncmp (boolean_string, "false", 5) == 0))
        {
          packed_string_float_result = packed_string_float_initialize_to_zero (packed_string_float_result);
        }
      break;
      default:
      break;
    }
  }
  return packed_string_float_result;
}


