This commit is contained in:
Murii 2018-11-25 15:21:17 +02:00
commit 4c49d8280a
4 changed files with 196 additions and 0 deletions

19
License.md Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2018 Muresan Vlad Mihail
Contact Info muresanvladmihail@gmail.com murii@tilde.team
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software.
Shall you use this software in a product, an acknowledgment and the
contact info(if there is any) of the author(s) must be placed in
the product documentation.
2. This notice may not be removed or altered from any source distribution.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE
BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

26
README.md Normal file
View File

@ -0,0 +1,26 @@
```
import libc {...}
priv data:char* = """
player_coins=20;
player_life=0;
player_x=200;
player_y=300;
current_level=3;
""";
func main(argc:int, argv:char**):int {
save:save_data;
save_data_init(&save, "player.stat"); //Creates and overrides file "player.stat"
save_data_write(&save, data);
save_data_read(&save);
for (i := 0; i < save.total; i++) {
printf("Pair: %s %s \n", save.keys[i], save.values[i]);
}
return 0;
}
```

43
main.rii Normal file
View File

@ -0,0 +1,43 @@
/*
Copyright (c) 2018 Muresan Vlad Mihail
Contact Info muresanvladmihail@gmail.com murii@tilde.team
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. Shall you use this software
in a product, an acknowledgment and the contact info(if there is any)
of the author(s) must be placed in the product documentation.
This notice may not be removed or altered from any source distribution.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import libc {...}
priv data:char* = """
player_coins=20;
player_life=0;
player_x=200;
player_y=300;
current_level=3;
""";
func main(argc:int, argv:char**):int {
save:save_data;
save_data_init(&save, "player.stat"); //Creates and overrides file "player.stat"
save_data_write(&save, data);
save_data_read(&save);
for (i := 0; i < save.total; i++) {
printf("Pair: %s %s \n", save.keys[i], save.values[i]);
}
return 0;
}

108
savier.rii Normal file
View File

@ -0,0 +1,108 @@
/*
Copyright (c) 2018 Muresan Vlad Mihail
Contact Info muresanvladmihail@gmail.com murii@tilde.team
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. Shall you use this software
in a product, an acknowledgment and the contact info(if there is any)
of the author(s) must be placed in the product documentation.
This notice may not be removed or altered from any source distribution.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const SAVE_DATA_TOTAL = 128;
const SAVE_DATA_MAX_KEY_LEN = 32;
const SAVE_DATA_MAX_VALUE_LEN = 64;
struct save_data {
file:FILE*;
path:char const*;
keys:char[SAVE_DATA_TOTAL][SAVE_DATA_MAX_KEY_LEN];
values:char[SAVE_DATA_TOTAL][SAVE_DATA_MAX_VALUE_LEN];
total:usize; // length of total parsed keys/values
}
func save_data_init(save:save_data*, path:char const*) {
save.path = path;
save.total = 0;
}
func save_data_write(save:save_data*, data_to_be_saved:char const*) {
save.file = fopen(save.path, "w");
if (!save.file) {
printf("Error opening file: %s\n", save.path);
}
fprintf(save.file, "%s", data_to_be_saved);
fclose(save.file);
}
func save_data_read(save:save_data*) {
save.file = fopen(save.path, "r");
if (!save.file) {
printf("Error opening file: %s\n", save.path);
}
fseek(save.file, 0, SEEK_END);
file_len:long = ftell(save.file);
fseek(save.file, 0, SEEK_SET);
got:char* = malloc(file_len);
fread(got, 1, file_len, save.file);
c:int;
key:char[SAVE_DATA_MAX_KEY_LEN];
key_index:uchar = 0;
value:char[SAVE_DATA_MAX_VALUE_LEN];
value_index:uchar = 0;
while (c < file_len) {
curr:int = got[c];
if (curr != '=') { //parse key
key[key_index++] = curr;
if (key_index > SAVE_DATA_MAX_KEY_LEN) {
printf("Error: The key's size you're trying to read is bigger than what we can store\n");
}
} else if (curr == '=') { // parse value
c++;
strcpy(save.keys[save.total], key);
while (got[c] != ';') {
value[value_index++] = got[c];
if (value_index > SAVE_DATA_MAX_VALUE_LEN) {
printf("Error: Value's size you're trying to read is bigger than what we can store for key: %s\n", key);
}
c++;
}
strcpy(save.values[save.total], value);
save.total++;
if (save.total > SAVE_DATA_TOTAL) {
puts("Error: You're trying to parse more data than reserved space!");
break;
}
memset(key, 0, SAVE_DATA_MAX_KEY_LEN);
memset(value, 0, SAVE_DATA_MAX_VALUE_LEN);
key_index = 0;
value_index = 0;
c++;
}
c++;
}
fclose(save.file);
free(got);
}