Blender V4.3
utf_winfunc.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#ifndef _WIN32_IE
10# define _WIN32_IE 0x0501
11#endif
12
13#include "utf_winfunc.hh"
14#include "utfconv.hh"
15#include <io.h>
16#include <wchar.h>
17#include <windows.h>
18
19FILE *ufopen(const char *filename, const char *mode)
20{
21 FILE *f = NULL;
22 UTF16_ENCODE(filename);
23 UTF16_ENCODE(mode);
24
25 if (filename_16 && mode_16) {
26 f = _wfopen(filename_16, mode_16);
27 }
28
29 UTF16_UN_ENCODE(mode);
30 UTF16_UN_ENCODE(filename);
31
32 if (!f) {
33 if ((f = fopen(filename, mode))) {
34 printf("WARNING: %s is not utf path. Please update it.\n", filename);
35 }
36 }
37
38 return f;
39}
40
41int uopen(const char *filename, int oflag, int pmode)
42{
43 int f = -1;
44 UTF16_ENCODE(filename);
45
46 if (filename_16) {
47 f = _wopen(filename_16, oflag, pmode);
48 }
49
50 UTF16_UN_ENCODE(filename);
51
52 if (f == -1) {
53 if ((f = open(filename, oflag, pmode)) != -1) {
54 printf("WARNING: %s is not utf path. Please update it.\n", filename);
55 }
56 }
57
58 return f;
59}
60
61int uaccess(const char *filename, int mode)
62{
63 int r = -1;
64 UTF16_ENCODE(filename);
65
66 if (filename_16) {
67 r = _waccess(filename_16, mode);
68 }
69
70 UTF16_UN_ENCODE(filename);
71
72 return r;
73}
74
75int urename(const char *oldname, const char *newname, const bool do_replace)
76{
77 int r = -1;
78 UTF16_ENCODE(oldname);
79 UTF16_ENCODE(newname);
80
81 if (oldname_16 && newname_16) {
82 /* Closer to UNIX `rename` behavior, as it at least allows to replace an existing file.
83 * Return value logic is inverted however (returns non-zero on sucess, 0 on failure).
84 * Note that the operation will still fail if the 'newname' existing file is opened anywhere.
85 */
86 r = (MoveFileExW(oldname_16, newname_16, do_replace ? MOVEFILE_REPLACE_EXISTING : 0) == 0);
87 }
88
89 UTF16_UN_ENCODE(newname);
90 UTF16_UN_ENCODE(oldname);
91 return r;
92}
93
94int umkdir(const char *pathname)
95{
96
97 BOOL r = 0;
98 UTF16_ENCODE(pathname);
99
100 if (pathname_16) {
101 r = CreateDirectoryW(pathname_16, NULL);
102 }
103
104 UTF16_UN_ENCODE(pathname);
105
106 return r ? 0 : -1;
107}
108
109char *u_alloc_getenv(const char *varname)
110{
111 char *r = 0;
112 wchar_t *str;
113 UTF16_ENCODE(varname);
114 if (varname_16) {
115 str = _wgetenv(varname_16);
116 r = alloc_utf_8_from_16(str, 0);
117 }
118 UTF16_UN_ENCODE(varname);
119
120 return r;
121}
122void u_free_getenv(char *val)
123{
124 free(val);
125}
126
127int uput_getenv(const char *varname, char *value, size_t buffsize)
128{
129 int r = 0;
130 wchar_t *str;
131
132 if (!buffsize) {
133 return r;
134 }
135
136 UTF16_ENCODE(varname);
137 if (varname_16) {
138 str = _wgetenv(varname_16);
139 conv_utf_16_to_8(str, value, buffsize);
140 r = 1;
141 }
142 UTF16_UN_ENCODE(varname);
143
144 if (!r) {
145 value[0] = 0;
146 }
147
148 return r;
149}
150
151int uputenv(const char *name, const char *value)
152{
153 int r = -1;
154 UTF16_ENCODE(name);
155
156 if (value) {
157 /* set */
158 UTF16_ENCODE(value);
159
160 if (name_16 && value_16) {
161 r = (SetEnvironmentVariableW(name_16, value_16) != 0) ? 0 : -1;
162 }
163 UTF16_UN_ENCODE(value);
164 }
165 else {
166 /* clear */
167 if (name_16) {
168 r = (SetEnvironmentVariableW(name_16, NULL) != 0) ? 0 : -1;
169 }
170 }
171
172 UTF16_UN_ENCODE(name);
173
174 return r;
175}
void BLI_kdtree_nd_ free(KDTree *tree)
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
#define printf
#define NULL
#define str(s)
int urename(const char *oldname, const char *newname, const bool do_replace)
FILE * ufopen(const char *filename, const char *mode)
int uput_getenv(const char *varname, char *value, size_t buffsize)
int uputenv(const char *name, const char *value)
int umkdir(const char *pathname)
void u_free_getenv(char *val)
int uopen(const char *filename, int oflag, int pmode)
int uaccess(const char *filename, int mode)
char * u_alloc_getenv(const char *varname)
char * alloc_utf_8_from_16(const wchar_t *in16, size_t add)
Definition utfconv.cc:280
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84