forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

fopen VS. sceIoOpen ?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Mon Feb 15, 2010 10:54 pm    Post subject: fopen VS. sceIoOpen ? Reply with quote

Hi there,

I'm not sure whether this question was ever asked in this forum. However, I would like to understand why and when it is best suited to use either fopen or sceIoOpen to access files in read mode (and the corresponding functions to write etc...)

What is the difference between the functions - if there is one ?
I'm using manly the fopen/fread etc. function to access files in my homebrew - is there a knwon disadvantege in using them ?

Thanks in advance for any suggestions and advice.
Back to top
View user's profile Send private message
Davee



Joined: 22 Jun 2009
Posts: 59

PostPosted: Mon Feb 15, 2010 11:33 pm    Post subject: Reply with quote

fopen/fread/fwrite/fclose etc are the standard libc functions. They aren't "standard" in the PSP kernel so the sdk has wrappers for them which eventually translates them into sceIo* calls.

So the disadvantage is, is that it takes longer to use f* than it does sceIo*.
Back to top
View user's profile Send private message
victorprosa



Joined: 14 Jan 2009
Posts: 38

PostPosted: Tue Feb 16, 2010 12:09 am    Post subject: Reply with quote

If you are able to do that, ALWAYS prefer Sce* functions, because it was designed especially for the PSP hardware, by guys that understand it better than us...
And, the standard libc functions are translated into Sce* functions, but usually you lost performance...
Back to top
View user's profile Send private message Send e-mail
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Tue Feb 16, 2010 12:16 am    Post subject: Reply with quote

Hi there,

thanks for your replies. Just one thought: could I use sceIoOpen with PSPLINK in the same way I would use fopen ?
I just have seen that you need to pass a full qualified path to sceIoOpen in all the threads (starting whith device). I would assume that the device is different when using the homebrew with PSPLINK instead of from the memory stick. Am I right ?

Thanks for any hint.
Back to top
View user's profile Send private message
unsigned int



Joined: 13 Aug 2009
Posts: 22

PostPosted: Tue Feb 16, 2010 12:44 am    Post subject: Reply with quote

You should be able to use the same file names for sceIoOpen and fopen. Looking at the source code for the PSPSDK libc the fopen function just passes the path through to sceIoOpen without altering it in any way.
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Tue Feb 16, 2010 1:07 am    Post subject: Reply with quote

Hi,

thanks for confirmation. I will give it a try :)
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Tue Feb 16, 2010 6:45 am    Post subject: Reply with quote

Hi,

I've tried sceIoOpen as I have used fopen. However, this results in error: 0x8002032c

fopen( params->fileName, "rb" ); works but sceIoOpen( params->fileName, PSP_O_RDONLY, 0777 ); does not. Any ideas ?

params->fileName = "testfile.txt"

Google returns "No Cwd ??" when searching for this error code...

Thanks for any hints...Am I assuming right I should pass "device:path/filename" to sceIoOpen ? If so, what is the path I need to put if I only know that the file is stored together with the Eboot on MS or together with the PRX on the harddisk using PSPLINK. In many threads I've seen MS0: as device for the memory stick, but shouldn't be host0: during PSPLINK ?
Back to top
View user's profile Send private message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Tue Feb 16, 2010 7:58 am    Post subject: Reply with quote

anmabagima wrote:

I've tried sceIoOpen as I have used fopen. However, this results in error: 0x8002032c


this looks like the dwd (current working directory) has not been set.
I don't know how to set it, (I had a thread few days ago about somenting smilar which I solved my way...)

But from the params passed to your program, argv[0] is the complete filename and path of your eboot. Take the filename out and you have your cwd. Add it in front of your filename and you should be done...


HTH

Cheers, A.
Back to top
View user's profile Send private message
victorprosa



Joined: 14 Jan 2009
Posts: 38

PostPosted: Tue Feb 16, 2010 1:07 pm    Post subject: Reply with quote

I don't understand well what you are trying to do, but i may tell that:

Relative paths do work in SceIo, and all commands related to it, like "./" also does...
The initial path in SceIo is always the path of your homebrew, ie:

Code:

   SceUID fd = sceIoOpen("lol.lol", PSP_O_WRONLY | PSP_O_TRUNC | PSP_O_CREAT, 0777);
   sceIoWrite(fd, data, sizeof(data));
   sceIoClose(fd);


In the case above, you are creating/editing a file in:
ms0:/PSP/GAME/MYHBDIR/

-------------------------------------------------------

By the way, i sugest you to store things like paths, in chars...

-------------------------------------------------------

Also, i don't use PSPlink, for me, in linux, is faster to add the exit callback and all the time i get stuck, just leave the homebrew, fix the source and test it again, i don't even disconnect the USB cable...
Btw, things that usually crashes the PSP (which are rare) i solve the old way...

So, i am not able to answer you if this crash is a PSPLink job, i just tell how the PSP reads the stuff in normal conditions...
Back to top
View user's profile Send private message Send e-mail
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Tue Feb 16, 2010 6:37 pm    Post subject: Reply with quote

Hi victorprosa,

thanks for your reply. It is exactly what I've assumed and what I'm trying.

I'm calling sceIoOpen( "myFile.lol", PSP_O_RDONLY, 0777 ); which results in the error mentioned.
However, fopen("myFile.lol", "rb") does work.

In both cases it was tested using PSPLINK. In some circumstances I need to debug my code as it is evolving. I found PSPLINK very useful for this in the past. If this does not work with sceIoOpen than may be I need to choose the same approach and compile, pass to MS , execute and if it failes correct my code....
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Wed Feb 17, 2010 12:23 am    Post subject: Reply with quote

Seeing that NOTHING cross platform will ever compile for the PSP without extensive modification, it seems redundant to leave the fopen calls as such. You might as well convert everything to sce* while porting.
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Wed Feb 17, 2010 7:22 am    Post subject: Reply with quote

Always use sceIo* functions instead of f* functions. Why ? f* uses buffers. The issue is if you let PSP to go in standby whereas you have FILE* handles open, you'll get problem when you wake up your PSP.
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Wed Feb 17, 2010 8:15 pm    Post subject: Reply with quote

Hi,

sorry for asking a stupid question, but what does this actually mean ?
Do I need to have a branch on each file activity statement which choses f* if compiling and running using PSPLINK and sceIo* when compiling finally fpr PSP memory stick ?

Thanks.
Back to top
View user's profile Send private message
unsigned int



Joined: 13 Aug 2009
Posts: 22

PostPosted: Wed Feb 17, 2010 9:33 pm    Post subject: Reply with quote

Just wanted to add that I stand corrected on my previous assumption that fopen() didn't change the path when calling sceIoOpen().

I found out that the PSPSDK libc implementation does this, but (unless otherwise specified in the Makefile) it is not used, but newlib is. And in there is in fact code to expand a relative path to a full path before calling sceIoOpen().

So it is probably the best to do what Alberto posted earlier, take the working directory from argv[0] and expand all filenames yourself.
Back to top
View user's profile Send private message
yokfran



Joined: 28 Dec 2009
Posts: 16

PostPosted: Thu Feb 18, 2010 3:10 am    Post subject: Reply with quote

I think sceIo* is better.

If you use fopen, you have to manage these FILE* pointers yourself. And I think the SceUIDs that returns by all sce* functions, is managed by some sce* threads which are built inside the PSP system.
So you do less work than using fopen/f* etc.

If sceIoOpen returns error code, it's better to use abs-path.

Sample code.

int main(int args, char *argv[])
{
char bootpath[64], *ptr;//i think 64 is long enough
strcpy(bootpath, argv[0]);
ptr = strrchr(bootpath, '/');
*++ptr = 0;
//Boot directory is in bootpath now.
}


Last edited by yokfran on Sun Feb 21, 2010 4:23 pm; edited 1 time in total
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Thu Feb 18, 2010 6:55 am    Post subject: Reply with quote

Hi,

thanks. will try this out.

Regards...
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Fri Feb 19, 2010 12:17 pm    Post subject: Reply with quote

sceIoOpen is buggy when it comes to relative paths. I don't recall the details, but the newlib wrappers just canonicalize everything before passing to sceIoOpen.

Unless you're really performance sensitive, you should use the newlib wrappers when available. They're easier to use (since they're well-documented everywhere) and we've already handled a whole mess of these sorts of bugs in the sce* interfaces.
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sat Feb 20, 2010 4:04 am    Post subject: Reply with quote

jimparis wrote:
sceIoOpen is buggy when it comes to relative paths. I don't recall the details, but the newlib wrappers just canonicalize everything before passing to sceIoOpen.

Unless you're really performance sensitive, you should use the newlib wrappers when available. They're easier to use (since they're well-documented everywhere) and we've already handled a whole mess of these sorts of bugs in the sce* interfaces.


included the case after a suspend mode ?
Back to top
View user's profile Send private message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Sat Feb 20, 2010 5:23 am    Post subject: Reply with quote

hlide wrote:
included the case after a suspend mode ?


I have always been told that before going to suspend mode I need to save my open files (together with theis file positions, of course), if any., and then allow suspend. When resuming from suspend (there is the callback...) reopen the files that were open and reposition them.

...although I don't find a good practice keeping any file open for the entire life of the application...

JM2C
A.
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Sun Feb 21, 2010 12:38 pm    Post subject: Reply with quote

hlide wrote:
jimparis wrote:
sceIoOpen is buggy when it comes to relative paths. I don't recall the details, but the newlib wrappers just canonicalize everything before passing to sceIoOpen.

Unless you're really performance sensitive, you should use the newlib wrappers when available. They're easier to use (since they're well-documented everywhere) and we've already handled a whole mess of these sorts of bugs in the sce* interfaces.


included the case after a suspend mode ?

Not yet, but it would be easy because of Rafael's fd management that keeps track of the filename that goes with each open file. You could add a field to __psp_descriptormap_type to store the offset, and then add two hook functions: one that loops through all open file FDs and gets the offset, then another that closes/reopens/seeks to the right offset in each file.

But libc doesn't know about suspending, so you'd have to call those hooks manually.
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Mon Feb 22, 2010 6:02 am    Post subject: Reply with quote

jimparis wrote:
Not yet, but it would be easy because of Rafael's fd management that keeps track of the filename that goes with each open file. You could add a field to __psp_descriptormap_type to store the offset, and then add two hook functions: one that loops through all open file FDs and gets the offset, then another that closes/reopens/seeks to the right offset in each file.

But libc doesn't know about suspending, so you'd have to call those hooks manually.

maybe using scePowerRegisterCallback to let a callback calls those hooks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group