diff --git a/stdlib/io.krak b/stdlib/io.krak index 967e16b..f573404 100644 --- a/stdlib/io.krak +++ b/stdlib/io.krak @@ -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 { + 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 + 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) { + 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); + """ + } +} diff --git a/stdlib/map.krak b/stdlib/map.krak index 3b4ef2e..36adf6b 100644 --- a/stdlib/map.krak +++ b/stdlib/map.krak @@ -51,7 +51,7 @@ obj map (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) } diff --git a/stdlib/serialize.krak b/stdlib/serialize.krak new file mode 100644 index 0000000..ff4ce0f --- /dev/null +++ b/stdlib/serialize.krak @@ -0,0 +1,17 @@ +import vector +import conversions +import mem + +fun serialize(it: T): vector::vector { + var char_data = conversions::cast_ptr<*T,*char>(&it) + var toRet = vector::vector() + for (var i = 0; i < mem::sizeof(); i++;) + toRet.add(char_data[i]) + return toRet +} + +fun unserialize(it: vector::vector):T { + return *conversions::cast_ptr<*char,*T>(it.getBackingMemory()) +} + + diff --git a/stdlib/vector.krak b/stdlib/vector.krak index 9138b2c..c53a8bf 100644 --- a/stdlib/vector.krak +++ b/stdlib/vector.krak @@ -121,7 +121,7 @@ obj vector (Object) { println(index); print("Max Index of vector: "); println(size-1); - while(true) {} + /*while(true) {}*/ return data[0]; } return data[index]; diff --git a/tests/test_serialization.expected_results b/tests/test_serialization.expected_results new file mode 100644 index 0000000..873fe18 --- /dev/null +++ b/tests/test_serialization.expected_results @@ -0,0 +1 @@ +7 = 7 diff --git a/tests/test_serialization.krak b/tests/test_serialization.krak new file mode 100644 index 0000000..94df585 --- /dev/null +++ b/tests/test_serialization.krak @@ -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(read_file_binary(string("bintest.bin"))) + print(intA) + print(" = ") + println(intB) + return 0 +} +