/*  Test av biblioteket "några C-funktioner".
 *  Gratis programvara utan garanti. Använd den hur ni vill, men skyll inte på mig.
 *
 *  Kompilerat och provkört, utan ändringar, på:
 *   Red Hat Linux 8.0, gcc 3.2, unixODBC 2.2.2
 *     Som C: gcc -Wall -o testa-c-funktionerna testa-c-funktionerna.c c-funktioner.c
 *     Som C++: g++ -Wall -o testa-c-funktionerna testa-c-funktionerna.cc c-funktioner.cc
 *   Windows 2000 Professional, Microsoft Visual C++ 6.0
 *   Windows 2000 Professional, Borland C++ Builder 5.0
 *
 *  Thomas Padron-McCarthy (Thomas.Padron-McCarthy@tech.oru.se)
 *  Denna fil senast ändrad: 18 maj 2004
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "c-funktioner.h"

/*---------------------------------------------------------------------------*/

int main(void) {
    int again;
    int yes_answer;
    char buffer[6];
    char *cp;
    int i, j;
#if defined(_MSDOS) || defined(_WIN32)
    int c;
#endif

    printf("\nVälkommen till en liten test av några C-funktioner!\n\n");

    printf("Test av add_strings: 'foo' + 'fummelumm' = '%s'\n",
	   add_strings("foo", "fummelumm"));

    do {
	printf("Test av smart_fgets:\n");
	fflush(stdout);
	buffer[0] = '\0';
	buffer[1] = '\0';
	buffer[2] = '\0';
	buffer[3] = '\0';
	buffer[4] = '\0';
	buffer[5] = '\0';
	cp = smart_fgets(buffer, 5, stdin);
	printf("Inmatad rad: '%s'. Returvärde: %p.\n", buffer, cp);
	if (buffer[4] != '\0' || buffer[4] != '\0')
	    fatal_error("smart_fgets fungerar inte.\n"
			"    Inmatad sträng: '%s'.", buffer);

	printf("Tester av ask_yes_or_no:\n");
	yes_answer = ask_yes_or_no("Svara!");
	printf("Svaret var %s.\n", yes_answer ? "ja" : "nej");

	printf("Test av f_read_line:\n");
	cp = f_read_line(stdin);
	printf("Inmatad rad: '%s'\n", cp);
	safe_free(cp);

	printf("Test av read_line:\n");
	cp = read_line();
	printf("Inmatad rad: '%s'\n", cp);
	safe_free(cp);

	printf("Test av f_read_integer_line:\n");
	i = f_read_integer_line(stdin);
	printf("Inmatat heltal: %d\n", i);

	printf("Test av read_integer_line:\n");
	i = read_integer_line();
	printf("Inmatat heltal: %d\n", i);

	printf("Test av först read_integer och sen read_integer_line:\n");
	i = read_integer();
	j = read_integer_line();
	printf("Inmatade heltal: %d %d\n", i, j);

	printf("\n");
	fflush(stdout);

	again = ask_yes_or_no("Ett varv till?");
    } while (again);

    printf("\n");
    printf("Slut i rutan.\n");

    printf("Vi avslutar programmet genom att provköra fatal_error:\n");
    printf("\n");
    fatal_error("Hej %s, svaret är %d. Allt gick bra!", "hopp", 42);

    /* Some systems, such as Borland C++ on Windows,
       just closes the text window when the program exits. */
#if defined(_MSDOS) || defined(_WIN32)
    printf("Press RETURN to exit the program: ");
    fflush(stdout);
    while ((c = getchar()) != '\n' && c != EOF)
        ;
#endif

    return 0;
} /* main */

