X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Ffiles.c;h=a77b4463660c5821680defce31430e15ed4d09b7;hb=d7d97176a88ee47f893055480c76b9c05a253d05;hp=284ed51fc0c7a8099d7834c099b903756e8de1b2;hpb=cfe0b06c84dc509e45c616c21ca1d5ef71f2304f;p=rocksndiamonds.git diff --git a/src/files.c b/src/files.c index 284ed51f..a77b4463 100644 --- a/src/files.c +++ b/src/files.c @@ -1,27 +1,28 @@ /*********************************************************** * 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 * ***********************************************************/ +#include + #include "files.h" #include "tools.h" #include "misc.h" #include "tape.h" #include "joystick.h" -BOOL CreateNewScoreFile() +boolean CreateNewScoreFile() { int i,j,k; - char filename[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; char empty_alias[MAX_NAMELEN]; FILE *file; @@ -52,9 +53,9 @@ BOOL CreateNewScoreFile() return(TRUE); } -BOOL CreateNewNamesFile(int mode) +boolean CreateNewNamesFile(int mode) { - char filename[MAX_FILENAME]; + char filename[MAX_FILENAME_LEN]; FILE *file; if (mode==PLAYER_LEVEL) @@ -73,25 +74,25 @@ BOOL CreateNewNamesFile(int mode) return(TRUE); } -BOOL LoadLevelInfo() +boolean 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 read 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 +113,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 +123,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 read level '%s' - creating new level", filename); else { fgets(cookie,LEVEL_COOKIE_LEN,file); @@ -142,8 +139,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,10 +213,10 @@ 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; + boolean levelrec_10 = FALSE; #ifndef MSDOS sprintf(filename,"%s/%s/%d.tape", @@ -238,8 +234,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 +269,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 +284,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 +292,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 read score for level %d", level_nr); } if (file) @@ -324,7 +312,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 +344,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 +368,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 read player information file '%s'", filename); } if (file) @@ -401,8 +383,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 +419,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;ilevel_nr; - fclose(file); + + if (file) + fclose(file); } 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 +477,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,9 +519,9 @@ 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; + boolean new_tape = TRUE; #ifndef MSDOS sprintf(filename,"%s/%s/%d.tape", @@ -556,14 +537,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 +570,7 @@ void SaveLevelTape(int level_nr) int j; for(j=0; j= 100) + return s; + + strcpy(s_lower, s); + + for (i=0; itoken) + free(setup_file_info->token); + if (setup_file_info->value) + free(setup_file_info->value); + if (setup_file_info->next) + freeSetupFileInfo(setup_file_info->next); + free(setup_file_info); +} + +static struct SetupFileInfo *newSetupFileInfo(char *token, char *value) +{ + struct SetupFileInfo *new = checked_malloc(sizeof(struct SetupFileInfo)); + + new->token = checked_malloc(strlen(token) + 1); + strcpy(new->token, token); + + new->value = checked_malloc(strlen(value) + 1); + strcpy(new->value, value); + + new->next = NULL; + + return new; +} + +static char *lookupSetupFileValue(struct SetupFileInfo *setup_file_info, + char *token) +{ + if (!setup_file_info) + return NULL; + + if (strcmp(setup_file_info->token, token) == 0) + return setup_file_info->value; + else + return lookupSetupFileValue(setup_file_info->next, token); +} + +#ifdef DEBUG +static void printSetupFileInfo(struct SetupFileInfo *setup_file_info) +{ + if (!setup_file_info) + return; + + printf("token: '%s'\n", setup_file_info->token); + printf("value: '%s'\n", setup_file_info->value); + + printSetupFileInfo(setup_file_info->next); +} +#endif + +static void decodeSetupFileInfo(struct SetupFileInfo *setup_file_info) +{ + int i; + int token_nr = -1; + int player_nr = 0; + char *token; + char *token_value; + int token_integer_value; + boolean token_boolean_value; + int token_player_prefix_len; + + if (!setup_file_info) + return; + + token = setup_file_info->token; + token_value = setup_file_info->value; + token_integer_value = get_string_integer_value(token_value); + token_boolean_value = get_string_boolean_value(token_value); + + token_player_prefix_len = strlen(SETUP_TOKEN_PLAYER_PREFIX); + + if (strncmp(token, SETUP_TOKEN_PLAYER_PREFIX, token_player_prefix_len) == 0) + { + token += token_player_prefix_len; + + if (*token >= '0' && *token <= '9') + { + player_nr = ((int)(*token - '0') - 1 + MAX_PLAYERS) % MAX_PLAYERS; + token++; + } + } + + for (i=0; i 1) + token_integer_value = 1; + setup.input[player_nr].joystick_nr = token_integer_value - 1; + break; + case SETUP_TOKEN_JOY_SNAP: + setup.input[player_nr].joy.snap = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_JOY_BOMB : + setup.input[player_nr].joy.bomb = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_KEY_LEFT: + setup.input[player_nr].key.left = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_RIGHT: + setup.input[player_nr].key.right = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_UP: + setup.input[player_nr].key.up = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_DOWN: + setup.input[player_nr].key.down = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_SNAP: + setup.input[player_nr].key.snap = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_BOMB: + setup.input[player_nr].key.bomb = getKeySymFromX11KeyName(token_value); + break; + default: + break; + } + + decodeSetupFileInfo(setup_file_info->next); +} + +void LoadSetup() +{ + int line_len; + char filename[MAX_FILENAME_LEN]; + char line[MAX_LINE_LEN]; + char *token, *value, *line_ptr; + struct SetupFileInfo *setup_file_info, **next_entry = &setup_file_info; + FILE *file; + + + + printf("LoadSetup\n"); + + + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "r"))) + { + int i; + + Error(ERR_RETURN, "cannot open setup file '%s'", filename); + + /* use default values for setup */ + + setup.sound_on = TRUE; + setup.sound_loops_on = FALSE; + setup.sound_music_on = FALSE; + setup.sound_simple_on = FALSE; + setup.toons_on = TRUE; + setup.direct_draw_on = FALSE; + setup.scroll_delay_on = FALSE; + setup.soft_scrolling_on = TRUE; + setup.fading_on = FALSE; + setup.autorecord_on = FALSE; + setup.quick_doors = FALSE; + + for (i=0; i line; line_ptr--) + if ((*line_ptr == ' ' || *line_ptr == '\t') && line_ptr[1] == '\0') + *line_ptr = '\0'; + + /* ignore empty lines */ + if (*line == '\0') + continue; + + line_len = strlen(line); + + + /* + printf("line: '%s'\n", line); + */ + + + /* cut leading whitespaces from token */ + for (token = line; *token; token++) + if (*token != ' ' && *token != '\t') + break; + + /* find end of token */ + for (line_ptr = token; *line_ptr; line_ptr++) + { + if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':') + { + *line_ptr = '\0'; + break; + } + } + + if (line_ptr < line + line_len) + value = line_ptr + 1; + else + value = "\0"; + + /* cut leading whitespaces from value */ + for (; *value; value++) + if (*value != ' ' && *value != '\t') + break; + + + /* + printf("token / value: '%s' / '%s'\n", token, value); + */ + + + if (*token && *value) + { + /* allocate new token/value pair */ + + *next_entry = newSetupFileInfo(token, value); + next_entry = &((*next_entry)->next); + } + } + + fclose(file); + +#if 0 + printf("Content of setup file info:\n"); + + printSetupFileInfo(setup_file_info); +#endif + + + + printf("decodeSetupFileInfo\n"); + + + + decodeSetupFileInfo(setup_file_info); + freeSetupFileInfo(setup_file_info); +} + +void SaveSetup() +{ + int i; + char filename[MAX_FILENAME_LEN]; + FILE *file; + + + + printf("SaveSetup\n"); + + + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "w"))) + { + Error(ERR_RETURN, "cannot write setup file '%s'", filename); + return; + } + + fprintf(file, "file_identifier: %s\n", + SETUP_COOKIE); + + fprintf(file, "\n"); + + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND, + setup.sound_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_LOOPS, + setup.sound_loops_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_MUSIC, + setup.sound_music_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_SIMPLE, + setup.sound_simple_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_TOONS, + setup.toons_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_DIRECT_DRAW, + setup.direct_draw_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SCROLL_DELAY, + setup.scroll_delay_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOFT_SCROLLING, + setup.soft_scrolling_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_FADING, + setup.fading_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_AUTORECORD, + setup.autorecord_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_QUICK_DOORS, + setup.quick_doors)); + + for (i=0; i