Advertisement
PauSix

PSP File IO main.c

Feb 20th, 2017
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. // include psp stuff
  2. #include <pspkernel.h>
  3. #include <pspdebug.h>
  4. #include <pspdisplay.h>
  5.  
  6. // calloc, malloc, relloc
  7. #include <stdlib.h>
  8.  
  9. // include commons stuff
  10. #include "../common/callback.h"
  11.  
  12.  
  13. // configure PSP stuff
  14. #define VERS    1
  15. #define REVS    0
  16.  
  17. PSP_MODULE_INFO("FileIO", PSP_MODULE_USER, VERS, REVS);
  18. PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
  19.  
  20. // make printing easier on us
  21. #define printf pspDebugScreenPrintf
  22.  
  23.  
  24. #define ROOT "umd0:/"
  25.  
  26.  
  27.  
  28. void crash(int error, const char* crasher, const char* message) {
  29.     sceDisplayWaitVblankStart();
  30.     printf("Error (%d) : %s\n", error, crasher);
  31.     printf("%s\n", message);
  32.     sceKernelSleepThread();
  33. }
  34.  
  35. void test_crash() {
  36.     sceDisplayWaitVblankStart();
  37.     printf("No crash occured.");
  38.     sceKernelSleepThread();
  39. }
  40.  
  41.  
  42.  
  43. void check_file(SceIoStat* info, const char* path) {
  44.     // open file description
  45.     int status;
  46.     if((status = sceIoGetstat(path, info)) < 0)
  47.         crash(status, "Checking File", "File not found or no access!");
  48. }
  49.  
  50. int open_file(const char* path, int params, int chmod) {
  51.     // open file handle
  52.     int uid = sceIoOpen(path, params, chmod);
  53.     if(uid < 0)
  54.         crash(uid, "Opening File", "File not found or no access!");
  55.     return uid;
  56. }
  57.  
  58. void close_file(int uid) {
  59.     int status;
  60.     if((status = sceIoClose(uid)) < 0)
  61.         crash(status, "Close File", "File could not be closed!");
  62. }
  63.  
  64. void write_file(int uid, const char* out, int size) {
  65.     // write to file
  66.     if(sceIoWrite(uid, out, size) != size)
  67.         crash(uid, "Writing File", "File wrote incorrect number of bytes!");
  68. }
  69.  
  70. void read_file(int uid, char** out, int size) {
  71.     // allocate, read into buffer, pad with \0
  72.     char* buffer = (char*)calloc(size+1, sizeof(char));
  73.     if(buffer == 0)
  74.         crash(0, "Memory Allocation", "Memory allocation failed!");
  75.     int read = sceIoRead(uid, buffer, size);
  76.     if(read != size)
  77.         crash(read, "Read File", "File size read doesn't match expected!");
  78.     buffer[size] = '\0';
  79.     *out = buffer;
  80. }
  81.  
  82.  
  83.  
  84. void do_example1() {
  85.     const char* src = ROOT "test.txt";
  86.    
  87.     // Reading file (existant) src > (char*) file_data
  88.     char* file_data;
  89.     SceIoStat info;
  90.     check_file(&info, src);
  91.    
  92.     printf("%d\n", (int)info.st_size);
  93.    
  94.     int uid = open_file(src, PSP_O_RDONLY, 0777);
  95.     read_file(uid, &file_data, (int)info.st_size);
  96.     close_file(uid);
  97.    
  98.     printf("Read data:\n");
  99.     printf("%s\n", file_data);
  100.     free(file_data);
  101.     file_data = 0;
  102. }
  103.  
  104. void do_example2() {
  105.     const char* new = ROOT "test2.txt";
  106.     const char output[] = "Hello World 123\nabc";
  107.    
  108.     // Writing new file output[] > (non-existant) test2.txt
  109.     int uid = open_file(new, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
  110.     write_file(uid, output, sizeof(output)-1);
  111.     close_file(uid);
  112. }
  113.  
  114. void do_example3() {
  115.     const char* src = ROOT "test.txt";
  116.     const char* out = ROOT "diff.txt";
  117.    
  118.     // Copy file test.txt > diff.txt
  119.     char* file_data;
  120.     SceIoStat info;
  121.     check_file(&info, src);
  122.    
  123.     // multiple files can be open at once, but since using one variable
  124.     // closing the file would be wise before losing the data
  125.     int uid = open_file(src, PSP_O_RDONLY, 0777);
  126.     read_file(uid, &file_data, (int)info.st_size);
  127.     close_file(uid);
  128.    
  129.     uid = open_file(out, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777);
  130.     write_file(uid, file_data, (int)info.st_size);
  131.     close_file(uid);
  132.    
  133.     free(file_data);
  134.     file_data = 0;
  135. }
  136.  
  137.  
  138.  
  139. int main(int argc, char** argv)
  140. {
  141.     // basic init
  142.     setupExitCallback();
  143.     pspDebugScreenInit();
  144.    
  145.     sceDisplayWaitVblankStart();
  146.     pspDebugScreenSetXY(0, 0);
  147.  
  148.     printf("Reading file\n");
  149.     do_example1();
  150.     printf("Done.\n");
  151.     printf("Writing file\n");
  152.     do_example2();
  153.     printf("Done.\n");
  154.     printf("Copying file\n");
  155.     do_example3();
  156.     printf("Done.\n");
  157.    
  158.     // Sleep thread
  159.     sceKernelSleepThread();
  160.    
  161.     sceKernelExitGame();
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement