]> pd.if.org Git - pdclib/blobdiff - platform/win32/functions/_PDCLIB/_PDCLIB_fileops.c
PDCLIB-8: First phase of intergation of new I/O backend system (with minimal
[pdclib] / platform / win32 / functions / _PDCLIB / _PDCLIB_fileops.c
diff --git a/platform/win32/functions/_PDCLIB/_PDCLIB_fileops.c b/platform/win32/functions/_PDCLIB/_PDCLIB_fileops.c
new file mode 100644 (file)
index 0000000..1d48e11
--- /dev/null
@@ -0,0 +1,87 @@
+/* _PDCLIB_fileops\r
+\r
+   This file is part of the Public Domain C Library (PDCLib).\r
+   Permission is granted to use, modify, and / or redistribute at will.\r
+*/\r
+\r
+#ifndef REGTEST\r
+#include <stdio.h>\r
+#include <stdint.h>\r
+#include <_PDCLIB_glue.h>\r
+#include <errno.h>\r
+#include <windows.h>\r
+\r
+#if _PDCLIB_C_MIN(2011)\r
+_Static_assert(SEEK_SET == FILE_BEGIN, "SEEK_SET is incorrect");\r
+_Static_assert(SEEK_CUR == FILE_CURRENT, "SEEK_CUR is incorrect");\r
+_Static_assert(SEEK_END == FILE_END, "SEEK_END is incorrect");\r
+#endif\r
+\r
+void _PDCLIB_w32errno(void);\r
+\r
+static bool readf( _PDCLIB_fd_t self, void * buf, size_t length, \r
+                   size_t * numBytesRead )\r
+{\r
+    DWORD dwLen = length > INT32_MAX ? INT32_MAX : length;\r
+    DWORD dwBytesRead;\r
+    if(ReadFile(self.pointer, buf, dwLen, &dwBytesRead, NULL)) {\r
+        *numBytesRead = dwBytesRead;\r
+        return true;\r
+    } else {\r
+        _PDCLIB_w32errno();\r
+        return false;\r
+    }\r
+}\r
+\r
+static bool writef( _PDCLIB_fd_t self, const void * buf, size_t length, \r
+                   size_t * numBytesWritten )\r
+{\r
+    DWORD dwLen = length > INT32_MAX ? INT32_MAX : length;\r
+    DWORD dwBytesWritten;\r
+\r
+    if(WriteFile(self.pointer, buf, dwLen, &dwBytesWritten, NULL)) {\r
+        *numBytesWritten = dwBytesWritten;\r
+        return true;\r
+    } else {\r
+        _PDCLIB_w32errno();\r
+        return false;\r
+    }\r
+}\r
+static bool seekf( _PDCLIB_fd_t self, int_fast64_t offset, int whence,\r
+    int_fast64_t* newPos )\r
+{\r
+    LARGE_INTEGER liOffset;\r
+    liOffset.QuadPart = offset;\r
+    if(!SetFilePointerEx( self.pointer, liOffset, &liOffset, whence )) {\r
+        _PDCLIB_w32errno();\r
+        return false;\r
+    }\r
+\r
+    *newPos = liOffset.QuadPart;\r
+    return true;\r
+}\r
+\r
+static void closef( _PDCLIB_fd_t self )\r
+{\r
+    CloseHandle( self.pointer );\r
+}\r
+\r
+const _PDCLIB_fileops_t _PDCLIB_fileops = {\r
+    .read  = readf,\r
+    .write = writef,\r
+    .seek  = seekf,\r
+    .close = closef,\r
+};\r
+\r
+#endif\r
+\r
+#ifdef TEST\r
+#include <_PDCLIB_test.h>\r
+\r
+int main( void )\r
+{\r
+    // Tested by stdio test cases\r
+    return TEST_RESULTS;\r
+}\r
+\r
+#endif\r