Sega Saturn Development > Jo Engine Wish List

Reading binary files with pointers instead of returned char stream?

<< < (2/4) > >>

mindslight:
Hi XL2,

You will find below, the prototypes of the functions I have added in the next release:


--- Code: ---
/** @brief Open a file
 *  @param file Pointer to an allocated jo_file struct
 *  @param filename Filename (upper case and shorter as possible like "A.TXT")
 *  @return true if succeed
 */
bool            jo_fs_open(jo_file * const file, const char *const filename);

/** @brief Close a file
 *  @param file Pointer to an allocated and valid jo_file struct
 */
void            jo_fs_close(jo_file * const file);

/** @brief Read bytes from a file
 *  @param file Pointer to an allocated and valid jo_file struct
 *  @param buffer Pointer to an allocated buffer (length >= nbytes)
 *  @param nbytes number of bytes to read
 *  @return Number of bytes read (<= 0 means EOF)
 */
int             jo_fs_read_next_bytes(jo_file * const file, char *buffer, unsigned int nbytes);


--- End code ---

Sample:


--- Code: ---
    jo_file     file;
    char        buf[2048];

    if (jo_fs_open(&file, "TEST.TXT"))
    {
        while (jo_fs_read_next_bytes(&file, buf, 2048) > 0)
            /*DO SOMETHING */;
        jo_fs_close(&file);
    }

--- End code ---

XL2:
Would it be possible to add a function to read from an user-defined pointer?

Like, if I'm loading map 2 and it's using the same background as map 1, I want to be able to skip it and just keep it without loading it all over again.

So something like :
jo_fs_read_from_pointer(&file, buffer, StartingByte, Length);

Also, I've had a few failed read on real Saturn.
The Saturns are getting old, so I think it would be nice to implement a re-read feature in case of failure to read.
Like, in your read functions, if it fails, you try again.

Thanks!

mindslight:
Noted  ;)

mindslight:
I made a new version (http://jo-engine.org/download/) with CD read-retry and a new function:


--- Code: ---
/** @brief Seek forward from current position of a file
 *  @param file Pointer to an allocated and valid jo_file struct
 *  @param nbytes number of bytes to skip
 *  @return true if succeed
 */
bool            jo_fs_seek_forward(jo_file * const file, unsigned int nbytes);

--- End code ---

Now you can skip a part of a file and use jo_fs_read_next_bytes() after (just don't forget to shift the pointer : buffer + nbytes)

Example:


--- Code: ---
jo_file file;
char *file_contents;
...

jo_fs_open(&file, "FILE.TXT");
jo_fs_seek_forward(&file, 4096); /* I assume that 4096 is the size of the part to skip in the file and to preserve in the buffer */
jo_fs_read_next_bytes(&file, file_contents + 4096, 42);
jo_fs_close(&file);


--- End code ---

XL2:
Thanks for the function, I'm using it already as I write palette data but I'm not using it (I tried to make 3d quads work with it by using the img_8_bits loading function, but it didn't go too well, so I'll have to write my own sprite loading function for 4 bits sprites textures).

Now, one more request (last one about this I hope) : instead of using a char for the buffer parameter, can you allow us to just put the memory address for direct loading?
I swapped the endianness on the PC side to support Saturn's endianness and I am using the jo_fs_read_next_bytes function to write directly to memory, which should speed up things nicely.
The only problem is that the compiler doesn't like this and throws warnings, which are annoying obviously, but otherwise it works fine.
So I'm not using a buffer at all, just straight to memory, and I think it would be nice that the function works this way right away (since I'm probably the only one using it at the moment, I don't think it will cause huge issues if you make the change on the existing function).
Thanks!

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Sitemap 1 2 3 4 5 6 7 8 9 10 
Go to full version