Added primitive serilization
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import string;
|
import string;
|
||||||
|
import vector;
|
||||||
import mem:*
|
import mem:*
|
||||||
|
|
||||||
__if_comp__ __C__ simple_passthrough """
|
__if_comp__ __C__ simple_passthrough """
|
||||||
@@ -75,27 +76,7 @@ fun print(toPrint: double) : void{
|
|||||||
|
|
||||||
// Ok, just some DEAD simple file io for now
|
// Ok, just some DEAD simple file io for now
|
||||||
fun read_file(path: string::string): string::string {
|
fun read_file(path: string::string): string::string {
|
||||||
var char_path = path.toCharArray()
|
var toRet.construct(read_file_binary(path)): string::string
|
||||||
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)
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
return toRet
|
return toRet
|
||||||
}
|
}
|
||||||
fun write_file(path: string::string, data: string::string) {
|
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)
|
var key_loc = keys.find(key)
|
||||||
if (key_loc == -1) {
|
if (key_loc == -1) {
|
||||||
io::println("trying to access nonexistant key-value!")
|
io::println("trying to access nonexistant key-value!")
|
||||||
while (true) {}
|
/*while (true) {}*/
|
||||||
}
|
}
|
||||||
return values.get(key_loc)
|
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);
|
println(index);
|
||||||
print("Max Index of vector: ");
|
print("Max Index of vector: ");
|
||||||
println(size-1);
|
println(size-1);
|
||||||
while(true) {}
|
/*while(true) {}*/
|
||||||
return data[0];
|
return data[0];
|
||||||
}
|
}
|
||||||
return data[index];
|
return data[index];
|
||||||
|
|||||||
1
tests/test_serialization.expected_results
Normal file
1
tests/test_serialization.expected_results
Normal file
@@ -0,0 +1 @@
|
|||||||
|
7 = 7
|
||||||
14
tests/test_serialization.krak
Normal file
14
tests/test_serialization.krak
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import io:*
|
||||||
|
import serialize:*
|
||||||
|
import string:*
|
||||||
|
|
||||||
|
fun main():int {
|
||||||
|
var intA = 7
|
||||||
|
write_file_binary(string("bintest.bin"), serialize(intA))
|
||||||
|
var intB = unserialize<int>(read_file_binary(string("bintest.bin")))
|
||||||
|
print(intA)
|
||||||
|
print(" = ")
|
||||||
|
println(intB)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user