#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "tools.h"

/**********************************************************/
/**********************************************************/
void clearScreen()
{
   system("clear"); // Linux
                    // system("CLS");   // Windows
}

/**********************************************************/
/**********************************************************/
void clearBuffer()
{
   char Dummy;

   do
   {
      scanf("%c", &Dummy);
   } while (Dummy != '\n');
}

/**********************************************************/
/**********************************************************/
void waitForEnter()
{
   printf("\nBitte Eingabetaste druecken ...");
   clearBuffer();
}

/**********************************************************/
/**********************************************************/
int askYesOrNo(char *Question)
{
   char Input;

   do
   {
      printf("%s", Question);
      scanf("%c", &Input);
      if (Input != '\n')
         clearBuffer();
   } while ((Input != 'j') && (Input != 'J') && (Input != 'n') && (Input != 'N'));
   return ((Input == 'j') || (Input == 'J'));
}
/**********************************************************/
/**********************************************************/
void printLogo()
{
   FILE *titel = fopen("Logo.txt", "r");
   char output[150];
   if (titel)
   {
      while (!feof(titel))
      {
         fgets(output, 150, titel);
         printf("%s", output);
      }
      puts("\n");
      fclose(titel);
   }
   else
   {
      puts("Logo not found\n");
   }
}

/**********************************************************/
/**********************************************************/
void delay(unsigned int number_of_seconds)
{
   // Converting time into milli_seconds
   unsigned int milli_seconds = 1000 * number_of_seconds;

   // Storing start time
   clock_t start_time = clock();

   // looping till required time is not achieved
   while ((unsigned int)clock() < start_time + milli_seconds)
      ;
}
/**********************************************************/
/**********************************************************/
void printLine(char zeichen, unsigned int limit)
{
   for (unsigned int i = 0; i < limit; ++i)
   {
      printf("%c", zeichen);
   }
   puts("\n");
}
/*******************************************************************************************************************************************/
/* function receives a prompt as a string, the maximum input *******************************************************************************/
/* length of the text to be entered, a pointer to a string,*********************************************************************************/
/*as well as a boolean value whether an empty string is also allowed as input. *************************************************************/
/*******************************************************************************************************************************************/
int getText(char *prompt, unsigned int maxLength, char **pointerToString, int stringOption)
{

   if (!pointerToString)
      return 0;
   *pointerToString = NULL;
   char *input;
   char format[20];
   unsigned int stringLength;
   int loopExit = 0;
   input = malloc(maxLength + 1);
   if (input)
   {
      sprintf(format, "%%%i[^\n]", maxLength);
      do
      {
         puts(prompt);
         *input = '\0';
         scanf(format, input);
         clearBuffer();
         stringLength = (unsigned int)strlen(input) + 1;
         if (stringLength > 1)
         {
            *pointerToString = malloc(stringLength);
            if (*pointerToString)
            {
               strncpy(*pointerToString, input, stringLength + 1);
               loopExit = 1;
            }
         }
         else if (stringOption)
            loopExit = 1;
      } while (loopExit != 1);
      free(input);
      return 1;
   }
   else
      return 0;
}
/*******************************************************************************************************************************************/
/* function receives a prompt as a string,, a pointer to an integer, two unsigned integers to define the ***********************************/
/* max and min, a pointer to an integer,****************************************************************************************************/
/* and delevers a boolen value as return ***************************************************************************************************/
/*******************************************************************************************************************************************/
void getNumber(char *prompt, unsigned int *pointerToInteger, unsigned int min, unsigned int max)
{

   unsigned int input;
   do
   {
      puts(prompt);
      scanf("%ui", &input);
      clearBuffer();
   } while ((input < min) || (input > max));

   *pointerToInteger = input;
}
/**********************************************************/
/**********************************************************/
void spacesRemove(char **zeilenanfang, char zeile[101])
{
   *zeilenanfang = zeile;

   while ((**zeilenanfang == ' ') || (**zeilenanfang == 9))
   {
      
      (*zeilenanfang)++;
      
   }
  
}

/**********************************************************/
/**********************************************************/