PageRenderTime 68ms CodeModel.GetById 30ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 10module scaffold.file;
 11
 12import platform.unix.common;
 13
 14import platform.vars.file;
 15
 16import core.definitions;
 17import core.string;
 18import core.main;
 19
 20import io.file;
 21import io.console;
 22
 23// FILE //
 24
 25bool FileOpen(ref FilePlatformVars fileVars, ref string filename) {
 26	string fn = filename.dup;
 27	fn ~= '\0';
 28
 29	fileVars.file = fopen(fn.ptr, "r+b");
 30
 31	return (fileVars.file !is null);
 32}
 33
 34bool FileCreate(ref FilePlatformVars fileVars, ref string filename) {
 35	string fn = filename.dup;
 36	fn ~= '\0';
 37
 38	fileVars.file = fopen(fn.ptr, "w+b");
 39
 40	return (fileVars.file !is null);
 41}
 42
 43void FileClose(ref FilePlatformVars fileVars) {
 44	fclose(fileVars.file);
 45}
 46
 47void FileGetSize(ref FilePlatformVars fileVars, ref ulong length) {
 48	fseek(fileVars.file, 0, SEEK_END);
 49
 50	length = ftell(fileVars.file);
 51
 52	//writefln(length);
 53
 54	fseek(fileVars.file, 0, SEEK_SET);
 55}
 56
 57void FileRewindAll(ref FilePlatformVars fileVars) {
 58	fseek(fileVars.file, 0, SEEK_SET);
 59}
 60
 61void FileRewind(ref FilePlatformVars fileVars, ulong amount) {
 62	fseek(fileVars.file, -cast(long)amount, SEEK_CUR);
 63}
 64
 65void FileSkipAll(ref FilePlatformVars fileVars) {
 66	fseek(fileVars.file, 0, SEEK_END);
 67}
 68
 69void FileSkip(ref FilePlatformVars fileVars, ulong amount) {
 70	fseek(fileVars.file, amount, SEEK_CUR);
 71}
 72
 73void FileRead(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
 74	fread(buffer, 1, len, fileVars.file);
 75}
 76
 77void FileWrite(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
 78	fwrite(buffer, 1, len, fileVars.file);
 79}
 80
 81void FileAppend(ref FilePlatformVars fileVars, ubyte* buffer, ulong len) {
 82}
 83
 84bool FileMove(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
 85	string exec = "mv " ~ oldFullPath ~ " " ~ newFullPath ~ "\0";
 86
 87	system(exec.ptr);
 88	return true;
 89}
 90
 91bool FileCopy(ref FilePlatformVars fileVars, string oldFullPath, string newFullPath) {
 92	string exec = "cp " ~ oldFullPath ~ " " ~ newFullPath ~ "\0";
 93
 94	system(exec.ptr);
 95	return true;
 96}
 97
 98bool FileRename(ref FilePlatformVars fileVars, ref string path, ref string newName) {
 99	string old = path.dup;
100	old ~= '\0';
101
102	string str;
103
104	foreach_reverse(int i, chr; path) {
105		if (chr == '/' && i < path.length - 1) {
106			// truncate
107			str = path[0..i].dup;
108			break;
109		}
110	}
111
112	if (str is null) { return false; }
113
114	str ~= newName ~ "\0";
115
116	rename(old.ptr, str.ptr);
117	return true;
118}
119
120void FileRemove(ref FilePlatformVars fileVars, string fullPath) {
121	string fn = fullPath.dup;
122	fn ~= '\0';
123
124	remove(fn.ptr);
125}