2
0
Эх сурвалжийг харах

Merge pull request #34 from bendl/master

Windows build - Fix file read/write and windows includes
Marco Bambini 8 жил өмнө
parent
commit
d7dde1f90b

+ 6 - 1
Makefile

@@ -13,7 +13,12 @@ SRC = $(wildcard $(COMPILER_DIR)*.c) \
 INCLUDE = -I$(COMPILER_DIR) -I$(RUNTIME_DIR) -I$(SHARED_DIR) -I$(UTILS_DIR)
 INCLUDE = -I$(COMPILER_DIR) -I$(RUNTIME_DIR) -I$(SHARED_DIR) -I$(UTILS_DIR)
 CFLAGS = $(INCLUDE) -O2 -std=gnu99
 CFLAGS = $(INCLUDE) -O2 -std=gnu99
 OBJ = $(SRC:.c=.o)
 OBJ = $(SRC:.c=.o)
-LDFLAGS = -lm
+
+ifeq ($(OS),Windows_NT)
+	LDFLAGS = -lm -lShlwapi
+else
+	LDFLAGS = -lm
+endif
 
 
 all: unittest gravity
 all: unittest gravity
 
 

+ 13 - 5
src/utils/gravity_utils.c

@@ -22,6 +22,8 @@
 #endif
 #endif
 #if defined(_WIN32)
 #if defined(_WIN32)
 #include <windows.h>
 #include <windows.h>
+#include <Shlwapi.h>
+#include <tchar.h>
 #endif
 #endif
 
 
 #include "gravity_utils.h"
 #include "gravity_utils.h"
@@ -36,7 +38,7 @@ nanotime_t nanotime (void) {
 	
 	
 	#if defined(_WIN32)
 	#if defined(_WIN32)
 	static LARGE_INTEGER	win_frequency;
 	static LARGE_INTEGER	win_frequency;
-	static BOOL				flag = QueryPerformanceFrequency(&s_frequency);
+	QueryPerformanceFrequency(&win_frequency);
 	LARGE_INTEGER			t;
 	LARGE_INTEGER			t;
 	
 	
 	if (!QueryPerformanceCounter(&t)) return 0;
 	if (!QueryPerformanceCounter(&t)) return 0;
@@ -115,9 +117,9 @@ const char *file_read(const char *path, size_t *len) {
 	buffer[fsize] = 0;
 	buffer[fsize] = 0;
 	
 	
 	fsize2 = read(fd, buffer, fsize);
 	fsize2 = read(fd, buffer, fsize);
-	if (fsize != fsize2) goto abort_read;
+	if (fsize2 == -1) goto abort_read;
 	
 	
-	if (len) *len = fsize;
+	if (len) *len = fsize2;
 	close(fd);
 	close(fd);
 	return (const char *)buffer;
 	return (const char *)buffer;
 	
 	
@@ -172,7 +174,13 @@ const char *file_buildpath (const char *filename, const char *dirpath) {
 }
 }
 
 
 bool file_write (const char *path, const char *buffer, size_t len) {
 bool file_write (const char *path, const char *buffer, size_t len) {
-	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // RW for owner, R for group, R for others
+	// RW for owner, R for group, R for others
+	#ifdef _WIN32
+	mode_t mode = _S_IWRITE;
+	#else
+	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+	#endif
+	
 	int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
 	int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
 	if (fd < 0) return false;
 	if (fd < 0) return false;
 	
 	
@@ -229,7 +237,7 @@ const char *directory_read (DIRREF ref) {
 		#ifdef WIN32
 		#ifdef WIN32
 		WIN32_FIND_DATA findData;
 		WIN32_FIND_DATA findData;
 		if (FindNextFile(ref, &findData) == 0) {
 		if (FindNextFile(ref, &findData) == 0) {
-			FindClose(dref);
+			FindClose(ref);
 			return NULL;
 			return NULL;
 		}
 		}
 		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
 		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;

+ 1 - 0
src/utils/gravity_utils.h

@@ -13,6 +13,7 @@
 #include <stdbool.h>
 #include <stdbool.h>
 
 
 #if defined(_WIN32)
 #if defined(_WIN32)
+#include <windows.h>
 typedef unsigned __int64 nanotime_t;
 typedef unsigned __int64 nanotime_t;
 #define DIRREF			HANDLE
 #define DIRREF			HANDLE
 #else
 #else