2015-07-03 18:34:46 -04:00
|
|
|
import string;
|
2015-08-21 11:03:10 -04:00
|
|
|
import vector;
|
2015-06-01 01:43:23 -04:00
|
|
|
import mem:*
|
2015-03-11 01:58:10 -04:00
|
|
|
|
2015-05-09 06:24:56 -04:00
|
|
|
fun println() : void {
|
2014-06-30 01:57:50 -07:00
|
|
|
print("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 01:43:23 -04:00
|
|
|
fun println<T>(toPrint: T) : void {
|
|
|
|
|
print(toPrint)
|
|
|
|
|
print("\n")
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun print<T>(toPrint: *T)
|
|
|
|
|
print((toPrint) cast ulong)
|
2016-01-11 23:41:09 -05:00
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
ext fun printf(fmt_str: *char, to_print: *char): int
|
|
|
|
|
ext fun fflush(file: int): int
|
2015-07-04 17:02:51 -04:00
|
|
|
fun print(toPrint: *char) : void {
|
2016-04-30 15:38:28 -04:00
|
|
|
printf("%s", toPrint)
|
|
|
|
|
fflush(0)
|
2015-06-08 21:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun print(toPrint: char) : void
|
|
|
|
|
print(string::string(toPrint))
|
2014-05-01 01:18:01 -04:00
|
|
|
|
2015-07-03 18:34:46 -04:00
|
|
|
fun print(toPrint: string::string) : void {
|
2015-06-01 01:43:23 -04:00
|
|
|
var charArr = toPrint.toCharArray()
|
|
|
|
|
defer delete(charArr)
|
|
|
|
|
print(charArr);
|
2015-03-11 01:58:10 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-27 18:06:02 -04:00
|
|
|
fun print(toPrint: bool): void {
|
|
|
|
|
if (toPrint)
|
|
|
|
|
print("true")
|
|
|
|
|
else
|
|
|
|
|
print("false")
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun print(toPrint: int): void
|
|
|
|
|
print(string::to_string(toPrint))
|
|
|
|
|
fun print(toPrint: ulong): void
|
|
|
|
|
print(string::to_string(toPrint))
|
2014-05-01 01:18:01 -04:00
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
ext fun sprintf(to_str: *char, format: *char, d: double)
|
|
|
|
|
fun print(toPrint: float)
|
|
|
|
|
print((toPrint) cast double)
|
2014-06-26 01:45:44 -07:00
|
|
|
|
2016-04-30 15:38:28 -04:00
|
|
|
fun print(toPrint: double) {
|
|
|
|
|
var int_str = new<char>((#sizeof<double>) cast int)
|
|
|
|
|
sprintf(int_str, "%f", toPrint)
|
|
|
|
|
print(int_str)
|
|
|
|
|
delete(int_str)
|
2014-07-18 08:52:15 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-03 18:34:46 -04:00
|
|
|
// Ok, just some DEAD simple file io for now
|
2016-04-30 15:38:28 -04:00
|
|
|
ext fun fopen(path: *char, mode: *char): *void
|
|
|
|
|
ext fun fclose(file: *void): int
|
|
|
|
|
ext fun fprintf(file: *void, format: *char, data: *char): int
|
|
|
|
|
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
|
2015-08-26 03:45:34 -04:00
|
|
|
fun file_exists(path: string::string): bool {
|
|
|
|
|
var char_path = path.toCharArray()
|
|
|
|
|
defer delete(char_path)
|
2016-04-30 15:38:28 -04:00
|
|
|
var fp = fopen(char_path, "r")
|
|
|
|
|
if (fp) {
|
|
|
|
|
fclose(fp)
|
|
|
|
|
return true
|
2015-08-26 03:45:34 -04:00
|
|
|
}
|
2016-04-30 15:38:28 -04:00
|
|
|
return false
|
2015-08-26 03:45:34 -04:00
|
|
|
}
|
2015-07-03 18:34:46 -04:00
|
|
|
fun read_file(path: string::string): string::string {
|
2016-02-05 05:11:02 -05:00
|
|
|
if (!file_exists(path))
|
|
|
|
|
return 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)
|
2016-04-30 15:38:28 -04:00
|
|
|
var fp = fopen(char_path, "w")
|
|
|
|
|
fprintf(fp, "%s", char_data)
|
|
|
|
|
fclose(fp)
|
2015-08-21 11:03:10 -04:00
|
|
|
}
|
|
|
|
|
fun read_file_binary(path: string::string): vector::vector<char> {
|
2015-07-03 18:34:46 -04:00
|
|
|
var char_path = path.toCharArray()
|
|
|
|
|
defer delete(char_path)
|
2016-04-30 15:38:28 -04:00
|
|
|
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>
|
2015-08-21 11:03:10 -04:00
|
|
|
for (var i = 0; i < size; i++;)
|
|
|
|
|
toRet.add(data[i])
|
2016-04-30 15:38:28 -04:00
|
|
|
delete(data)
|
2015-07-03 18:34:46 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
2015-08-21 11:03:10 -04:00
|
|
|
fun write_file_binary(path: string::string, vdata: vector::vector<char>) {
|
2015-08-11 01:07:16 -04:00
|
|
|
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
|
2016-04-30 15:38:28 -04:00
|
|
|
var fp = fopen(char_path, "w")
|
|
|
|
|
fwrite((data) cast *void, (1) cast ulong, (size) cast ulong, fp)
|
|
|
|
|
fclose(fp)
|
2015-08-11 01:07:16 -04:00
|
|
|
}
|
2015-07-03 18:34:46 -04:00
|
|
|
|
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{
|
2016-01-11 23:41:09 -05:00
|
|
|
print("\033[0m");
|
2015-08-28 18:06:08 -04:00
|
|
|
}
|
|
|
|
|
|