How to work with file I/O in kernel PRX?
How to work with file I/O in kernel PRX?
Hey,
I'm writing a KPRX plugin and I need to work with files.
I tried fopen and simillar functions (I can't tell it better - just that F functions like fopen, fread and so on) and Cygwin gave me errors like undefined reference to 'fopen'. So next I tried sceIo functions and my PSP crashes after start (after start of plugin).
Is there any way to work with files in Kernel PRX plugin? How to check "If file 'config.ini' exist" -> No: create it wih default values -> Yes: read value 1 and set variable A to that value for example?
I'll be happy for replies. Thanks..
kweensey
I'm writing a KPRX plugin and I need to work with files.
I tried fopen and simillar functions (I can't tell it better - just that F functions like fopen, fread and so on) and Cygwin gave me errors like undefined reference to 'fopen'. So next I tried sceIo functions and my PSP crashes after start (after start of plugin).
Is there any way to work with files in Kernel PRX plugin? How to check "If file 'config.ini' exist" -> No: create it wih default values -> Yes: read value 1 and set variable A to that value for example?
I'll be happy for replies. Thanks..
kweensey
Hey,
firstly, thanks for reply. Secondary, really I can? I didn't find some guide to learn what I have in example in my first post to do it with sceIo so I only tried check file and if doesn't exist so create it and my PSP crashed. Could you write an algorithm If file "file.ini" exists then read number 1 (from file) and set variable A to it and then read number 2 and set variable B to it. Else create new "file.ini" and write default numbers to it.
Thank you.
kweensey
firstly, thanks for reply. Secondary, really I can? I didn't find some guide to learn what I have in example in my first post to do it with sceIo so I only tried check file and if doesn't exist so create it and my PSP crashed. Could you write an algorithm If file "file.ini" exists then read number 1 (from file) and set variable A to it and then read number 2 and set variable B to it. Else create new "file.ini" and write default numbers to it.
Thank you.
kweensey
Some thing like this:
This will store/read the raw values like how its stored in memory. Its not meant to be human readable. If you want to have and editable file with numbers like a=15 then you will have to read it like a string and write code to interpret it, since fopen/fscanf doesn't work in kernel mode.
Code: Select all
int a=10, b=15;
int bytesRead;
SceUID fd = sceIoOpen("file.ini");
if (fd < 0) //doesn't exist or some other error while opening
{
if((fd = sceIoOpen("file.ini", PSP_O_WRONLY|PSP_O_TRUNC|PSP_O_CREAT, 0777)) >= 0) //successfully created file
{
sceIoWrite(fd, &a, sizeof(int));
sceIoWrite(fd, &b, sizeof(int));
sceIoClose(fd);
}
}
else
{
bytesRead = sceIoRead(fd, &a, sizeof(int));
if (bytesRead != sizeof(int))
{
//error reading a
}
//you probably shouldn't read b if reading a failed
bytesRead = sceIoRead(fd, &b, sizeof(int));
if (bytesRead != sizeof(int))
{
//error reading b
}
sceIoClose(fd);
}
So I tried that and my PSP successfully started.. :) But that's all. All my plugin works good but no file created.. :(
EDIT: I gave some visible actions for file create, error while reading and successfully read and I discovered nothing of that was executed.. I don't get it.. Why? Everything before and after works great..
EDIT: I gave some visible actions for file create, error while reading and successfully read and I discovered nothing of that was executed.. I don't get it.. Why? Everything before and after works great..
Last edited by kweensey on Wed Feb 11, 2009 6:13 am, edited 1 time in total.
-
- Posts: 388
- Joined: Tue Aug 12, 2008 12:46 am
Torch,
I tested which part of conditions is executed in code you wrote here and it is if (fd < 0) (true) and if((fd = sceIoOpen("file.ini", PSP_O_WRONLY|PSP_O_TRUNC|PSP_O_CREAT, 0777)) >= 0) (else). That means file wasn't created, right? So that script works but why it can't create that file?
Thank you.
kweensey
I tested which part of conditions is executed in code you wrote here and it is if (fd < 0) (true) and if((fd = sceIoOpen("file.ini", PSP_O_WRONLY|PSP_O_TRUNC|PSP_O_CREAT, 0777)) >= 0) (else). That means file wasn't created, right? So that script works but why it can't create that file?
Thank you.
kweensey
I've always had to specify the path myself, else it'd think the files not there, for me. Usually however people set up a little thing in module_start function or main that would set the preset path(like, say you have a 40 byte buffer) to sceIoChdir so that way it'd be all ready.
Programming with:
Geany + Latest PSPSDK from svn
Geany + Latest PSPSDK from svn
int module_start(SceSize args, void *argp)kweensey wrote:Ehm.. :)
I know I'm noob, but could you give me some example, please? I don't understand what you mean..
Thanks.
kweensey
{
...
strcpy(some_buffer, argp);
sceIoChdir(some_buffer);
...
}
that's what works for me, though(not sure if it's just prx, because I don't make kernel eboots) you'll probably have to trim the filename off it.
For example, it'd give you like ms0:/seplugins/module.prx for a prx, so you'd note that the file length is 10 characters.
int module_start(SceSize args, void *argp)
{
...
memcpy(some_buffer, strlen(argp)-10);
sceIoChdir(some_buffer);
...
}
so that way it'd give you the directory rather than the file.
Programming with:
Geany + Latest PSPSDK from svn
Geany + Latest PSPSDK from svn
I only got this in module_start():
Shall I add
there?
Code: Select all
int module_start(SceSize args, void *argp)
{
int thid = sceKernelCreateThread("sthing", main_thread, 10, 0x2000, 0, NULL);
if(thid >= 0)
{
sceKernelStartThread(thid, args, argp);
}
return 0;
}
Code: Select all
strcpy(some_buffer, argp);
sceIoChdir(some_buffer);
memcpy(some_buffer, strlen(argp)-10);
sceIoChdir(some_buffer);
I know torch, I was saying, since my module's name that I was using in the example was 10 chars, you would get the length of the module you are using(lol8.prx would be 8, loolz.prx would be 9, z.prx would be 5, and so on).
But yeah, I knew some source I was gandering at had it, of course the one I didn't look at had it.
But yeah, I knew some source I was gandering at had it, of course the one I didn't look at had it.
Programming with:
Geany + Latest PSPSDK from svn
Geany + Latest PSPSDK from svn
Torch: That was example, I just wanted to know, if my "module_start" doesn't contain memspy or strcpy so shall I add
strcpy(some_buffer, argp);
sceIoChdir(some_buffer);
memcpy(some_buffer, strlen(argp)-10);
sceIoChdir(some_buffer);
there? (Instead of 10 will be different number of course.) And may that some_buffer be new buffer or that has to be some specific? And if I add that to "module_start" so that's all? Will work with files work good?
I saw to VLF sample and I didn't see anything special about save/load configuration.. I tried to save/load in normal EBOOT and that's allright but in KPRX I can't do that..
Thank you for help me, that's important for me..
moonlight: Thank you, I will.
strcpy(some_buffer, argp);
sceIoChdir(some_buffer);
memcpy(some_buffer, strlen(argp)-10);
sceIoChdir(some_buffer);
there? (Instead of 10 will be different number of course.) And may that some_buffer be new buffer or that has to be some specific? And if I add that to "module_start" so that's all? Will work with files work good?
I saw to VLF sample and I didn't see anything special about save/load configuration.. I tried to save/load in normal EBOOT and that's allright but in KPRX I can't do that..
Thank you for help me, that's important for me..
moonlight: Thank you, I will.
I really don't get you, Torch.. :( I have "/" symbol only for notes or as dividing (i.e. 24/2 - 24:2)..
Could you make me a kprx, which loads variable from file, plese? I know you think I only want to copy that and have it without work but I'll study that code and I maybe understand that. It is very important to me and that is needed thing for my plugin.. :(
Thank you..
kweensey
Could you make me a kprx, which loads variable from file, plese? I know you think I only want to copy that and have it without work but I'll study that code and I maybe understand that. It is very important to me and that is needed thing for my plugin.. :(
Thank you..
kweensey