WIN32: file naming
Allow single character variations in player names to remain unique in file names by encoding rather than substituting. "plnam one", "plnam_one", and "plnam~one" at the "Who are you?" prompt get unique filenames after this patch.
This commit is contained in:
@@ -595,6 +595,8 @@ E void NDECL(makerogueghost);
|
|||||||
|
|
||||||
/* ### files.c ### */
|
/* ### files.c ### */
|
||||||
|
|
||||||
|
E char *FDECL(fname_encode, (const char *, char, char *, char *, int));
|
||||||
|
E char *FDECL(fname_decode, (char, char *, char *, int));
|
||||||
E const char *FDECL(fqname, (const char *, int, int));
|
E const char *FDECL(fqname, (const char *, int, int));
|
||||||
E FILE *FDECL(fopen_datafile, (const char *,const char *,int));
|
E FILE *FDECL(fopen_datafile, (const char *,const char *,int));
|
||||||
E boolean FDECL(uptodate, (int,const char *));
|
E boolean FDECL(uptodate, (int,const char *));
|
||||||
|
|||||||
+118
-2
@@ -68,7 +68,7 @@ char lock[PL_NSIZ+25]; /* long enough for username+-+name+.99 */
|
|||||||
#define SAVESIZE (PL_NSIZ + 22) /* [.save]<uid>player.e;1 */
|
#define SAVESIZE (PL_NSIZ + 22) /* [.save]<uid>player.e;1 */
|
||||||
# else
|
# else
|
||||||
# if defined(WIN32)
|
# if defined(WIN32)
|
||||||
#define SAVESIZE (PL_NSIZ + 40) /* username-player.NetHack-saved-game */
|
#define SAVESIZE (PL_NSIZ + 60) /* username-player.NetHack-saved-game */
|
||||||
# else
|
# else
|
||||||
#define SAVESIZE FILENAME /* from macconf.h or pcconf.h */
|
#define SAVESIZE FILENAME /* from macconf.h or pcconf.h */
|
||||||
# endif
|
# endif
|
||||||
@@ -127,6 +127,114 @@ int FDECL(parse_config_line, (FILE *,char *,char *,char *));
|
|||||||
STATIC_DCL void FDECL(adjust_prefix, (char *, int));
|
STATIC_DCL void FDECL(adjust_prefix, (char *, int));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* fname_encode()
|
||||||
|
*
|
||||||
|
* Args:
|
||||||
|
* legal zero-terminated list of acceptable file name characters
|
||||||
|
* quotechar lead-in character used to quote illegal characters as hex digits
|
||||||
|
* s string to encode
|
||||||
|
* callerbuf buffer to house result
|
||||||
|
* bufsz size of callerbuf
|
||||||
|
*
|
||||||
|
* Notes:
|
||||||
|
* The hex digits 0-9 and A-F are always part of the legal set due to
|
||||||
|
* their use in the encoding scheme, even if not explicitly included in 'legal'.
|
||||||
|
*
|
||||||
|
* Sample:
|
||||||
|
* The following call:
|
||||||
|
* (void)fname_encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
||||||
|
* '%', "This is a % test!", buf, 512);
|
||||||
|
* results in this encoding:
|
||||||
|
* "This%20is%20a%20%25%20test%21"
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
fname_encode(legal, quotechar, s, callerbuf, bufsz)
|
||||||
|
const char *legal;
|
||||||
|
char quotechar;
|
||||||
|
char *s, *callerbuf;
|
||||||
|
int bufsz;
|
||||||
|
{
|
||||||
|
char *sp, *op;
|
||||||
|
int cnt = 0;
|
||||||
|
char hexdigits[] = "0123456789ABCDEF";
|
||||||
|
|
||||||
|
sp = s;
|
||||||
|
op = callerbuf;
|
||||||
|
*op = '\0';
|
||||||
|
|
||||||
|
while (*sp) {
|
||||||
|
/* Do we have room for one more character or encoding? */
|
||||||
|
if ((bufsz - cnt) <= 4) return callerbuf;
|
||||||
|
|
||||||
|
if (*sp == quotechar) {
|
||||||
|
(void)sprintf(op, "%c%02X", quotechar, *sp);
|
||||||
|
op += 3;
|
||||||
|
cnt += 3;
|
||||||
|
} else if ((index(legal, *sp) != 0) || (index(hexdigits, *sp) != 0)) {
|
||||||
|
*op++ = *sp;
|
||||||
|
*op = '\0';
|
||||||
|
cnt++;
|
||||||
|
} else {
|
||||||
|
(void)sprintf(op,"%c%02X", quotechar, *sp);
|
||||||
|
op += 3;
|
||||||
|
cnt += 3;
|
||||||
|
}
|
||||||
|
sp++;
|
||||||
|
}
|
||||||
|
return callerbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* fname_decode()
|
||||||
|
*
|
||||||
|
* Args:
|
||||||
|
* quotechar lead-in character used to quote illegal characters as hex digits
|
||||||
|
* s string to decode
|
||||||
|
* callerbuf buffer to house result
|
||||||
|
* bufsz size of callerbuf
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
fname_decode(quotechar, s, callerbuf, bufsz)
|
||||||
|
char quotechar;
|
||||||
|
char *s, *callerbuf;
|
||||||
|
int bufsz;
|
||||||
|
{
|
||||||
|
char *sp, *op;
|
||||||
|
int k,calc,cnt = 0;
|
||||||
|
char hexdigits[] = "0123456789ABCDEF";
|
||||||
|
int hexvalues[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
|
||||||
|
|
||||||
|
sp = s;
|
||||||
|
op = callerbuf;
|
||||||
|
*op = '\0';
|
||||||
|
calc = 0;
|
||||||
|
|
||||||
|
while (*sp) {
|
||||||
|
/* Do we have room for one more character? */
|
||||||
|
if ((bufsz - cnt) <= 2) return callerbuf;
|
||||||
|
if (*sp == quotechar) {
|
||||||
|
sp++;
|
||||||
|
for (k=0; k < 15; ++k) if (*sp == hexdigits[k]) break;
|
||||||
|
if (k >= 15) return callerbuf; /* impossible, so bail */
|
||||||
|
calc = hexvalues[k] << 4;
|
||||||
|
sp++;
|
||||||
|
for (k=0; k < 15; ++k) if (*sp == hexdigits[k]) break;
|
||||||
|
if (k >= 15) return callerbuf; /* impossible, so bail */
|
||||||
|
calc += hexvalues[k];
|
||||||
|
sp++;
|
||||||
|
*op++ = calc;
|
||||||
|
*op = '\0';
|
||||||
|
} else {
|
||||||
|
*op++ = *sp++;
|
||||||
|
*op = '\0';
|
||||||
|
}
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
return callerbuf;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef PREFIXES_IN_USE
|
#ifndef PREFIXES_IN_USE
|
||||||
/*ARGSUSED*/
|
/*ARGSUSED*/
|
||||||
#endif
|
#endif
|
||||||
@@ -540,6 +648,9 @@ compress_bonesfile()
|
|||||||
void
|
void
|
||||||
set_savefile_name()
|
set_savefile_name()
|
||||||
{
|
{
|
||||||
|
#if defined(WIN32)
|
||||||
|
char fnamebuf[BUFSZ], encodedfnamebuf[BUFSZ];
|
||||||
|
#endif
|
||||||
#ifdef VMS
|
#ifdef VMS
|
||||||
Sprintf(SAVEF, "[.save]%d%s", getuid(), plname);
|
Sprintf(SAVEF, "[.save]%d%s", getuid(), plname);
|
||||||
regularize(SAVEF+7);
|
regularize(SAVEF+7);
|
||||||
@@ -563,7 +674,12 @@ set_savefile_name()
|
|||||||
Strcat(SAVEF, ".sav");
|
Strcat(SAVEF, ".sav");
|
||||||
# else
|
# else
|
||||||
# if defined(WIN32)
|
# if defined(WIN32)
|
||||||
Sprintf(SAVEF,"%s-%s.NetHack-saved-game",get_username(0), plname);
|
/* Obtain the name of the logged on user and incorporate
|
||||||
|
* it into the name. */
|
||||||
|
Sprintf(fnamebuf, "%s-%s", get_username(0), plname);
|
||||||
|
(void)fname_encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.",
|
||||||
|
'%', fnamebuf, encodedfnamebuf, BUFSZ);
|
||||||
|
Sprintf(SAVEF, "%s.NetHack-saved-game", encodedfnamebuf);
|
||||||
# else
|
# else
|
||||||
Sprintf(SAVEF, "save/%d%s", (int)getuid(), plname);
|
Sprintf(SAVEF, "save/%d%s", (int)getuid(), plname);
|
||||||
regularize(SAVEF+5); /* avoid . or / in name */
|
regularize(SAVEF+5); /* avoid . or / in name */
|
||||||
|
|||||||
+8
-2
@@ -105,6 +105,9 @@ char *argv[];
|
|||||||
|
|
||||||
register int fd;
|
register int fd;
|
||||||
register char *dir;
|
register char *dir;
|
||||||
|
#if defined(WIN32)
|
||||||
|
char fnamebuf[BUFSZ], encodedfnamebuf[BUFSZ];
|
||||||
|
#endif
|
||||||
#ifdef NOCWD_ASSUMPTIONS
|
#ifdef NOCWD_ASSUMPTIONS
|
||||||
char failbuf[BUFSZ];
|
char failbuf[BUFSZ];
|
||||||
#endif
|
#endif
|
||||||
@@ -306,8 +309,11 @@ char *argv[];
|
|||||||
# if defined(WIN32)
|
# if defined(WIN32)
|
||||||
/* Obtain the name of the logged on user and incorporate
|
/* Obtain the name of the logged on user and incorporate
|
||||||
* it into the name. */
|
* it into the name. */
|
||||||
Sprintf(lock, "%s-%s",get_username(0),plname);
|
Sprintf(fnamebuf, "%s-%s", get_username(0), plname);
|
||||||
regularize(lock);
|
(void)fname_encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.",
|
||||||
|
'%', fnamebuf, encodedfnamebuf, BUFSZ);
|
||||||
|
Sprintf(lock, "%s",encodedfnamebuf);
|
||||||
|
/* regularize(lock); */ /* we encode now, rather than substitute */
|
||||||
# else
|
# else
|
||||||
Strcpy(lock,plname);
|
Strcpy(lock,plname);
|
||||||
regularize(lock);
|
regularize(lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user