1#ifdef _WIN32
2
3#include "mupdf/fitz.h"
4
5#include <stdio.h>
6#include <errno.h>
7#include <time.h>
8#include <windows.h>
9
10#ifdef _MSC_VER
11#ifndef _WINRT
12
13#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
14
15int gettimeofday(struct timeval *tv, struct timezone *tz)
16{
17 FILETIME ft;
18 unsigned __int64 tmpres = 0;
19
20 if (tv)
21 {
22 GetSystemTimeAsFileTime(&ft);
23
24 tmpres |= ft.dwHighDateTime;
25 tmpres <<= 32;
26 tmpres |= ft.dwLowDateTime;
27
28 tmpres /= 10; /*convert into microseconds*/
29 /*converting file time to unix epoch*/
30 tmpres -= DELTA_EPOCH_IN_MICROSECS;
31 tv->tv_sec = (long)(tmpres / 1000000UL);
32 tv->tv_usec = (long)(tmpres % 1000000UL);
33 }
34
35 return 0;
36}
37
38#endif /* !_WINRT */
39#endif /* _MSC_VER */
40
41char *
42fz_utf8_from_wchar(const wchar_t *s)
43{
44 const wchar_t *src = s;
45 char *d;
46 char *dst;
47 int len = 1;
48
49 while (*src)
50 {
51 len += fz_runelen(*src++);
52 }
53
54 d = malloc(len);
55 if (d != NULL)
56 {
57 dst = d;
58 src = s;
59 while (*src)
60 {
61 dst += fz_runetochar(dst, *src++);
62 }
63 *dst = 0;
64 }
65 return d;
66}
67
68wchar_t *
69fz_wchar_from_utf8(const char *s)
70{
71 wchar_t *d, *r;
72 int c;
73 r = d = malloc((strlen(s) + 1) * sizeof(wchar_t));
74 if (!r)
75 return NULL;
76 while (*s) {
77 s += fz_chartorune(&c, s);
78 *d++ = c;
79 }
80 *d = 0;
81 return r;
82}
83
84void *
85fz_fopen_utf8(const char *name, const char *mode)
86{
87 wchar_t *wname, *wmode;
88 FILE *file;
89
90 wname = fz_wchar_from_utf8(name);
91 if (wname == NULL)
92 {
93 return NULL;
94 }
95
96 wmode = fz_wchar_from_utf8(mode);
97 if (wmode == NULL)
98 {
99 free(wname);
100 return NULL;
101 }
102
103 file = _wfopen(wname, wmode);
104
105 free(wname);
106 free(wmode);
107 return file;
108}
109
110int
111fz_remove_utf8(const char *name)
112{
113 wchar_t *wname;
114 int n;
115
116 wname = fz_wchar_from_utf8(name);
117 if (wname == NULL)
118 {
119 errno = ENOMEM;
120 return -1;
121 }
122
123 n = _wremove(wname);
124
125 free(wname);
126 return n;
127}
128
129char **
130fz_argv_from_wargv(int argc, wchar_t **wargv)
131{
132 char **argv;
133 int i;
134
135 argv = calloc(argc, sizeof(char *));
136 if (argv == NULL)
137 {
138 fprintf(stderr, "Out of memory while processing command line args!\n");
139 exit(1);
140 }
141
142 for (i = 0; i < argc; i++)
143 {
144 argv[i] = fz_utf8_from_wchar(wargv[i]);
145 if (argv[i] == NULL)
146 {
147 fprintf(stderr, "Out of memory while processing command line args!\n");
148 exit(1);
149 }
150 }
151
152 return argv;
153}
154
155void
156fz_free_argv(int argc, char **argv)
157{
158 int i;
159 for (i = 0; i < argc; i++)
160 free(argv[i]);
161 free(argv);
162}
163
164#else
165
166int fz_time_dummy;
167
168#endif /* _WIN32 */
169