156 lines
3.9 KiB
Plaintext
156 lines
3.9 KiB
Plaintext
import string;
|
|
import vector;
|
|
import mem:*
|
|
|
|
ext fun printf(fmt_str: *char, ...): int
|
|
ext fun fprintf(file: *void, format: *char, ...): int
|
|
ext fun fflush(file: int): int
|
|
ext var stderr: *void
|
|
ext fun fgets(buff: *char, size: int, file: *void): *char
|
|
ext var stdin: *void
|
|
|
|
// dead simple stdin
|
|
fun get_line(prompt: string::string, line_size: int): string::string {
|
|
print(prompt)
|
|
return get_line(line_size)
|
|
}
|
|
fun get_line(line_size: int): string::string {
|
|
var buff = new<char>(line_size)
|
|
fgets(buff, line_size, stdin)
|
|
var to_ret = string::string(buff)
|
|
delete(buff)
|
|
return to_ret.slice(0,-2) // remove '\n'
|
|
}
|
|
fun printlnerr<T>(toPrint: T) : void {
|
|
printerr(toPrint)
|
|
printerr("\n")
|
|
}
|
|
fun printlnerr()
|
|
printerr("\n")
|
|
fun printerr(toPrint: string::string) : void {
|
|
var charArr = toPrint.toCharArray()
|
|
printerr(charArr)
|
|
delete(charArr)
|
|
}
|
|
fun printerr(toPrint: *char) : void {
|
|
fprintf(stderr, "%s", toPrint)
|
|
// stderr is already flushed
|
|
}
|
|
|
|
fun println<T>(toPrint: T) : void {
|
|
print(toPrint)
|
|
print("\n")
|
|
}
|
|
fun print(toPrint: *char) : void {
|
|
printf("%s", toPrint)
|
|
fflush(0)
|
|
}
|
|
fun println()
|
|
print("\n")
|
|
|
|
fun print(toPrint: char) : void
|
|
print(string::string(toPrint))
|
|
|
|
fun print(toPrint: string::string) : void {
|
|
var charArr = toPrint.toCharArray()
|
|
print(charArr)
|
|
delete(charArr)
|
|
}
|
|
fun print(toPrint: bool) {
|
|
if (toPrint)
|
|
print("true")
|
|
else
|
|
print("false")
|
|
}
|
|
fun print<T>(toPrint: T): void
|
|
print(string::to_string(toPrint))
|
|
|
|
// Ok, just some DEAD simple file io for now
|
|
ext fun fopen(path: *char, mode: *char): *void
|
|
ext fun fclose(file: *void): int
|
|
// fprintf is already used for stderr above
|
|
ext fun ftell(file: *void): long
|
|
ext fun fseek(file: *void, offset: long, whence: int): int
|
|
ext fun fread(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong
|
|
ext fun fwrite(ptr: *void, size: ulong, nmemb: ulong, file: *void): ulong
|
|
fun file_exists(path: string::string): bool {
|
|
var char_path = path.toCharArray()
|
|
defer delete(char_path)
|
|
var fp = fopen(char_path, "r")
|
|
if (fp) {
|
|
fclose(fp)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
fun read_file(path: string::string): string::string {
|
|
if (!file_exists(path))
|
|
return string::string()
|
|
var toRet.construct(read_file_binary(path)): string::string
|
|
return toRet
|
|
}
|
|
fun write_file(path: string::string, data: string::string) {
|
|
var char_path = path.toCharArray()
|
|
defer delete(char_path)
|
|
var char_data = data.toCharArray()
|
|
defer delete(char_data)
|
|
var fp = fopen(char_path, "w")
|
|
fprintf(fp, "%s", char_data)
|
|
fclose(fp)
|
|
}
|
|
fun read_file_binary(path: string::string): vector::vector<char> {
|
|
var char_path = path.toCharArray()
|
|
defer delete(char_path)
|
|
var fp = fopen(char_path, "r")
|
|
fseek(fp, (0) cast long, 2)// fseek(fp, 0L, SEEK_END)
|
|
var size = ftell(fp)
|
|
fseek(fp, (0) cast long, 0)//fseek(fp, 0L, SEEK_SET)
|
|
var data = new<char>((size+1) cast int)
|
|
var readSize = fread((data) cast *void, (1) cast ulong, (size) cast ulong, fp)
|
|
fclose(fp)
|
|
data[readSize] = 0
|
|
var toRet.construct((size) cast int): vector::vector<char>
|
|
for (var i = 0; i < size; i++;)
|
|
toRet.add(data[i])
|
|
delete(data)
|
|
return toRet
|
|
}
|
|
fun write_file_binary(path: string::string, vdata: vector::vector<char>) {
|
|
var char_path = path.toCharArray()
|
|
defer delete(char_path)
|
|
var data = vdata.getBackingMemory()
|
|
var size = vdata.size
|
|
var fp = fopen(char_path, "w")
|
|
fwrite((data) cast *void, (1) cast ulong, (size) cast ulong, fp)
|
|
fclose(fp)
|
|
}
|
|
|
|
fun BoldRed(): void{
|
|
print("\033[1m\033[31m");
|
|
}
|
|
|
|
fun BoldGreen(): void{
|
|
print("\033[1m\033[32m");
|
|
}
|
|
|
|
fun BoldYellow(): void{
|
|
print("\033[1m\033[33m");
|
|
}
|
|
|
|
fun BoldBlue(): void{
|
|
print("\033[1m\033[34m");
|
|
}
|
|
|
|
fun BoldMagenta(): void{
|
|
print("\033[1m\033[35m");
|
|
}
|
|
|
|
fun BoldCyan(): void{
|
|
print("\033[1m\033[36m");
|
|
}
|
|
|
|
fun Reset(): void{
|
|
print("\033[0m");
|
|
}
|
|
|