mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
avoid using do statement in odin codebase
This commit is contained in:
@@ -7,46 +7,51 @@ import "base:runtime"
|
|||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
read_from_clipboard :: proc(wnd_handle: win32.HWND, allocator: runtime.Allocator) -> (result: string)
|
read_from_clipboard :: proc(wnd_handle: win32.HWND, allocator: runtime.Allocator) -> (result: string, ok: win32.BOOL)
|
||||||
{
|
{
|
||||||
if !win32.IsClipboardFormatAvailable(win32.CF_TEXT) do return;
|
win32.IsClipboardFormatAvailable(win32.CF_TEXT) or_return;
|
||||||
|
|
||||||
if !win32.OpenClipboard(wnd_handle) do return;
|
win32.OpenClipboard(wnd_handle) or_return;
|
||||||
defer win32.CloseClipboard();
|
defer win32.CloseClipboard();
|
||||||
|
|
||||||
clipboard_data := win32.GetClipboardData(win32.CF_TEXT);
|
clipboard_data := win32.GetClipboardData(win32.CF_TEXT);
|
||||||
if clipboard_data != nil {
|
if clipboard_data != nil {
|
||||||
if cstr := cstring(win32.GlobalLock(win32.HGLOBAL(clipboard_data))); cstr != nil {
|
if cstr := cstring(win32.GlobalLock(win32.HGLOBAL(clipboard_data))); cstr != nil {
|
||||||
result = strings.clone_from_cstring(cstr, allocator);
|
result = strings.clone_from_cstring(cstr, allocator);
|
||||||
|
ok = true;
|
||||||
}
|
}
|
||||||
win32.GlobalUnlock(win32.HGLOBAL(clipboard_data));
|
win32.GlobalUnlock(win32.HGLOBAL(clipboard_data));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
write_to_clipboard :: proc(wnd_handle: win32.HWND, text: string)
|
write_to_clipboard :: proc(wnd_handle: win32.HWND, text: string) -> win32.BOOL
|
||||||
{
|
{
|
||||||
if !win32.OpenClipboard(wnd_handle) do return;
|
win32.OpenClipboard(wnd_handle) or_return;
|
||||||
defer win32.CloseClipboard();
|
defer win32.CloseClipboard();
|
||||||
win32.EmptyClipboard();
|
win32.EmptyClipboard();
|
||||||
|
|
||||||
h_mem := win32.HGLOBAL(win32.GlobalAlloc(win32.GMEM_MOVEABLE, len(text) + 1));
|
h_mem := win32.HGLOBAL(win32.GlobalAlloc(win32.GMEM_MOVEABLE, len(text) + 1));
|
||||||
if h_mem == nil do return;
|
if h_mem == nil {return false};
|
||||||
|
|
||||||
cstr_dst := cast([^]u8)win32.GlobalLock(h_mem);
|
cstr_dst := cast([^]u8)win32.GlobalLock(h_mem);
|
||||||
defer win32.GlobalUnlock(h_mem);
|
defer win32.GlobalUnlock(h_mem);
|
||||||
if cstr_dst == nil do return;
|
if cstr_dst == nil {return false};
|
||||||
|
|
||||||
mem.copy(rawptr(cstr_dst), raw_data(text), len(text));
|
mem.copy(rawptr(cstr_dst), raw_data(text), len(text));
|
||||||
cstr_dst[len(text)] = 0;
|
cstr_dst[len(text)] = 0;
|
||||||
|
|
||||||
win32.SetClipboardData(win32.CF_TEXT, win32.HANDLE(h_mem));
|
win32.SetClipboardData(win32.CF_TEXT, win32.HANDLE(h_mem));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
verify_win32_clipboard :: proc(t: ^testing.T) {
|
verify_win32_clipboard :: proc(t: ^testing.T) {
|
||||||
write_to_clipboard(nil, "Hello everynyan! OH MY GAH");
|
ok1 := write_to_clipboard(nil, "Hello everynyan! OH MY GAH");
|
||||||
clipboard_content := read_from_clipboard(nil, context.temp_allocator);
|
testing.expect_value(t, ok1, true);
|
||||||
|
|
||||||
|
clipboard_content, ok2 := read_from_clipboard(nil, context.temp_allocator);
|
||||||
|
testing.expect_value(t, ok2, true);
|
||||||
testing.expect_value(t, clipboard_content, "Hello everynyan! OH MY GAH");
|
testing.expect_value(t, clipboard_content, "Hello everynyan! OH MY GAH");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user