Súbor: [Platon] / games / mines / hiscore.c (stiahnutie)
Revízia 1.6, Tue Apr 6 09:54:17 2004 UTC (20 years ago) by nepto
Zmeny od 1.5: +13 -6
[lines]
Headers update:
- switch to standard/classic Platon SDG source header
- bumped copyright year to 2004
- changelog dates reformatted to new format YYYY-MM-DD
|
/*
* mines - Super Mines game for MS-Dos/Win32/SVGAlib/X11
*
* hiscore.c - high score table handler
* ____________________________________________________________
*
* Developed by Ondrej Jombik <nepto@platon.sk>
* Copyright (c) 2003-2004 Platon SDG, http://platon.sk/
* All rights reserved.
*
* See README file for more information about this software.
* See COPYING file for license information.
*
* Download the latest version from
* http://platon.sk/projects/games/
*
* Changelog:
* 2003-05-18 - created
* 2003-08-12 - X11 adaptations
*/
/* $Platon: games/mines/hiscore.c,v 1.5 2003/08/12 20:51:00 nepto Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <typedef.h>
#include <macros.h>
#include <system.h>
/* struct date, struct time */
#if PLATON_SYSTEM_SVGALIB || PLATON_SYSTEM_X11
# include <sys/types.h>
# include <sys/stat.h>
# include <dos2linux.h>
#endif
/* struct date, struct time */
#if PLATON_SYSTEM_WIN32
# include <dos2win32.h>
#endif
#if PLATON_SYSTEM_MSDOS
# include <dos.h>
#endif
#include "hiscore.h"
static char *hsc_filename = NULL;
static FILE *hsc_stream = NULL;
static long hsc_offset = 0;
static void CodeStruct(BYTE*p, int size) /* {{{ */
{
for (size--; size >= 0; size--) {
p[size] ^= 0xff;
#if 1
p[size] ^= 0xff;
#endif
}
} /* }}} */
static int check_hsc_stream(void) /* {{{ */
{
if (hsc_stream == NULL) {
if (hsc_filename == NULL)
return -1;
#if PLATON_SYSTEM_SVGALIB
{
register int i, len;
for (i = 0, len = strlen(hsc_filename); i < len; i++) {
if (hsc_filename[i] == '/') {
hsc_filename[i] = '\0';
mkdir(hsc_filename, 0644);
hsc_filename[i] = '/';
}
}
}
#endif
if ((hsc_stream = fopen(hsc_filename, "rb+")) == NULL
&& (hsc_stream = fopen(hsc_filename, "wb+")) == NULL)
return -1;
}
if (hsc_offset == HISCORE_AUTODETECT) {
#if 0
fseek(hsc_stream, 0L, SEEK_END);
hsc_offset = ftell(hsc_stream)
- (sizeof(HISCORE_ITEM) * HISCORE_MAX_ITEMS);
#else
fseek(hsc_stream, 0L - (long)(sizeof(HISCORE_ITEM)*HISCORE_MAX_ITEMS),
SEEK_END);
hsc_offset = ftell(hsc_stream);
#endif
#if 0
fprintf(stderr, "check_hsc_stream(): autodetection in effect;"
" new hsc_offset = %ld", hsc_offset);
#endif
}
if (fseek(hsc_stream, hsc_offset, SEEK_SET) != 0)
return -1;
return 0;
} /* }}} */
void hiscore_init(char *filename, long int offset) /* {{{ */
{
hiscore_close();
hsc_filename = strdup(filename);
hsc_offset = offset;
} /* }}} */
void hiscore_close(void) /* {{{ */
{
if (hsc_filename != NULL) {
free(hsc_filename);
hsc_filename = NULL;
}
if (hsc_stream != NULL) {
fclose(hsc_stream);
hsc_stream = NULL;
}
} /* }}} */
int hiscore_items(void) /* {{{ */
{
if (check_hsc_stream() < 0)
return -1;
fseek(hsc_stream, 0L, SEEK_END);
return (int) ((ftell(hsc_stream) - hsc_offset) / sizeof(HISCORE_ITEM));
} /* }}} */
int hiscore_get_item(HISCORE_ITEM*item,BYTE n) /* {{{ */
{
if (check_hsc_stream() < 0)
return -1;
if (fseek(hsc_stream, hsc_offset + n * sizeof(HISCORE_ITEM), SEEK_SET)
|| fread((void *) item, sizeof(HISCORE_ITEM), 1, hsc_stream) != 1)
return -1;
CodeStruct((BYTE *)item, sizeof(HISCORE_ITEM));
return 0;
} /* }}} */
int hiscore_set_item(HISCORE_ITEM*item,BYTE n) /* {{{ */
{
register int k;
HISCORE_ITEM pom;
if (check_hsc_stream() < 0)
return -1;
for (k=min(hiscore_items(), HISCORE_MAX_ITEMS-1)-1;k>=n;k--) {
if (fseek(hsc_stream, hsc_offset + k * sizeof(HISCORE_ITEM), SEEK_SET)
|| fread((void*)&pom, sizeof(HISCORE_ITEM), 1, hsc_stream) != 1)
return -1;
if (fseek(hsc_stream, hsc_offset + (k+1)*sizeof(HISCORE_ITEM), SEEK_SET)
|| fwrite((void*)&pom, sizeof(HISCORE_ITEM),1, hsc_stream) != 1)
return -1;
}
CodeStruct((BYTE*)item, sizeof(HISCORE_ITEM));
if (fseek(hsc_stream, hsc_offset + n * sizeof(HISCORE_ITEM), SEEK_SET)
|| fwrite((void *) item, sizeof(HISCORE_ITEM), 1, hsc_stream) != 1)
return -1;
fflush(hsc_stream);
return 0;
} /* }}} */
/* Modeline for ViM {{{
* vim: set ts=4:
* vim600: fdm=marker fdl=0 fdc=0:
* }}} */
Platon Group <platon@platon.sk> http://platon.sk/
|