utf_winfunc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2012 Blender Foundation.
  17. * All rights reserved.
  18. */
  19. #ifndef _WIN32_IE
  20. # define _WIN32_IE 0x0501
  21. #endif
  22. #include "utf_winfunc.h"
  23. #include "utfconv.h"
  24. #include <io.h>
  25. #include <windows.h>
  26. #include <wchar.h>
  27. FILE *ufopen(const char *filename, const char *mode)
  28. {
  29. FILE *f = NULL;
  30. UTF16_ENCODE(filename);
  31. UTF16_ENCODE(mode);
  32. if (filename_16 && mode_16) {
  33. f = _wfopen(filename_16, mode_16);
  34. }
  35. UTF16_UN_ENCODE(mode);
  36. UTF16_UN_ENCODE(filename);
  37. if (!f) {
  38. if ((f = fopen(filename, mode))) {
  39. printf("WARNING: %s is not utf path. Please update it.\n", filename);
  40. }
  41. }
  42. return f;
  43. }
  44. int uopen(const char *filename, int oflag, int pmode)
  45. {
  46. int f = -1;
  47. UTF16_ENCODE(filename);
  48. if (filename_16) {
  49. f = _wopen(filename_16, oflag, pmode);
  50. }
  51. UTF16_UN_ENCODE(filename);
  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. return f;
  58. }
  59. int uaccess(const char *filename, int mode)
  60. {
  61. int r = -1;
  62. UTF16_ENCODE(filename);
  63. if (filename_16) {
  64. r = _waccess(filename_16, mode);
  65. }
  66. UTF16_UN_ENCODE(filename);
  67. return r;
  68. }
  69. int urename(const char *oldname, const char *newname)
  70. {
  71. int r = -1;
  72. UTF16_ENCODE(oldname);
  73. UTF16_ENCODE(newname);
  74. if (oldname_16 && newname_16) {
  75. r = _wrename(oldname_16, newname_16);
  76. }
  77. UTF16_UN_ENCODE(newname);
  78. UTF16_UN_ENCODE(oldname);
  79. return r;
  80. }
  81. int umkdir(const char *pathname)
  82. {
  83. BOOL r = 0;
  84. UTF16_ENCODE(pathname);
  85. if (pathname_16) {
  86. r = CreateDirectoryW(pathname_16, NULL);
  87. }
  88. UTF16_UN_ENCODE(pathname);
  89. return r ? 0 : -1;
  90. }
  91. char *u_alloc_getenv(const char *varname)
  92. {
  93. char *r = 0;
  94. wchar_t *str;
  95. UTF16_ENCODE(varname);
  96. if (varname_16) {
  97. str = _wgetenv(varname_16);
  98. r = alloc_utf_8_from_16(str, 0);
  99. }
  100. UTF16_UN_ENCODE(varname);
  101. return r;
  102. }
  103. void u_free_getenv(char *val)
  104. {
  105. free(val);
  106. }
  107. int uput_getenv(const char *varname, char *value, size_t buffsize)
  108. {
  109. int r = 0;
  110. wchar_t *str;
  111. if (!buffsize) {
  112. return r;
  113. }
  114. UTF16_ENCODE(varname);
  115. if (varname_16) {
  116. str = _wgetenv(varname_16);
  117. conv_utf_16_to_8(str, value, buffsize);
  118. r = 1;
  119. }
  120. UTF16_UN_ENCODE(varname);
  121. if (!r) {
  122. value[0] = 0;
  123. }
  124. return r;
  125. }
  126. int uputenv(const char *name, const char *value)
  127. {
  128. int r = -1;
  129. UTF16_ENCODE(name);
  130. if (value) {
  131. /* set */
  132. UTF16_ENCODE(value);
  133. if (name_16 && value_16) {
  134. r = (SetEnvironmentVariableW(name_16, value_16) != 0) ? 0 : -1;
  135. }
  136. UTF16_UN_ENCODE(value);
  137. }
  138. else {
  139. /* clear */
  140. if (name_16) {
  141. r = (SetEnvironmentVariableW(name_16, NULL) != 0) ? 0 : -1;
  142. }
  143. }
  144. UTF16_UN_ENCODE(name);
  145. return r;
  146. }