rnd-19981005-1
[rocksndiamonds.git] / src / files.c
index 284ed51fc0c7a8099d7834c099b903756e8de1b2..d34fe40da717af4a1ab00e4a1b927d58f4a255c7 100644 (file)
@@ -1,13 +1,12 @@
 /***********************************************************
 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
 *----------------------------------------------------------*
-*  ©1995 Artsoft Development                               *
-*        Holger Schemel                                    *
-*        33659 Bielefeld-Senne                             *
-*        Telefon: (0521) 493245                            *
-*        eMail: aeglos@valinor.owl.de                      *
-*               aeglos@uni-paderborn.de                    *
-*               q99492@pbhrzx.uni-paderborn.de             *
+*  (c) 1995-98 Artsoft Entertainment                       *
+*              Holger Schemel                              *
+*              Oststrasse 11a                              *
+*              33604 Bielefeld                             *
+*              phone: ++49 +521 290471                     *
+*              email: aeglos@valinor.owl.de                *
 *----------------------------------------------------------*
 *  files.h                                                 *
 ***********************************************************/
@@ -21,7 +20,7 @@
 BOOL CreateNewScoreFile()
 {
   int i,j,k;
-  char filename[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
   char empty_alias[MAX_NAMELEN];
   FILE *file;
 
@@ -54,7 +53,7 @@ BOOL CreateNewScoreFile()
 
 BOOL CreateNewNamesFile(int mode)
 {
-  char filename[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
   FILE *file;
 
   if (mode==PLAYER_LEVEL)
@@ -76,22 +75,22 @@ BOOL CreateNewNamesFile(int mode)
 BOOL LoadLevelInfo()
 {
   int i;
-  char filename[MAX_FILENAME];
-  char cookie[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
+  char cookie[MAX_FILENAME_LEN];
   FILE *file;
 
   sprintf(filename,"%s/%s",level_directory,LEVDIR_FILENAME);
 
   if (!(file=fopen(filename,"r")))
   {
-    fprintf(stderr,"%s: cannot load level info '%s'!\n",progname,filename);
+    Error(ERR_RETURN, "cannot load level info '%s'", filename);
     return(FALSE);
   }
 
   fscanf(file,"%s\n",cookie);
   if (strcmp(cookie,LEVELDIR_COOKIE))  /* ungültiges Format? */
   {
-    fprintf(stderr,"%s: wrong format of level info file!\n",progname);
+    Error(ERR_RETURN, "wrong format of level info file");
     fclose(file);
     return(FALSE);
   }
@@ -112,7 +111,7 @@ BOOL LoadLevelInfo()
 
   if (!num_leveldirs)
   {
-    fprintf(stderr,"%s: empty level info '%s'!\n",progname,filename);
+    Error(ERR_RETURN, "empty level info '%s'", filename);
     return(FALSE);
   }
 
@@ -122,19 +121,15 @@ BOOL LoadLevelInfo()
 void LoadLevel(int level_nr)
 {
   int i,x,y;
-  char filename[MAX_FILENAME];
-  char cookie[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
+  char cookie[MAX_FILENAME_LEN];
   FILE *file;
 
   sprintf(filename,"%s/%s/%d",
          level_directory,leveldir[leveldir_nr].filename,level_nr);
 
-  if (!(file=fopen(filename,"r")))
-  {
-/*
-    fprintf(stderr,"%s: cannot load level '%s'!\n",progname,filename);
-*/
-  }
+  if (!(file = fopen(filename,"r")))
+    Error(ERR_RETURN, "cannot load level '%s' - creating new level", filename);
   else
   {
     fgets(cookie,LEVEL_COOKIE_LEN,file);
@@ -142,8 +137,7 @@ void LoadLevel(int level_nr)
 
     if (strcmp(cookie,LEVEL_COOKIE))   /* ungültiges Format? */
     {
-      fprintf(stderr,"%s: wrong format of level file '%s'!\n",
-             progname,filename);
+      Error(ERR_RETURN, "wrong format of level file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -217,8 +211,8 @@ void LoadLevel(int level_nr)
 void LoadLevelTape(int level_nr)
 {
   int i;
-  char filename[MAX_FILENAME];
-  char cookie[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
+  char cookie[MAX_FILENAME_LEN];
   FILE *file;
   BOOL levelrec_10 = FALSE;
 
@@ -238,8 +232,7 @@ void LoadLevelTape(int level_nr)
       levelrec_10 = TRUE;
     else if (strcmp(cookie,LEVELREC_COOKIE))   /* unknown tape format */
     {
-      fprintf(stderr,"%s: wrong format of level recording file '%s'!\n",
-             progname,filename);
+      Error(ERR_RETURN, "wrong format of level recording file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -274,10 +267,10 @@ void LoadLevelTape(int level_nr)
     {
       if (levelrec_10 && j>0)
       {
-       tape.pos[i].joystickdata[j] = MV_NO_MOVING;
+       tape.pos[i].action[j] = MV_NO_MOVING;
        continue;
       }
-      tape.pos[i].joystickdata[j] = fgetc(file);
+      tape.pos[i].action[j] = fgetc(file);
     }
 
     tape.pos[i].delay = fgetc(file);
@@ -289,8 +282,7 @@ void LoadLevelTape(int level_nr)
   fclose(file);
 
   if (i != tape.length)
-    fprintf(stderr,"%s: level recording file '%s' corrupted!\n",
-           progname,filename);
+    Error(ERR_RETURN, "level recording file '%s' corrupted", filename);
 
   tape.length_seconds = GetTapeLength();
 }
@@ -298,25 +290,19 @@ void LoadLevelTape(int level_nr)
 void LoadScore(int level_nr)
 {
   int i,j;
-  char filename[MAX_FILENAME];
-  char cookie[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
+  char cookie[MAX_FILENAME_LEN];
   FILE *file;
 
   sprintf(filename,"%s/%s/%s",
          level_directory,leveldir[leveldir_nr].filename,SCORE_FILENAME);
 
-  if (!(file=fopen(filename,"r")))
+  if (!(file = fopen(filename,"r")))
   {
     if (!CreateNewScoreFile())
-    {
-      fprintf(stderr,"%s: cannot create score file '%s'!\n",
-             progname,filename);
-    }
-    else if (!(file=fopen(filename,"r"))) 
-    {
-      fprintf(stderr,"%s: cannot load score for level %d!\n",
-             progname,level_nr);
-    }
+      Error(ERR_RETURN, "cannot create score file '%s'", filename);
+    else if (!(file = fopen(filename,"r"))) 
+      Error(ERR_RETURN, "cannot load score for level %d", level_nr);
   }
 
   if (file)
@@ -324,7 +310,7 @@ void LoadScore(int level_nr)
     fgets(cookie,SCORE_COOKIE_LEN,file);
     if (strcmp(cookie,SCORE_COOKIE))   /* ungültiges Format? */
     {
-      fprintf(stderr,"%s: wrong format of score file!\n",progname);
+      Error(ERR_RETURN, "wrong format of score file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -356,8 +342,8 @@ void LoadScore(int level_nr)
 void LoadPlayerInfo(int mode)
 {
   int i;
-  char filename[MAX_FILENAME];
-  char cookie[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
+  char cookie[MAX_FILENAME_LEN];
   FILE *file;
   char *login_name = GetLoginName();
   struct PlayerInfo default_player, new_player;
@@ -380,18 +366,12 @@ void LoadPlayerInfo(int mode)
 
   new_player = default_player;
 
-  if (!(file=fopen(filename,"r")))
+  if (!(file = fopen(filename,"r")))
   {
     if (!CreateNewNamesFile(mode))
-    {
-      fprintf(stderr,"%s: cannot create names file '%s'!\n",
-             progname,filename);
-    }
-    else if (!(file=fopen(filename,"r"))) 
-    {
-      fprintf(stderr,"%s: cannot load player information '%s'!\n",
-             progname,filename);
-    }
+      Error(ERR_RETURN, "cannot create names file '%s'", filename);
+    else if (!(file = fopen(filename,"r"))) 
+      Error(ERR_RETURN, "cannot load player information file '%s'", filename);
   }
 
   if (file)
@@ -401,8 +381,7 @@ void LoadPlayerInfo(int mode)
       version_10_file = TRUE;
     else if (strcmp(cookie,NAMES_COOKIE))      /* ungültiges Format? */
     {
-      fprintf(stderr,"%s: wrong format of names file '%s'!\n",
-             progname,filename);
+      Error(ERR_RETURN, "wrong format of names file '%s'", filename);
       fclose(file);
       file = NULL;
     }
@@ -438,11 +417,9 @@ void LoadPlayerInfo(int mode)
       new_player = default_player;
 
       fclose(file);
-      if (!(file=fopen(filename,"a")))
-      {
-       fprintf(stderr,"%s: cannot append new player to names file '%s'!\n",
-               progname,filename);
-      }
+      if (!(file = fopen(filename,"a")))
+       Error(ERR_RETURN, "cannot append new player to names file '%s'",
+             filename);
       else
       {
        for(i=0;i<MAX_NAMELEN;i++)
@@ -488,7 +465,7 @@ void LoadPlayerInfo(int mode)
 void SaveLevel(int level_nr)
 {
   int i,x,y;
-  char filename[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
   FILE *file;
 
   sprintf(filename,"%s/%s/%d",
@@ -496,7 +473,7 @@ void SaveLevel(int level_nr)
 
   if (!(file=fopen(filename,"w")))
   {
-    fprintf(stderr,"%s: cannot save level file '%s'!\n",progname,filename);
+    Error(ERR_RETURN, "cannot save level file '%s'", filename);
     return;
   }
 
@@ -538,7 +515,7 @@ void SaveLevel(int level_nr)
 void SaveLevelTape(int level_nr)
 {
   int i;
-  char filename[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
   FILE *file;
   BOOL new_tape = TRUE;
 
@@ -556,14 +533,13 @@ void SaveLevelTape(int level_nr)
     new_tape = FALSE;
     fclose(file);
 
-    if (!AreYouSure("Replace old tape ?",AYS_ASK))
+    if (!Request("Replace old tape ?",REQ_ASK))
       return;
   }
 
   if (!(file=fopen(filename,"w")))
   {
-    fprintf(stderr,"%s: cannot save level recording file '%s'!\n",
-           progname,filename);
+    Error(ERR_RETURN, "cannot save level recording file '%s'", filename);
     return;
   }
 
@@ -590,7 +566,7 @@ void SaveLevelTape(int level_nr)
     int j;
 
     for(j=0; j<MAX_PLAYERS; j++)
-      fputc(tape.pos[i].joystickdata[j],file);
+      fputc(tape.pos[i].action[j],file);
 
     fputc(tape.pos[i].delay,file);
   }
@@ -602,13 +578,13 @@ void SaveLevelTape(int level_nr)
   tape.changed = FALSE;
 
   if (new_tape)
-    AreYouSure("tape saved !",AYS_CONFIRM);
+    Request("tape saved !",REQ_CONFIRM);
 }
 
 void SaveScore(int level_nr)
 {
   int i,j;
-  char filename[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
   FILE *file;
 
   sprintf(filename,"%s/%s/%s",
@@ -616,8 +592,7 @@ void SaveScore(int level_nr)
 
   if (!(file=fopen(filename,"r+")))
   {
-    fprintf(stderr,"%s: cannot save score for level %d!\n",
-           progname,level_nr);
+    Error(ERR_RETURN, "cannot save score for level %d", level_nr);
     return;
   }
 
@@ -637,8 +612,8 @@ void SaveScore(int level_nr)
 void SavePlayerInfo(int mode)
 {
   int i;
-  char filename[MAX_FILENAME];
-  char cookie[MAX_FILENAME];
+  char filename[MAX_FILENAME_LEN];
+  char cookie[MAX_FILENAME_LEN];
   FILE *file;
   struct PlayerInfo default_player;
   int version_10_file = FALSE;
@@ -649,10 +624,9 @@ void SavePlayerInfo(int mode)
   else
     sprintf(filename,"%s/%s",CONFIG_PATH,NAMES_FILENAME);
 
-  if (!(file=fopen(filename,"r+")))
+  if (!(file = fopen(filename,"r+")))
   {
-    fprintf(stderr,"%s: cannot save player information '%s'!\n",
-           progname,filename);
+    Error(ERR_RETURN, "cannot save player information to file '%s'", filename);
     return;
   }
 
@@ -661,8 +635,7 @@ void SavePlayerInfo(int mode)
     version_10_file = TRUE;
   else if (strcmp(cookie,NAMES_COOKIE))        /* ungültiges Format? */
   {
-    fprintf(stderr,"%s: wrong format of names file '%s'!\n",
-           progname,filename);
+    Error(ERR_RETURN, "wrong format of names file '%s'", filename);
     fclose(file);
     return;
   }
@@ -732,7 +705,7 @@ void LoadJoystickData()
   fscanf(file,"%s",cookie);
   if (strcmp(cookie,JOYSTICK_COOKIE))  /* ungültiges Format? */
   {
-    fprintf(stderr,"%s: wrong format of joystick file!\n",progname);
+    Error(ERR_RETURN, "wrong format of joystick file '%s'", JOYDAT_FILE);
     fclose(file);
     return;
   }
@@ -766,7 +739,8 @@ void SaveJoystickData()
 
   if (!(file=fopen(JOYDAT_FILE,"w")))
   {
-    fprintf(stderr,"%s: cannot save joystick calibration data!\n",progname);
+    Error(ERR_RETURN, "cannot save joystick calibration data to file '%s'",
+         JOYDAT_FILE);
     return;
   }