home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / game / think / chaos / src / project.c < prev    next >
C/C++ Source or Header  |  1994-11-19  |  10KB  |  397 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.3
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: Project.c,v $
  20.     $Revision: 3.3 $
  21.     $Date: 1994/11/19 19:32:01 $
  22.  
  23.     This file contains the functions of the Project-menu.
  24.  
  25.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  26.  
  27.     Author:    Jochen Wiedmann
  28.         Am Eisteich 9
  29.       72555 Metzingen
  30.         Tel. 07123 / 14881
  31.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  32. */
  33.  
  34.  
  35. #ifndef CHAOS_H
  36. #include "chaos.h"
  37. #endif
  38.  
  39.  
  40.  
  41. static char FILEVERSION[9] = "CHAOS5.3";
  42.  
  43.  
  44.  
  45.  
  46. /*
  47.     DeleteTournament creates a new tournament with default settings.
  48.  
  49.     Inputs: name    pointer to a string containing the new tournaments
  50.             name.
  51. */
  52. void *TrnMem = NULL;
  53. void DeleteTournament(char *name)
  54.  
  55. {
  56.   /*
  57.       Deallocate data used by the old tournament.
  58.   */
  59.   PutMemList (&TrnMem);
  60.  
  61.   /*
  62.       Initialize the new tournament.
  63.   */
  64.   NewList(&PlayerList);
  65.   PlayerList.lh_Type = (UBYTE) -1;
  66.   *TrnFileName = '\0';
  67.   NumRounds = 0;
  68.   NumPlayers = 0;
  69.   if (name == NULL)
  70.   { *TrnName = '\0';
  71.   }
  72.   else
  73.   { strcpy (TrnName, name);
  74.   }
  75.   TrnMode = 0;
  76.   WinnerPoints = DefaultWinnerPoints;
  77.   DrawPoints = DefaultDrawPoints;
  78.   IsSaved = TRUE;
  79. }
  80.  
  81.  
  82.  
  83.  
  84. /*
  85.     SaveTournament() saves a tournament.
  86.  
  87.     Inputs: name    Name of the tournament file; FileRequest() is called,
  88.             if name is NULL or the empty string.
  89.  
  90.     Result: TRUE, if successfull, FALSE otherwise
  91. */
  92. int SaveTournament(char *name)
  93.  
  94.   { FILE *fh;
  95.     struct Player *tn;
  96.     struct Game *gm;
  97.  
  98.     if (name == NULL  ||  *name == '\0')
  99.       { if ((name = FileRequest(TrnFileName, NULL, NULL, TRUE))  ==  NULL)
  100.       { return(FALSE);
  101.       }
  102.       }
  103.  
  104.     if ((fh = fopen(name, "w"))  ==  NULL)
  105.       { ShowError((char *) GetChaosString(MSG_NO_WRITE_FILE), name, IoErr());
  106.     return(FALSE);
  107.       }
  108.  
  109.     if (fwrite(FILEVERSION, sizeof(FILEVERSION)-1, 1, fh) != 1)
  110.       { goto WriteError;
  111.       }
  112.  
  113.     if (fwrite(&NumRounds, sizeof(NumRounds), 1, fh) != 1               ||
  114.     fwrite(&RankingFirst, sizeof(RankingFirst), 1, fh) != 1         ||
  115.     fwrite(&NumPlayers, sizeof(NumPlayers), 1, fh) != 1             ||
  116.     fwrite(&NumGamesMissing, sizeof(NumGamesMissing), 1, fh) != 1   ||
  117.     fwrite(TrnName, sizeof(TrnName), 1, fh) != 1                    ||
  118.     fwrite(&TrnMode, sizeof(TrnMode), 1, fh) != 1                   ||
  119.     fwrite(&WinnerPoints, sizeof(WinnerPoints), 1, fh) != 1         ||
  120.     fwrite(&DrawPoints, sizeof(DrawPoints), 1, fh) != 1)
  121.       { goto WriteError;
  122.       }
  123.  
  124.     for (tn = (struct Player *) PlayerList.lh_Head;
  125.      tn->Tn_Node.ln_Succ != NULL;
  126.      tn = (struct Player *) tn->Tn_Node.ln_Succ)
  127.       { tn->Helpptr = tn;
  128.     if (fwrite(tn, sizeof(*tn), 1, fh)  !=  1)
  129.       { goto WriteError;
  130.       }
  131.     for (gm = tn->First_Game;  gm != NULL;  gm = gm->Next)
  132.       { if (fwrite(gm, sizeof(*gm), 1, fh)  !=  1)
  133.           { goto WriteError;
  134.           }
  135.       }
  136.       }
  137.  
  138.     fclose(fh);
  139.     IsSaved = TRUE;
  140.     strcpy(TrnFileName, name);
  141.     CreateIcon(name);
  142.     return(TRUE);
  143.  
  144. WriteError:
  145.     fclose(fh);
  146.     ShowError((char *) GetChaosString(MSG_WRITE_ERROR), IoErr(), name);
  147.     return(FALSE);
  148.   }
  149.  
  150.  
  151.  
  152.  
  153. /*
  154.     The function NewAddress() initializes a pointer after loading.
  155. */
  156. static void NewAddress(struct Player **tptr)
  157.  
  158. { struct Player *tn;
  159.  
  160.   for (tn = (struct Player *) PlayerList.lh_Head;
  161.        tn->Tn_Node.ln_Succ != NULL;
  162.        tn = (struct Player *) tn->Tn_Node.ln_Succ)
  163.   { if (tn->Helpptr == *tptr)
  164.     { *tptr = tn;
  165.       return;
  166.     }
  167.   }
  168. }
  169.  
  170.  
  171.  
  172.  
  173. /*
  174.     LoadTournament() loads a tournament from disk.
  175.  
  176.     Inputs: name    - Name of the tournament file; FileRequest() is called,
  177.               if name is NULL or the empty string.
  178.         memlist - a pointer to the memlist, where the player should
  179.               be inserted; this may be NULL, in which case TrnMem
  180.               is used
  181.         plrlist - a pointer to a list, where the new player should be
  182.               inserted; this may be NULL, in which case PlayerList
  183.               is used
  184.  
  185.  
  186.     Result: RETURN_OK, if successful, RETURN_WARN or RETURN_ERROR otherwise
  187. */
  188. int LoadTournament(char *name, void **memlist, struct List *plrlist)
  189.  
  190. { FILE *fh;
  191.   struct Player *tn, *rankingfirst;
  192.   struct Game *gm;
  193.   int i, j;
  194.   char fileversion[sizeof(FILEVERSION)-1];
  195.   char trnname[TRNNAME_LEN+1];
  196.   int import;
  197.   int numplayers, numrounds, numgamesmissing, trnmode, winnerpoints, drawpoints;
  198.   int version = 53;
  199.  
  200.   if (plrlist == NULL)
  201.   { plrlist = &PlayerList;
  202.   }
  203.   if (memlist == NULL)
  204.   { memlist = &TrnMem;
  205.   }
  206.   import = (plrlist != &PlayerList);
  207.  
  208.   if (name == NULL  ||  *name == '\0')
  209.   { if ((name = FileRequest(TrnFileName, NULL, NULL, FALSE))  ==  NULL)
  210.     { return(RETURN_WARN);
  211.     }
  212.   }
  213.  
  214.   if ((fh = fopen(name, "r"))  ==  NULL)
  215.   { ShowError((char *) GetChaosString(MSG_NO_READ_FILE), name, IoErr());
  216.     return(RETURN_ERROR);
  217.   }
  218.  
  219.   if (fread(fileversion, sizeof(FILEVERSION)-1, 1, fh)  !=  1)
  220.   { goto ReadError;
  221.   }
  222.   if (strncmp(fileversion, FILEVERSION, sizeof(FILEVERSION)-1) != 0)
  223.   { if(strncmp(fileversion, "CHAOS5.0", 8) != 0)
  224.     { ShowError((char *) GetChaosString(MSG_NO_CHAOS_FILE), name);
  225.       fclose(fh);
  226.       return(RETURN_ERROR);
  227.     }
  228.     version = 50;
  229.   }
  230.  
  231.   if (!import)
  232.   { DeleteTournament(NULL);
  233.   }
  234.   NewList(plrlist);
  235.   if (fread(&numrounds, sizeof(numrounds), 1, fh) != 1              ||
  236.       fread(&rankingfirst, sizeof(rankingfirst), 1, fh) != 1        ||
  237.       fread(&numplayers, sizeof(numplayers), 1, fh) != 1            ||
  238.       fread(&numgamesmissing, sizeof(numgamesmissing), 1, fh) != 1  ||
  239.       fread(trnname, sizeof(trnname), 1, fh) != 1                   ||
  240.       fread(&trnmode, sizeof(trnmode), 1, fh) != 1)
  241.     { goto ReadError;
  242.     }
  243.   if (version >= 53)
  244.   { if (fread(&winnerpoints, sizeof(winnerpoints), 1, fh) != 1      ||
  245.     fread(&drawpoints, sizeof(drawpoints), 1, fh) != 1)
  246.     { goto ReadError;
  247.     }
  248.   }
  249.   else
  250.   { winnerpoints = 2;
  251.     drawpoints = 1;
  252.   }
  253.   if (!import)
  254.   { NumRounds = numrounds;
  255.     RankingFirst = rankingfirst;
  256.     NumPlayers = numplayers;
  257.     NumGamesMissing = numgamesmissing;
  258.     strcpy(TrnName, trnname);
  259.     TrnMode = trnmode;
  260.     WinnerPoints = winnerpoints;
  261.     DrawPoints = drawpoints;
  262.   }
  263.  
  264.  
  265.   for (i = 0;  i < numplayers;  i++)
  266.   { int readsize = sizeof(*tn) + (import ? 0 : sizeof(*gm)*numrounds);
  267.     int seeksize = import ? sizeof(*gm)*numrounds : 0;
  268.  
  269.     if ((tn = GetMem(memlist, readsize))  ==  NULL)
  270.     { goto Error;
  271.     }
  272.  
  273.     if (fread (tn, readsize, 1, fh)  !=  1   ||
  274.     (seeksize != 0  &&  fseek(fh, seeksize, SEEK_CUR)))
  275.     { goto ReadError;
  276.     }
  277.  
  278.     tn->Tn_Node.ln_Name = tn->Name;
  279.     AddTail(plrlist, (struct Node *) tn);
  280.  
  281.     if (!import)
  282.     { if (NumRounds != 0)
  283.       tn->First_Game = gm = (struct Game *)(tn+1);
  284.       for (j = 0;  j < NumRounds-1;  j++, gm++)
  285.       { gm->Next = gm+1;
  286.       }
  287.     }
  288.   }
  289.  
  290.   if (!import)
  291.   { for (tn = (struct Player *) PlayerList.lh_Head;
  292.      tn->Tn_Node.ln_Succ != NULL;
  293.      tn = (struct Player *) tn->Tn_Node.ln_Succ)
  294.     { NewAddress(&tn->RankNext);
  295.       for (gm = tn->First_Game;  gm != NULL;  gm = gm->Next)
  296.       { NewAddress(&gm->Opponent);
  297.       }
  298.     }
  299.     NewAddress(&RankingFirst);
  300.  
  301.     IsSaved = TRUE;
  302.     strcpy(TrnFileName, name);
  303.   }
  304.  
  305.  
  306.   fclose(fh);
  307.   return(RETURN_OK);
  308.  
  309. ReadError:
  310.   ShowError((char *) GetChaosString(MSG_READ_ERROR), IoErr(), name);
  311. Error:
  312.   fclose(fh);
  313.   if (!import)
  314.   { DeleteTournament(NULL);
  315.   }
  316.   return(RETURN_ERROR);
  317. }
  318.  
  319.  
  320.  
  321.  
  322. /*
  323.     TestSaved() checks, if the current tournament has changed since the
  324.     last load or save. The user is asked for saving, if it has.
  325.  
  326.     Result: FALSE,