changeset 33130:4f60cd16ac04

Clean up and simplify fExist(). Replace fopen() to check file existence by access(), define variables differently and use FF_ARRAY_ELEMS macro in loop test expression.
author ib
date Tue, 05 Apr 2011 09:12:35 +0000
parents 5f3171d4d7a4
children aa753565a901
files gui/util/bitmap.c
diffstat 1 files changed, 9 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/gui/util/bitmap.c	Mon Apr 04 19:12:31 2011 +0000
+++ b/gui/util/bitmap.c	Tue Apr 05 09:12:35 2011 +0000
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "bitmap.h"
 
@@ -160,26 +161,18 @@
 
 static unsigned char *fExist(unsigned char *fname)
 {
-    static unsigned char tmp[512];
-    FILE *file;
-    unsigned char ext[][6] = { ".png\0", ".PNG\0" };
-    int i;
+    unsigned char *ext[] = { "png", "PNG" };
+    static unsigned char buf[512];
+    unsigned int i;
 
-    file = fopen(fname, "rb");
-
-    if (file) {
-        fclose(file);
+    if (access(fname, R_OK) == 0)
         return fname;
-    }
 
-    for (i = 0; i < 2; i++) {
-        snprintf(tmp, sizeof(tmp), "%s%s", fname, ext[i]);
-        file = fopen(tmp, "rb");
+    for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) {
+        snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]);
 
-        if (file) {
-            fclose(file);
-            return tmp;
-        }
+        if (access(buf, R_OK) == 0)
+            return buf;
     }
 
     return NULL;