User:AerialX/Coding Sandbox/Boost.Regex
This is an old revision of this page, as edited by AerialX (talk | contribs) at 09:42, 26 August 2008. It may differ significantly from the current revision. |
This is a simple example of how to use Boost's regular expressions library on the Wii. The example is a very simple usage of Boost.Regex. Note that while all the Boost headers are included (and therefore most of Boost's functionality), I've only included the precompiled binary for Boost.Regex. The other libraries that require precompilation were not included as I haven't tested them yet. To compile boost libraries for the Wii, I loosely followed these instructions (making the appropritate changes for devkitPPC). If anyone does want others precompiled because they're too lazy to do so themselves, you can contact me since I have them all (untested); it's just impractical to upload 900MB worth of libraries for me.
Anyway, all of the Boost headers have worked perfectly for me, as well as the Boost.Regex library. As per the nature of Boost, this of course only works with C++.
Download
The download contains the source and binary of the simple example program, as well as a precompiled boost_regex static library (no sources for it though; Boost is huge). Boost v1.35.0 was used to compile this; I'm aware that 1.36.0 has been released, but the Boost.Regex library was not changed. The supplied binaries were compiled with devkitPPC r15 and libogc r20080602 on Linux x86_64.
Usage
To use my sample you must use the homebrew channel and wiiload to send arguments. Run it without arguments to see the usage instructions: USAGE: wiiload regex.dol "string" "regex pattern" "replacement"
For those who don't feel like conjuring up the parameters on their own, feel free to try wiiload regex.dol "y halo thar" "h\w+ " "hello " It should transform "y halo thar" to "y hello thar"
template.cpp
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <boost/regex.hpp>
#include <string>
using std::string;
using boost::regex;
using boost::regex_replace;
static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------
// Initialise the video system
VIDEO_Init();
// This function initialises the attached controllers
WPAD_Init();
// Obtain the preferred video mode from the system
// This will correspond to the settings in the Wii menu
rmode = VIDEO_GetPreferredMode(NULL);
// Allocate memory for the display in the uncached region
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
// Initialise the console, required for printf
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
// Set up the video registers with the chosen mode
VIDEO_Configure(rmode);
// Tell the video hardware where our display memory is
VIDEO_SetNextFramebuffer(xfb);
// Make the display visible
VIDEO_SetBlack(FALSE);
// Flush the video register changes to the hardware
VIDEO_Flush();
// Wait for Video setup to complete
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
// The console understands VT terminal escape codes
// This positions the cursor on row 2, column 0
// we can use variables for this with format codes too
// e.g. printf ("\x1b[%d;%dH", row, column );
printf("\x1b[2;0H");
if (argc != 4) {
printf("USAGE: wiiload regex.dol \"string\" \"regex pattern\" \"replacement\"");
} else {
// Make the replacement via Boost::Regex
string output = regex_replace(string(argv[1]), regex(string(argv[2])), string(argv[3]), (boost::regex_constants::_match_flags)(boost::match_default | boost::regex_constants::format_perl));
printf("Original: %s\nPattern: %s\nReplacement: %s\nOutput: %s\n", argv[1], argv[2], argv[3], output.c_str());
}
while(1) {
// Call WPAD_ScanPads each loop, this reads the latest controller states
WPAD_ScanPads();
// WPAD_ButtonsDown tells us which buttons were pressed in this loop
// this is a "one shot" state which will not fire again until the button has been released
u32 pressed = WPAD_ButtonsDown(0);
// We return to the launcher application via exit
if ( pressed & WPAD_BUTTON_HOME ) exit(0);
// Wait for the next frame
VIDEO_WaitVSync();
}
return 0;
}