Make get_streamfile_dos_line read CR/LF too and rename accordingly

CR used in some Falcom looping files as separator
This commit is contained in:
bnnm 2017-11-23 22:53:43 +01:00
parent 7c7af0bf18
commit dbbbfcc9f5
5 changed files with 36 additions and 42 deletions

View File

@ -127,7 +127,7 @@ static int get_falcom_looping(STREAMFILE *streamFile, int *out_loop_start, int *
char name[TXT_LINE_MAX];
int ok, line_done, loop, bytes_read;
bytes_read = get_streamfile_dos_line(TXT_LINE_MAX,line, txt_offset,streamText, &line_done);
bytes_read = get_streamfile_text_line(TXT_LINE_MAX,line, txt_offset,streamText, &line_done);
if (!line_done) goto end;
txt_offset += bytes_read;

View File

@ -125,7 +125,7 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
if (strcasecmp("mus",filename_extension(filename))) goto fail;
/* read file name base */
line_bytes = get_streamfile_dos_line(sizeof(line_buffer),line_buffer,
line_bytes = get_streamfile_text_line(sizeof(line_buffer),line_buffer,
mus_offset, streamFile, &whole_line_read);
if (!whole_line_read) goto fail;
mus_offset += line_bytes;
@ -140,7 +140,7 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
/*printf("name base: %s\n",name_base);*/
/* read track entry count */
line_bytes = get_streamfile_dos_line(sizeof(line_buffer),line_buffer,
line_bytes = get_streamfile_text_line(sizeof(line_buffer),line_buffer,
mus_offset, streamFile, &whole_line_read);
if (!whole_line_read) goto fail;
if (line_buffer[0] == '\0') goto fail;
@ -184,7 +184,7 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
{
int fields_matched;
line_bytes =
get_streamfile_dos_line(sizeof(line_buffer),line_buffer,
get_streamfile_text_line(sizeof(line_buffer),line_buffer,
mus_offset, streamFile, &whole_line_read);
if (!whole_line_read) goto fail;
mus_offset += line_bytes;

View File

@ -54,7 +54,7 @@ VGMSTREAM * init_vgmstream_sli_ogg(STREAMFILE *streamFile) {
while ((loop_start == -1 || loop_length == -1) && sli_offset < get_streamfile_size(streamFile)) {
char *endptr;
char *foundptr;
bytes_read=get_streamfile_dos_line(sizeof(linebuffer),linebuffer,sli_offset,streamFile,&done);
bytes_read=get_streamfile_text_line(sizeof(linebuffer),linebuffer,sli_offset,streamFile,&done);
if (!done) goto fail;
if (!memcmp("LoopStart=",linebuffer,10) && linebuffer[10]!='\0') {

View File

@ -298,63 +298,59 @@ STREAMFILE * open_stdio_streamfile_by_file(FILE * file, const char * filename) {
/* **************************************************** */
/* Read a line into dst. The source files are MS-DOS style,
* separated (not terminated) by CRLF. Return 1 if the full line was
* retrieved (if it could fit in dst), 0 otherwise. In any case the result
* will be properly terminated. The CRLF will be removed if there is one.
* Return the number of bytes read (including CRLF line ending). Note that
* this is not the length of the string, and could be larger than the buffer.
* *line_done_ptr is set to 1 if the complete line was read into dst,
* otherwise it is set to 0. line_done_ptr can be NULL if you aren't
* interested in this info.
/* Read a line into dst. The source files are lines separated by CRLF (Windows) / LF (Unux) / CR (Mac).
* The line will be null-terminated and CR/LF removed if found.
*
* Returns the number of bytes read (including CR/LF), note that this is not the string length.
* line_done_ptr is set to 1 if the complete line was read into dst; NULL can be passed to ignore.
*/
size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset,
STREAMFILE * infile, int *line_done_ptr)
{
size_t get_streamfile_text_line(int dst_length, char * dst, off_t offset, STREAMFILE * streamfile, int *line_done_ptr) {
int i;
off_t file_length = get_streamfile_size(infile);
/* how many bytes over those put in the buffer were read */
int extra_bytes = 0;
off_t file_length = get_streamfile_size(streamfile);
int extra_bytes = 0; /* how many bytes over those put in the buffer were read */
if (line_done_ptr) *line_done_ptr = 0;
for (i=0;i<dst_length-1 && offset+i < file_length;i++)
{
char in_char = read_8bit(offset+i,infile);
for (i = 0; i < dst_length-1 && offset+i < file_length; i++) {
char in_char = read_8bit(offset+i,streamfile);
/* check for end of line */
if (in_char == 0x0d &&
read_8bit(offset+i+1,infile) == 0x0a)
{
if (in_char == 0x0d && read_8bit(offset+i+1,streamfile) == 0x0a) { /* CRLF */
extra_bytes = 2;
if (line_done_ptr) *line_done_ptr = 1;
break;
}
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
extra_bytes = 1;
if (line_done_ptr) *line_done_ptr = 1;
break;
}
dst[i]=in_char;
dst[i] = in_char;
}
dst[i]='\0';
dst[i] = '\0';
/* did we fill the buffer? */
if (i==dst_length) {
if (i == dst_length) {
char in_char = read_8bit(offset+i,streamfile);
/* did the bytes we missed just happen to be the end of the line? */
if (read_8bit(offset+i,infile) == 0x0d &&
read_8bit(offset+i+1,infile) == 0x0a)
{
if (in_char == 0x0d && read_8bit(offset+i+1,streamfile) == 0x0a) { /* CRLF */
extra_bytes = 2;
/* if so be proud! */
if (line_done_ptr) *line_done_ptr = 1;
}
else if (in_char == 0x0d || in_char == 0x0a) { /* CR or LF */
extra_bytes = 1;
if (line_done_ptr) *line_done_ptr = 1;
}
}
/* did we hit the file end? */
if (offset+i == file_length)
{
if (offset+i == file_length) {
/* then we did in fact finish reading the last line */
if (line_done_ptr) *line_done_ptr = 1;
}
return i+extra_bytes;
return i + extra_bytes;
}
@ -380,9 +376,7 @@ fail:
return 0;
}
/**
* Opens an stream using the base streamFile name plus a new extension (ex. for headers in a separate file)
*/
/* Opens an stream using the base streamFile name plus a new extension (ex. for headers in a separate file) */
STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext) {
char filename_ext[PATH_LIMIT];
@ -392,7 +386,7 @@ STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext) {
return streamFile->open(streamFile,filename_ext,STREAMFILE_DEFAULT_BUFFER_SIZE);
}
/* Opens an stream in the same folder */
/* Opens an stream using the passed name (in the same folder) */
STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * name) {
char foldername[PATH_LIMIT];
char filename[PATH_LIMIT];

View File

@ -156,7 +156,7 @@ static inline int8_t read_8bit(off_t offset, STREAMFILE * streamfile) {
/* various STREAMFILE helpers functions */
size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset, STREAMFILE * infile, int *line_done_ptr);
size_t get_streamfile_text_line(int dst_length, char * dst, off_t offset, STREAMFILE * streamfile, int *line_done_ptr);
STREAMFILE * open_stream_ext(STREAMFILE *streamFile, const char * ext);
STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * ext);