tutorial:file_operations

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial:file_operations [2020/11/19 22:18]
iridescence
tutorial:file_operations [2021/12/23 09:45] (current)
daveon_newearthlife [File Operations]
Line 2: Line 2:
  
 In most software applications that require large sets of data or interaction which cannot be expressed before compile time, we use files to store large sets of data and other information that we may require for our programs. These files and folders can be accessed by our program in order to carry out the program's intended purpose. This could be configuration files in formats like JSON, INI, PROPERTIES, TOML, etc. or maybe textures for your games and applications or audio for music playback and so many more uses! In most software applications that require large sets of data or interaction which cannot be expressed before compile time, we use files to store large sets of data and other information that we may require for our programs. These files and folders can be accessed by our program in order to carry out the program's intended purpose. This could be configuration files in formats like JSON, INI, PROPERTIES, TOML, etc. or maybe textures for your games and applications or audio for music playback and so many more uses!
 +
 +
 +
  
 ===== General Theory ===== ===== General Theory =====
Line 31: Line 34:
 **main.cpp** **main.cpp**
 <code C++> <code C++>
 +#include <iostream>
 +#include <fstream>
 +#include <string>
  
 +// Rest is the same
 +
 +auto main() -> int {
 +    setupCallbacks();
 +
 +    std::ofstream file("test.txt");
 +    file << "1 2 3! Hello PSP!" << std::endl;
 +    file.close();
 +
 +    std::ifstream file2("test.txt");
 +    std::string str;
 +    std::getline(file2, str);
 +    file2.close();
 +
 +    pspDebugScreenInit();
 +    pspDebugScreenPrintf(str.c_str());
 +}
 </code> </code>
 +
 +In the C example we use the C FILE* in order to open the files via fopen and uses fprintf and fread in order to work with the file handle. In C++ we have to use an ifstream and ofstream to read and write respectively. The C example also will work in C++ - and you technically only need one FILE* in C. A pure fstream does not work in this instance on the PSP. In C++ we also use the getline method to read until a newline.
  
 ===== Rust ===== ===== Rust =====
Line 38: Line 63:
 **main.rs** **main.rs**
 <code Rust> <code Rust>
 +#![no_std]
 +#![no_main]
  
 +psp::module!("Tutorial", 1, 0);
 +
 +use psp::sys::{IoOpenFlags};
 +use core::ffi::c_void;
 +
 +pub fn psp_main() {
 +    psp::enable_home_button();
 +    
 +    unsafe {
 +        let fd = psp::sys::sceIoOpen(b"ms0:/PSP/GAME/Rust/test.txt\0".as_ptr(), IoOpenFlags::CREAT|IoOpenFlags::WR_ONLY, 0777 );
 +        psp::sys::sceIoWrite(fd, b"1 2 3! Hello PSP!\n\0".as_ptr() as *const c_void , 19);
 +        psp::sys::sceIoClose(fd);
 +
 +        let fd2 = psp::sys::sceIoOpen(b"ms0:/PSP/GAME/Rust/test.txt\0".as_ptr(), IoOpenFlags::RD_ONLY, 0777 );
 +        let array: [u8; 19] = [0; 19];
 +
 +        psp::sys::sceIoRead(fd, array.as_ptr() as *mut c_void, 19);
 +        psp::sys::sceIoClose(fd2);
 +
 +        if let Ok(s) = core::str::from_utf8(&array[0..]) {
 +            psp::dprintln!("{}", s);
 +        }
 +    }
 +
 +}
 </code> </code>
 +
 +In Rust, we use the IoFileMgr module in order to work on our application. This application requires that the hardcoded path is valid. We then write to a file opened in create mode and then close it. We then reopen it in read mode and then read to our array. Finally, we derive a string from the raw utf8 and print it out to the screen.
  
 ===== Zig ===== ===== Zig =====
Line 45: Line 99:
 **main.zig** **main.zig**
 <code Rust> <code Rust>
 +const psp = @import("Zig-PSP/src/psp/utils/psp.zig");
 +usingnamespace @import("Zig-PSP/src/psp/include/pspdisplay.zig");
 +const gfx = @import("gfx.zig");
 +pub const os = @import("Zig-PSP/src/psp/pspos.zig");
 +pub const panic = @import("Zig-PSP/src/psp/utils/debug.zig").panic;
 +const std = @import("std");
 +
 +comptime {
 +    asm(psp.module_info("Zig Tutorial", 0, 1, 0));
 +}
 +
 +pub fn main() !void {
 +    psp.utils.enableHBCB();
 +    
 +    var f = try std.fs.cwd().createFile("test.txt", .{ .truncate = true });
 +    var writ = f.writer();
 +    try writ.print("1 2 3! Hello PSP!", .{});
 +    f.close();
 +
 +    var fr = try std.fs.cwd().openFile("test.txt", .{ .read = true });
 +    var read = f.reader();
 +    var buf : [20]u8 = undefined;
 +    var read_size = try read.readAll(buf[0..]);
 +    fr.close();
 +
 +    psp.debug.screenInit();
 +    try psp.debug.printFormat("{}", .{buf[0..read_size]});
 +}
  
 </code> </code>
 +
 +In Zig, we set up the Custom OS for STD via pub const os = @import("pspos.zig");. This sets up our custom OS so that Zig can support our application. Then it's just a matter of creating a file in truncate mode in order to overwrite it. Then we simply use print to print out, and read into the buffer to read our text back in. Finally, we use the printFormat method in order to create a printed string.
  • tutorial/file_operations.1605824315.txt.gz
  • Last modified: 2020/11/19 22:18
  • by iridescence