1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus-core - osal_files_win32.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2009 Richard Goedeken *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 /* This file contains the definitions for the unix-specific file handling
27 #include <sys/types.h>
34 #include "osal_files.h"
36 /* global functions */
38 int osal_is_directory(const char* name)
40 char DirName[MAX_PATH + 1];
43 /* we must remove any trailing backslash on the end of the pathname, or this will fail */
44 strncpy(DirName, name, MAX_PATH);
45 DirName[MAX_PATH] = 0;
46 namelen = strlen(DirName);
47 if (namelen > 0 && DirName[namelen-1] == '\\')
48 DirName[namelen-1] = 0;
49 return (GetFileAttributes(DirName) & FILE_ATTRIBUTE_DIRECTORY);
52 int osal_mkdirp(const char *dirpath, int mode)
54 struct _stat fileinfo;
55 size_t dirpathlen = strlen(dirpath);
56 char *currpath = _strdup(dirpath);
58 /* first, remove sub-dirs on the end (by replacing slashes with NULL chars) until we find an existing directory */
59 while (strlen(currpath) > 1 && _stat(currpath, &fileinfo) != 0)
61 char *lastslash = strrchr(currpath, '\\');
62 if (lastslash == NULL)
65 return 1; /* error: we never found an existing directory, this path is bad */
70 /* then walk up the path chain, creating directories along the way */
73 if (currpath[strlen(currpath)-1] != '\\' && _stat(currpath, &fileinfo) != 0)
75 if (_mkdir(currpath) != 0)
78 return 2; /* mkdir failed */
81 if (strlen(currpath) == dirpathlen)
84 currpath[strlen(currpath)] = '\\';
93 WIN32_FIND_DATA find_data;
96 void * osal_search_dir_open(const char *pathname)
98 char SearchString[MAX_PATH + 1];
99 dir_search_info *pInfo = malloc(sizeof(dir_search_info));
104 pInfo->hFind = INVALID_HANDLE_VALUE;
105 pInfo->find_data.cFileName[0] = 0;
107 if (pathname[strlen(pathname)-1] == '\\')
108 _snprintf(SearchString, MAX_PATH, "%s*", pathname);
110 _snprintf(SearchString, MAX_PATH, "%s\\*", pathname);
111 SearchString[MAX_PATH] = 0;
113 pInfo->hFind = FindFirstFile(SearchString, &pInfo->find_data);
114 return (void *) pInfo;
117 const char *osal_search_dir_read_next(void * search_info)
119 static char last_filename[_MAX_PATH];
120 dir_search_info *pInfo = (dir_search_info *) search_info;
122 if (pInfo == NULL || pInfo->hFind == INVALID_HANDLE_VALUE || pInfo->find_data.cFileName[0] == 0)
125 strcpy(last_filename, pInfo->find_data.cFileName);
127 if (FindNextFile(pInfo->hFind, &pInfo->find_data) == 0)
129 pInfo->find_data.cFileName[0] = 0;
132 return last_filename;
135 void osal_search_dir_close(void * search_info)
137 dir_search_info *pInfo = (dir_search_info *) search_info;
141 if (pInfo->hFind != INVALID_HANDLE_VALUE)
142 FindClose(pInfo->hFind);