Factor out four instances of duplicate code into append_ext_UNSAFE().

git-svn-id: https://svn.eduke32.com/eduke32@3735 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-05-06 19:43:38 +00:00
parent 34af7e6353
commit 82d6c2d5f7
5 changed files with 20 additions and 46 deletions

View file

@ -675,6 +675,7 @@ char *Bstrlwr(char *);
char *Bstrupr(char *); char *Bstrupr(char *);
#endif #endif
// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL.
static inline char *Bstrncpyz(char *dst, const char *src, bsize_t n) static inline char *Bstrncpyz(char *dst, const char *src, bsize_t n)
{ {
Bstrncpy(dst, src, n); Bstrncpy(dst, src, n);
@ -682,6 +683,19 @@ static inline char *Bstrncpyz(char *dst, const char *src, bsize_t n)
return dst; return dst;
} }
// Append extension when <outbuf> contains no dot.
// <ext> should be like ".mhk", i.e. it MUST start with a dot.
// The ugly name is deliberate: we should be checking the sizes of all buffers!
static inline void append_ext_UNSAFE(char *outbuf, const char *ext)
{
char *p = Bstrrchr(outbuf,'.');
if (!p)
Bstrcat(outbuf, ext);
else
Bstrcpy(p+1, ext+1);
}
#ifdef EXTERNC #ifdef EXTERNC
} }
#endif #endif

View file

@ -822,22 +822,13 @@ CANCEL:
static int32_t mhk=0; static int32_t mhk=0;
static void loadmhk(int32_t domessage) static void loadmhk(int32_t domessage)
{ {
char *p; char levname[BMAX_PATH]; char levname[BMAX_PATH];
if (!mhk) if (!mhk)
return; return;
Bstrcpy(levname, boardfilename); Bstrcpy(levname, boardfilename);
p = Bstrrchr(levname,'.'); append_ext_UNSAFE(levname, ".mhk");
if (!p)
Bstrcat(levname,".mhk");
else
{
p[1]='m';
p[2]='h';
p[3]='k';
p[4]=0;
}
if (!loadmaphack(levname)) if (!loadmaphack(levname))
{ {

View file

@ -9932,21 +9932,10 @@ int32_t loadboard(char *filename, char flags, vec3_t *dapos, int16_t *daang, int
#endif #endif
{ {
char *p, fn[BMAX_PATH]; char fn[BMAX_PATH];
Bstrcpy(fn, filename); Bstrcpy(fn, filename);
append_ext_UNSAFE(fn, ".cfg");
p = Bstrrchr(fn, '.');
if (!p)
Bstrcat(fn, ".cfg");
else
{
p[1]='c';
p[2]='f';
p[3]='g';
p[4]=0;
}
OSD_Exec(fn); OSD_Exec(fn);
} }

View file

@ -1759,16 +1759,7 @@ static void G_LoadMapHack(char *outbuf, const char *filename)
if (filename != NULL) if (filename != NULL)
Bstrcpy(outbuf, filename); Bstrcpy(outbuf, filename);
p = Bstrrchr(outbuf,'.'); append_ext_UNSAFE(outbuf, ".mhk");
if (!p)
Bstrcat(outbuf,".mhk");
else
{
p[1]='m';
p[2]='h';
p[3]='k';
p[4]=0;
}
if (!loadmaphack(outbuf)) if (!loadmaphack(outbuf))
initprintf("Loaded map hack file \"%s\"\n",outbuf); initprintf("Loaded map hack file \"%s\"\n",outbuf);

View file

@ -285,18 +285,7 @@ int32_t G_LoadPlayer(int32_t spot)
// TODO: this stuff needs to be factored out, too... // TODO: this stuff needs to be factored out, too...
if (currentboardfilename[0]) if (currentboardfilename[0])
{ {
char *p; append_ext_UNSAFE(currentboardfilename, ".mhk");
p = Bstrrchr(currentboardfilename,'.');
if (!p) Bstrcat(currentboardfilename,".mhk");
else
{
p[1]='m';
p[2]='h';
p[3]='k';
p[4]=0;
}
loadmaphack(currentboardfilename); loadmaphack(currentboardfilename);
} }