COM.c

/* Datei: com.c */

#include <stdio.h>
#include <string.h>
#include "util.h"
#include "tab.h"
#include "tourn.h"
#include "games.h"
#include "eval.h"
#include "stat.h"
#include "sum.h"

static char ComAbbrev[5]= "";

void ComSetCommandLineAbbrev(char *abbrev)
{
    if (strlen(abbrev) < 1)
    {
        printf("ComSetCommandLineAbbrev: kein Kuerzel angegeben\n");
        return;
    }
    strncpy(ComAbbrev, abbrev, 4);
    ComAbbrev[4]= '\0';
} /* ComSetCommandLineAbbrev */

void ComSetFiles(void)
{
    int FileExists;
    char StringInput[100];
    char String2Input[100];
    int ReadOldFile= 0;

    TabWriteFile();

    printf("Identifikationskuerzel fuer Dateinamen? ");
    if (strlen(ComAbbrev) > 0)
    {
        printf("%s\n", ComAbbrev);
        strcpy(StringInput, ComAbbrev);
        ComAbbrev[0]= '\0';
    }
    else
        UtilInput(StringInput);
    if (strlen(StringInput)<1)
    {
        printf("Abbruch!\n");
        return;
    }
    if (strlen(StringInput) > 4)
    {
        printf("Kuerzel zu lang...Abbruch\n");
        return;
    }
    TabFileExists(StringInput, &FileExists);
    if (!FileExists)
    {
        /*
            uebernimm evtl. Daten aus einer alten Datei...
        */
        printf("Daten aus alter Datei uebernehmen\n");
        printf("(<return>: nein,  <Identifikationskuerzel>: ja) ? ");
        UtilInput(String2Input);
        if (strlen(String2Input) > 0)
        {
            ReadOldFile= (strlen(String2Input) <= 4);
            if (!ReadOldFile)
            {
                printf("Kuerzel zu lang...Abbruch\n");
                return;
            }
            TabCopyFile(String2Input, StringInput);
        } /* if */
    } /* if */
    StatSetAbbrev(StringInput);
    TabReadFile();
    if (ReadOldFile)
    {
        TabSweepAfterCopyFile();
        TabWriteFile();
    }
} /* ComSetFile */

static int ComEnterMember(int index)
/* Funktionswert:  0: Eingabe wiederholen;
                   1: Eingabe ok;
                  -1: Eingabe des Stimmzettels abbrechen;
*/
{
    int  member;
    char UserInput[100];
    int  res;

    printf("Mitglied\n");
    printf("    (1 --> ja; 0 --> nein; <return> --> keine Aenderung");
    printf("; q --> Abbruch)\n");
    printf("    Alter Wert: %d .   Neuer Wert ? ", TabGetMember(index));
    UtilInput(UserInput);

    if ((strlen(UserInput) == 1) && (UserInput[0] == 'q'))
        return -1;
       
    if (strlen(UserInput) == 0)
        return 1;

    res= sscanf(UserInput, "%d", &member);
    if (res != 1)
    {
        printf("Eingabefehler!\n");
        return 0;
    }      
       
    TabSetMember(index, member);
    return 1;
} /* ComEnterMember */

static int ComEnterCharts(int index)
/* Funktionswert:  0: Eingabe wiederholen;
                   1: Eingabe ok;
                  -1: Eingabe des Stimmzettels abbrechen
*/
{
    int i; /* Nummer des aktuellen HippoCHARTS eintrages;
              1: Platz 1 (5 Punkte); 5: Platz 4 (1 Punkt) */
    char command= '\0', command2;
    char StringInput[100];
    char name[100];
    char author[100];
    char *FoundName, *FoundAuthor, *FoundPublisher;
    int CurrentIndex; /* Index, ab dem in der Spieldatenbank gesucht wird*/
    int state; /* aktueller Zustand des endlichen Automaten */
    int ReadInput;
    int FoundIndex, NewIndex;
    int RequireNonEmptyInput;
    int GameIndex;
    int GameCount= 0;

    /*
        gib alte HippoCHARTS-Auswahl aus:
    */
    printf("alte HippoCHARTS-Auswahl:\n");
    for (i= 0; i < 5; i++)
    {
        TabGetCharts(index, i, &FoundName, &GameIndex);
        if (FoundName != NULL)
        {
            if (strlen(FoundName) > 0)
            {
                GameCount++;
                printf("    %s %d (%d %s): %s\n",
                    "Platz", i+1, 5-i, "Punkte", FoundName);
            }
        }
    }
    if (!GameCount)
        printf("    nicht vorhanden\n");

    i= 0;
    CurrentIndex= 0;
    state= 1;
    do {
       /* endlicher Automat, der auf Eingaben reagiert
          (moegliche Eingaben:
                   String oder
                   Befehl( w(eitersuchen), n(eueingabe), h(ilfe),
                           q(abbruch)
                         )
          ***** Start/Hauptschleife *****
          0: Start:
               i= 0; CurrentIndex= 0; --> 1;
          1: Erhoehe i:
               i<5: i++; Name[i][0]= '\0'; --> 2
               i>=5: --> 20
          ***** Eingabe vorhandenes Spiel *****
          2:(E) lese Befehl oder Name[i]:
               Eingabe Name: speichere Name; CurrentIndex= 0; --> 3
               Eingabe <Return>: speichere ""; CurrentIndex= 0; --> 1
               Eingabe n: --> 10 (vermindere i)
                       w: --> 15 (suche letztes i)
                       a: --> 25 (aendere letztes i auch in Datenbank)
                       q: --> 20
          3: suche Name:
               suche; gefunden: speichere Spieleindex --> 1
                      nicht gefunden: --> 5
          ***** Spiel nicht gefunden *****
          5: (E) Name nicht gefunden:
               Eingabe Name: speichere Spielename --> 6;
               Eingabe n: --> 2;
                       q: --> 20;
          6: (E) lese Spieleautor:
               Eingabe Autor: speichere Autor --> 7
               Eingabe n: --> 2;
                       q: --> 20;
          7: (E) lese Spieleverlag:
               Eingabe Verlag: speichere Datensatz; --> 1;
               Eingabe n: --> 2;
               Eingabe q: --> 20;
          ***** Neueingabe des vorangegangenen Spiels *****
          10: pruefe Neueingabe:
              i>= 2: i--; CurrentIndex= 0; --> 2
              i<= 1: --> 2
          ***** weitersuchen *****
          15: pruefe weitersuchen:
              i>= 2: i--; --> 3
              i<= 1: --> 2
          ***** pruefe Ende *****
          20: Eingabe <cr>: Ende
              Eingabe n: --> 2
              Eingabe w: --> 3
              Eingabe a: --> 25
          ***** gefundenes Spiel in Datenbank und Stimmzettel aendern *****
          25: pruefe Datenbank und Stimmzettel aendern:
              i>= 2: i--; --> 26
              i<= 1: --> 2
          26: (E) Datensatz aendern:
               Eingabe Name: speichere Spielename --> 27;
               Eingabe n: --> 2;
                       q: --> 20;
          27: (E) lese Spieleautor:
               Eingabe Autor: speichere Autor --> 28
               Eingabe n: --> 2;
                       q: --> 20;
          28: (E) lese Spieleverlag:
               Eingabe Verlag: speichere Datensatz i auch in Datenbank;
                               i++; --> 1;
               Eingabe n: --> 2;
               Eingabe q: --> 20;
        */
        if ((state == 2) || (state == 5) || (state == 6) || (state == 7)
            || (state == 26) || (state == 27) || (state == 28)
           )
        {
            ReadInput= 1;
            do {
                do
                {
                    switch(state)
                    {
                    case 5:
                        printf("Eingabe eines neuen Spiele-Datensatzes:\n");
                        printf("Name   (h --> Hilfe) ? ");
                        break;
                    case 26:
                        printf("Eingabe des geaenderten Spiele-Datensatzes:\n");
                        printf("Name   (h --> Hilfe) ? ");
                        break;
                    case 6:
                    case 27:
                        printf("Autor  (h --> Hilfe) ? ");
                        break;
                    case 7:
                    case 28:
                        printf("Verlag (h --> Hilfe) ? ");
                        break;
                    default:
                        printf(
                        "Name oder Namensteil eines Spieles fuer Platz %d (%d Punkte)\n",
                            i, 6-i);
                        printf("(h --> Hilfe");
                        if (i == 1)
                            printf(", <return> --> keine Aenderung");
                        printf(") ? ");
                        break;
                    }
                    UtilInput(StringInput);
                    RequireNonEmptyInput=
                        (state != 5) && (state != 6) && (state != 7)
                        && (state != 2);
                    if ((StringInput[0]=='\0') && RequireNonEmptyInput)
                        printf("Eingabefehler!\n");
                } while((StringInput[0]=='\0') && RequireNonEmptyInput);
                if (StringInput[0]=='\0')
                {
                    StringInput[0]= ' '; StringInput[1]= '\0';
                }
                /* setze den zu bearbeitenden Befehl fest: */
                command= (StringInput[1] == '\0') ? StringInput[0] : 'p';
                if (command == 'p')
                {
                    /* es wurden Daten eingegeben: */
                    ReadInput= (strlen(StringInput) > 30);
                    if (ReadInput)
                        printf("zu lang\n");
                }
                else
                {
                    /* es wurde in Befehl eingegeben: */
                    if (command == 'h')
                    {
                        /* es soll Hilfe zum aktuellen Eingabezustand
                           ausgegeben werden:
                        */
                        int platz;
                       
                        platz=
                            ((state >= 5) && (state <= 7)) ? i : i-1;
                   
                        printf("q --> Abbruch");
                        if (i>1)
                        {
                            printf("; n --> Neueingabe fuer Platz %d",
                                   platz);
                            if (CurrentIndex > 0)
                            {
                                printf(
                                    "; w --> fuer Platz %d weitersuchen;\n",
                                    platz);
                                printf(
                                "a --> Spieledatenbank mit Platz %d aendern",
                                    platz);
                            }
                        }
                        else if (i == 1)
                        {
                            printf("%s %s\n",
                            "<return> --> keine Aenderung der Spiele;",
                            "Eingaben fuer HippoCHARTS beenden");
                        }
                        printf("\n");
                    }
                    else
                    {
                        /* es wurde ein Befehl ungleich h eingegeben;
                           pruefe, ob der Befehl korrekt ist:
                        */
                        ReadInput=
                              ( (command != 'w') && (command != 'n')
                                 && (command != 'q') && (command != 'a')
                                 && (command != ' ')
                              );
                        if (ReadInput)
                            printf("unbekannter Befehl\n");
                        else
                        {
                            ReadInput=
                                ( ( (command == 'w') || (command == 'a') )
                                  && (CurrentIndex <= 0)
                                );
                            if (ReadInput)
                                printf("Befehl nicht sinnvoll!");
                        }
                    }
                } /* if (command == 'p') */
            } while (ReadInput);
        }

        /* bearbeite den aktuellen Zustand: */
        switch (state)
        {
        case 1:
            if (i<5)
            {
                i++;
                /* <return> fuer Platz 1: keine Aenderung, Ende
                   der Funktion; daher:
                */
                if (i > 1)
                    TabSetCharts(index, i-1, "", -1);
               
                state= 2;
            }
            else
                state= 20;
            break;
        case 2:
            /* Eingabe vorhandenes Spiel... */
            switch (command)
            {
            case 'n':
                state= 10;
                break;
            case 'p':
                CurrentIndex= 0;
                strcpy(name, StringInput);
                state= 3;
                break;
            case ' ': /* keine Eingabe --> kein Spiel fuer diesen Platz */
                if (i == 1)
                {
                    /* <return> bei Platz 1: Eingabe sofort beenden,
                       Funktion verlassen
                    */
                    state= -1;
                }
                else
                {
                    CurrentIndex= 0;
                    TabSetCharts(index, i-1, "", -1);
                    state= 1;
                }
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
                state= 15;
                break;
            case 'a':
                state= 25;
                break;
            default:
                printf("Eingabefehler im Zustand 2 !\n");
                break;
            }
            break;
        case 3:
            GamesSearch(name, CurrentIndex, 0, &FoundIndex);
            if (FoundIndex >= 0)
            { /* gefunden */
                CurrentIndex= FoundIndex+1;
                printf("gefunden:\n");
                GamesGet(FoundIndex, &FoundName, &FoundAuthor,
                         &FoundPublisher);
                printf("Name    : %s\n", FoundName);
                printf("Autor   : %s\n", FoundAuthor);
                printf("Verlag  : %s\n", FoundPublisher);

                TabSetCharts(index, i-1, FoundName, FoundIndex);
                state= 1;
            }
            else
            { /* nicht gefunden */
                printf("nicht gefunden\n");

                state= 5;
                CurrentIndex= GamesTableSize();
            }
            break;
        case 5:
            /* Spiel nicht gefunden... */
            switch(command)
            {
            case 'n':
                state= 2;
                break;
            case 'p':
                strcpy(name, StringInput);
                state= 6;
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
            case 'a':
                printf("Befehl hier nicht sinnvoll\n");
                break;
            default:
                printf("Eingabefehler im Zustand 5 !\n");
                break;
            }
            break;
        case 6:
            switch(command)
            {
            case 'n':
                state= 2;
                break;
            case 'p':
                strcpy(author, StringInput);
                state= 7;
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
            case 'a':
                printf("Befehl hier nicht sinnvoll\n");
                break;
            default:
                printf("Eingabefehler im Zustand 6 !\n");
                break;
            }
            break;
        case 7:
            switch(command)
            {
            case 'n':
                state= 2;
                break;
            case 'p':
                GamesNew(&NewIndex, 0);
                GamesSet(NewIndex, name, author, StringInput, 0);
                TabSetCharts(index, i-1, name, NewIndex);
                state= 1;
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
            case 'a':
                printf("Befehl hier nicht sinnvoll\n");
                break;
            default:
                printf("Eingabefehler im Zustand 7 !\n");
                break;
            }
            break;
        case 10:
            if (i>=2)
            {
                i--;
                CurrentIndex= 0;
                state= 2;
            }
            else
                state= 2;
            break;
        case 15:
            if (i>=2)
            {
                i--;
                state= 3;
            }
            else
                state= 2;
            break;
        case 20:
            /* pruefe Ende... */
            if (command == 'q')
            {
                StringInput[0]= '\0';
                while (   (StringInput[0] == '\0')
                       || (StringInput[1] != '\0')
                       || (    (StringInput[0] != 'j')
                            && (StringInput[0] != 'n')
                          )
                      )
                {
                    printf(
                    "Der Stimmzettel wird dadurch geloescht! Sicher (j/n)? ");
                    UtilInput(StringInput);
                }
                if (StringInput[0] == 'j')
                    return -1;
            }
            do
            {
                printf("\n<return> --> weiter");
                printf("; n --> Neueingabe fuer Platz %d;\n", i);
                if (CurrentIndex < GamesTableSize())
                {
                    printf("w --> fuer Platz %d weitersuchen; ", i);
                    printf("a --> Spieledatenbank mit Platz %d aendern\n",
                           i);
                }
                printf(" ? ");
                UtilInput(StringInput);
                if (strlen(StringInput) == 0)
                {
                    StringInput[0]= 'p';
                    StringInput[1]= '\0';
                }
                command2= StringInput[0];
                if ( (strlen(StringInput) > 1) ||
                     (   (command2 != 'p') && (command2 != 'w')
                      && (command2 != 'n') && (command2 != 'a')
                     )
                   )
                    command2= '.';  
                if ((CurrentIndex >= GamesTableSize())
                    && (command2 == 'w'))
                {
                    command2= '.'; /* --> falsche Eingabe */
                    printf("Ende erreicht (w nicht sinnvoll)!\n");
                }
                else if (command2 == '.')
                    printf("Eingabefehler!\n");
            }
            while(command2 == '.');
            switch (command2)
            {
            case 'p':
                state= -1;
                break;
            case 'w':
                state= 3;
                break;
            case 'n':
                state= 2;
                break;
            case 'a':
                state= 25;
                break;
            default:
                printf("Eingabefehler im Zustand 20 !\n");
                return 0;
            }
            break;
        case 25:
            /*
                gefundenes Spiel in Stimmzettel und Datenbank aendern...
            */
            if ((i>=2) && (CurrentIndex > 0))
            {
                GamesGet(CurrentIndex-1, &FoundName, &FoundAuthor,
                         &FoundPublisher);
                printf("alter Datensatz:\n");
                printf("Name    : %s\n", FoundName);
                printf("Autor   : %s\n", FoundAuthor);
                printf("Verlag  : %s\n", FoundPublisher);

                i--;
                state= 26;
            }
            else
                state= 2;
            break;
        case 26:
            switch(command)
            {
            case 'n':
                state= 2;
                break;
            case 'p':
                strcpy(name, StringInput);
                state= 27;
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
            case 'a':
                printf("Befehl hier nicht sinnvoll\n");
                break;
            default:
                printf("Eingabefehler im Zustand !\n");
                break;
            }
            break;
        case 27:
            switch(command)
            {
            case 'n':
                state= 2;
                break;
            case 'p':
                strcpy(author, StringInput);
                state= 28;
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
            case 'a':
                printf("Befehl hier nicht sinnvoll\n");
                break;
            default:
                printf("Eingabefehler im Zustand 27 !\n");
                break;
            }
            break;
        case 28:
            switch(command)
            {
            case 'n':
                state= 2;
                break;
            case 'p':
                GamesSet(CurrentIndex-1, name, author, StringInput,
                         0);
                TabSetCharts(index, i-1, name, CurrentIndex-1);
                i++;
                state= 1;
                break;
            case 'q':
                state= 20;
                break;
            case 'w':
            case 'a':
                printf("Befehl hier nicht sinnvoll\n");
                break;
            default:
                printf("Eingabefehler im Zustand 28 !\n");
                break;
            }
            break;
        default:
            printf("ComEnterCharts: unbekannter Bearbeitungszustand %d\n",
                   state);
            return 0;
        }
    }
    while ((0 <= state) && (state <= 28));
    if (state >= 0)
        printf("ComEnterCharts: Funktion im Zustand %d abgebrochen\n",
            state);
    return 1;
} /* ComEnterCharts */

static int ComEnterIce(int index)
/* Funktionswert:  0: Eingabe wiederholen;
                   1: Eingabe beenden;
                  -1: Eingabe abbrechen
*/
{  
    int  points[4][2];
    int  IceValid;
    int  sum;
    int  i;
    int  UserInput= 0;
    int  res;
    char StringInput[100];
    int  ReadInput;

    TabGetIce(index,
        &points[0][0], &points[0][1],
        &points[1][0], &points[1][1],
        &points[2][0], &points[2][1],
        &points[3][0], &points[3][1]);
    TabGetIceValid(index, &IceValid);
    if (IceValid)
    {
    printf("alte Aufstellung: (%d, %d; %d, %d; %d, %d; %d, %d)\n",
        points[0][0], points[0][1], points[1][0], points[1][1],
        points[2][0], points[2][1], points[3][0], points[3][1]);
    printf("(Of.1, De.1; Of.2, De.2; Of.3, De.3; Of.SD, De.SD)\n");
    }
    else
        printf("keine alte Aufstellung\n");

    sum= 0;
    for(i=0; i<4; i++)
    {
        do
        {
        if (i<3)
            printf("%d. Drittel\n", i+1);
        else
            printf("Sudden Death\n");

        printf("    Offensive");
        if (i == 0)
        {
            printf("\n        (<return> --> keine Aenderung;");
            printf(" q --> Abbruch; n --> kein Eishockey)\n");
            printf("        ? ");
        }
        else
            printf(" ? ");
       
        UtilInput(StringInput);
       
        if (strlen(StringInput) == 0)
        {
            if (i == 0)
                return 1;
            else
            {
                printf("Eingabefehler!\n");
                ReadInput= 1;
            }           
        }
        else if ((strlen(StringInput) == 1)
                 && (StringInput[0] == 'q') && (i == 0))
        {
            return -1;
        }
        else if ((strlen(StringInput) == 1)
                 && (StringInput[0] == 'n') && (i == 0))
        {
            TabSetIceValid(index, 0);
            return 1;
        }
        else
        {
            res= sscanf(StringInput, "%d", &UserInput);
            if (res != 1)
            {
                printf("Eingabefehler!\n");
                ReadInput= 1;
            }
            else
            {
                if ((UserInput < 0) || (UserInput > 100))
                {
                    printf("Eingabefehler!\n");
                    ReadInput= 1;
                }
                else
                    ReadInput= 0;
            }
        }
    } while (ReadInput);

        points[i][0]= UserInput;
        sum= sum + UserInput;;

        do
        {
        printf("    Defensive ? ");
        UtilIntInput(&UserInput);
        ReadInput= ((UserInput < 0) || (UserInput > 100));
        if (ReadInput)
            printf("Eingabefehler!\n");
    } while (ReadInput);
   
        points[i][1]= UserInput;
        sum= sum + points[i][1];
    }
    if (sum > 100)
    {
        printf("Punktesumme betraegt %d;\n", sum);
        return 0;
    }
    TabSetIce(index,
        points[0][0], points[0][1],
        points[1][0], points[1][1],
        points[2][0], points[2][1],
        points[3][0], points[3][1]);

    return 1;
} /* ComEnterIce */

static int ComEnterTennis(int index)
/* Funktionswert:  0: Eingabe wiederholen;
                   1: Eingabe beenden;
                  -1: Eingabe abbrechen
*/
{  
    int  points[3];
    int  TennisValid;
    int  sum;
    int  i;
    int  UserInput= 0;
    int  res;
    char StringInput[100];
    int  ReadInput;

    TabGetTennis(index, &points[0], &points[1], &points[2]);
    TabGetTennisValid(index, &TennisValid);
    if (TennisValid)
    {
    printf("alte Punkteverteilung: (%d, %d, %d)\n",
        points[0], points[1], points[2]);
    printf("(1. Satz, 2. Satz, 3. Satz)\n");
    }
    else
        printf("keine alte Punkteverteilung\n");

    sum= 0;
    for(i=0; i<3; i++)
    {
        do
        {
        printf("%d. Satz", i+1);

        if (i == 0)
        {
            printf(
            "\n    (<return> --> keine Aenderung; q --> Abbruch");
            printf("; n --> kein Tennis)\n    ");
        }
        printf(" ? ");
       
        UtilInput(StringInput);
       
        if (strlen(StringInput) == 0)
        {
            if (i == 0)
                return 1;
            else
            {
                printf("Eingabefehler!\n");
                ReadInput= 1;
            }           
        }
        else if ((strlen(StringInput) == 1)
                 && (StringInput[0] == 'q') && (i == 0))
        {
            return -1;
        }
        else if ((strlen(StringInput) == 1)
                 && (StringInput[0] == 'n') && (i == 0))
        {
            TabSetTennisValid(index, 0);
            return 1;
        }
        else
        {
            res= sscanf(StringInput, "%d", &UserInput);
            if (res != 1)
            {
                printf("Eingabefehler!\n");
                ReadInput= 1;
            }
            else
            {
                if ((UserInput < 0) || (UserInput > 100))
                {
                    printf("Eingabefehler!\n");
                    ReadInput= 1;
                }
                else
                    ReadInput= 0;
            }
        } /* ...else */
    } while (ReadInput);

        points[i]= UserInput;
        sum= sum + UserInput;;
    } /* for */
    if (sum != 100)
    {
        printf("Punktesumme betraegt %d;\n", sum);
        return 0;
    }
    TabSetTennis(index, points[0], points[1], points[2]);

    return 1;
} /* ComEnterTennis */

#define FIELD_COUNT 17
    /* Anzahl der von  ComEnterOnePlayer  zu bearbeitenden Eingabefelder */
typedef char tComString[100];

static void ComEnterOnePlayer(int index)
/* XXXX neu; nicht fertig; bisher unbenutzt;

    Es wird vom Benutzer die Eingabe des Spielers mit dem Index
    'index' entgegengenommen.
*/
{
    int CurrentField= 0;
        /* Feld, das fuer den aktuellen Stimmzettel eingegeben werden
           soll:
           0        : Mitglied
           1  bis  5: HippoCHARTS
           6  bis 13: HippoGAME
           14 bis 16: neues Spiel fuer Datenbank
        */
    int CurrentFieldBackup= 0;
    int IncrementField;
        /* 1: CurrentField inkrementieren;
           0: CurrentField nicht veraendern
        */
    int InputError;
    char command;
   
    tComString UserInput;
    int        UserIntInput;
    int IntFields[FIELD_COUNT];
    tComString CharFields[FIELD_COUNT];
    int ValidFields[FIELD_COUNT];
        /* 1: Daten ins Feld eingegeben;
           0: keine Daten im Feld
        */
    int IntInfo[FIELD_COUNT];
/* XXXX    char *FoundName, *FoundAuthor, *FoundPublisher;
    int FoundIndex, NewIndex; */
    int SearchIndex= 0;
        /* Index, ab dem in der Spieledatenbank gesucht wird */
    int i;

    for(CurrentField= 0; CurrentField < FIELD_COUNT; CurrentField++)
    {
        IntFields[CurrentField]= 0;
        CharFields[CurrentField][0]= '\0';
        ValidFields[CurrentField]= 0;
        IntInfo[CurrentField]= 0;
    }

    while (CurrentField < 17)
    {
        IncrementField= 1; InputError= 0;

        /*
            gib Prompt aus:
        */
        if (ValidFields[CurrentField])
            printf(">>>>%s\n", CharFields[CurrentField]);
        if (CurrentField == 0)
        {
            printf("Mitglied (1: ja, 0: nein)");
        }
        else if ((CurrentField >= 1) && (CurrentField <= 5))
        {
            printf("HippoCHARTS Platz %d ? ", CurrentField);
        }
        else if ((CurrentField >= 6) && (CurrentField <= 13))
        {/*HippoGAME*/
            if (CurrentField < 12)
            {
                printf("HippoGAME %d. Drittel %s",
                    (CurrentField-6) / 2 + 1,
                    ((CurrentField % 2) == 0) ?
                        "Offensive" : "Defensive");
            }
            else
            {
                printf("HippoGAME Sudden Death %s",
                    ((CurrentField % 2) == 0) ?
                        "Offensive" : "Defensive");
            }
        }
        else if ((CurrentField >= 14) && (CurrentField <= 16))
        {
            switch(CurrentField)
            {
            case 14:
                printf("Name");
                break;
            case 15:
                printf("Verlag");
                break;
            case 16:
                printf("Autor");
                break;
            default:
                printf(
                    "ComEnterOnePlayer: Wert fuer CurrentField %s",
                    "fehlerhaft\n");
                break;
            }
        }
        printf(" (h: Hilfe) ? ");

        /*
            lese Eingabe:
        */
        UtilInput(UserInput);
        if (strlen(UserInput) == 0)
            strcpy(UserInput, "n");
        if (    (strlen(UserInput) == 1)
             && (   (UserInput[0] == 'h') || (UserInput[0] == 'd')
                 || (UserInput[0] == 'b') || (UserInput[0] == 'n')
                 || (UserInput[0] == 'f') || (UserInput[0] == 'c')
                )
           )
        {
            command= UserInput[0];
        }
        else
        {
            command= 'i'; /* Daten eingegeben */
            sscanf(UserInput, "%d", &UserIntInput);
        }
        switch (command)
        {
        case 'h':
            printf(
            "d: (Delete) Eingabe abbrechen; Stimmzettel nicht speichern\n");
            if (CurrentField >= 1)
                printf(
                    "b: (Back) zurueck zum vorangegangenen Eingabefeld\n");
            if (CurrentField <= 12)
                printf("n, <cr>: (Next) zum naechsten Eingabefeld\n");
            if ((CurrentField >= 2) && (CurrentField <= 6))
            {
                if (SearchIndex > 0)
                {
                    printf("f: (Find) Weitersuchen HippoCHARTS Platz %d\n",
                        CurrentField-1);
                }
                printf("c: (Change) Spieledatenbank mit Platz %d aendern\n",
                    CurrentField-1);
            }
            break;
        case 'd': /* delete */
        case 'b': /* back */
        case 'n': /* next */
        case 'f': /* find */
        case 'c': /* change */
        case 'i': /* input */
            /*
                werte Dateneingabe aus:
            */
            if (CurrentField == 0)
            {
                InputError= ((UserIntInput != 0) && (UserIntInput != 1));
            }
            else if ((CurrentField >= 1) && (CurrentField <= 5))
            {
                InputError= (strlen(UserInput) == 0);
            }
            else if ((CurrentField >= 6) && (CurrentField <= 13))
            {
            }
            else if ((CurrentField >= 14) && (CurrentField <= 16))
            {
                if (CurrentField == 14)
                    InputError= (strlen(UserInput) == 0);
            }
            break;
        default:
            InputError= 1;
            break;
        } /* switch */
       
        /*
            setze CurrentField auf das naechste Feld:
        */
        if (InputError)
            printf("Eingabefehler !\n");
        else
        {
            strcpy(CharFields[CurrentField], UserInput);
            IntFields[CurrentField]= UserIntInput;
            ValidFields[CurrentField]= 1;
        }
        IncrementField= IncrementField && (!InputError);
        if (IncrementField)
        {
            if (CurrentField == 13)
                CurrentField= 17;
            else if (CurrentField == 16)
                CurrentField= CurrentFieldBackup;
            else
                CurrentField++;
        }
    } /* while */

    /*
        speichere den eingegebenen Stimmzettel:
    */
    TabSetMember(index, IntFields[0]);
    for(i=0; i<5; i++)
    {
        TabSetCharts(index, i, CharFields[1+i], IntInfo[1+i]);
    }
    TabSetIce(index,
        IntFields[6], IntFields[7],
        IntFields[8], IntFields[9],
        IntFields[10], IntFields[11],
        IntFields[12], IntFields[13]);
} /* ComEnterOnePlayer */

void ComEnterPlayers(void)
{
    int  ContinueLoop;
    int  NewIndex= 0;
    int  InputResult= -1; /* 1: ok; 0: wiederholen; -1: Abbruch */
    int  InputRoutine; /* 1: Member; 2: Charts; 3: Ice */
    int  ReadInput;
    char command;
    char UserInput[100];
    char NameEntered[100];
    int  CurrentIndex;
    int  TabGame;

    if (TabGetFirstUnused() >= TabMaxPlayers)
    {
        printf("Stimmzetteltabelle voll");
        return;
    }
    if (StatFileName(STAT_DATA) == NULL)
    {
        printf("zuerst Stimmzetteldatei bestimmen...Abbruch\n");
        return;
    }
   
    TabGame= TabGetGame();
   
    do
    {
        NameEntered[0]= '\0';
        CurrentIndex= 0;
        do
        {
            printf("Spielername (bzw. Namensteil) (h --> Hilfe) ? ");
            UtilInput(UserInput);
            if (strlen(UserInput) == 0)
            {
                if (strlen(NameEntered) > 0)
                    command= 'e';
                else
                    command= 'q'; /* --> Eingabe beenden */
            }
            else if (strlen(UserInput) > 1)
            {
                command= ' ';
                strcpy(NameEntered, UserInput);
                CurrentIndex= 0;
            }
            else
            {
                command= UserInput[0];
                if ((command == ' ') || (command == 'e'))
                    command= 'x'; /* --> Eingabefehler */
            }
            switch (command)
            {
            case ' ': /* Name eingegeben */
                if (strlen(NameEntered) > TabNameLength)
                    printf("zu lang!\n");
                else
                {
                    CurrentIndex= 0;
                    TabSearch(NameEntered, CurrentIndex, 0,
                              &CurrentIndex);
                    if (CurrentIndex < 0)
                    {
                        printf(
                        "nicht gefunden (<return> --> neu anlegen)\n");
                    }
                    else
                    {
                        printf(
                        "gefunden (w --> weitersuchen) : %s\n",
                            TabGetName(CurrentIndex));
                    }
                } /* if () else */
                ReadInput= 1;
                break;
            case 'e': /* zuerst Name und dann nur <return> eingegeben
                         --> 'NameEntered' ist hier gueltig */
                if (CurrentIndex < 0)
                {
                    /* Stimmzettel neu anlegen: */
                    NewIndex= TabNew();
                    TabSetName(NewIndex, NameEntered);
                }
                else
                {
                    /* gefundenen Stimmzettel editieren: */
                    NewIndex= CurrentIndex;
                }
                ReadInput= 0;
                break;
            case 'w': /* weitersuchen */
                if ((CurrentIndex >= 0) && (strlen(NameEntered) > 0))
                {
                TabSearch(NameEntered, CurrentIndex+1, 0,
                          &CurrentIndex);
                if (CurrentIndex < 0)
                {
                    printf(
                    "nicht gefunden (<return> --> neu anlegen)\n");
                }
                else
                {
                    printf(
                    "gefunden (w --> weitersuchen) : %s\n",
                        TabGetName(CurrentIndex));
                }
            }
            else
            {
                printf("Befehl w jetzt nicht sinnvoll\n");
            }
                ReadInput= 1;
                break;
            case 'h': /* Hilfe ausgeben */
                printf("moegliche Eingaben:\n");
                printf(
                "<return> nach Eingabe eines Stimmzettels: Eingabe beenden\n");
                printf(
                "<return> nach Eingabe eines Namen(-teils): Stimmzettel mit\n");
                printf(
                "                                  diesem Namen neu anlegen\n");
                printf(
                "w  nach Eingabe eines Namen(-teils), der gefunden wurde:\n");
                printf(
                "             weiter in Datenbank nach Namen(-teil) suchen\n");
                printf(
                "q  Eingabe beenden\n");
                printf(
                "h  Hilfe ausgeben\n");
                ReadInput= 1;
                break;
            case 'q': /* Eingabe beenden */
                ReadInput= 0;
                break;
            default:
                printf("Eingabefehler!\n");
                ReadInput= 1;
                break;
            } /* switch */
        } while(ReadInput);
       
        ContinueLoop= (command != 'q');
        if (!ContinueLoop) continue;

        InputRoutine= 1;
        do
        {
            switch(InputRoutine)
            {
            case 1: /* member */
                InputResult= ComEnterMember(NewIndex);
                break;
            case 2: /* charts */
                InputResult= ComEnterCharts(NewIndex);
                break;
            case 3: /* ice */
                if (TabGame & 1)
                    InputResult= ComEnterIce(NewIndex);
                else
                    InputResult= 1;
                break;
            case 4: /* tennis */
                if (TabGame & 2)
                    InputResult= ComEnterTennis(NewIndex);
                else
                    InputResult= 1;
                break;
            default:
                printf(
                  "ComEnterPlayers: unbekannte Eingaberoutine %d\n",
                  InputRoutine);
                break;
            } /* switch */
            switch(InputResult)
            {
            case 1:
                InputRoutine++;
                break;
            case 0:
                /* tue nichts (--> dasselbe nochmal) */
                break;
            case -1:
                /* Abbruch */
                InputRoutine= 100;
                TabDelete(NewIndex);
                break;
            default:
                printf(
                  "ComEnterPlayers: unbekanntes Eingabeergebnis %d\n",
                  InputResult);
                break;
            }
        } while (InputRoutine <= 4);
    } while(ContinueLoop);
    TabSort();
    TabWriteFile();
    GamesFlush();
} /* ComEnterPlayers */

void ComDeletePlayer()
{
    int i;
    char StringInput[100];

    printf("Index des Datensatzes? ");
    UtilIntInput(&i);
    printf("Sicher (j/n)? ");
    UtilInput(StringInput);
    if ((StringInput[0] == 'j') || (StringInput[1] == 'J'))
    {
        printf("loesche Stimmzetteldatensatz...");
        if (TabDelete(i))
            printf("fehlgeschlagen\n");
        else
            printf("OK\n");
    }
} /* ComDeletePlayer */

void ComPrintGames(void)
{
    int i, j;
    char *name, *author, *publisher;
    char UserInput[100];
    char command;

    printf("Nummer| Name | Verlag | Autor \n");
    i= 0; j= 3;
    do
    {
        GamesGet(i, &name, &author, &publisher);
        printf("%3d| %-23s| %-23s| %-23s\n",i, name, author, publisher);
        i++; j++;
        if (j >= 20)
        {
            do
            {
                printf("<return> --> weiter; q --> Abbruch ? ");
                UtilInput(UserInput);
                if (strlen(UserInput) == 0)
                    command= ' ';
                else if (strlen(UserInput) > 1)
                    command= 'f';
                else
                    command= UserInput[0];
                if (command == 'f')
                    printf("Eingabefehler!\n");
            } while ((command != ' ') && (command != 'q'));
            if (command == 'q')
                i= GamesTableSize();
            j= 0;
        }
    } while (i < GamesTableSize());
} /* ComPrintGames */

void ComInputGames(void)
{
    char NameEntered[100], AuthorEntered[100],
         PublisherEntered[100], UserInput[100];
    char *NameFound= NULL, *AuthorFound= NULL, *PublisherFound= NULL;
    int  changed= 0;
    int  CurrentIndex;
    char command;
    int  ReadInput= 0;
    int  NewIndex;

    NameEntered[0]= '\0';
    CurrentIndex= 0;
    do
    {
        printf("Name (h --> Hilfe) ? ");
        UtilInput(UserInput);
        if (strlen(UserInput) == 0)
        {
            if (strlen(NameEntered) > 0)
                command= 'e';
            else
                command= 'q'; /* --> Eingabe beenden */
        }
        else if (strlen(UserInput) > 1)
        {
            command= ' ';
            strcpy(NameEntered, UserInput);
            CurrentIndex= 0;
        }
        else
        {
            command= UserInput[0];
            if ((command == ' ') || (command == 'e'))
                command= 'x'; /* --> Eingabefehler */
        }
        switch (command)
        {
        case ' ': /* Name eingegeben */
            if (strlen(NameEntered) > TabNameLength)
                printf("zu lang!\n");
            else
            {
                CurrentIndex= 0;
                GamesSearch(NameEntered, CurrentIndex, 0,
                            &CurrentIndex);
                if (CurrentIndex < 0)
                {
                    printf(
                    "nicht gefunden (<return> --> neu anlegen)\n");
                }
                else
                {
                    GamesGet(CurrentIndex, &NameFound,
                             &AuthorFound, &PublisherFound);
                    printf(
                    "gefunden (w --> weitersuchen, <return> --> aendern) :\n");
                    printf("    Name  : %s\n", NameFound);
                    printf("    Autor : %s\n", AuthorFound);
                    printf("    Verlag: %s\n", PublisherFound);
                }
            } /* if () else */
            ReadInput= 1;
            break;
        case 'e': /* zuerst Name und dann nur <return> eingegeben
                     --> 'NameEntered' ist hier gueltig */
            do
            {
                printf("Autor  ? ");
                UtilInput(AuthorEntered);
                if (strlen(AuthorEntered) > 30)
                    printf("zu lang!\n");
            } while (strlen(AuthorEntered) > 30);

            do
            {
                printf("Verlag ? ");
                UtilInput(PublisherEntered);
                if (strlen(PublisherEntered) > 30)
                    printf("zu lang!\n");
            } while (strlen(PublisherEntered) > 30);

            if (CurrentIndex < 0)
            {
                /* Spiel neu anlegen: */
                GamesNew(&NewIndex, 0);
                GamesSet(NewIndex, NameEntered, AuthorEntered,
                         PublisherEntered, 0);
            }
            else
            {
                /* gefundenes Spiel aendern: */
                NewIndex= CurrentIndex;
                GamesSet(NewIndex, NameFound, AuthorEntered,
                         PublisherEntered, 0);
            }
            GamesGet(NewIndex, &NameFound, &AuthorFound,
                     &PublisherFound);
            printf("neuer Datensatz:\n");
            printf("    Name  : %s\n", NameFound);
            printf("    Autor : %s\n", AuthorFound);
            printf("    Verlag: %s\n\n", PublisherFound);
            ReadInput= 1;
            changed= 1;
            NameEntered[0]= '\0';
            CurrentIndex= 0;
            break;
        case 'w': /* weitersuchen */
            if ((CurrentIndex >= 0) && (strlen(NameEntered) > 0))
            {
                GamesSearch(NameEntered, CurrentIndex+1, 0,
                            &CurrentIndex);
                if (CurrentIndex < 0)
                {
                    printf(
                    "nicht gefunden (<return> --> neu anlegen)\n");
                }
                else
                {
                    GamesGet(CurrentIndex, &NameFound,
                             &AuthorFound, &PublisherFound);
                    printf(
                    "gefunden (w --> weitersuchen) :\n");
                    printf("    Name  : %s\n", NameFound);
                    printf("    Autor : %s\n", AuthorFound);
                    printf("    Verlag: %s\n", PublisherFound);
                }
            }
            else
            {
                printf("Befehl w jetzt nicht sinnvoll\n");
            }
            ReadInput= 1;
            break;
        case 'h': /* Hilfe ausgeben */
            printf("moegliche Eingaben:\n");
            printf(
            "<return> nach Eingabe/Aenderung eines Spiels: Eingabe beenden\n");
            printf(
            "<return> nach Eingabe eines Namen(-teils):\n");
            printf(
            "                 Spiel mit diesem Namen neu anlegen\n");
            printf(
            "w  nach Eingabe eines Namen(-teils), der gefunden wurde:\n");
            printf(
            "           weiter in Datenbank nach Namen(-teil) suchen\n");
            printf(
            "q  Eingabe beenden\n");
            printf(
            "h  Hilfe ausgeben\n");
            ReadInput= 1;
            break;
        case 'q': /* Eingabe beenden */
            ReadInput= 0;
            break;
        default:
            printf("Eingabefehler!");
            ReadInput= 1;
            break;
        } /* switch */
    } while(ReadInput);

    if (changed)
    {
        TabDeleteAllGamesReferences();
        GamesSort();
        GamesFlush();
        TabSetGamesReferences();
    }
} /* ComInputGames */

void ComDeleteGames(void)
{
    int  i;
    char input[100];
    int  res;
    int  ok;
    char *GotName= NULL, *GotAuthor= NULL, *GotPublisher= NULL;

    do
    {
        do
        {
            ok= 1;
        printf("Index (<return> --> Ende)? ");
        UtilInput(input);
        if (strlen(input) > 0)
        {
            res= sscanf(input, "%d", &i);
            ok= (res == 1);
            if (ok)
                ok= (i>=0);
            if (ok)
                ok= (res= GamesGet(i, &GotName, &GotAuthor,
                                   &GotPublisher)) == 0;
            if (!ok)
                if (res)
                    printf("Index fehlerhaft!\n");
                else
                    printf("Eingabefehler!\n");
        }
        else
            i= -1;
    } while (!ok);

        if (i>=0)
        {
            printf("Zu loeschender Datensatz:\n");
            printf("    Name  : %s\n", GotName);
            printf("    Autor : %s\n", GotAuthor);
            printf("    Verlag: %s\n", GotPublisher);
            printf("  Sicher (j/n)? ");
            UtilInput(input);

            if ((input[0] == 'j') || (input[0] == 'J'))
                GamesDelete(i);
        }
    } while(i >= 0);
    TabDeleteAllGamesReferences();
    TabSetGamesReferences();
    GamesFlush();
} /* ComDeleteGames */

void ComSumDelete(void)
{
    char buffer[101];
    int  res;
   
    printf("Kuerzel der HippoCHARTS-Einzelauswertung, die aus der\n");
    printf("Sammelauswertungsdatenbank zu loeschen ist ? ");
    UtilInput(buffer);
    if (strlen(buffer) < 1)
        printf("zu kurz\n");
    else if (strlen(buffer) > 4)
        printf("zu lang\n");
    else
    {
        printf("loesche Datensaetze...");
        res= SumDelete(buffer);
        if (res)
            printf("Fehler\n");
        else
            printf("OK\n");
    }
} /* ComSumDelete */

void ComSumListAbbrev(void)
{
    int  i, j;
    char UserInput[100];
    char command;
    int  res;
    char abbrev[10];

    printf("bereite Liste auf...");
    res= SumListAbbrev();
    if (res)
    {
        printf("Fehler\n");
        return;
    }
    printf("OK\n\n");
   
    i= 0; j= 5;
    do
    {
        res= SumNextAbbrev(abbrev);
        if (res == -1)
        {
            printf("Fehler\n");
            return;
        }
        if (!res)
        {
            printf("%-5s", abbrev);
            i++;
            if (i >= 15)
            {
                printf("\n");
                j++;
            }
        }
        else
            printf("\n");
        if (j >= 20)
        {
            do
            {
                printf("<return> --> weiter; q --> Abbruch ? ");
                UtilInput(UserInput);
                if (strlen(UserInput) == 0)
                    command= ' ';
                else if (strlen(UserInput) > 1)
                    command= 'f';
                else
                    command= UserInput[0];
                if (command == 'f')
                    printf("Eingabefehler!\n");
            } while ((command != ' ') && (command != 'q'));
            if (command == 'q')
                res= 1;
            i= 0; j= 0;
        }
    } while (!res);
   
    printf("\n");
} /* ComSumListAbbrev */