Cleanup: better naming and no bad level access in BLI_winstuff

This commit is contained in:
Brecht Van Lommel 2020-08-26 15:50:48 +02:00
parent 9de18c361b
commit f699ba3d30
7 changed files with 27 additions and 51 deletions

@ -28,6 +28,8 @@
# error "This include is for Windows only!" # error "This include is for Windows only!"
#endif #endif
#include "BLI_sys_types.h"
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
@ -86,6 +88,7 @@ typedef long ssize_t;
# endif # endif
#endif #endif
/* Directory reading compatibility with UNIX. */
struct dirent { struct dirent {
int d_ino; int d_ino;
int d_off; int d_off;
@ -99,13 +102,12 @@ typedef struct __dirstream DIR;
DIR *opendir(const char *path); DIR *opendir(const char *path);
struct dirent *readdir(DIR *dp); struct dirent *readdir(DIR *dp);
int closedir(DIR *dp); int closedir(DIR *dp);
void RegisterBlendExtension(void);
void get_default_root(char *root);
int check_file_chars(char *filename);
const char *dirname(char *path); const char *dirname(char *path);
int BLI_getInstallationDir(char *str); /* Windows utility functions. */
void BLI_windows_register_blend_extension(const bool background);
void BLI_windows_get_default_root_dir(char *root_dir);
int BLI_windows_get_executable_dir(char *str);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -532,7 +532,7 @@ void BLI_path_rel(char *file, const char *relfile)
char *ptemp; char *ptemp;
/* fix missing volume name in relative base, /* fix missing volume name in relative base,
* can happen with old recent-files.txt files */ * can happen with old recent-files.txt files */
get_default_root(temp); BLI_windows_get_default_root_dir(temp);
ptemp = &temp[2]; ptemp = &temp[2];
if (relfile[0] != '\\' && relfile[0] != '/') { if (relfile[0] != '\\' && relfile[0] != '/') {
ptemp++; ptemp++;
@ -1026,7 +1026,7 @@ bool BLI_path_abs(char *path, const char *basepath)
*/ */
if (!wasrelative && !BLI_path_is_abs(path)) { if (!wasrelative && !BLI_path_is_abs(path)) {
char *p = path; char *p = path;
get_default_root(tmp); BLI_windows_get_default_root_dir(tmp);
// get rid of the slashes at the beginning of the path // get rid of the slashes at the beginning of the path
while (ELEM(*p, '\\', '/')) { while (ELEM(*p, '\\', '/')) {
p++; p++;
@ -1385,7 +1385,7 @@ void BLI_make_file_string(const char *relabase, char *string, const char *dir, c
string[3] = '\0'; string[3] = '\0';
} }
else { /* we're out of luck here, guessing the first valid drive, usually c:\ */ else { /* we're out of luck here, guessing the first valid drive, usually c:\ */
get_default_root(string); BLI_windows_get_default_root_dir(string);
} }
/* ignore leading slashes */ /* ignore leading slashes */

@ -36,14 +36,12 @@
# include "BLI_utildefines.h" # include "BLI_utildefines.h"
# include "BLI_winstuff.h" # include "BLI_winstuff.h"
# include "../blenkernel/BKE_global.h" /* G.background, bad level include (no function calls) */
# include "utf_winfunc.h" # include "utf_winfunc.h"
# include "utfconv.h" # include "utfconv.h"
/* FILE_MAXDIR + FILE_MAXFILE */ /* FILE_MAXDIR + FILE_MAXFILE */
int BLI_getInstallationDir(char *str) int BLI_windows_get_executable_dir(char *str)
{ {
char dir[FILE_MAXDIR]; char dir[FILE_MAXDIR];
int a; int a;
@ -60,19 +58,19 @@ int BLI_getInstallationDir(char *str)
return 1; return 1;
} }
static void RegisterBlendExtension_Fail(HKEY root) static void register_blend_extension_failed(HKEY root, const bool background)
{ {
printf("failed\n"); printf("failed\n");
if (root) { if (root) {
RegCloseKey(root); RegCloseKey(root);
} }
if (!G.background) { if (!background) {
MessageBox(0, "Could not register file extension.", "Blender error", MB_OK | MB_ICONERROR); MessageBox(0, "Could not register file extension.", "Blender error", MB_OK | MB_ICONERROR);
} }
TerminateProcess(GetCurrentProcess(), 1); TerminateProcess(GetCurrentProcess(), 1);
} }
void RegisterBlendExtension(void) void BLI_windows_register_blend_extension(const bool background)
{ {
LONG lresult; LONG lresult;
HKEY hkey = 0; HKEY hkey = 0;
@ -108,7 +106,7 @@ void RegisterBlendExtension(void)
usr_mode = true; usr_mode = true;
lresult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Classes", 0, KEY_ALL_ACCESS, &root); lresult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
if (lresult != ERROR_SUCCESS) { if (lresult != ERROR_SUCCESS) {
RegisterBlendExtension_Fail(0); register_blend_extension_failed(0, background);
} }
} }
@ -120,7 +118,7 @@ void RegisterBlendExtension(void)
RegCloseKey(hkey); RegCloseKey(hkey);
} }
if (lresult != ERROR_SUCCESS) { if (lresult != ERROR_SUCCESS) {
RegisterBlendExtension_Fail(root); register_blend_extension_failed(root, background);
} }
lresult = RegCreateKeyEx(root, lresult = RegCreateKeyEx(root,
@ -138,7 +136,7 @@ void RegisterBlendExtension(void)
RegCloseKey(hkey); RegCloseKey(hkey);
} }
if (lresult != ERROR_SUCCESS) { if (lresult != ERROR_SUCCESS) {
RegisterBlendExtension_Fail(root); register_blend_extension_failed(root, background);
} }
lresult = RegCreateKeyEx(root, lresult = RegCreateKeyEx(root,
@ -156,7 +154,7 @@ void RegisterBlendExtension(void)
RegCloseKey(hkey); RegCloseKey(hkey);
} }
if (lresult != ERROR_SUCCESS) { if (lresult != ERROR_SUCCESS) {
RegisterBlendExtension_Fail(root); register_blend_extension_failed(root, background);
} }
lresult = RegCreateKeyEx( lresult = RegCreateKeyEx(
@ -167,10 +165,10 @@ void RegisterBlendExtension(void)
RegCloseKey(hkey); RegCloseKey(hkey);
} }
if (lresult != ERROR_SUCCESS) { if (lresult != ERROR_SUCCESS) {
RegisterBlendExtension_Fail(root); register_blend_extension_failed(root, background);
} }
BLI_getInstallationDir(InstallDir); BLI_windows_get_executable_dir(InstallDir);
GetSystemDirectory(SysDir, FILE_MAXDIR); GetSystemDirectory(SysDir, FILE_MAXDIR);
ThumbHandlerDLL = "BlendThumb.dll"; ThumbHandlerDLL = "BlendThumb.dll";
snprintf( snprintf(
@ -179,7 +177,7 @@ void RegisterBlendExtension(void)
RegCloseKey(root); RegCloseKey(root);
printf("success (%s)\n", usr_mode ? "user" : "system"); printf("success (%s)\n", usr_mode ? "user" : "system");
if (!G.background) { if (!background) {
sprintf(MBox, sprintf(MBox,
"File extension registered for %s.", "File extension registered for %s.",
usr_mode ? "the current user. To register for all users, run as an administrator" : usr_mode ? "the current user. To register for all users, run as an administrator" :
@ -189,7 +187,7 @@ void RegisterBlendExtension(void)
TerminateProcess(GetCurrentProcess(), 0); TerminateProcess(GetCurrentProcess(), 0);
} }
void get_default_root(char *root) void BLI_windows_get_default_root_dir(char *root)
{ {
char str[MAX_PATH + 1]; char str[MAX_PATH + 1];
@ -236,7 +234,7 @@ void get_default_root(char *root)
} }
} }
if (0 == rc) { if (0 == rc) {
printf("ERROR in 'get_default_root': can't find a valid drive!\n"); printf("ERROR in 'BLI_windows_get_default_root_dir': can't find a valid drive!\n");
root[0] = 'C'; root[0] = 'C';
root[1] = ':'; root[1] = ':';
root[2] = '\\'; root[2] = '\\';
@ -246,30 +244,6 @@ void get_default_root(char *root)
} }
} }
/* UNUSED */
# if 0
int check_file_chars(char *filename)
{
char *p = filename;
while (*p) {
switch (*p) {
case ':':
case '?':
case '*':
case '|':
case '\\':
case '/':
case '\"':
return 0;
break;
}
p++;
}
return 1;
}
# endif
#else #else
/* intentionally empty for UNIX */ /* intentionally empty for UNIX */

@ -2297,7 +2297,7 @@ static void file_expand_directory(bContext *C)
} }
#else #else
{ {
get_default_root(sfile->params->dir); BLI_windows_get_default_root_dir(sfile->params->dir);
} }
/* change "C:" --> "C:\", [#28102] */ /* change "C:" --> "C:\", [#28102] */
else if ((isalpha(sfile->params->dir[0]) && (sfile->params->dir[1] == ':')) && else if ((isalpha(sfile->params->dir[0]) && (sfile->params->dir[1] == ':')) &&

@ -1127,7 +1127,7 @@ static void parent_dir_until_exists_or_default_root(char *dir)
{ {
if (!BLI_path_parent_dir_until_exists(dir)) { if (!BLI_path_parent_dir_until_exists(dir)) {
#ifdef WIN32 #ifdef WIN32
get_default_root(dir); BLI_windows_get_default_root_dir(dir);
#else #else
strcpy(dir, "/"); strcpy(dir, "/");
#endif #endif

@ -1576,7 +1576,7 @@ void wm_autosave_location(char *filepath)
* Blender installed on D:\ drive, D:\ drive has D:\tmp\ * Blender installed on D:\ drive, D:\ drive has D:\tmp\
* Now, BLI_exists() will find '/tmp/' exists, but * Now, BLI_exists() will find '/tmp/' exists, but
* BLI_make_file_string will create string that has it most likely on C:\ * BLI_make_file_string will create string that has it most likely on C:\
* through get_default_root(). * through BLI_windows_get_default_root_dir().
* If there is no C:\tmp autosave fails. */ * If there is no C:\tmp autosave fails. */
if (!BLI_exists(BKE_tempdir_base())) { if (!BLI_exists(BKE_tempdir_base())) {
savedir = BKE_appdir_folder_id_create(BLENDER_USER_AUTOSAVE, NULL); savedir = BKE_appdir_folder_id_create(BLENDER_USER_AUTOSAVE, NULL);

@ -1300,7 +1300,7 @@ static int arg_handle_register_extension(int UNUSED(argc), const char **UNUSED(a
if (data) { if (data) {
G.background = 1; G.background = 1;
} }
RegisterBlendExtension(); BLI_windows_register_blend_extension(G.background);
# else # else
(void)data; /* unused */ (void)data; /* unused */
# endif # endif