/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. module scaffold.file;
  10. import platform.win.common;
  11. import platform.win.main;
  12. import platform.vars.file;
  13. import scaffold.directory;
  14. import core.stream;
  15. import core.string;
  16. import core.main;
  17. import core.definitions;
  18. import core.unicode;
  19. import io.console;
  20. import io.directory;
  21. import io.file;
  22. // OPERATIONS //
  23. bool FileMove(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
  24. wstring oldPath = Unicode.toUtf16(oldFullPath);
  25. oldPath ~= '\0';
  26. wstring newPath = Unicode.toUtf16(newFullPath);
  27. newPath ~= '\0';
  28. MoveFileW(oldPath.ptr, newPath.ptr);
  29. return true;
  30. }
  31. bool FileCopy(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
  32. wstring oldPath = Unicode.toUtf16(oldFullPath);
  33. oldPath ~= '\0';
  34. wstring newPath = Unicode.toUtf16(newFullPath);
  35. newPath ~= '\0';
  36. CopyFileW(oldPath.ptr, newPath.ptr, 0);
  37. return true;
  38. }
  39. bool FileRename(ref FilePlatformVars fileVars, ref string path, ref string newName) {
  40. wstring old = Unicode.toUtf16(path);
  41. old ~= '\0';
  42. string str;
  43. foreach_reverse(int i, chr; path)
  44. {
  45. if (chr == '/')
  46. {
  47. // truncate
  48. str = path[0..i];
  49. break;
  50. }
  51. }
  52. if (str is null) { return false; }
  53. str ~= newName;
  54. str ~= '\0';
  55. wstring strw = Unicode.toUtf16(str);
  56. MoveFileW(old.ptr, strw.ptr);
  57. return true;
  58. }
  59. bool FileMove(ref string from, ref Directory to) {
  60. wstring old = Unicode.toUtf16(from);
  61. old ~= '\0';
  62. string fn;
  63. foreach_reverse(int i, chr; from)
  64. {
  65. if (chr == '/')
  66. {
  67. // truncate (include the slash)
  68. fn = from[i..from.length];
  69. break;
  70. }
  71. }
  72. if (fn is null) { return false; }
  73. wstring str = Unicode.toUtf16(to.path);
  74. str ~= Unicode.toUtf16(fn);
  75. str ~= '\0';
  76. MoveFileW(old.ptr, str.ptr);
  77. return true;
  78. }
  79. // FILE //
  80. bool FileOpen(ref FilePlatformVars fileVars, ref string filename) {
  81. wstring newString = Unicode.toUtf16(filename);
  82. newString ~= '\0';
  83. wchar[] foo = _ConvertFrameworkPath(newString);
  84. fileVars.f = CreateFileW( foo.ptr, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null,OPEN_ALWAYS,0,null);
  85. return (fileVars.f !is null);
  86. }
  87. bool FileCreate(ref FilePlatformVars fileVars, ref string filename) {
  88. wstring newString = Unicode.toUtf16(filename);
  89. newString ~= '\0';
  90. wchar[] foo = _ConvertFrameworkPath(newString);
  91. fileVars.f = CreateFileW( foo.ptr, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null,CREATE_ALWAYS,0,null);
  92. return (fileVars.f !is null);
  93. }
  94. void FileClose(ref FilePlatformVars fileVars) {
  95. CloseHandle(fileVars.f);
  96. }
  97. void FileGetSize(ref FilePlatformVars fileVars, ref ulong length) {
  98. GetFileSizeEx(fileVars.f, cast(PLARGE_INTEGER)&length);
  99. }
  100. void FileRewindAll(ref FilePlatformVars fileVars) {
  101. SetFilePointer(fileVars.f, 0, null, FILE_BEGIN);
  102. }
  103. void FileRewind(ref FilePlatformVars fileVars, ulong amount) {
  104. long theamount = cast(long)amount;
  105. theamount = -theamount;
  106. int low_word = cast(int)(theamount & 0xFFFFFFFF);
  107. int high_word = cast(int)(theamount >> 32);
  108. SetFilePointer(fileVars.f, low_word, &high_word, FILE_CURRENT);
  109. }
  110. void FileSkipAll(ref FilePlatformVars fileVars) {
  111. SetFilePointer(fileVars.f, 0, null, FILE_END);
  112. }
  113. void FileSkip(ref FilePlatformVars fileVars, ulong amount) {
  114. long theamount = cast(long)amount;
  115. int low_word = cast(int)(theamount & 0xFFFFFFFF);
  116. int high_word = cast(int)(theamount >> 32);
  117. SetFilePointer(fileVars.f, low_word, &high_word, FILE_CURRENT);
  118. }
  119. void FileRead(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
  120. DWORD ret;
  121. ulong total_bytes = 0;
  122. ubyte* curbuffer = buffer;
  123. while (len > 0xFFFFFFFF) {
  124. ReadFile(fileVars.f, curbuffer, 0xFFFFFFFF, &ret, null);
  125. total_bytes += ret;
  126. len -= 0xFFFFFFFF;
  127. curbuffer += 0xFFFFFFFF;
  128. }
  129. ReadFile(fileVars.f, curbuffer, cast(uint)len, &ret, null);
  130. total_bytes += ret;
  131. }
  132. void FileWrite(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
  133. DWORD ret;
  134. ulong total_bytes = 0;
  135. ubyte* curbuffer = buffer;
  136. // we are given a long for length, windows only has an int function
  137. while (len > 0xFFFFFFFF) {
  138. WriteFile(fileVars.f, curbuffer, 0xFFFFFFFF, &ret, null);
  139. total_bytes += ret;
  140. len -= 0xFFFFFFFF;
  141. curbuffer += 0xFFFFFFFF;
  142. }
  143. WriteFile(fileVars.f, curbuffer, cast(uint)len, &ret, null);
  144. total_bytes += ret;
  145. }
  146. void FileAppend(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
  147. /* Console.putln("append");
  148. ulong pos = file.getPosition();
  149. file.skip();
  150. DWORD ret;
  151. ulong total_bytes = 0;
  152. ubyte* curbuffer = buffer;
  153. // we are given a long for length, windows only has an int function
  154. while (len > 0xFFFFFFFF)
  155. {
  156. WriteFile(fileVars.f, curbuffer, 0xFFFFFFFF, &ret, null);
  157. total_bytes += ret;
  158. len -= 0xFFFFFFFF;
  159. curbuffer += 0xFFFFFFFF;
  160. }
  161. WriteFile(fileVars.f, curbuffer, cast(uint)len, &ret, null);
  162. total_bytes += ret;
  163. file.rewind();
  164. file.skip(pos);*/
  165. }