Skip to content

Commit

Permalink
Added read operation support
Browse files Browse the repository at this point in the history
  • Loading branch information
yobonez committed Aug 24, 2023
1 parent 8b1cf11 commit a5b884f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 29 deletions.
81 changes: 66 additions & 15 deletions aoslang_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
#include "aoslang_io.h"
#include "aoslang_types.h"

size_t aoslang_get_first_string(FILE* langfile_ptr) {
size_t aoslang_get_size(FILE* langfile_ptr)
{
fseek(langfile_ptr, 0, SEEK_END);
return ftell(langfile_ptr);
}

size_t aoslang_get_first_offset(FILE* langfile_ptr) {
fseek(langfile_ptr, 8, SEEK_SET); // skip 4 byte header and 4 byte unknown data
size_t str_loc = 0;
fread(&str_loc, sizeof(int), 1, langfile_ptr);
Expand All @@ -17,7 +23,7 @@ int check_header(FILE* langfile_ptr)
{
char header[5];

if (fgets(header, 5, langfile_ptr) == NULL) { printf("Error occured while trying to check the header."); return -1; };
if (fgets(header, 5, langfile_ptr) == NULL) { printf("Error occured while trying to check the header."); return -1; }
if (strcmp(header, "STR0") != 0) { return -1; }
rewind(langfile_ptr);

Expand All @@ -36,7 +42,7 @@ FILE* open_aoslang(const char* aoslang_filename)
}


void get_next_string(FILE* fptr, char* buffer, size_t until_EOF, size_t* i, size_t zero_align)
void get_next_string(FILE* fptr, char* buffer, size_t until_EOF, size_t* i, size_t zero_align, const char* mode)
{
for (; *i < until_EOF; (*i)++)
{
Expand All @@ -45,27 +51,77 @@ void get_next_string(FILE* fptr, char* buffer, size_t until_EOF, size_t* i, size
buffer[*i - zero_align] = c;
}
else {
if (strcmp(mode, "read") == 0) {
buffer[*i - zero_align] = c;
break;
}
buffer[*i - zero_align] = '\n'; // insert newline before \0
buffer[*i + 1 - zero_align] = c; // \0
(*i)++; // when breaking the i still needs to be incremented
break;
}
if (c == '\n') buffer[*i - zero_align] = '`'; // some strings already have newlines in them, i have to preserve them somehow for later
if (c == '\n' && strcmp(mode, "export") == 0) buffer[*i - zero_align] = '`'; // some strings already have newlines in them, i have to preserve them somehow for later
}
}


int aoslang_read(FILE* langfile_ptr)
int aoslang_read(FILE* langfile_ptr, const char* mode)
{
//fseek(langfile_ptr, 8, SEEK_SET); // skip 4 byte header and 4 byte unknown data
size_t strings_loc = aoslang_get_first_offset(langfile_ptr);
size_t strings_amount = (strings_loc - 8) / 4;

fseek(langfile_ptr, 8, SEEK_SET); // jump over 4 byte header and 4 bytes of unknown data
AosLangEntry* entries = (AosLangEntry*)malloc(sizeof(AosLangEntry) * strings_amount);

int* offsets = (int*)malloc(sizeof(int) * strings_amount);

if (entries == NULL || offsets == NULL) { printf("malloc error"); return -1; }

for (size_t i = 0; i < strings_amount; i++)
{
int offset = 0;
fread(&offset, sizeof(int), 1, langfile_ptr);
offsets[i] = offset;
}


AosLangEntry entry = { 0 };
size_t byte_counter = 0;
size_t until_EOF = aoslang_get_size(langfile_ptr) - strings_loc;
fseek(langfile_ptr, strings_loc, SEEK_SET);
size_t zero_align = 0;

for (size_t i = 0; i < strings_amount; i++)
{
entry.offset = offsets[i];


char lang_string[MAX_STR_LEN] = { 0 };
get_next_string(langfile_ptr, lang_string, until_EOF, &byte_counter, zero_align, mode);
strcpy_s(entry.lang_string, sizeof(lang_string), lang_string);

memcpy(&entries[i], &entry, sizeof(AosLangEntry));
zero_align = byte_counter;
}

printf("-----------------------------------------------------------------------\n");
for (size_t i = 0; i < strings_amount; i++)
{
printf("Offset: 0x%x\nContent: %s\n", entries[i].offset, entries[i].lang_string);
printf("-----------------------------------------------------------------------\n");
}

free(offsets);
free(entries);
offsets = NULL;
entries = NULL;
return 0;
}

int aoslang_export(FILE* langfile_ptr)
int aoslang_export(FILE* langfile_ptr, const char* mode)
{
// determine amount of strings in lang file and its first string offset
size_t strings_loc = aoslang_get_first_string(langfile_ptr);
size_t strings_loc = aoslang_get_first_offset(langfile_ptr);
size_t amount_of_strings = (strings_loc - 8) / 4;

FILE* destination_file;
Expand All @@ -74,12 +130,7 @@ int aoslang_export(FILE* langfile_ptr)
return -1;
}

if (fseek(langfile_ptr, 0, SEEK_END) != 0) {
printf("fseek error: (ferror: %i)", ferror(langfile_ptr));
return -1;
}

size_t langfile_size = ftell(langfile_ptr);
size_t langfile_size = aoslang_get_size(langfile_ptr);

fseek(langfile_ptr, strings_loc, SEEK_SET);
size_t file_cursor_pos = ftell(langfile_ptr);
Expand All @@ -90,7 +141,7 @@ int aoslang_export(FILE* langfile_ptr)
size_t chars_to_EOF = langfile_size - file_cursor_pos;
printf("Exporting %d strings...\n", amount_of_strings);
while (i < chars_to_EOF) {
get_next_string(langfile_ptr, string_to_write, chars_to_EOF, &i, zero_align);
get_next_string(langfile_ptr, string_to_write, chars_to_EOF, &i, zero_align, mode);
fwrite(string_to_write, sizeof(char), strlen(string_to_write), destination_file);
string_to_write[0] = '\0';
zero_align = i;
Expand Down
4 changes: 2 additions & 2 deletions aoslang_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

//int check_header(FILE* langfile_ptr);
FILE* open_aoslang(const char* aoslang_filename);
int aoslang_read(FILE* langfile_ptr);
int aoslang_export(FILE* langfile_ptr);
int aoslang_read(FILE* langfile_ptr, const char* mode);
int aoslang_export(FILE* langfile_ptr, const char* mode);
void aoslang_pack();
4 changes: 2 additions & 2 deletions aoslang_types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define MAX_STR_LEN 256
#define MAX_STR_LEN 252

typedef struct aoslang_entry {
int offset;
char entry[MAX_STR_LEN];
char lang_string[MAX_STR_LEN];
} AosLangEntry ;
19 changes: 9 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ int main(int argc, char* argv[])
if (argc < 3 || !*argv[i]) { printf("Usage: %s <export|read|pack> <filename> \n", argv[0]); return 1; }
}

const char* provided_filename = argv[2];
const char* mode = argv[1];
const char* filename = argv[2];

if (strcmp(argv[1], "export") == 0) {
FILE* aoslang_file = open_aoslang(provided_filename);
if (strcmp(mode, "export") == 0) {
FILE* aoslang_file = open_aoslang(filename);

if (aoslang_file == NULL || aoslang_export(aoslang_file) != 0)
if (aoslang_file == NULL || aoslang_export(aoslang_file, mode) != 0)
{ printf("An error occured.\n"); return -1; }
}
if (strcmp(argv[1], "read") == 0) {
FILE* aoslang_file = open_aoslang(provided_filename);
if (strcmp(mode, "read") == 0) {
FILE* aoslang_file = open_aoslang(filename);

if (aoslang_file == NULL || aoslang_read(aoslang_file) != 0)
if (aoslang_file == NULL || aoslang_read(aoslang_file, mode) != 0)
{ printf("An error occured.\n"); return -1; }
}
if (strcmp(argv[1], "pack") == 0) {
if (strcmp(mode, "pack") == 0) {
printf("\"pack\" Not implemented yet...");
return -1;
}

printf("Done. Press any key to continue...");
getchar();
return 0;
}

0 comments on commit a5b884f

Please sign in to comment.