[BACK]Return to save.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / urogue

Annotation of early-roguelike/urogue/save.c, Revision 1.1

1.1     ! rubenllo    1: /*
        !             2:     save.c - save and restore routines
        !             3:
        !             4:     UltraRogue: The Ultimate Adventure in the Dungeons of Doom
        !             5:     Copyright (C) 1985, 1986, 1992, 1993, 1995 Herb Chong
        !             6:     All rights reserved.
        !             7:
        !             8:     Based on "Advanced Rogue"
        !             9:     Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka
        !            10:     All rights reserved.
        !            11:
        !            12:     Based on "Rogue: Exploring the Dungeons of Doom"
        !            13:     Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
        !            14:     All rights reserved.
        !            15:
        !            16:     See the file LICENSE.TXT for full copyright and licensing information.
        !            17: */
        !            18:
        !            19: #define _ALL_SOURCE /* need to remove need for this AIXism */
        !            20:
        !            21: #include <time.h>
        !            22: #include <stdlib.h>
        !            23: #include <string.h>
        !            24: #include <ctype.h>
        !            25: #include <errno.h>
        !            26: #include "rogue.h"
        !            27:
        !            28: int save_savedir_game(void);
        !            29:
        !            30: int
        !            31: save_game(void)
        !            32: {
        !            33:     FILE *savefd;
        !            34:     char    buf[2 * LINELEN];
        !            35:     char    oldfile[2*LINELEN];
        !            36:
        !            37:     if (use_savedir)
        !            38:        return save_savedir_game();
        !            39:
        !            40:     /* get file name */
        !            41:
        !            42:     strcpy(oldfile,file_name);
        !            43:
        !            44:     do
        !            45:     {
        !            46:         mpos = 0;
        !            47:
        !            48:         if (oldfile[0] != '\0')
        !            49:             msg("Save file [%s]: ", file_name);
        !            50:         else
        !            51:             msg("Save file as: ");
        !            52:
        !            53:         mpos = 0;
        !            54:         buf[0] = '\0';
        !            55:
        !            56:         if (get_string(buf, cw) == QUIT)
        !            57:         {
        !            58:             msg("");
        !            59:             return(FALSE);
        !            60:         }
        !            61:
        !            62:         if ( (buf[0] == 0) && (oldfile[0] != 0) )
        !            63:             strcpy(file_name, oldfile);
        !            64:         else if (buf[0] != 0)
        !            65:             strcpy(file_name, buf);
        !            66:         else
        !            67:         {
        !            68:             msg("");
        !            69:             return(FALSE);
        !            70:         }
        !            71:
        !            72:         wclear(hw);
        !            73:         wmove(hw, LINES - 1, 0);
        !            74:         wrefresh(hw);
        !            75:
        !            76:         if ((savefd = fopen(file_name, "w")) == NULL)
        !            77:             msg(strerror(errno));    /* fake perror() */
        !            78:     }
        !            79:     while (savefd == NULL);
        !            80:
        !            81:     /* write out [compressed?] file */
        !            82:
        !            83:     save_file(savefd);
        !            84:     return(TRUE);
        !            85: }
        !            86:
        !            87: /*
        !            88:  * save_savedir_game()
        !            89:  * Simplified save function for when system savefiles are used.
        !            90:  */
        !            91: int
        !            92: save_savedir_game(void)
        !            93: {
        !            94:     FILE *savef;
        !            95:     char c;
        !            96:
        !            97:     mpos = 0;
        !            98:     msg("Save game? ");
        !            99:     c = readcharw(cw);
        !           100:     if (c == 'y' || c == 'Y')
        !           101:     {
        !           102:         if ((savef = fopen(file_name, "w")) == NULL)
        !           103:        {
        !           104:             msg(strerror(errno));
        !           105:             return(FALSE);
        !           106:        }
        !           107:         msg("");
        !           108:         save_file(savef);
        !           109:         return(TRUE);
        !           110:     }
        !           111:     else
        !           112:     {
        !           113:         msg("");
        !           114:         return(FALSE);
        !           115:     }
        !           116: }
        !           117:
        !           118: void
        !           119: auto_save(int sig)
        !           120: {
        !           121:     FILE *savef;
        !           122:
        !           123:     md_ignore_signals();
        !           124:     if (file_name[0] && (savef = fopen(file_name, "w"))) {
        !           125:         save_file(savef);
        !           126:     }
        !           127:     exit(0);
        !           128: }
        !           129:
        !           130: int
        !           131: restore(char *file)
        !           132: {
        !           133:     FILE *infd;
        !           134:     char    *sp;
        !           135:
        !           136:     if (strcmp(file, "-r") == 0)
        !           137:         file = file_name;
        !           138:
        !           139:     if ((infd = fopen(file, "r")) == NULL)
        !           140:     {
        !           141:         if (use_savedir && errno == ENOENT)
        !           142:             return TRUE;
        !           143:         perror(file);
        !           144:         return(FALSE);
        !           145:     }
        !           146:
        !           147:     if ( restore_file(infd) == FALSE )
        !           148:         return(FALSE);
        !           149:
        !           150:     /*
        !           151:      * we do not close the file so that we will have a hold of the inode
        !           152:      * for as long as possible
        !           153:      */
        !           154:
        !           155:     if (remove(file) < 0)
        !           156:     {
        !           157:         printf("Cannot unlink file\n");
        !           158:         return(FALSE);
        !           159:     }
        !           160:
        !           161:     if ((sp = getenv("OPTIONS")) != NULL)
        !           162:         parse_opts(sp);
        !           163:
        !           164:     strcpy(file_name, file);
        !           165:
        !           166:     clearok(cw, TRUE);
        !           167:     touchwin(cw);
        !           168:     noecho();
        !           169:     nonl();
        !           170:
        !           171:     while(playing)
        !           172:     {
        !           173:         do_daemons(BEFORE);
        !           174:         do_fuses(BEFORE);
        !           175:
        !           176:         command();  /* Command execution */
        !           177:
        !           178:         if (after)
        !           179:             do_after_effects();
        !           180:     }
        !           181:
        !           182:     fatal("");
        !           183:
        !           184:     return(FALSE);
        !           185: }

CVSweb