/platform/unix/scaffold/file.d

http://github.com/wilkie/djehuty · D · 125 lines · 78 code · 36 blank · 11 comment · 4 complexity · 9509082ff63fd397a1bf2dd47e4e4cac MD5 · raw file

  1. /*
  2. * file.d
  3. *
  4. * This Scaffold holds the File implementations for the Linux platform
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module scaffold.file;
  10. import platform.unix.common;
  11. import platform.vars.file;
  12. import core.definitions;
  13. import core.string;
  14. import core.main;
  15. import io.file;
  16. import io.console;
  17. // FILE //
  18. bool FileOpen(ref FilePlatformVars fileVars, ref string filename) {
  19. string fn = filename.dup;
  20. fn ~= '\0';
  21. fileVars.file = fopen(fn.ptr, "r+b");
  22. return (fileVars.file !is null);
  23. }
  24. bool FileCreate(ref FilePlatformVars fileVars, ref string filename) {
  25. string fn = filename.dup;
  26. fn ~= '\0';
  27. fileVars.file = fopen(fn.ptr, "w+b");
  28. return (fileVars.file !is null);
  29. }
  30. void FileClose(ref FilePlatformVars fileVars) {
  31. fclose(fileVars.file);
  32. }
  33. void FileGetSize(ref FilePlatformVars fileVars, ref ulong length) {
  34. fseek(fileVars.file, 0, SEEK_END);
  35. length = ftell(fileVars.file);
  36. //writefln(length);
  37. fseek(fileVars.file, 0, SEEK_SET);
  38. }
  39. void FileRewindAll(ref FilePlatformVars fileVars) {
  40. fseek(fileVars.file, 0, SEEK_SET);
  41. }
  42. void FileRewind(ref FilePlatformVars fileVars, ulong amount) {
  43. fseek(fileVars.file, -cast(long)amount, SEEK_CUR);
  44. }
  45. void FileSkipAll(ref FilePlatformVars fileVars) {
  46. fseek(fileVars.file, 0, SEEK_END);
  47. }
  48. void FileSkip(ref FilePlatformVars fileVars, ulong amount) {
  49. fseek(fileVars.file, amount, SEEK_CUR);
  50. }
  51. void FileRead(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
  52. fread(buffer, 1, len, fileVars.file);
  53. }
  54. void FileWrite(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
  55. fwrite(buffer, 1, len, fileVars.file);
  56. }
  57. void FileAppend(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
  58. }
  59. bool FileMove(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
  60. string exec = "mv " ~ oldFullPath ~ " " ~ newFullPath ~ "\0";
  61. system(exec.ptr);
  62. return true;
  63. }
  64. bool FileCopy(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
  65. string exec = "cp " ~ oldFullPath ~ " " ~ newFullPath ~ "\0";
  66. system(exec.ptr);
  67. return true;
  68. }
  69. bool FileRename(ref FilePlatformVars fileVars, ref string path, ref string newName) {
  70. string old = path.dup;
  71. old ~= '\0';
  72. string str;
  73. foreach_reverse(int i, chr; path) {
  74. if (chr == '/' && i < path.length - 1) {
  75. // truncate
  76. str = path[0..i].dup;
  77. break;
  78. }
  79. }
  80. if (str is null) { return false; }
  81. str ~= newName ~ "\0";
  82. rename(old.ptr, str.ptr);
  83. return true;
  84. }
  85. void FileRemove(ref FilePlatformVars fileVars, string fullPath) {
  86. string fn = fullPath.dup;
  87. fn ~= '\0';
  88. remove(fn.ptr);
  89. }