4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (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 Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Windows-posix compatibility layer, windows-specific functions.
36 #include "MEM_guardedalloc.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39 #define WIN32_SKIP_HKEY_PROTECTION // need to use HKEY
40 #include "BLI_winstuff.h"
42 #include "BKE_utildefines.h" /* FILE_MAXDIR + FILE_MAXFILE */
44 int BLI_getInstallationDir( char * str ) {
45 char dir[FILE_MAXDIR];
46 char file[FILE_MAXFILE];
49 GetModuleFileName(NULL,str,FILE_MAXDIR+FILE_MAXFILE);
50 BLI_split_dirfile(str,dir,file); /* shouldn't be relative */
52 if(dir[a-1] == '\\') dir[a-1]=0;
60 void RegisterBlendExtension(char * str) {
66 lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\shell\\open\\command", 0,
67 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
69 if (lresult == ERROR_SUCCESS) {
70 sprintf(buffer, "\"%s\" \"%%1\"", str);
71 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
75 lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\DefaultIcon", 0,
76 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
78 if (lresult == ERROR_SUCCESS) {
79 sprintf(buffer, "\"%s\",1", str);
80 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
84 lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, ".blend", 0,
85 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
87 if (lresult == ERROR_SUCCESS) {
88 sprintf(buffer, "%s", "blendfile");
89 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
94 DIR *opendir (const char *path) {
95 if (GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY) {
96 DIR *newd= MEM_mallocN(sizeof(DIR), "opendir");
98 newd->handle = INVALID_HANDLE_VALUE;
99 sprintf(newd->path, "%s\\*",path);
101 newd->direntry.d_ino= 0;
102 newd->direntry.d_off= 0;
103 newd->direntry.d_reclen= 0;
104 newd->direntry.d_name= NULL;
112 struct dirent *readdir(DIR *dp) {
113 if (dp->direntry.d_name) {
114 MEM_freeN(dp->direntry.d_name);
115 dp->direntry.d_name= NULL;
118 if (dp->handle==INVALID_HANDLE_VALUE) {
119 dp->handle= FindFirstFile(dp->path, &(dp->data));
120 if (dp->handle==INVALID_HANDLE_VALUE)
123 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
125 return &dp->direntry;
126 } else if (FindNextFile (dp->handle, &(dp->data))) {
127 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
129 return &dp->direntry;
135 int closedir (DIR *dp) {
136 if (dp->direntry.d_name) MEM_freeN(dp->direntry.d_name);
137 if (dp->handle!=INVALID_HANDLE_VALUE) FindClose(dp->handle);
144 void get_default_root(char* root) {
145 char str[MAX_PATH+1];
147 /* the default drive to resolve a directory without a specified drive
148 should be the Windows installation drive, since this was what the OS
150 if (GetWindowsDirectory(str,MAX_PATH+1)) {
156 /* if GetWindowsDirectory fails, something has probably gone wrong,
157 we are trying the blender install dir though */
158 if (GetModuleFileName(NULL,str,MAX_PATH+1)) {
159 printf("Error! Could not get the Windows Directory - Defaulting to Blender installation Dir!");
168 /* now something has gone really wrong - still trying our best guess */
169 printf("Error! Could not get the Windows Directory - Defaulting to first valid drive! Path might be invalid!");
170 tmp= GetLogicalDrives();
171 for (i=2; i < 26; i++) {
177 if (GetFileAttributes(root) != 0xFFFFFFFF) {
184 printf("ERROR in 'get_default_root': can't find a valid drive!");
194 int check_file_chars(char *filename)
215 /* Copied from http://sourceware.org/ml/newlib/2005/msg00248.html */
216 /* Copyright 2005 Shaun Jackman
217 * Permission to use, copy, modify, and distribute this software
218 * is freely granted, provided that this notice is preserved.
221 char* dirname(char *path)
224 if( path == NULL || *path == '\0' )
226 p = path + strlen(path) - 1;
232 while( p >= path && *p != '/' )
239 /* End of copied part */
243 /* intentionally empty for UNIX */