No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

93 líneas
3.1 KiB

  1. #ifndef OS_IMPL_WIN32_CPP
  2. #define OS_IMPL_WIN32_CPP
  3. #include "Windows.h"
  4. #include "os.h"
  5. void *os_alloc(size_t commitSize) {
  6. return VirtualAlloc(NULL, commitSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  7. }
  8. void os_reserve(void *ptr) {
  9. }
  10. void os_decommit(void *ptr) {
  11. }
  12. void os_free(void *ptr, size_t size) {
  13. VirtualFree(ptr, NULL, MEM_RELEASE);
  14. }
  15. string os_readEntireFile(Arena *arena, string filename) {
  16. string result = {0};
  17. HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  18. if (fileHandle != INVALID_HANDLE_VALUE) {
  19. LARGE_INTEGER fileSize;
  20. if (GetFileSizeEx(fileHandle, &fileSize)) {
  21. string readfile = PushString(arena, (size_t)fileSize.QuadPart);
  22. if (readfile.str) {
  23. DWORD bytesRead;
  24. if (ReadFile(fileHandle, readfile.str, (DWORD)fileSize.QuadPart, &bytesRead, NULL) && (fileSize.QuadPart == bytesRead)) {
  25. result = readfile;
  26. }
  27. }
  28. }
  29. CloseHandle(fileHandle);
  30. }
  31. return result;
  32. }
  33. bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
  34. bool result = false;
  35. HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
  36. if (fileHandle != INVALID_HANDLE_VALUE) {
  37. DWORD bytesWritten;
  38. if (WriteFile(fileHandle, contents, (DWORD)contentsLength, &bytesWritten, NULL)) {
  39. // file written successfully
  40. result = bytesWritten == contentsLength;
  41. }
  42. CloseHandle(fileHandle);
  43. }
  44. return result;
  45. }
  46. bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
  47. bool result = false;
  48. HANDLE fileHandle = CreateFileA(cstring(arena, filename), FILE_APPEND_DATA | FILE_GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  49. if (fileHandle != INVALID_HANDLE_VALUE) {
  50. DWORD bytesWritten;
  51. DWORD position = SetFilePointer(fileHandle, 0, NULL, FILE_END);
  52. if (WriteFile(fileHandle, contents, (DWORD)contentsLength, &bytesWritten, NULL)) {
  53. // file written successfully
  54. result = bytesWritten == contentsLength;
  55. }
  56. CloseHandle(fileHandle);
  57. }
  58. return result;
  59. }
  60. function void os_log(LogTarget target, const char *fmt, va_list argList) {
  61. Scratch scratch = scratchStart(0, 0);
  62. string result = strPrintfv(scratch.arena, fmt, argList);
  63. DWORD done;
  64. HANDLE stdHandle;
  65. switch (target) {
  66. case LogTarget_stdin:
  67. stdHandle = GetStdHandle(STD_INPUT_HANDLE);
  68. break;
  69. case LogTarget_stdout:
  70. stdHandle = GetStdHandle(STD_ERROR_HANDLE);
  71. break;
  72. case LogTarget_stderr:
  73. stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  74. break;
  75. default:
  76. stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  77. break;
  78. }
  79. WriteFile(stdHandle, result.str, (DWORD)result.length, &done, 0);
  80. scratchEnd(scratch);
  81. }
  82. #endif