Added primitive serilization
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import string;
|
||||
import vector;
|
||||
import mem:*
|
||||
|
||||
__if_comp__ __C__ simple_passthrough """
|
||||
@@ -75,27 +76,7 @@ fun print(toPrint: double) : void{
|
||||
|
||||
// Ok, just some DEAD simple file io for now
|
||||
fun read_file(path: string::string): string::string {
|
||||
var char_path = path.toCharArray()
|
||||
defer delete(char_path)
|
||||
var data: *char
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(char_path = char_path:data = data:) """
|
||||
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);
|
||||
"""
|
||||
}
|
||||
var toRet = string::string(data)
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(data = data::) """
|
||||
free(data)
|
||||
"""
|
||||
}
|
||||
var toRet.construct(read_file_binary(path)): string::string
|
||||
return toRet
|
||||
}
|
||||
fun write_file(path: string::string, data: string::string) {
|
||||
@@ -111,4 +92,45 @@ fun write_file(path: string::string, data: string::string) {
|
||||
"""
|
||||
}
|
||||
}
|
||||
fun read_file_binary(path: string::string): vector::vector<char> {
|
||||
var char_path = path.toCharArray()
|
||||
defer delete(char_path)
|
||||
var data: *char
|
||||
var size: int
|
||||
__if_comp__ __C__ {
|
||||
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);
|
||||
"""
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(char_path, data, size::) """
|
||||
FILE *fp = fopen(char_path, "w");
|
||||
fwrite(data, 1, size, fp);
|
||||
fclose(fp);
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ obj map<T,U> (Object) {
|
||||
var key_loc = keys.find(key)
|
||||
if (key_loc == -1) {
|
||||
io::println("trying to access nonexistant key-value!")
|
||||
while (true) {}
|
||||
/*while (true) {}*/
|
||||
}
|
||||
return values.get(key_loc)
|
||||
}
|
||||
|
||||
17
stdlib/serialize.krak
Normal file
17
stdlib/serialize.krak
Normal file
@@ -0,0 +1,17 @@
|
||||
import vector
|
||||
import conversions
|
||||
import mem
|
||||
|
||||
fun serialize<T>(it: T): vector::vector<char> {
|
||||
var char_data = conversions::cast_ptr<*T,*char>(&it)
|
||||
var toRet = vector::vector<char>()
|
||||
for (var i = 0; i < mem::sizeof<T>(); i++;)
|
||||
toRet.add(char_data[i])
|
||||
return toRet
|
||||
}
|
||||
|
||||
fun unserialize<T>(it: vector::vector<char>):T {
|
||||
return *conversions::cast_ptr<*char,*T>(it.getBackingMemory())
|
||||
}
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ obj vector<T> (Object) {
|
||||
println(index);
|
||||
print("Max Index of vector: ");
|
||||
println(size-1);
|
||||
while(true) {}
|
||||
/*while(true) {}*/
|
||||
return data[0];
|
||||
}
|
||||
return data[index];
|
||||
|
||||
Reference in New Issue
Block a user