#import "" as namespace

This commit is contained in:
Ginger Bill
2016-09-14 19:35:13 +01:00
parent bb109b47d6
commit 79f575ae8e
12 changed files with 322 additions and 275 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
#import "print.odin"
#import "print.odin" as _
test_proc :: proc() {
println("Hello?")
-106
View File
@@ -1,106 +0,0 @@
#import "win32.odin"
File :: type struct {
Handle :: type HANDLE
handle: Handle
}
file_open :: proc(name: string) -> (File, bool) {
buf: [300]byte
_ = copy(buf[:], name as []byte)
f := File{CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null)}
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
return f, success
}
file_create :: proc(name: string) -> (File, bool) {
buf: [300]byte
_ = copy(buf[:], name as []byte)
f := File{
handle = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null),
}
success := f.handle != INVALID_HANDLE_VALUE as File.Handle
return f, success
}
file_close :: proc(f: ^File) {
CloseHandle(f.handle)
}
file_write :: proc(f: ^File, buf: []byte) -> bool {
bytes_written: i32
return WriteFile(f.handle, ^buf[0], buf.count as i32, ^bytes_written, null) != 0
}
File_Standard :: type enum {
INPUT,
OUTPUT,
ERROR,
COUNT,
}
__std_files := __set_file_standards();
__set_file_standards :: proc() -> [File_Standard.COUNT as int]File {
return [File_Standard.COUNT as int]File{
File{handle = GetStdHandle(STD_INPUT_HANDLE)},
File{handle = GetStdHandle(STD_OUTPUT_HANDLE)},
File{handle = GetStdHandle(STD_ERROR_HANDLE)},
}
}
file_get_standard :: proc(std: File_Standard) -> ^File {
return ^__std_files[std]
}
read_entire_file :: proc(name: string) -> (string, bool) {
buf: [300]byte
_ = copy(buf[:], name as []byte)
c_string := ^buf[0]
f, file_ok := file_open(name)
if !file_ok {
return "", false
}
defer file_close(^f)
length: i64
file_size_ok := GetFileSizeEx(f.handle as HANDLE, ^length) != 0
if !file_size_ok {
return "", false
}
data := new_slice(u8, length)
if ^data[0] == null {
return "", false
}
single_read_length: i32
total_read: i64
for total_read < length {
remaining := length - total_read
to_read: u32
MAX :: 0x7fffffff
if remaining <= MAX {
to_read = remaining as u32
} else {
to_read = MAX
}
ReadFile(f.handle as HANDLE, ^data[total_read], to_read, ^single_read_length, null)
if single_read_length <= 0 {
free(^data[0])
return "", false
}
total_read += single_read_length as i64
}
return data as string, true
}
+10 -11
View File
@@ -1,6 +1,5 @@
#import "runtime.odin"
#import "win32.odin"
#import "file.odin"
#import "runtime.odin" as _
#import "os.odin" as os
print_byte_buffer :: proc(buf: ^[]byte, b: []byte) {
if buf.count < buf.capacity {
@@ -555,31 +554,31 @@ print_to_buffer :: proc(buf: ^[]byte, fmt: string, args: ..any) {
PRINT_BUF_SIZE :: 1<<12
print_to_file :: proc(f: ^File, fmt: string, args: ..any) {
print_to_file :: proc(f: ^os.File, fmt: string, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
print_to_buffer(^buf, fmt, ..args)
file_write(f, buf)
os.write(f, buf)
}
println_to_file :: proc(f: ^File, fmt: string, args: ..any) {
println_to_file :: proc(f: ^os.File, fmt: string, args: ..any) {
data: [PRINT_BUF_SIZE]byte
buf := data[:0]
print_to_buffer(^buf, fmt, ..args)
print_nl_to_buffer(^buf)
file_write(f, buf)
os.write(f, buf)
}
print :: proc(fmt: string, args: ..any) {
print_to_file(file_get_standard(File_Standard.OUTPUT), fmt, ..args)
print_to_file(os.file_get_standard(os.File_Standard.OUTPUT), fmt, ..args)
}
print_err :: proc(fmt: string, args: ..any) {
print_to_file(file_get_standard(File_Standard.ERROR), fmt, ..args)
print_to_file(os.file_get_standard(os.File_Standard.ERROR), fmt, ..args)
}
println :: proc(fmt: string, args: ..any) {
println_to_file(file_get_standard(File_Standard.OUTPUT), fmt, ..args)
println_to_file(os.file_get_standard(os.File_Standard.OUTPUT), fmt, ..args)
}
println_err :: proc(fmt: string, args: ..any) {
println_to_file(file_get_standard(File_Standard.ERROR), fmt, ..args)
println_to_file(os.file_get_standard(os.File_Standard.ERROR), fmt, ..args)
}
+7 -7
View File
@@ -1,9 +1,9 @@
#global_scope
// TODO(bill): Remove #import in runtime.odin
#import "win32.odin"
#import "file.odin"
#import "print.odin"
#import "win32.odin" as win32
#import "os.odin" as os
#import "print.odin" as _
// IMPORTANT NOTE(bill): Do not change the order of any of this data
// The compiler relies upon this _exact_ order
@@ -84,15 +84,15 @@ fmuladd_f32 :: proc(a, b, c: f32) -> f32 #foreign "llvm.fmuladd.f32"
fmuladd_f64 :: proc(a, b, c: f64) -> f64 #foreign "llvm.fmuladd.f64"
heap_alloc :: proc(len: int) -> rawptr {
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len)
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, len)
}
heap_free :: proc(ptr: rawptr) {
_ = HeapFree(GetProcessHeap(), 0, ptr)
_ = win32.HeapFree(win32.GetProcessHeap(), 0, ptr)
}
current_thread_id :: proc() -> int {
id := GetCurrentThreadId()
id := win32.GetCurrentThreadId()
return id as int
}
@@ -171,7 +171,7 @@ __string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >
__assert :: proc(msg: string) {
file_write(file_get_standard(File_Standard.ERROR), msg as []byte)
os.write(os.file_get_standard(os.File_Standard.ERROR), msg as []byte)
__debug_trap()
}