svn merge -r 31314:31370 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / blenlib / intern / winstuff.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
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 Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Windows-posix compatibility layer, windows-specific functions.
29  */
30
31 #ifdef WIN32
32
33 #include <stdlib.h>
34 #include <stdio.h>
35
36
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"
41
42 #include "BKE_utildefines.h" /* FILE_MAXDIR + FILE_MAXFILE */
43
44 int BLI_getInstallationDir( char * str ) {
45         char dir[FILE_MAXDIR];
46         char file[FILE_MAXFILE];
47         int a;
48         
49         GetModuleFileName(NULL,str,FILE_MAXDIR+FILE_MAXFILE);
50         BLI_split_dirfile(str,dir,file); /* shouldn't be relative */
51         a = strlen(dir);
52         if(dir[a-1] == '\\') dir[a-1]=0;
53         
54         strcpy(str,dir);
55         
56         return 1;
57 }
58
59
60 void RegisterBlendExtension(char * str) {
61         LONG lresult;
62         HKEY hkey = 0;
63         DWORD dwd = 0;
64         char buffer[128];
65         
66         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\shell\\open\\command", 0,
67                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
68
69         if (lresult == ERROR_SUCCESS) {
70                 sprintf(buffer, "\"%s\" \"%%1\"", str);
71                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
72                 RegCloseKey(hkey);
73         }
74
75         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\DefaultIcon", 0,
76                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
77
78         if (lresult == ERROR_SUCCESS) {
79                 sprintf(buffer, "\"%s\",1", str);
80                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
81                 RegCloseKey(hkey);
82         }
83
84         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, ".blend", 0,
85                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
86
87         if (lresult == ERROR_SUCCESS) {
88                 sprintf(buffer, "%s", "blendfile");
89                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
90                 RegCloseKey(hkey);
91         }
92 }
93
94 DIR *opendir (const char *path) {
95         if (GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY) {
96                 DIR *newd= MEM_mallocN(sizeof(DIR), "opendir");
97
98                 newd->handle = INVALID_HANDLE_VALUE;
99                 sprintf(newd->path, "%s\\*",path);
100                 
101                 newd->direntry.d_ino= 0;
102                 newd->direntry.d_off= 0;
103                 newd->direntry.d_reclen= 0;
104                 newd->direntry.d_name= NULL;
105                 
106                 return newd;
107         } else {
108                 return NULL;
109         }
110 }
111
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;
116         }
117                 
118         if (dp->handle==INVALID_HANDLE_VALUE) {
119                 dp->handle= FindFirstFile(dp->path, &(dp->data));
120                 if (dp->handle==INVALID_HANDLE_VALUE)
121                         return NULL;
122                         
123                 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
124
125                 return &dp->direntry;
126         } else if (FindNextFile (dp->handle, &(dp->data))) {
127                 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
128
129                 return &dp->direntry;
130         } else {
131                 return NULL;
132         }
133 }
134
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);
138
139         MEM_freeN(dp);
140         
141         return 0;
142 }
143
144 void get_default_root(char* root) {
145         char str[MAX_PATH+1];
146         
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
149            assumes. */
150         if (GetWindowsDirectory(str,MAX_PATH+1)) {
151                 root[0] = str[0];
152                 root[1] = ':';
153                 root[2] = '\\';
154                 root[3] = '\0';
155         } else {                
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!");
160                         root[0] = str[0];
161                         root[1] = ':';
162                         root[2] = '\\';
163                         root[3] = '\0';
164                 } else {
165                         DWORD tmp;
166                         int i;
167                         int rc = 0;
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++) {
172                                 if ((tmp>>i) & 1) {
173                                         root[0] = 'a'+i;
174                                         root[1] = ':';
175                                         root[2] = '\\';
176                                         root[3] = '\0';
177                                         if (GetFileAttributes(root) != 0xFFFFFFFF) {
178                                                 rc = i;
179                                                 break;                  
180                                         }
181                                 }
182                         }
183                         if (0 == rc) {
184                                 printf("ERROR in 'get_default_root': can't find a valid drive!");
185                                 root[0] = 'C';
186                                 root[1] = ':';
187                                 root[2] = '\\';
188                                 root[3] = '\0';
189                         }
190                 }               
191         }
192 }
193
194 int check_file_chars(char *filename)
195 {
196         char *p = filename;
197         while (*p) {
198                 switch (*p) {
199                         case ':':
200                         case '?':
201                         case '*':
202                         case '|':
203                         case '\\':
204                         case '/':
205                         case '\"':
206                                 return 0;
207                                 break;
208                 }
209
210                 p++;
211         }
212         return 1;
213 }
214
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.
219  */
220 #include <string.h>
221 char* dirname(char *path)
222 {
223            char *p;
224            if( path == NULL || *path == '\0' )
225                            return ".";
226            p = path + strlen(path) - 1;
227            while( *p == '/' ) {
228                            if( p == path )
229                                            return path;
230                            *p-- = '\0';
231            }
232            while( p >= path && *p != '/' )
233                            p--;
234            return
235                            p < path ? "." :
236                            p == path ? "/" :
237                            (*p = '\0', path);
238 }
239 /* End of copied part */
240
241 #else
242
243 /* intentionally empty for UNIX */
244
245 #endif