Skip to content

Instantly share code, notes, and snippets.

@lifthrasiir
Last active December 12, 2015 02:29
Show Gist options
  • Save lifthrasiir/4699308 to your computer and use it in GitHub Desktop.
Save lifthrasiir/4699308 to your computer and use it in GitHub Desktop.
비교분석 C vs. Rust
#include <stdio.h>
static const char VERSION[] = "Angolmois 2.0.0 alpha 2";
static const char *argv0 = "angolmois";
static const char *preset, *leftkeys, *rightkeys;
static enum mode { PLAY_MODE, AUTOPLAY_MODE, EXCLUSIVE_MODE } opt_mode = PLAY_MODE;
static enum modf { NO_MODF, MIRROR_MODF, SHUFFLE_MODF, SHUFFLEEX_MODF, RANDOM_MODF, RANDOMEX_MODF } opt_modf = NO_MODF;
static enum bga { BGA_AND_MOVIE, BGA_BUT_NO_MOVIE, NO_BGA } opt_bga = BGA_AND_MOVIE;
static int opt_showinfo = 1, opt_fullscreen = 1, opt_joystick = -1;
static double playspeed = 1;
static void die(const char *msg, ...); /* platform dependent */
static int play(void)
{
/* ... */
return 0;
}
int usage(void)
{
fprintf(stderr,
"%s -- the simple BMS player\n"
"http://mearie.org/projects/angolmois/\n\n"
"Usage: %s <options> <path>\n"
" Accepts any BMS, BME, BML or PMS file.\n"
" Resources should be in the same directory as the BMS file.\n\n"
"Options:\n"
" -h, --help This help\n"
" -V, --version Shows the version\n"
" -a #.#, --speed #.# Sets the initial play speed (default: 1.0x)\n"
" -# Same as '-a #.0'\n"
" -v, --autoplay Enables AUTO PLAY (viewer) mode\n"
" -x, --exclusive Enables exclusive (BGA and sound only) mode\n"
" -X, --sound-only Enables sound only mode, equivalent to -xB\n"
" --fullscreen Enables the fullscreen mode (default)\n"
" -w, --no-fullscreen Disables the fullscreen mode\n"
" --info Shows a brief information about the song (default)\n"
" -q, --no-info Do not show an information about the song\n"
" -m, --mirror Uses a mirror modifier\n"
" -s, --shuffle Uses a shuffle modifier\n"
" -S, --shuffle-ex Uses a shuffle modifier, even for scratches\n"
" -r, --random Uses a random modifier\n"
" -R, --random-ex Uses a random modifier, even for scratches\n"
" -k NAME, --preset NAME Forces a use of given key preset (default: bms)\n"
" -K LEFT RIGHT, --key-spec LEFT RIGHT\n"
" Sets a custom key specification (see the manual)\n"
" --bga Loads and shows the BGA (default)\n"
" -B, --no-bga Do not load and show the BGA\n"
" -M, --no-movie Do not load and show the BGA movie\n"
" -j #, --joystick # Enable the joystick with index # (normally 0)\n\n"
"Environment Variables:\n"
" ANGOLMOIS_1P_KEYS=<scratch>|<key 1>|<2>|<3>|<4>|<5>|<6>|<7>|<pedal>\n"
" ANGOLMOIS_2P_KEYS=<pedal>|<key 1>|<2>|<3>|<4>|<5>|<6>|<7>|<scratch>\n"
" ANGOLMOIS_PMS_KEYS=<key 1>|<2>|<3>|<4>|<5>|<6>|<7>|<8>|<9>\n"
" ANGOLMOIS_SPEED_KEYS=<speed down>|<speed up>\n"
" ANGOLMOIS_XXy_KEY=<keys for channel XX and channel kind y>\n"
" Sets keys used for game play. Use either SDL key names or joystick names\n"
" like 'button #' or 'axis #' can be used. Separate multiple keys by '%%'.\n"
" See the manual for more information.\n\n",
VERSION, argv0);
return 1;
}
int main(int argc, char **argv)
{
static const char *longargs[] =
{"h--help", "V--version", "a--speed", "v--autoplay", "x--exclusive",
"X--sound-only", "w--windowed", "w--no-fullscreen", " --fullscreen",
" --info", "q--no-info", "m--mirror", "s--shuffle", "S--shuffle-ex",
"r--random", "R--random-ex", "k--preset", "K--key-spec", " --bga",
"B--no-bga", " --movie", "M--no-movie", "j--joystick", NULL};
char buf[512] = "", *arg;
int i, j;
#define FETCH_ARG(arg, opt) \
if (argv[i] && ((arg) = j<1 || argv[i][++j] ? argv[i]+j : argv[++i])) ++i, j = 0; \
else die("No argument to the option -%c", (opt)) \
argv0 = argv[0];
for (i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
if (!bmspath) bmspath = argv[i];
} else if (!strcmp(argv[i], "--")) {
if (!bmspath) bmspath = argv[++i];
break;
} else {
if (argv[i][1] == '-') {
for (j = 0; longargs[j]; ++j) {
if (!strcmp(argv[i], longargs[j]+1)) {
argv[i][1] = longargs[j][0];
argv[i][2] = '\0';
break;
}
}
if (!longargs[j]) die("Invalid option: %s", argv[i]);
}
for (j = 1; argv[i][j]; ++j) {
switch (argv[i][j]) {
case 'h': return usage();
case 'V': printf("%s\n", VERSION); return 0;
case 'v': opt_mode = AUTOPLAY_MODE; break;
case 'x': opt_mode = EXCLUSIVE_MODE; break;
case 'X': opt_mode = EXCLUSIVE_MODE; opt_bga = NO_BGA; break;
case 'w': opt_fullscreen = 0; break;
case 'q': opt_showinfo = 0; break;
case 'm': opt_modf = MIRROR_MODF; break;
case 's': opt_modf = SHUFFLE_MODF; break;
case 'S': opt_modf = SHUFFLEEX_MODF; break;
case 'r': opt_modf = RANDOM_MODF; break;
case 'R': opt_modf = RANDOMEX_MODF; break;
case 'k': FETCH_ARG(preset, 'k'); goto endofarg;
case 'K': FETCH_ARG(leftkeys, 'K'); FETCH_ARG(rightkeys, 'K'); goto endofarg;
case 'a':
FETCH_ARG(arg, 'a');
playspeed = atof(arg);
if (playspeed <= 0) playspeed = 1;
if (playspeed < .1) playspeed = .1;
if (playspeed > 99) playspeed = 99;
goto endofarg;
case 'B': opt_bga = NO_BGA; break;
case 'M': opt_bga = BGA_BUT_NO_MOVIE; break;
case 'j': FETCH_ARG(arg, 'j'); opt_joystick = atoi(arg); goto endofarg;
case ' ': break; /* for ignored long options */
default:
if (argv[i][j] >= '1' && argv[i][j] <= '9') {
playspeed = argv[i][j] - '0';
} else {
die("Invalid option: -%c", argv[i][j]);
}
}
}
endofarg:;
}
}
//if (!bmspath && filedialog(buf)) bmspath = buf;
return bmspath ? play() : usage();
}
extern mod std;
use io::WriterUtil;
pure fn version() -> ~str { ~"Angolmois 2.0.0 alpha 2 (rust edition)" }
trait StrUtils {
pure fn slice_to_end(begin: uint) -> ~str;
pure fn each_chari_byte(it: fn(uint, char) -> bool);
}
impl &str: StrUtils {
pure fn slice_to_end(begin: uint) -> ~str { self.slice(begin, self.len()) }
pure fn each_chari_byte(it: fn(uint, char) -> bool) {
let mut pos = 0u;
let len = self.len();
while pos < len {
let str::CharRange {ch, next} = str::char_range_at(self, pos);
if !it(pos, ch) { break; }
pos = next;
}
}
}
enum Mode { PlayMode, AutoPlayMode, ExclusiveMode }
enum Modf { NoModf, MirrorModf, ShuffleModf, ShuffleExModf, RandomModf, RandomExModf }
enum Bga { BgaAndMovie, BgaButNoMovie, NoBga }
struct Options {
mut bmspath: Option<~str>,
mut mode: Mode,
mut modf: Modf,
mut bga: Bga,
mut showinfo: bool,
mut fullscreen: bool,
mut joystick: Option<int>,
mut preset: Option<~str>,
mut leftkeys: Option<~str>,
mut rightkeys: Option<~str>,
mut playspeed: float
}
fn play(opts: &Options) {
repr::write_repr(io::stdout(), opts);
io::println("");
}
fn usage() {
let args = os::args();
let exename = if args.is_empty() { ~"angolmois" } else { copy args[0] };
io::stderr().write_str(
version() + ~" -- the simple BMS player\n"
+ ~"http://mearie.org/projects/angolmois/\n\n"
+ ~"Usage: " + exename + ~" <options> <path>\n"
+ ~" Accepts any BMS, BME, BML or PMS file.\n"
+ ~" Resources should be in the same directory as the BMS file.\n\n"
+ ~"Options:\n"
+ ~" -h, --help This help\n"
+ ~" -V, --version Shows the version\n"
+ ~" -a #.#, --speed #.# Sets the initial play speed (default: 1.0x)\n"
+ ~" -# Same as '-a #.0'\n"
+ ~" -v, --autoplay Enables AUTO PLAY (viewer) mode\n"
+ ~" -x, --exclusive Enables exclusive (BGA and sound only) mode\n"
+ ~" -X, --sound-only Enables sound only mode, equivalent to -xB\n"
+ ~" --fullscreen Enables the fullscreen mode (default)\n"
+ ~" -w, --no-fullscreen Disables the fullscreen mode\n"
+ ~" --info Shows a brief information about the song (default)\n"
+ ~" -q, --no-info Do not show an information about the song\n"
+ ~" -m, --mirror Uses a mirror modifier\n"
+ ~" -s, --shuffle Uses a shuffle modifier\n"
+ ~" -S, --shuffle-ex Uses a shuffle modifier, even for scratches\n"
+ ~" -r, --random Uses a random modifier\n"
+ ~" -R, --random-ex Uses a random modifier, even for scratches\n"
+ ~" -k NAME, --preset NAME Forces a use of given key preset (default: bms)\n"
+ ~" -K LEFT RIGHT, --key-spec LEFT RIGHT\n"
+ ~" Sets a custom key specification (see the manual)\n"
+ ~" --bga Loads and shows the BGA (default)\n"
+ ~" -B, --no-bga Do not load and show the BGA\n"
+ ~" -M, --no-movie Do not load and show the BGA movie\n"
+ ~" -j #, --joystick # Enable the joystick with index # (normally 0)\n\n"
+ ~"Environment Variables:\n"
+ ~" ANGOLMOIS_1P_KEYS=<scratch>|<key 1>|<2>|<3>|<4>|<5>|<6>|<7>|<pedal>\n"
+ ~" ANGOLMOIS_2P_KEYS=<pedal>|<key 1>|<2>|<3>|<4>|<5>|<6>|<7>|<scratch>\n"
+ ~" ANGOLMOIS_PMS_KEYS=<key 1>|<2>|<3>|<4>|<5>|<6>|<7>|<8>|<9>\n"
+ ~" ANGOLMOIS_SPEED_KEYS=<speed down>|<speed up>\n"
+ ~" ANGOLMOIS_XXy_KEY=<keys for channel XX and channel kind y>\n"
+ ~" Sets keys used for game play. Use either SDL key names or joystick names\n"
+ ~" like 'button #' or 'axis #' can be used. Separate multiple keys by '%%'.\n"
+ ~" See the manual for more information.\n\n");
os::set_exit_status(1);
}
fn main() {
let longargs = std::map::hash_from_vec::<&str,char>([
("--help", 'h'), ("--version", 'V'), ("--speed", 'a'),
("--autoplay", 'v'), ("--exclusive", 'x'), ("--sound-only", 'X'),
("--windowed", 'w'), ("--no-fullscreen", 'w'), ("--fullscreen", ' '),
("--info", ' '), ("--no-info", 'q'), ("--mirror", 'm'),
("--shuffle", 's'), ("--shuffle-ex", 'S'), ("--random", 'r'),
("--random-ex", 'R'), ("--preset", 'k'), ("--key-spec", 'K'),
("--bga", ' '), ("--no-bga", 'B'), ("--movie", ' '),
("--no-movie", 'M'), ("--joystick", 'j'),
]);
let args = os::args();
let nargs = args.len();
let opts = ~Options { bmspath: None, mode: PlayMode, modf: NoModf, bga: BgaAndMovie,
showinfo: true, fullscreen: true, joystick: None,
preset: None, leftkeys: None, rightkeys: None, playspeed: 1.0 };
let mut i = 1;
while i < nargs {
if !args[i].starts_with("-") {
if opts.bmspath.is_none() { opts.bmspath = Some(copy args[i]); }
} else if args[i] == ~"--" {
if opts.bmspath.is_none() { opts.bmspath = Some(copy args[i]); }
break;
} else {
let shortargs =
if args[i].starts_with("--") {
match longargs.find(args[i]) {
Some(c) => str::from_char(c),
None => fail fmt!("Invalid option: %s", args[i])
}
} else {
args[i].slice_to_end(1)
};
let nshortargs = shortargs.len();
let mut inside = true;
for shortargs.each_chari_byte |j, c| {
let fetch_arg: fn(char) -> ~str = |opt| { // XXX doesn't work without sig
let off = if inside { j + 1 } else { j };
let nextarg =
if inside && off < nshortargs {
shortargs.slice_to_end(off)
} else {
i += 1;
if i < nargs {
copy args[i]
} else {
fail fmt!("No argument to the option -%c", opt);
}
};
inside = false;
nextarg
};
match c {
'h' => { usage(); },
'V' => { io::println(version()); return; },
'v' => { opts.mode = AutoPlayMode; },
'x' => { opts.mode = ExclusiveMode; },
'X' => { opts.mode = ExclusiveMode; opts.bga = NoBga; },
'w' => { opts.fullscreen = false; },
'q' => { opts.showinfo = false; },
'm' => { opts.modf = MirrorModf; },
's' => { opts.modf = ShuffleModf; },
'S' => { opts.modf = ShuffleExModf; },
'r' => { opts.modf = RandomModf; },
'R' => { opts.modf = RandomExModf; },
'k' => { opts.preset = Some(fetch_arg('k')); },
'K' => {
opts.leftkeys = Some(fetch_arg('K'));
opts.rightkeys = Some(fetch_arg('K'));
},
'a' => match float::from_str(fetch_arg('a')) {
Some(speed) if speed > 0.0 => {
opts.playspeed =
if speed < 0.1 { 0.1 }
else if speed > 99.0 { 99.0 }
else { speed };
},
_ => fail fmt!("Invalid argument to option -a")
},
'B' => { opts.bga = NoBga; },
'M' => { opts.bga = BgaButNoMovie; },
'j' => match int::from_str(fetch_arg('j')) {
Some(idx) if idx >= 0 => { opts.joystick = Some(idx); },
_ => fail fmt!("Invalid argument to option -j")
},
' ' => {}, // for ignored long options
'1'..'9' => { opts.playspeed = char::to_digit(c, 10).get() as float; },
_ => fail fmt!("Invalid option: -%c", c)
}
if !inside { break; }
}
}
i += 1;
}
//if opts.bmspath.is_none() { opts.bmspath = filedialog(); }
if opts.bmspath.is_none() { usage(); } else { play(opts); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment