PageRenderTime 65ms CodeModel.GetById 43ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/win/scaffold/file.d

http://github.com/wilkie/djehuty
D | 224 lines | 134 code | 52 blank | 38 comment | 8 complexity | 9a0cfbd92d680e63dd4e1f516cdb0ce4 MD5 | raw file
  1/*
  2 * file.d
  3 *
  4 * This file implements the Scaffold for platform specific File for Windows.
  5 *
  6 * Author: Dave Wilkinson
  7 *
  8 */
  9
 10module scaffold.file;
 11
 12import platform.win.common;
 13import platform.win.main;
 14
 15import platform.vars.file;
 16
 17import scaffold.directory;
 18
 19import core.stream;
 20import core.string;
 21import core.main;
 22import core.definitions;
 23import core.unicode;
 24
 25import io.console;
 26import io.directory;
 27import io.file;
 28
 29// OPERATIONS //
 30
 31bool FileMove(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
 32	wstring oldPath = Unicode.toUtf16(oldFullPath);
 33	oldPath ~= '\0';
 34
 35	wstring newPath = Unicode.toUtf16(newFullPath);
 36	newPath ~= '\0';
 37
 38	MoveFileW(oldPath.ptr, newPath.ptr);
 39	return true;
 40}
 41
 42bool FileCopy(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
 43	wstring oldPath = Unicode.toUtf16(oldFullPath);
 44	oldPath ~= '\0';
 45
 46	wstring newPath = Unicode.toUtf16(newFullPath);
 47	newPath ~= '\0';
 48
 49	CopyFileW(oldPath.ptr, newPath.ptr, 0);
 50	return true;
 51}
 52
 53bool FileRename(ref FilePlatformVars fileVars, ref string path, ref string newName) {
 54	wstring old = Unicode.toUtf16(path);
 55	old ~= '\0';
 56
 57	string str;
 58
 59	foreach_reverse(int i, chr; path)
 60	{
 61		if (chr == '/')
 62		{
 63			// truncate
 64			str = path[0..i];
 65			break;
 66		}
 67	}
 68
 69	if (str is null) { return false; }
 70
 71	str ~= newName;
 72	str ~= '\0';
 73	
 74	wstring strw = Unicode.toUtf16(str);
 75
 76	MoveFileW(old.ptr, strw.ptr);
 77	return true;
 78}
 79
 80bool FileMove(ref string from, ref Directory to) {
 81	wstring old = Unicode.toUtf16(from);
 82	old ~= '\0';
 83
 84	string fn;
 85
 86	foreach_reverse(int i, chr; from)
 87	{
 88		if (chr == '/')
 89		{
 90			// truncate (include the slash)
 91			fn = from[i..from.length];
 92			break;
 93		}
 94	}
 95
 96	if (fn is null) { return false; }
 97
 98	wstring str = Unicode.toUtf16(to.path);
 99	str ~= Unicode.toUtf16(fn);
100	str ~= '\0';
101
102	MoveFileW(old.ptr, str.ptr);
103	return true;
104}
105
106// FILE //
107
108bool FileOpen(ref FilePlatformVars fileVars, ref string filename) {
109	wstring newString = Unicode.toUtf16(filename);
110	newString ~= '\0';
111	wchar[] foo = _ConvertFrameworkPath(newString);
112	fileVars.f = CreateFileW( foo.ptr, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null,OPEN_ALWAYS,0,null);
113
114	return (fileVars.f !is null);
115}
116
117bool FileCreate(ref FilePlatformVars fileVars, ref string filename) {
118	wstring newString = Unicode.toUtf16(filename);
119	newString ~= '\0';
120	wchar[] foo = _ConvertFrameworkPath(newString);
121	fileVars.f = CreateFileW( foo.ptr, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null,CREATE_ALWAYS,0,null);
122
123	return (fileVars.f !is null);
124}
125
126void FileClose(ref FilePlatformVars fileVars) {
127	CloseHandle(fileVars.f);
128}
129
130void FileGetSize(ref FilePlatformVars fileVars, ref ulong length) {
131	GetFileSizeEx(fileVars.f, cast(PLARGE_INTEGER)&length);
132}
133
134void FileRewindAll(ref FilePlatformVars fileVars) {
135	SetFilePointer(fileVars.f, 0, null, FILE_BEGIN);
136}
137
138void FileRewind(ref FilePlatformVars fileVars, ulong amount) {
139	long theamount = cast(long)amount;
140	theamount = -theamount;
141
142	int low_word = cast(int)(theamount & 0xFFFFFFFF);
143	int high_word = cast(int)(theamount >> 32);
144
145	SetFilePointer(fileVars.f, low_word, &high_word, FILE_CURRENT);
146}
147
148void FileSkipAll(ref FilePlatformVars fileVars) {
149	SetFilePointer(fileVars.f, 0, null, FILE_END);
150}
151
152void FileSkip(ref FilePlatformVars fileVars, ulong amount) {
153	long theamount = cast(long)amount;
154
155	int low_word = cast(int)(theamount & 0xFFFFFFFF);
156	int high_word = cast(int)(theamount >> 32);
157
158	SetFilePointer(fileVars.f, low_word, &high_word, FILE_CURRENT);
159}
160
161void FileRead(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
162	DWORD ret;
163	ulong total_bytes = 0;
164
165	ubyte* curbuffer = buffer;
166
167	while (len > 0xFFFFFFFF) {
168		ReadFile(fileVars.f, curbuffer, 0xFFFFFFFF, &ret, null);
169
170		total_bytes += ret;
171		len -= 0xFFFFFFFF;
172		curbuffer += 0xFFFFFFFF;
173	}
174
175	ReadFile(fileVars.f, curbuffer, cast(uint)len, &ret, null);
176	total_bytes += ret;
177}
178
179void FileWrite(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
180	DWORD ret;
181	ulong total_bytes = 0;
182
183	ubyte* curbuffer = buffer;
184
185	// we are given a long for length, windows only has an int function
186	while (len > 0xFFFFFFFF) {
187		WriteFile(fileVars.f, curbuffer, 0xFFFFFFFF, &ret, null);
188
189		total_bytes += ret;
190		len -= 0xFFFFFFFF;
191		curbuffer += 0xFFFFFFFF;
192	}
193
194	WriteFile(fileVars.f, curbuffer, cast(uint)len, &ret, null);
195	total_bytes += ret;
196}
197
198void FileAppend(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
199/*	Console.putln("append");
200	ulong pos = file.getPosition();
201
202	file.skip();
203
204	DWORD ret;
205	ulong total_bytes = 0;
206
207	ubyte* curbuffer = buffer;
208
209	// we are given a long for length, windows only has an int function
210	while (len > 0xFFFFFFFF)
211	{
212		WriteFile(fileVars.f, curbuffer, 0xFFFFFFFF, &ret, null);
213
214		total_bytes += ret;
215		len -= 0xFFFFFFFF;
216		curbuffer += 0xFFFFFFFF;
217	}
218
219	WriteFile(fileVars.f, curbuffer, cast(uint)len, &ret, null);
220	total_bytes += ret;
221
222	file.rewind();
223	file.skip(pos);*/
224}