#include "definition.h"

char *addChar(char *pointer, const char character)
{
    char *tempAddr = NULL;
    if(pointer == NULL)
    {
        tempAddr = (char *) realloc(pointer, 2*sizeof(char));
        if(tempAddr == NULL)
            return NULL;
        else
        {
            pointer = tempAddr;
            pointer[0] = character;
            pointer[1] = '\0';
        }
    }
    else
    {
        tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char));
        if(tempAddr == NULL)
        {
            free(pointer);
            return NULL;
        }
        else
        {
            pointer = tempAddr;
            pointer[strlen(pointer)+1] = '\0';
            pointer[strlen(pointer)] = character;
        }
    }

    return pointer;
}

char *dateTimeFormater(char *pointer, const uint8_t value, const char character)
{
  if(pointer == NULL)
    return pointer;

  if(value < 10)
  {
    sprintf(pointer,"%d", value);
    *(pointer+1) = *(pointer);*(pointer) = character;*(pointer+2) = '\0';
  }
  else
    sprintf(pointer,"%d", value);
  
  return pointer;
}

char *lastIndexOf(char *str, const char character)
{
  char *last(NULL), *current(str); 
  do
  {
    current = strchr(current, character);
    if(current != NULL)
    {
      last = current;
      if(*(current+1) == '\0')break;
        
      current += 1;
    }
  }while(current != NULL);

  return last;
}
