- got tired of str[n]casecmp not declared warnings
[blender.git] / source / blender / blenlib / intern / winstuff.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Windows-posix compatibility layer, windows-specific functions.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39
40 #include <stdlib.h>
41 #include <stdio.h>
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_util.h"
47 #define WIN32_SKIP_HKEY_PROTECTION              // need to use HKEY
48 #include "BLI_winstuff.h"
49
50 #include "BKE_utildefines.h" /* FILE_MAXDIR + FILE_MAXFILE */
51
52 int BLI_getInstallationDir( char * str ) {
53         char dir[FILE_MAXDIR];
54         char file[FILE_MAXFILE];
55         int a;
56         
57         GetModuleFileName(NULL,str,FILE_MAXDIR+FILE_MAXFILE);
58         BLI_split_dirfile(str,dir,file);
59         a = strlen(dir);
60         if(dir[a-1] == '\\') dir[a-1]=0;
61         
62         strcpy(str,dir);
63         
64         return 1;
65 }
66
67
68 void RegisterBlendExtension(char * str) {
69         LONG lresult;
70         HKEY hkey = 0;
71         DWORD dwd = 0;
72         char buffer[128];
73         
74         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\shell\\open\\command", 0,
75                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
76
77         if (lresult == ERROR_SUCCESS) {
78                 sprintf(buffer, "\"%s\" \"%%1\"", str);
79                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
80                 RegCloseKey(hkey);
81         }
82
83         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, "blendfile\\DefaultIcon", 0,
84                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
85
86         if (lresult == ERROR_SUCCESS) {
87                 sprintf(buffer, "\"%s\",1", str);
88                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
89                 RegCloseKey(hkey);
90         }
91
92         lresult = RegCreateKeyEx(HKEY_CLASSES_ROOT, ".blend", 0,
93                 "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
94
95         if (lresult == ERROR_SUCCESS) {
96                 sprintf(buffer, "%s", "blendfile");
97                 lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, buffer, strlen(buffer) + 1);
98                 RegCloseKey(hkey);
99         }
100 }
101
102 DIR *opendir (const char *path) {
103         if (GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY) {
104                 DIR *newd= MEM_mallocN(sizeof(DIR), "opendir");
105
106                 newd->handle = INVALID_HANDLE_VALUE;
107                 sprintf(newd->path, "%s/*.*",path);
108                 
109                 newd->direntry.d_ino= 0;
110                 newd->direntry.d_off= 0;
111                 newd->direntry.d_reclen= 0;
112                 newd->direntry.d_name= NULL;
113                 
114                 return newd;
115         } else {
116                 return NULL;
117         }
118 }
119
120 struct dirent *readdir(DIR *dp) {
121         if (dp->direntry.d_name) {
122                 MEM_freeN(dp->direntry.d_name);
123                 dp->direntry.d_name= NULL;
124         }
125                 
126         if (dp->handle==INVALID_HANDLE_VALUE) {
127                 dp->handle= FindFirstFile(dp->path, &(dp->data));
128                 if (dp->handle==INVALID_HANDLE_VALUE)
129                         return NULL;
130                         
131                 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
132
133                 return &dp->direntry;
134         } else if (FindNextFile (dp->handle, &(dp->data))) {
135                 dp->direntry.d_name= BLI_strdup(dp->data.cFileName);
136
137                 return &dp->direntry;
138         } else {
139                 return NULL;
140         }
141 }
142
143 int closedir (DIR *dp) {
144         if (dp->direntry.d_name) MEM_freeN(dp->direntry.d_name);
145         if (dp->handle!=INVALID_HANDLE_VALUE) FindClose(dp->handle);
146
147         MEM_freeN(dp);
148         
149         return 0;
150 }
151
152 #else
153
154 static void BLI_WINSTUFF_C_IS_EMPTY_FOR_UNIX(void) 
155 {
156   /*intentionally empty*/
157 }
158
159 #endif