doxygen: prevent GPL license block from being parsed as doxygen comment.
[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 #include "MEM_guardedalloc.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39
40 #include "BKE_utildefines.h"
41
42 #define WIN32_SKIP_HKEY_PROTECTION              // need to use HKEY
43 #include "BLI_winstuff.h"
44
45  /* FILE_MAXDIR + FILE_MAXFILE */
46
47 int BLI_getInstallationDir( char * str ) {
48         char dir[FILE_MAXDIR];
49         char file[FILE_MAXFILE];
50         int a;
51         
52         GetModuleFileName(NULL,str,FILE_MAXDIR+FILE_MAXFILE);
53         BLI_split_dirfile(str,dir,file); /* shouldn't be relative */
54         a = strlen(dir);
55         if(dir[a-1] == '\\') dir[a-1]=0;
56         
57         strcpy(str,dir);
58         
59         return 1;
60 }
61
62
63 void RegisterBlendExtension(char * str) {
64         LONG lresult;
65         HKEY hkey = 0;
66         DWORD dwd = 0;
67         char buffer[128];
68         
69         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\shell\\open\\command", 0,
70                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
71
72         if (lresult == ERROR_SUCCESS) {
73                 sprintf(buffer, "\"%s\" \"%%1\"", str);
74                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
75                 RegCloseKey(hkey);
76         }
77
78         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\DefaultIcon", 0,
79                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
80
81         if (lresult == ERROR_SUCCESS) {
82                 sprintf(buffer, "\"%s\",1", str);
83                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
84                 RegCloseKey(hkey);
85         }
86
87         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, ".blend", 0,
88                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
89
90         if (lresult == ERROR_SUCCESS) {
91                 sprintf(buffer, "%s", "blendfile");
92                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
93                 RegCloseKey(hkey);
94         }
95 }
96
97 DIR *opendir (const char *path) {
98         if (GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY) {
99                 DIR *newd= MEM_mallocN(sizeof(DIR), "opendir");
100
101                 newd->handle = INVALID_HANDLE_VALUE;
102                 sprintf(newd->path, "%s\\*",path);
103                 
104                 newd->direntry.d_ino= 0;
105                 newd->direntry.d_off= 0;
106                 newd->direntry.d_reclen= 0;
107                 newd->direntry.d_name= NULL;
108                 
109                 return newd;
110         } else {
111                 return NULL;
112         }
113 }
114
115 struct dirent *readdir(DIR *dp) {
116         if (dp->direntry.d_name) {
117                 MEM_freeN(dp->direntry.d_name);
118                 dp->direntry.d_name= NULL;
119         }
120                 
121         if (dp->handle==INVALID_HANDLE_VALUE) {
122                 dp->handle= FindFirstFile(dp->path, &(dp->data));
123                 if (dp->handle==INVALID_HANDLE_VALUE)
124                         return NULL;
125                         
126                 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
127
128                 return &dp->direntry;
129         } else if (FindNextFile (dp->handle, &(dp->data))) {
130                 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
131
132                 return &dp->direntry;
133         } else {
134                 return NULL;
135         }
136 }
137
138 int closedir (DIR *dp) {
139         if (dp->direntry.d_name) MEM_freeN(dp->direntry.d_name);
140         if (dp->handle!=INVALID_HANDLE_VALUE) FindClose(dp->handle);
141
142         MEM_freeN(dp);
143         
144         return 0;
145 }
146
147 void get_default_root(char* root) {
148         char str[MAX_PATH+1];
149         
150         /* the default drive to resolve a directory without a specified drive 
151            should be the Windows installation drive, since this was what the OS
152            assumes. */
153         if (GetWindowsDirectory(str,MAX_PATH+1)) {
154                 root[0] = str[0];
155                 root[1] = ':';
156                 root[2] = '\\';
157                 root[3] = '\0';
158         } else {                
159                 /* if GetWindowsDirectory fails, something has probably gone wrong, 
160                    we are trying the blender install dir though */
161                 if (GetModuleFileName(NULL,str,MAX_PATH+1)) {
162                         printf("Error! Could not get the Windows Directory - Defaulting to Blender installation Dir!");
163                         root[0] = str[0];
164                         root[1] = ':';
165                         root[2] = '\\';
166                         root[3] = '\0';
167                 } else {
168                         DWORD tmp;
169                         int i;
170                         int rc = 0;
171                         /* now something has gone really wrong - still trying our best guess */
172                         printf("Error! Could not get the Windows Directory - Defaulting to first valid drive! Path might be invalid!");
173                         tmp= GetLogicalDrives();
174                         for (i=2; i < 26; i++) {
175                                 if ((tmp>>i) & 1) {
176                                         root[0] = 'a'+i;
177                                         root[1] = ':';
178                                         root[2] = '\\';
179                                         root[3] = '\0';
180                                         if (GetFileAttributes(root) != 0xFFFFFFFF) {
181                                                 rc = i;
182                                                 break;                  
183                                         }
184                                 }
185                         }
186                         if (0 == rc) {
187                                 printf("ERROR in 'get_default_root': can't find a valid drive!");
188                                 root[0] = 'C';
189                                 root[1] = ':';
190                                 root[2] = '\\';
191                                 root[3] = '\0';
192                         }
193                 }               
194         }
195 }
196
197 int check_file_chars(char *filename)
198 {
199         char *p = filename;
200         while (*p) {
201                 switch (*p) {
202                         case ':':
203                         case '?':
204                         case '*':
205                         case '|':
206                         case '\\':
207                         case '/':
208                         case '\"':
209                                 return 0;
210                                 break;
211                 }
212
213                 p++;
214         }
215         return 1;
216 }
217
218 /* Copied from http://sourceware.org/ml/newlib/2005/msg00248.html */
219 /* Copyright 2005 Shaun Jackman
220  * Permission to use, copy, modify, and distribute this software
221  * is freely granted, provided that this notice is preserved.
222  */
223 #include <string.h>
224 char* dirname(char *path)
225 {
226            char *p;
227            if( path == NULL || *path == '\0' )
228                            return ".";
229            p = path + strlen(path) - 1;
230            while( *p == '/' ) {
231                            if( p == path )
232                                            return path;
233                            *p-- = '\0';
234            }
235            while( p >= path && *p != '/' )
236                            p--;
237            return
238                            p < path ? "." :
239                            p == path ? "/" :
240                            (*p = '\0', path);
241 }
242 /* End of copied part */
243
244 #else
245
246 /* intentionally empty for UNIX */
247
248 #endif