dav/system.c

changeset 410
0b9bea2d7283
child 411
a182e503617b
equal deleted inserted replaced
409:90a6d6952d83 410:0b9bea2d7283
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <dirent.h>
33
34 #include "system.h"
35
36 void sys_freedirent(SysDirEnt *ent) {
37 free(ent->name);
38 free(ent);
39 }
40
41 #ifndef _WIN32
42 /* ---------- POSIX implementation ---------- */
43
44 SYS_DIR sys_opendir(const char *path) {
45 return opendir(path);
46 }
47
48 SysDirEnt* sys_readdir(SYS_DIR dir) {
49 struct dirent *ent = readdir(dir);
50 if(ent) {
51 SysDirEnt *e = malloc(sizeof(SysDirEnt));
52 e->name = strdup(ent->d_name);
53 return e;
54 }
55 return NULL;
56 }
57
58 void sys_closedir(SYS_DIR dir) {
59 closedir(dir);
60 }
61
62 FILE* sys_fopen(const char *path, const char *mode) {
63 return fopen(path, mode);
64 }
65
66 int sys_stat(const char *path, SYS_STAT *s) {
67 return stat(path, s);
68 }
69
70
71 #else
72 /* ---------- Windows implementation ---------- */
73
74 static wchar_t* path2winpath(const char *path, int dir, int *newlen) {
75 size_t len = strlen(path);
76 size_t lenadd = dir ? 2 : 0;
77
78
79 wchar_t *wpath = calloc(len+lenadd, sizeof(wchar_t));
80 int wlen = MultiByteToWideChar(
81 CP_UTF8,
82 0,
83 path,
84 len,
85 wpath,
86 len+1
87 );
88 if(newlen) {
89 *newlen = wlen;
90 }
91 for(int i=0;i<wlen;i++) {
92 if(wpath[i] == L'/') {
93 wpath[i] = L'\\';
94 }
95 }
96
97 if(dir) {
98 if(wpath[wlen-1] != L'\\') {
99 wpath[wlen++] = L'\\';
100 }
101 wpath[wlen++] = L'*';
102 }
103 wpath[wlen] = 0;
104
105 return wpath;
106 }
107
108 SYS_DIR sys_opendir(const char *path) {
109 struct WinDir *dir = malloc(sizeof(struct WinDir));
110 wchar_t *dirpath = path2winpath(path, TRUE, NULL);
111 if(!dirpath) {
112 fprintf(stderr, "Cannot convert path \"%s\" to UTF16\n", path);
113 free(dir);
114 return NULL;
115 }
116 dir->first = 1;
117 dir->handle = FindFirstFileW(dirpath, &dir->finddata);
118 free(dirpath);
119 if(dir->handle == INVALID_HANDLE_VALUE) {
120 free(dir);
121 return NULL;
122 }
123 return dir;
124 }
125
126 SysDirEnt* sys_readdir(SYS_DIR dir) {
127 if(dir->first) {
128 dir->first = 0;
129 } else {
130 if(FindNextFileW(dir->handle, &dir->finddata) == 0) {
131 return NULL;
132 }
133 }
134
135 size_t namelen = wcslen(dir->finddata.cFileName);
136
137 char *name = malloc((namelen+1)*4);
138 int nlen = WideCharToMultiByte(
139 CP_UTF8,
140 0,
141 dir->finddata.cFileName,
142 -1,
143 name,
144 256,
145 NULL,
146 NULL);
147 if(nlen > 0) {
148 name[nlen] = 0;
149 SysDirEnt *ent = malloc(sizeof(SysDirEnt));
150 ent->name = name;
151 return ent;
152 } else {
153 return NULL;
154 }
155 }
156
157 void sys_closedir(SYS_DIR dir) {
158 FindClose(dir->handle);
159 free(dir);
160 }
161
162 FILE* sys_fopen(const char *path, const char *mode) {
163 wchar_t *fpath = path2winpath(path, FALSE, NULL);
164 wchar_t *fmode = path2winpath(mode, FALSE, NULL);
165
166 FILE *file = (fpath && fmode) ? _wfopen(fpath, fmode) : NULL;
167 free(fpath);
168 free(fmode);
169 return file;
170 }
171
172 int sys_stat(const char *path, SYS_STAT *s) {
173 wchar_t *fpath = path2winpath(path, FALSE, NULL);
174 if(!fpath) {
175 fprintf(stderr, "Cannot convert path \"%s\" to UTF16\n", path);
176 return -1;
177 }
178 int ret = _wstat64(fpath, s);
179 free(fpath);
180 return ret;
181 }
182
183 #endif

mercurial