using the SceIO functions

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
PsPfReAK
Posts: 61
Joined: Sat Mar 28, 2009 9:02 am
Contact:

using the SceIO functions

Post by PsPfReAK »

can someone show me how to write a file from ms to flash0, minus the assigning and reassgning flash in write

i only need to learn how to write stuff from a desired source to another.

thanks :D
User avatar
Coldbird
Posts: 97
Joined: Thu Feb 08, 2007 7:22 am

Post by Coldbird »

Don't quite know what you're after but I hope this little example out of my PRX Library can help you out.

Code: Select all

#include <pspiofilemgr.h>

int appendBufferToFile&#40;const char * path, void * buffer, int buflen&#41;
&#123;
  // Written Bytes
  int written = 0;
  
  // Open File for Appending
  SceUID file = sceIoOpen&#40;path, PSP_O_APPEND | PSP_O_CREAT | PSP_O_WRONLY, 0777&#41;;
  
  // Opened File
  if&#40;file >= 0&#41;
  &#123;
    // Write Buffer
    written = sceIoWrite&#40;file, buffer, buflen&#41;;
    
    // Close File
    sceIoClose&#40;file&#41;;
  &#125;
  
  // Return Written Bytes
  return written;
&#125;
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
PsPfReAK
Posts: 61
Joined: Sat Mar 28, 2009 9:02 am
Contact:

Post by PsPfReAK »

okay, say that i want to write a rco from ms0:/whatever.rco to flash0:/whatever.rco

how would i go about doing that?

like

Sce....<i dont know how to use it, so im asking how?

noob example? with HEAVY comments?

not a sample please, i would like to learn how to do it, noy copy and paste some elses code.

okay say this, continue the code, you are flashing a file from ms0 to flash0, minus the assinging of flash

Code: Select all

 break;
assignign flash0 blah blah...

///you guys help me here by input the commands, ie writing a file from mso to flash0
User avatar
Coldbird
Posts: 97
Joined: Thu Feb 08, 2007 7:22 am

Post by Coldbird »

Alright... Minus the assigning of flash you say...

Code: Select all

#include <pspiofilemgr.h> // Include for Sony IO Functions
#include <stdio.h> // Include for Seek Constants
#include <string.h> // Include for NULL Macro

SceOff fileSize&#40;SceUID file&#41;
&#123;
  // File Length
  SceOff length = 0;
  
  // Sanity Check for Valid File Handler
  if&#40;file >= 0&#41;
  &#123;
    // Save Position in File
    SceOff pos = sceIoLseek&#40;file, 0, SEEK_CUR&#41;;
    
    // Move to EOF & Grab Location &#40;Size&#41;
    length = sceIoLseek&#40;file, 0, SEEK_END&#41;;
    
    // Restore Position in File
    sceIoLseek&#40;file, pos, SEEK_SET&#41;;
  &#125;
  
  // Return File Length
  return length;
&#125;

int copyFile&#40;const char * from, const char * to&#41;
&#123;
  // Result &#40;0 = Worked, 1 = Failed&#41;
  int result = 0;
  
  // Sanity Check to avoid NULL Pointers
  if&#40;from && to&#41;
  &#123;
    // Open Input File
    SceUID infile = sceIoOpen&#40;from, PSP_O_RDONLY, 0777&#41;;
    
    // Opened Input File
    if&#40;infile >= 0&#41;
    &#123;
      // Get Filesize
      SceOff insize = fileSize&#40;infile&#41;;
      
      // Open Output File
      SceUID outfile = sceIoOpen&#40;to, PSP_O_WRONLY | PSP_O_CREAT, 0777&#41;;
      
      // Opened Output File
      if&#40;outfile >= 0&#41;
      &#123;
        // Transfer Buffer
        unsigned char buffer&#91;1024&#93;;
        
        // Copy Buffer Blocks
        int copied = 0;
        while&#40;copied < insize&#41;
        &#123;
          // Read Block from Input File
          int read = sceIoRead&#40;infile, buffer, 1024&#41;;
          
          // Written Bytes
          int written = sceIoWrite&#40;outfile, buffer, read&#41;;
          
          // Write Block to Output File
          if&#40;written&#41; copied += written;
          
          // Writing Error
          else
          &#123;
            // Change Result
            result = 1;
            
            // Break out of Loop
            break;
          &#125;
        &#125;
      &#125;
    &#125;
  &#125;
  
  // Return Result
  return result;
&#125;
Be warned. I quickly wrote this up. It's untested.
Anyway, it should give you a pretty good idea how to work with the sceIo API.
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
PsPfReAK
Posts: 61
Joined: Sat Mar 28, 2009 9:02 am
Contact:

Post by PsPfReAK »

so would this be adequate?

Code: Select all

#include <pspsdk.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <pspiofilemgr.h> // Include for Sony IO Functions


////////////////////////////////////////////////////////////////////////////////
#define MODULE "test"
////////////////////////////////////////////////////////////////////////////////
PSP_MODULE_INFO&#40;MODULE, 0x0800, 1, 0&#41;; 
PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_VSH&#41;;
////////////////////////////////////////////////////////////////////////////////
#define p2c pspDebugScreenPrintf
#define RED 0x111FFF
#define WHITE 0xFFFFF1
#define GREEN 0x0000FF00
#define TextColor pspDebugScreenSetTextColor
#define ClearScreen pspDebugScreenClear
#define test_size 20
char test_buffer&#91;test_size&#93;;
int readSize;

////////////////////////////////////////////////////////////////////////////////
char msg&#91;256&#93;;
int cbid;
int thid;
int written;
int file_size;
/////
SceUID fd;
/////
////////////////////////////////////////
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
    sceKernelExitGame&#40;&#41;;
	return 0; &#125;
////////////////////////////////////////
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
    cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
    sceKernelRegisterExitCallback&#40;cbid&#41;;
    sceKernelSleepThreadCB&#40;&#41;;
	return 0; &#125;
////////////////////////////////////////
int SetupCallbacks&#40;void&#41; &#123;
    thid = 0;
    thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, THREAD_ATTR_USER, 0&#41;;
    if &#40;thid >= 0&#41;
	sceKernelStartThread&#40;thid, 0, 0&#41;;
    return thid; &#125;
////////////////////////////////////////
void ErrorExit&#40;int milisecs, char *fmt, ...&#41; &#123;
	va_list list;
	va_start&#40;list, fmt&#41;;
	vsprintf&#40;msg, fmt, list&#41;;
	va_end&#40;list&#41;;
	p2c&#40;msg&#41;;
	sceKernelDelayThread&#40;milisecs*1000&#41;;
	sceKernelExitGame&#40;&#41;; &#125;
////////////////////////////////////////
SceOff fileSize&#40;SceUID file&#41;
&#123;
  // File Length
  SceOff length = 0;
 
  // Sanity Check for Valid File Handler
  if&#40;file >= 0&#41;
  &#123;
    // Save Position in File
    SceOff pos = sceIoLseek&#40;file, 0, SEEK_CUR&#41;;
   
    // Move to EOF & Grab Location &#40;Size&#41;
    length = sceIoLseek&#40;file, 0, SEEK_END&#41;;
   
    // Restore Position in File
    sceIoLseek&#40;file, pos, SEEK_SET&#41;;
  &#125;
 
  // Return File Length
  return length;
&#125;

int copyFile&#40;const char * from, const char * to&#41;
&#123;
  // Result &#40;0 = Worked, 1 = Failed&#41;
  int result = 0;
 
  // Sanity Check to avoid NULL Pointers
  if&#40;from && to&#41;
  &#123;
    // Open Input File
    SceUID infile = sceIoOpen&#40;from, PSP_O_RDONLY, 0777&#41;;
   
    // Opened Input File
    if&#40;infile >= 0&#41;
    &#123;
      // Get Filesize
      SceOff insize = fileSize&#40;infile&#41;;
     
      // Open Output File
      SceUID outfile = sceIoOpen&#40;to, PSP_O_WRONLY | PSP_O_CREAT, 0777&#41;;
     
      // Opened Output File
      if&#40;outfile >= 0&#41;
      &#123;
        // Transfer Buffer
        unsigned char buffer&#91;1024&#93;;
       
        // Copy Buffer Blocks
        int copied = 0;
        while&#40;copied < insize&#41;
        &#123;
          // Read Block from Input File
          int read = sceIoRead&#40;infile, buffer, 1024&#41;;
         
          // Written Bytes
          int written = sceIoWrite&#40;outfile, buffer, read&#41;;
         
          // Write Block to Output File
          if&#40;written&#41; copied += written;
         
          // Writing Error
          else
          &#123;
            // Change Result
            result = 1;
           
            // Break out of Loop
            break;
          &#125;
        &#125;
      &#125;
    &#125;
  &#125;
 
  // Return Result
  return result;
&#125;
////////////////////////////////////////
void remove_files&#40;char *files&#41; &#123;
     p2c&#40;"Removing File %s....", files&#41;;
     sceIoRemove&#40;files&#41;;
     p2c&#40;"OK\n"&#41;; &#125;
////////////////////////////////////////
// copies the binary file contents from the input file to the output file
int copyFileContents&#40;SceUID* InputFilePointer, SceUID* OutputFilePointer&#41;
&#123;
	int readsize=512; // just an arbitrary value, generally larger chunks are faster in file copies due to the way sectors are handled on disk writes
	u8 buffer&#91;readsize&#93;;
	int bytesRead, bytesWritten;
	bytesRead = sceIoRead&#40;*InputFilePointer, buffer, readSize&#41;;
	bytesWritten = sceIoWrite&#40;*OutputFilePointer, buffer, bytesRead&#41;;
	if&#40;bytesWritten != bytesRead&#41;
	&#123;
		// write error
		
		return -1;
	&#125;
	return 1; // bytecopy successful
&#125;
///////////////////////////////////////
int main&#40;&#41; &#123;
	pspDebugScreenInit&#40;&#41;;
	TextColor&#40;WHITE&#41;;
	SetupCallbacks&#40;&#41;;
    	
	p2c&#40;"test to\n"&#41;;
    p2c&#40;"flash test.txt with contents to flash0&#58;/\n"&#41;;
    p2c&#40;"To do so Press X to start at your own risk, R to exit.\n\n"&#41;;
    
	while &#40;1&#41; &#123;
        SceCtrlData pad;
        sceCtrlReadBufferPositive&#40;&pad, 1&#41;;

		if &#40;pad.Buttons & PSP_CTRL_CROSS&#41; &#123;
                        p2c&#40;"Assigning flash0.... "&#41;;                       
                        if &#40;sceIoUnassign&#40;"flash0&#58;"&#41; < 0&#41;
                        p2c&#40;"Cannot Un-Assign flash0&#58;"&#41;;
                        if &#40;sceIoAssign&#40;"flash0&#58;", "lflash0&#58;0,0", "flashfat0&#58;", IOASSIGN_RDWR, NULL, 0&#41; < 0&#41; &#123;
                                             p2c&#40;"Error Assigning flash0 in write mode"&#41;; &#125;
                        p2c&#40;"OK\n\n"&#41;;
						
                        copyFile&#40;"flash0&#58;/test.txt"&#41;;
                        break; &#125;

		else if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41; &#123;
             p2c&#40;"\n\nExited by the user"&#41;;
             sceKernelExitGame&#40;&#41;; &#125;

		sceKernelDelayThread&#40;10000&#41;; &#125;
		
		p2c&#40;"\n\nPress X to Exit"&#41;;
		
		while &#40;1&#41; &#123;
        SceCtrlData pad;
        sceCtrlReadBufferPositive&#40;&pad, 1&#41;;

		if &#40;pad.Buttons & PSP_CTRL_CROSS&#41; &#123;
                        break; &#125;

		sceKernelDelayThread&#40;10000&#41;; &#125;
	
	sceKernelExitGame&#40;&#41;;

	return 0; &#125;
////////////////////////////////////////////////////////////////////////////////
User avatar
Coldbird
Posts: 97
Joined: Thu Feb 08, 2007 7:22 am

Post by Coldbird »

Nope. Wouldn't.
copyFile("flash0:/test.txt"); -> wrong
copyFile("ms0:/test.txt", "flash0:/test.txt"); -> right

This command would then copy the file ms0:/test.txt to flash0:/test.txt.
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Post Reply