Browse Source

support case-insensitive filename

Wei Mingzhi 9 years ago
parent
commit
0be0055dbe
4 changed files with 80 additions and 4 deletions
  1. 1 1
      midi.c
  2. 3 3
      sound.c
  3. 70 0
      util.c
  4. 6 0
      util.h

+ 1 - 1
midi.c

@@ -70,7 +70,7 @@ MIDI_Play(
       return;
    }
 
-   fp = fopen(PAL_PREFIX "midi.mkf", "rb");
+   fp = UTIL_OpenFile("midi.mkf");
    if (fp == NULL)
    {
       return;

+ 3 - 3
sound.c

@@ -313,10 +313,10 @@ SOUND_OpenAudio(
    //
    // Load the MKF file.
    //
-   gSndPlayer.mkf = fopen(va("%s%s", PAL_PREFIX, "voc.mkf"), "rb");
+   gSndPlayer.mkf = UTIL_OpenFile("voc.mkf");
    if (gSndPlayer.mkf == NULL)
    {
-      gSndPlayer.mkf = fopen(va("%s%s", PAL_PREFIX, "sounds.mkf"), "rb");
+      gSndPlayer.mkf = UTIL_OpenFile("sounds.mkf");
       if (gSndPlayer.mkf == NULL)
       {
          return -2;
@@ -409,7 +409,7 @@ SOUND_ReloadVOC(
 )
 {
    fclose(gSndPlayer.mkf);
-   gSndPlayer.mkf = fopen(va("%s%s", PAL_PREFIX, "voc.mkf"), "rb");
+   gSndPlayer.mkf = UTIL_OpenFile("voc.mkf");
    g_fUseWav = FALSE;
 }
 #endif

+ 70 - 0
util.c

@@ -403,6 +403,28 @@ UTIL_OpenRequiredFile(
 
    fp = fopen(va("%s%s", PAL_PREFIX, lpszFileName), "rb");
 
+#ifndef _WIN32
+   if (fp == NULL)
+   {
+	  //
+	  // try converting the filename to upper-case.
+	  //
+	  char *pBuf = strdup(lpszFileName);
+	  char *p = pBuf;
+	  while (*p)
+	  {
+		 if (*p >= 'a' && *p <= 'z')
+		 {
+			*p -= 'a' - 'A';
+		 }
+		 p++;
+	  }
+
+	  fp = fopen(va("%s%s", PAL_PREFIX, pBuf), "rb");
+	  free(pBuf);
+   }
+#endif
+
    if (fp == NULL)
    {
       TerminateOnError("File not found: %s!\n", lpszFileName);
@@ -411,6 +433,54 @@ UTIL_OpenRequiredFile(
    return fp;
 }
 
+FILE *
+UTIL_OpenFile(
+   LPCSTR            lpszFileName
+)
+/*++
+  Purpose:
+
+    Open a file. If fails, return NULL.
+
+  Parameters:
+
+    [IN]  lpszFileName - file name to open.
+
+  Return value:
+
+    Pointer to the file.
+
+--*/
+{
+   FILE         *fp;
+
+   fp = fopen(va("%s%s", PAL_PREFIX, lpszFileName), "rb");
+
+#ifndef _WIN32
+   if (fp == NULL)
+   {
+	  //
+	  // try converting the filename to upper-case.
+	  //
+	  char *pBuf = strdup(lpszFileName);
+	  char *p = pBuf;
+	  while (*p)
+	  {
+		 if (*p >= 'a' && *p <= 'z')
+		 {
+			*p -= 'a' - 'A';
+		 }
+		 p++;
+	  }
+
+	  fp = fopen(va("%s%s", PAL_PREFIX, pBuf), "rb");
+	  free(pBuf);
+   }
+#endif
+
+   return fp;
+}
+
 VOID
 UTIL_CloseFile(
    FILE             *fp

+ 6 - 0
util.h

@@ -1,4 +1,5 @@
 /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
+
 //
 // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
 // All rights reserved.
@@ -80,6 +81,11 @@ UTIL_OpenRequiredFile(
    LPCSTR               lpszFileName
 );
 
+FILE *
+UTIL_OpenFile(
+   LPCSTR               lpszFileName
+);
+
 VOID
 UTIL_CloseFile(
    FILE                *fp