| 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 2 | * Mupen64plus-core - osal_files_win32.c * |
| 3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
| 4 | * Copyright (C) 2009 Richard Goedeken * |
| 5 | * * |
| 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. * |
| 10 | * * |
| 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. * |
| 15 | * * |
| 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 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 21 | |
| 22 | /* This file contains the definitions for the unix-specific file handling |
| 23 | * functions |
| 24 | */ |
| 25 | |
| 26 | #include <windows.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | #include <direct.h> |
| 33 | |
| 34 | #include "osal_files.h" |
| 35 | |
| 36 | /* global functions */ |
| 37 | |
| 38 | int osal_is_directory(const char* name) |
| 39 | { |
| 40 | char DirName[MAX_PATH + 1]; |
| 41 | int namelen = 0; |
| 42 | |
| 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); |
| 50 | } |
| 51 | |
| 52 | int osal_mkdirp(const char *dirpath, int mode) |
| 53 | { |
| 54 | struct _stat fileinfo; |
| 55 | size_t dirpathlen = strlen(dirpath); |
| 56 | char *currpath = _strdup(dirpath); |
| 57 | |
| 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) |
| 60 | { |
| 61 | char *lastslash = strrchr(currpath, '\\'); |
| 62 | if (lastslash == NULL) |
| 63 | { |
| 64 | free(currpath); |
| 65 | return 1; /* error: we never found an existing directory, this path is bad */ |
| 66 | } |
| 67 | *lastslash = 0; |
| 68 | } |
| 69 | |
| 70 | /* then walk up the path chain, creating directories along the way */ |
| 71 | do |
| 72 | { |
| 73 | if (currpath[strlen(currpath)-1] != '\\' && _stat(currpath, &fileinfo) != 0) |
| 74 | { |
| 75 | if (_mkdir(currpath) != 0) |
| 76 | { |
| 77 | free(currpath); |
| 78 | return 2; /* mkdir failed */ |
| 79 | } |
| 80 | } |
| 81 | if (strlen(currpath) == dirpathlen) |
| 82 | break; |
| 83 | else |
| 84 | currpath[strlen(currpath)] = '\\'; |
| 85 | } while (1); |
| 86 | |
| 87 | free(currpath); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | typedef struct { |
| 92 | HANDLE hFind; |
| 93 | WIN32_FIND_DATA find_data; |
| 94 | } dir_search_info; |
| 95 | |
| 96 | void * osal_search_dir_open(const char *pathname) |
| 97 | { |
| 98 | char SearchString[MAX_PATH + 1]; |
| 99 | dir_search_info *pInfo = malloc(sizeof(dir_search_info)); |
| 100 | |
| 101 | if (pInfo == NULL) |
| 102 | return NULL; |
| 103 | |
| 104 | pInfo->hFind = INVALID_HANDLE_VALUE; |
| 105 | pInfo->find_data.cFileName[0] = 0; |
| 106 | |
| 107 | if (pathname[strlen(pathname)-1] == '\\') |
| 108 | _snprintf(SearchString, MAX_PATH, "%s*", pathname); |
| 109 | else |
| 110 | _snprintf(SearchString, MAX_PATH, "%s\\*", pathname); |
| 111 | SearchString[MAX_PATH] = 0; |
| 112 | |
| 113 | pInfo->hFind = FindFirstFile(SearchString, &pInfo->find_data); |
| 114 | return (void *) pInfo; |
| 115 | } |
| 116 | |
| 117 | const char *osal_search_dir_read_next(void * search_info) |
| 118 | { |
| 119 | static char last_filename[_MAX_PATH]; |
| 120 | dir_search_info *pInfo = (dir_search_info *) search_info; |
| 121 | |
| 122 | if (pInfo == NULL || pInfo->hFind == INVALID_HANDLE_VALUE || pInfo->find_data.cFileName[0] == 0) |
| 123 | return NULL; |
| 124 | |
| 125 | strcpy(last_filename, pInfo->find_data.cFileName); |
| 126 | |
| 127 | if (FindNextFile(pInfo->hFind, &pInfo->find_data) == 0) |
| 128 | { |
| 129 | pInfo->find_data.cFileName[0] = 0; |
| 130 | } |
| 131 | |
| 132 | return last_filename; |
| 133 | } |
| 134 | |
| 135 | void osal_search_dir_close(void * search_info) |
| 136 | { |
| 137 | dir_search_info *pInfo = (dir_search_info *) search_info; |
| 138 | |
| 139 | if (pInfo != NULL) |
| 140 | { |
| 141 | if (pInfo->hFind != INVALID_HANDLE_VALUE) |
| 142 | FindClose(pInfo->hFind); |
| 143 | free(pInfo); |
| 144 | } |
| 145 | } |