Last active
November 12, 2015 19:54
-
-
Save jhass/cbc509e8ed1025b22a70 to your computer and use it in GitHub Desktop.
Platform indepedent standard library for Crystal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lib LibC | |
# Common bindings go here | |
fun chdir(...) | |
fun printf(...) | |
fun wprintf(...) | |
fun atoi(...) | |
fun watoic(...) | |
end | |
ifdef windows | |
require "./windows_c_wrapper" | |
elsdef linux | |
require "./linux_c_wrapper" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lib LibC | |
# Platform specific bindings that might not exist on other platforms or | |
# are incompatible | |
fun linux_only(...) | |
fun different_signature(...) | |
end | |
module CWrapper | |
# Boilerplate could be reduced by macro usage, for example a macro method_missing | |
def self.stat(path) | |
LibC.stat(path) | |
end | |
def self.chdir(dir) | |
LibC.chdir(dir) | |
end | |
def self.atoi(string) | |
LibC.atoi(string) | |
end | |
def self.printf(format, args) | |
LibC.printf(format, args) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./lib_c" | |
class File | |
def self.stat(path) | |
CWrapper.stat(path) | |
end | |
end | |
class Dir | |
def self.chdir(dir) | |
CWrapper.chdir(dir) | |
end | |
end | |
class String | |
def to_i | |
Cwrapper.atoi(self) | |
end | |
end | |
module Kernel | |
def printf(format, args) | |
CWrapper.printf(format, args) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lib LibC | |
# Platform specific bindings that might not exist on other platforms or | |
# are incompatible | |
fun windows_only(...) | |
fun different_signature(...) | |
end | |
module CWrapper | |
# Boilerplate could be reduced by macro usage, for example a macro method_missing | |
def self.stat(path) | |
LibC.stat(path.to_utf16) | |
end | |
def self.chdir(dir) | |
LibC.chdir(dir.to_utf16) | |
end | |
def self.atoi(string) | |
LibC.watoi(string.to_utf16) | |
end | |
def self.printf(format, args) | |
LibC.wprintf(format.to_utf16, args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An application that needs to care about different semantics between two functions, like
atoi
andwatoi
on Windows, is already by the fact that it needs to differentiate these, platform dependent. It has two choices:LibC.atoi
instead ofString#to_i
orCWrapper.atoi
.