Changeset 127

Show
Ignore:
Timestamp:
05/11/07 20:18:29 (2 years ago)
Author:
takkaria
Message:
  • Add directory-scanning code. (z-file.c)
  • Make the SDL port use that code rather than fontlist.txt. (main-sdl.c, closes #95)
  • Nuke fake usleep() function; POSIX specifies it, so we should be safe without it.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/configure.ac

    r84 r127  
    172172dnl AC_HEADER_STDBOOL -- not yet 
    173173AC_HEADER_TIME 
    174 AC_CHECK_HEADERS([unistd.h fcntl.h limits.h stdint.h sys/time.h termios.h]) 
     174AC_CHECK_HEADERS([unistd.h fcntl.h dirent.h limits.h stdint.h sys/time.h termios.h]) 
    175175 
    176176dnl Check for types. 
     
    187187AC_FUNC_STAT 
    188188AC_FUNC_SELECT_ARGTYPES 
    189 AC_CHECK_FUNCS([memmove memset strrchr mkdir strtol usleep mkstemp setegid can_change_color]) 
     189AC_CHECK_FUNCS([memmove memset strrchr mkdir strtol mkstemp setegid can_change_color]) 
    190190 
    191191dnl XXX: Work around some autoconf bugs. 
  • trunk/src/Makefile.src

    • Property svn:mime-type set to text/plain
    • Property svn:eol-style set to native
  • trunk/src/Makefile.std

    • Property svn:mime-type set to text/plain
    • Property svn:eol-style set to LF
  • trunk/src/autoconf.h.in

    r89 r127  
    66/* Define to 1 if you have the `can_change_color' function. */ 
    77#undef HAVE_CAN_CHANGE_COLOR 
     8 
     9/* Define to 1 if you have the <dirent.h> header file. */ 
     10#undef HAVE_DIRENT_H 
    811 
    912/* Define to 1 if you have the <fcntl.h> header file. */ 
  • trunk/src/externs.h

    r120 r127  
    791791 
    792792#ifdef SET_UID 
    793 # ifndef HAVE_USLEEP 
    794 /* util.c */ 
    795 extern int usleep(unsigned long usecs); 
    796 # endif /* HAVE_USLEEP */ 
    797793extern void user_name(char *buf, size_t len, int id); 
    798794#endif /* SET_UID */ 
  • trunk/src/h-config.h

    r89 r127  
    6767 */ 
    6868#if !defined(MACH_O_CARBON) && !defined(WINDOWS) && \ 
    69     !defined(MSDOS) && !defined(USE_EMX) && \ 
    70     !defined(AMIGA) && !defined(RISCOS) && !defined(GAMEBOY) 
     69    !defined(MSDOS) && !defined(RISCOS) && !defined(GAMEBOY) 
    7170# define SET_UID 
    7271#endif 
     
    105104# define FILE_TYPE(X) (_ftype = (X)) 
    106105 
    107  
    108 /* Mac OS X has usleep(). */ 
    109 #ifndef HAVE_USLEEP 
    110 # define HAVE_USLEEP 
    111 #endif 
    112  
    113106#else 
    114107 
     
    117110#endif 
    118111 
     112 
    119113/* 
    120  * OPTION: Define "HAVE_USLEEP" only if "usleep()" exists. 
    121  * Note that this is only relevant for "SET_UID" machines. 
    122  * 
    123  * (Set in autoconf.h when HAVE_CONFIG_H -- i.e. when configure is used.) 
     114 * Assume UNIX-style directory handling. 
    124115 */ 
    125 #if defined(SET_UID) && !defined(HAVE_CONFIG_H
    126 # define HAVE_USLEEP 
     116#if !defined(HAVE_CONFIG_H) && !defined(WINDOWS) && !defined(RISCOS
     117#define HAVE_DIRENT_H 
    127118#endif 
    128  
    129119 
    130120 
     
    132122 
    133123 
     124 
  • trunk/src/main-sdl.c

    r122 r127  
    34463446        char buf[1024]; 
    34473447        FILE *fff; 
     3448        ang_dir *dir; 
    34483449         
    34493450        /* Build the gfx path */ 
     
    34533454        /* Build the "font" path */ 
    34543455        path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "font"); 
    3455          
    3456         /* Allocate the path */ 
    34573456        ANGBAND_DIR_XTRA_FONT = string_make(path); 
    34583457         
     
    34643463         
    34653464        for (i = 0; i < MAX_FONTS; i++) 
    3466         { 
    34673465                FontList[i] = NULL; 
    3468         } 
    3469          
    3470         /* Load the fonts */ 
    3471         path_build(path, sizeof(path), ANGBAND_DIR_XTRA_FONT, "fontlist.txt"); 
    3472         validate_file(path); 
    3473          
    3474         /* Open the file */ 
    3475         fff = my_fopen(path, "r"); 
    3476          
    3477         /* Process the file */ 
    3478         while (0 == my_fgets(fff, buf, sizeof(buf))) 
    3479         { 
    3480                 if (!buf[0]) continue; 
    3481                  
    3482                 if (buf[0] == '#') continue; 
    3483                  
    3484                 FontList[num_fonts++] = string_make(buf); 
    3485                  
     3466 
     3467 
     3468        /** Scan for fonts **/ 
     3469 
     3470        /* Open the directory */ 
     3471        dir = my_dopen(ANGBAND_DIR_XTRA_FONT); 
     3472        if (!dir) return; 
     3473 
     3474        /* Read every font to the limit */ 
     3475        while (my_dread(dir, buf, sizeof buf)) 
     3476        { 
     3477                /* Check for at least an extension */ 
     3478                signed extension_pos = strlen(buf) - 4; 
     3479                if (extension_pos <= 0) continue; 
     3480 
     3481                /* Check for file extension */ 
     3482                if (strcmp((buf + extension_pos), ".fon") == 0) 
     3483                        FontList[num_fonts++] = string_make(buf); 
     3484 
     3485                /* Don't grow to long */ 
    34863486                if (num_fonts == MAX_FONTS) break; 
    34873487        } 
    3488          
    3489         my_fclose(fff); 
     3488 
     3489        /* Done */ 
     3490        my_dclose(dir); 
    34903491} 
    34913492 
  • trunk/src/util.c

    r120 r127  
    1111#include "angband.h" 
    1212#include "randname.h" 
    13  
    14  
    15 #ifdef SET_UID 
    16  
    17 # ifndef HAVE_USLEEP 
    18  
    19 /* 
    20  * struct timeval (used below) requires sys/time.h. 
    21  * 
    22  * Unix systems that neither have usleep nor sys/time.h are screwed, since 
    23  * they have no way of delaying. 
    24  */ 
    25 #include <sys/time.h> 
    26  
    27 /* 
    28  * For those systems that don't have "usleep()" but need it. 
    29  * 
    30  * Fake "usleep()" function grabbed from the inl netrek server -cba 
    31  */ 
    32 int usleep(unsigned long usecs) 
    33 { 
    34         struct timeval      Timer; 
    35  
    36         /* Paranoia -- No excessive sleeping */ 
    37         if (usecs > 4000000L) quit("Illegal usleep() call"); 
    38  
    39         /* Wait for it */ 
    40         Timer.tv_sec = (usecs / 1000000L); 
    41         Timer.tv_usec = (usecs % 1000000L); 
    42  
    43         /* Wait for it */ 
    44         if (select(0, NULL, NULL, NULL, &Timer) < 0) 
    45         { 
    46                 /* Hack -- ignore interrupts */ 
    47                 if (errno != EINTR) return -1; 
    48         } 
    49  
    50         /* Success */ 
    51         return 0; 
    52 } 
    53  
    54 # endif /* HAVE_USLEEP */ 
    55 #endif /* SET_UID */ 
    56  
    5713 
    5814 
  • trunk/src/wizard1.c

    r16 r127  
    1010 
    1111#include "angband.h" 
     12#include "z-file.h" 
    1213 
    1314 
  • trunk/src/z-file.c

    r48 r127  
    760760#endif /* RISCOS */ 
    761761 
     762/*** Directory scanning code ***/ 
     763 
     764/* 
     765 * This code was originally written for the SDL port so it could scan for fonts 
     766 * without needing a fontlist text file. 
     767 */ 
     768 
     769 
     770/* 
     771 * Opens a directory handle. 
     772 *  
     773 * `dirname` must be a system-specific pathname to the directory 
     774 * you want scanned. 
     775 * 
     776 * Returns a valid directory handle on success, NULL otherwise. 
     777 */ 
     778ang_dir *my_dopen(const char *dirname); 
     779 
     780 
     781/* 
     782 * Reads a directory entry. 
     783 * 
     784 * `dir` must point to a directory handle previously returned by my_dopen(). 
     785 * `fname` must be a pointer to a writeable chunk of memory `len` long. 
     786 * 
     787 * Returns TRUE on successful reading, FALSE otherwise. 
     788 * (FALSE generally indicates that there are no more files to be read.) 
     789 */ 
     790bool my_dread(ang_dir *dir, char *fname, size_t len); 
     791 
     792 
     793/* 
     794 * Close a directory handle. 
     795 */ 
     796void my_dclose(ang_dir *dir); 
     797 
     798 
     799 
     800#ifdef WINDOWS 
     801 
     802/* Paranoia */ 
     803# ifdef HAVE_DIRENT_H 
     804#  undef HAVE_DIRENT_H 
     805# endif 
     806 
     807/* Include Windows header */ 
     808#include <windows.h> 
     809 
     810/* System-specific struct */ 
     811struct ang_dir 
     812{ 
     813        HANDLE h; 
     814        const char *first_file; 
     815}; 
     816 
     817/* Specified above */ 
     818ang_dir *my_dopen(const char *dirname) 
     819{ 
     820        WIN32_FIND_DATA fd; 
     821        ang_dir *dir; 
     822 
     823        dir = ralloc(sizeof dir); 
     824        if (!dir) return NULL; 
     825 
     826        /* Try to open it */ 
     827        dir->h = FindFirstFile(format("%s\\*", dirname), &fd); 
     828 
     829        /* Abort */ 
     830        if (dir->h == INVALID_HANDLE_VALUE) 
     831        { 
     832                FREE(dir); 
     833                return NULL; 
     834        } 
     835 
     836        /* Remember this one */ 
     837        dir->first_file = string_make(fd.cFileName); 
     838 
     839        /* Success */ 
     840        return dir; 
     841} 
     842 
     843/* Specified above */ 
     844bool my_dread(ang_dir *dir, char *fname, size_t len) 
     845{ 
     846        WIN32_FIND_DATA fd; 
     847        BOOL ok; 
     848        bool next = TRUE; 
     849 
     850        /* Try the first file */ 
     851        if (dir->first_file) 
     852        { 
     853                /* Copy the string across, then free it */ 
     854                my_strcpy(fname, dir->first_file, len); 
     855                string_free(dir->first_file); 
     856                dir->first_file = NULL; 
     857 
     858                /* Wild success */ 
     859                return TRUE; 
     860        } 
     861 
     862        /* Try the next file */ 
     863        while (next) 
     864        { 
     865                ok = FindNextFile(dir->h, &fd); 
     866                if (!ok) return FALSE; 
     867 
     868                /* Skip directories */ 
     869                if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || 
     870                    strcmp(fd.cFileName, ".") == 0 || 
     871                    strcmp(fd.cFileName, "..") == 0) 
     872                        continue; 
     873 
     874                /* Take this one */ 
     875                my_strcpy(fname, fd.cFileName, len); 
     876                next = FALSE; 
     877        } 
     878 
     879        return TRUE; 
     880} 
     881 
     882void my_dclose(ang_dir *dir) 
     883{ 
     884        /* Close directory */ 
     885        if (dir->h) 
     886                FindClose(dir->h); 
     887 
     888        /* Free memory */ 
     889        FREE(dir); 
     890} 
     891 
     892#endif /* WINDOWS */ 
     893 
     894 
     895#ifdef HAVE_DIRENT_H 
     896 
     897/* Include relevant types */ 
     898#include <sys/types.h> 
     899#include <dirent.h> 
     900 
     901/* Define our ang_dir type */ 
     902struct ang_dir 
     903{ 
     904        DIR *d; 
     905}; 
     906 
     907/* Specified above */ 
     908ang_dir *my_dopen(const char *dirname) 
     909{ 
     910        ang_dir *dir; 
     911 
     912        dir = ralloc(sizeof dir); 
     913        if (!dir) return NULL; 
     914 
     915        /* Try to open the directory */ 
     916        dir->d = opendir(dirname); 
     917        if (!dir->d) 
     918        { 
     919                FREE(dir); 
     920                return NULL; 
     921        } 
     922 
     923        /* Success */ 
     924        return dir; 
     925} 
     926 
     927/* Specified above */ 
     928bool my_dread(ang_dir *dir, char *fname, size_t len) 
     929{ 
     930        struct dirent *entry; 
     931        struct stat filedata; 
     932 
     933        assert(dir != NULL); 
     934 
     935        /* Try reading another entry */ 
     936        entry = readdir(dir->d); 
     937        if (!entry) return FALSE; 
     938 
     939        /* Copy the filename */ 
     940        /* XXX ignore overflow */ 
     941        my_strcpy(fname, entry->d_name, len); 
     942 
     943        return TRUE; 
     944} 
     945 
     946void my_dclose(ang_dir *dir) 
     947{ 
     948        /* Close directory */ 
     949        if (dir->d) 
     950                closedir(dir->d); 
     951 
     952        /* Free memory */ 
     953        FREE(dir); 
     954} 
     955 
     956#endif /* HAVE_DIRENT_H */ 
     957 
  • trunk/src/z-file.h

    r113 r127  
    7373 
    7474 
     75typedef struct ang_dir ang_dir; 
     76 
     77ang_dir *my_dopen(const char *dirname); 
     78bool my_dread(ang_dir *dir, char *fname, size_t len); 
     79void my_dclose(ang_dir *dir); 
     80 
     81 
    7582#endif