Files
kraken/stdlib/io.krak

185 lines
3.6 KiB
Plaintext
Raw Normal View History

import string;
2015-08-21 11:03:10 -04:00
import vector;
import mem:*
2015-04-04 01:32:40 -04:00
__if_comp__ __C__ simple_passthrough """
#include <stdio.h>
"""
fun println() : void {
print("\n");
}
fun println<T>(toPrint: T) : void {
print(toPrint)
print("\n")
}
fun print(toPrint: *char) : void {
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(toPrint = toPrint::) """
printf("%s", toPrint);
"""
}
return;
}
fun print(toPrint: char) : void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
printf("%c", toPrint);
"""
}
return;
}
fun print(toPrint: string::string) : void {
var charArr = toPrint.toCharArray()
defer delete(charArr)
print(charArr);
}
fun print(toPrint: bool): void {
if (toPrint)
print("true")
else
print("false")
return;
}
fun print(toPrint: int): void {
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(toPrint = toPrint::) """
printf("%d", toPrint);
"""
}
return;
}
fun print(toPrint: float): void {
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(toPrint = toPrint::) """
printf("%f", toPrint);
"""
}
return;
}
fun print(toPrint: double) : void{
__if_comp__ __C__ {
2015-04-04 01:32:40 -04:00
simple_passthrough(toPrint = toPrint::) """
printf("%f", toPrint);
"""
}
return;
}
// Ok, just some DEAD simple file io for now
fun file_exists(path: string::string): bool {
var char_path = path.toCharArray()
defer delete(char_path)
var result = false
__if_comp__ __C__ {
simple_passthrough(char_path:result:) """
bool result = false;
FILE *fp = fopen(char_path, "r");
if (fp) {
result = true;
fclose(fp);
}
"""
}
return result
}
fun read_file(path: string::string): string::string {
2015-08-21 11:03:10 -04:00
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)
__if_comp__ __C__ {
simple_passthrough(char_path,char_data::) """
FILE *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 data: *char
2015-08-21 11:03:10 -04:00
var size: int
__if_comp__ __C__ {
2015-08-21 11:03:10 -04:00
simple_passthrough(char_path = char_path:data = data, size = size:) """
FILE *fp = fopen(char_path, "r");
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char *data = malloc(size+1);
size_t readSize = fread(data, 1, size, fp);
data[readSize] = 0;
fclose(fp);
"""
}
2015-08-21 11:03:10 -04:00
var toRet.construct(size): vector::vector<char>
for (var i = 0; i < size; i++;)
toRet.add(data[i])
__if_comp__ __C__ {
simple_passthrough(data = data::) """
free(data)
"""
}
return toRet
}
2015-08-21 11:03:10 -04:00
fun write_file_binary(path: string::string, vdata: vector::vector<char>) {
var char_path = path.toCharArray()
defer delete(char_path)
2015-08-21 11:03:10 -04:00
var data = vdata.getBackingMemory()
var size = vdata.size
__if_comp__ __C__ {
2015-08-21 11:03:10 -04:00
simple_passthrough(char_path, data, size::) """
FILE *fp = fopen(char_path, "w");
2015-08-21 11:03:10 -04:00
fwrite(data, 1, size, fp);
fclose(fp);
"""
}
}
2015-08-28 18:06:08 -04:00
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");
}