mirror of
https://github.com/vgmstream/vgmstream.git
synced 2025-01-18 15:54:05 +01:00
cleanup: BOM skip function
This commit is contained in:
parent
ac23eab7ed
commit
d7b277a671
@ -808,8 +808,7 @@ static int get_padding_size(txth_header* txth, int discard_empty);
|
||||
/* Simple text parser of "key = value" lines.
|
||||
* The code is meh and error handling not exactly the best. */
|
||||
static int parse_txth(txth_header* txth) {
|
||||
off_t txt_offset = 0x00;
|
||||
off_t file_size = get_streamfile_size(txth->sf_text);
|
||||
off_t txt_offset, file_size;
|
||||
|
||||
/* setup txth defaults */
|
||||
if (txth->sf_body)
|
||||
@ -818,14 +817,8 @@ static int parse_txth(txth_header* txth) {
|
||||
if (txth->target_subsong == 0) txth->target_subsong = 1;
|
||||
|
||||
|
||||
/* skip BOM if needed */
|
||||
if ((uint16_t)read_16bitLE(0x00, txth->sf_text) == 0xFFFE ||
|
||||
(uint16_t)read_16bitLE(0x00, txth->sf_text) == 0xFEFF) {
|
||||
txt_offset = 0x02;
|
||||
}
|
||||
else if (((uint32_t)read_32bitBE(0x00, txth->sf_text) & 0xFFFFFF00) == 0xEFBBBF00) {
|
||||
txt_offset = 0x03;
|
||||
}
|
||||
txt_offset = read_bom(txth->sf_text);
|
||||
file_size = get_streamfile_size(txth->sf_text);
|
||||
|
||||
/* read lines */
|
||||
{
|
||||
@ -1533,7 +1526,7 @@ static int read_name_table_keyval(txth_header* txth, const char* line, char* key
|
||||
/* try "(name): (val))" */
|
||||
ok = sscanf(line, " %[^\t#:] : %[^\t#\r\n] ", key, val);
|
||||
if (ok == 2) {
|
||||
;VGM_LOG("TXTH: name %s get\n", key);
|
||||
//;VGM_LOG("TXTH: name %s get\n", key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1541,14 +1534,14 @@ static int read_name_table_keyval(txth_header* txth, const char* line, char* key
|
||||
key[0] = '\0';
|
||||
ok = sscanf(line, " : %[^\t#\r\n] ", val);
|
||||
if (ok == 1) {
|
||||
;VGM_LOG("TXTH: default get\n");
|
||||
//;VGM_LOG("TXTH: default get\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* try "(name)#subsong: (val))" */
|
||||
ok = sscanf(line, " %[^\t#:]#%i : %[^\t#\r\n] ", key, &subsong, val);
|
||||
if (ok == 3 && subsong == txth->target_subsong) {
|
||||
VGM_LOG("TXTH: name %s + subsong %i get\n", key, subsong);
|
||||
//;VGM_LOG("TXTH: name %s + subsong %i get\n", key, subsong);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1556,7 +1549,7 @@ static int read_name_table_keyval(txth_header* txth, const char* line, char* key
|
||||
key[0] = '\0';
|
||||
ok = sscanf(line, " #%i: %[^\t#\r\n] ", &subsong, val);
|
||||
if (ok == 2 && subsong == txth->target_subsong) {
|
||||
VGM_LOG("TXTH: default + subsong %i get\n", subsong);
|
||||
//;VGM_LOG("TXTH: default + subsong %i get\n", subsong);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1596,18 +1589,9 @@ static int parse_name_table(txth_header* txth, char* name_list) {
|
||||
get_streamfile_basename(txth->sf_body, basename, sizeof(basename));
|
||||
//;VGM_LOG("TXTH: names full=%s, file=%s, base=%s\n", fullname, filename, basename);
|
||||
|
||||
txt_offset = 0x00;
|
||||
txt_offset = read_bom(sf_names);
|
||||
file_size = get_streamfile_size(sf_names);
|
||||
|
||||
/* skip BOM if needed */
|
||||
if ((uint16_t)read_16bitLE(0x00, sf_names) == 0xFFFE ||
|
||||
(uint16_t)read_16bitLE(0x00, sf_names) == 0xFEFF) {
|
||||
txt_offset = 0x02;
|
||||
}
|
||||
else if (((uint32_t)read_32bitBE(0x00, sf_names) & 0xFFFFFF00) == 0xEFBBBF00) {
|
||||
txt_offset = 0x03;
|
||||
}
|
||||
|
||||
/* in case of repeated name tables */
|
||||
memset(txth->name_values, 0, sizeof(txth->name_values));
|
||||
txth->name_values_count = 0;
|
||||
|
@ -1887,8 +1887,7 @@ fail:
|
||||
|
||||
static txtp_header* parse_txtp(STREAMFILE* sf) {
|
||||
txtp_header* txtp = NULL;
|
||||
off_t txt_offset = 0x00;
|
||||
off_t file_size = get_streamfile_size(sf);
|
||||
off_t txt_offset, file_size;
|
||||
|
||||
|
||||
txtp = calloc(1,sizeof(txtp_header));
|
||||
@ -1897,16 +1896,8 @@ static txtp_header* parse_txtp(STREAMFILE* sf) {
|
||||
/* defaults */
|
||||
txtp->is_segmented = 1;
|
||||
|
||||
|
||||
/* skip BOM if needed */
|
||||
if (file_size > 0 &&
|
||||
(read_u16le(0x00, sf) == 0xFFFE || read_u16le(0x00, sf) == 0xFEFF)) {
|
||||
txt_offset = 0x02;
|
||||
}
|
||||
else if ((read_u32be(0x00, sf) & 0xFFFFFF00) == 0xEFBBBF00) {
|
||||
txt_offset = 0x03;
|
||||
}
|
||||
|
||||
txt_offset = read_bom(sf);
|
||||
file_size = get_streamfile_size(sf);
|
||||
|
||||
/* read and parse lines */
|
||||
{
|
||||
|
@ -1001,6 +1001,19 @@ size_t read_line(char* buf, int buf_size, off_t offset, STREAMFILE* sf, int* p_l
|
||||
return i + extra_bytes;
|
||||
}
|
||||
|
||||
size_t read_bom(STREAMFILE* sf) {
|
||||
if (read_u16le(0x00, sf) == 0xFFFE ||
|
||||
read_u16le(0x00, sf) == 0xFEFF) {
|
||||
return 0x02;
|
||||
}
|
||||
|
||||
if ((read_u32be(0x00, sf) & 0xFFFFFF00) == 0xEFBBBF00) {
|
||||
return 0x03;
|
||||
}
|
||||
|
||||
return 0x00;
|
||||
}
|
||||
|
||||
size_t read_string(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf) {
|
||||
size_t pos;
|
||||
|
||||
@ -1142,17 +1155,9 @@ STREAMFILE* read_filemap_file_pos(STREAMFILE* sf, int file_num, int* p_pos) {
|
||||
|
||||
get_streamfile_filename(sf, filename, sizeof(filename));
|
||||
|
||||
txt_offset = 0x00;
|
||||
txt_offset = read_bom(sf_map);
|
||||
file_size = get_streamfile_size(sf_map);
|
||||
|
||||
/* skip BOM if needed */
|
||||
if (read_u16le(0x00, sf_map) == 0xFFFE ||
|
||||
read_u16le(0x00, sf_map) == 0xFEFF) {
|
||||
txt_offset = 0x02;
|
||||
} else if ((read_u32be(0x00, sf_map) & 0xFFFFFF00) == 0xEFBBBF00) {
|
||||
txt_offset = 0x03;
|
||||
}
|
||||
|
||||
/* read lines and find target filename, format is (filename): value1, ... valueN */
|
||||
while (txt_offset < file_size) {
|
||||
char line[0x2000];
|
||||
|
@ -355,6 +355,9 @@ static inline size_t align_size_to_block(size_t value, size_t block_align) {
|
||||
* p_line_ok is set to 1 if the complete line was read; pass NULL to ignore. */
|
||||
size_t read_line(char* buf, int buf_size, off_t offset, STREAMFILE* sf, int* p_line_ok);
|
||||
|
||||
/* skip BOM if needed */
|
||||
size_t read_bom(STREAMFILE* sf);
|
||||
|
||||
/* reads a c-string (ANSI only), up to bufsize or NULL, returning size. buf is optional (works as get_string_size). */
|
||||
size_t read_string(char* buf, size_t buf_size, off_t offset, STREAMFILE* sf);
|
||||
/* reads a UTF16 string... but actually only as ANSI (discards the upper byte) */
|
||||
|
Loading…
x
Reference in New Issue
Block a user