use stdio and malloc
This commit is contained in:
parent
0f8a756276
commit
b0b125cc20
@ -1,6 +1,5 @@
|
|||||||
sdmc
|
sdmc
|
||||||
=======
|
=======
|
||||||
|
|
||||||
very simple example of how to access SD files with ctrulib; requires proper exheader flags for direct SDMC access.
|
very simple example of how to access SD files with libctru;
|
||||||
|
|
||||||
**WARNING/TODO**: THIS EXAMPLE IS OUTDATED AND NEEDS TO BE FIXED - PLEASE DO NOT LOOK AT IT UNTIL IT IS UPDATED
|
|
||||||
|
@ -3,9 +3,11 @@
|
|||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
//this example shows you how to load a binary image file from the SD card and display it on the lower screen
|
//this example shows you how to load a binary image file from the SD card and display it on the lower screen
|
||||||
//for this to work you should copy test.bin to the root of your SD card
|
//for this to work you should copy test.bin to same folder as your .3dsx
|
||||||
//this file was generated with GIMP by saving a 240x320 image to raw RGB
|
//this file was generated with GIMP by saving a 240x320 image to raw RGB
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
#include "costable.h"
|
#include "costable.h"
|
||||||
@ -42,41 +44,32 @@ void renderEffect()
|
|||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
//initialize the services we're going to be using
|
|
||||||
srvInit(); //needed for everything
|
|
||||||
aptInit(); //needed for everything
|
|
||||||
hidInit(NULL); //needed for input
|
|
||||||
gfxInit(); //makes displaying to screen easier
|
gfxInit(); //makes displaying to screen easier
|
||||||
fsInit(); //needed for filesystem stuff
|
|
||||||
|
|
||||||
u64 size;
|
FILE *file = fopen("test.bin","rb");
|
||||||
u32 bytesRead;
|
if (file == NULL) goto exit;
|
||||||
Handle fileHandle;
|
|
||||||
//setup SDMC archive
|
|
||||||
FS_archive sdmcArchive=(FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
|
|
||||||
//create file path struct (note : FS_makePath actually only supports PATH_CHAR, it will change in the future)
|
|
||||||
FS_path filePath=FS_makePath(PATH_CHAR, "/test.bin");
|
|
||||||
|
|
||||||
//open file
|
// seek to end of file
|
||||||
Result ret=FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
|
fseek(file,0,SEEK_END);
|
||||||
//check for errors : exit if there is one
|
|
||||||
if(ret)goto exit;
|
|
||||||
|
|
||||||
//get file size
|
// file pointer tells us the size
|
||||||
ret=FSFILE_GetSize(fileHandle, &size);
|
off_t size = ftell(file);
|
||||||
if(ret)goto exit;
|
|
||||||
|
|
||||||
//allocate a buffer on linear heap (could just be a malloc fwiw)
|
// seek back to start
|
||||||
buffer=linearAlloc(size);
|
fseek(file,0,SEEK_SET);
|
||||||
|
|
||||||
|
//allocate a buffer
|
||||||
|
buffer=malloc(size);
|
||||||
if(!buffer)goto exit;
|
if(!buffer)goto exit;
|
||||||
|
|
||||||
//read contents !
|
//read contents !
|
||||||
ret=FSFILE_Read(fileHandle, &bytesRead, 0x0, buffer, size);
|
off_t bytesRead = fread(buffer,1,size,file);
|
||||||
if(ret || size!=bytesRead)goto exit;
|
|
||||||
|
|
||||||
//close the file because we like being nice and tidy
|
//close the file because we like being nice and tidy
|
||||||
ret=FSFILE_Close(fileHandle);
|
fclose(file);
|
||||||
if(ret)goto exit;
|
|
||||||
|
if(size!=bytesRead)goto exit;
|
||||||
|
|
||||||
while(aptMainLoop())
|
while(aptMainLoop())
|
||||||
{
|
{
|
||||||
@ -98,13 +91,8 @@ int main(int argc, char** argv)
|
|||||||
//cleanup and return
|
//cleanup and return
|
||||||
//returning from main() returns to hbmenu when run under ninjhax
|
//returning from main() returns to hbmenu when run under ninjhax
|
||||||
exit:
|
exit:
|
||||||
//closing all handles is super important
|
|
||||||
svcCloseHandle(fileHandle);
|
|
||||||
//closing all services even more so
|
//closing all services even more so
|
||||||
fsExit();
|
|
||||||
gfxExit();
|
gfxExit();
|
||||||
hidExit();
|
|
||||||
aptExit();
|
|
||||||
srvExit();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user