annotate scn2k/command_handler.cc @ 74:f8751d74918b default tip

Remove “duplicate” functions as they can be remplaced by a nearly-identical existing function.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 02 Apr 2011 19:13:54 +0200
parents f1a27ee7e03c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
1 /*
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
2 * Copyright (c) 2009 Thibaut GIRKA <thib@sitedehib.com>
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
3 * All rights reserved.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
4 *
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
7 * are met:
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
15 *
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
26 */
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
27
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
28 #include <map>
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
29 #include <stdlib.h>
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
30
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
31 #include "scn2k.h"
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
32
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
33 #define CMDMAP_DEBUG 1
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
34
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
35 void CommandHandler::RegisterCommand(int cmd1, int cmd2, int cmd3, const char* descr, CmdImpl func)
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
36 {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
37 if (descr == NULL && func == NULL)
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
38 return;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
39 SimpleCmd cmd(cmd1, cmd2, cmd3);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
40 CommandInfo info;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
41 info.function = func;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
42 info.descr = descr;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
43
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
44 #ifdef CMDMAP_DEBUG
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
45 CommandMap::iterator it;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
46 it = command_map.find(cmd);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
47 if (it != command_map.end()) {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
48 printf("ERROR: Command '%s' already registered as '%s'!\n", descr, it->second.descr);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
49 abort();
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
50 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
51 #endif
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
52
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
53 command_map[cmd] = info;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
54 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
55
55
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
56 void CommandHandler::PrintCmd(Cmd& cmd)
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
57 {
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
58 CommandMap::iterator it;
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
59 it = command_map.find(cmd);
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
60
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
61 fprintf(stderr, "%d - %d : ", cmd.scn, cmd.pos);
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
62 if (it == command_map.end())
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
63 fprintf(stderr, "%02d:%02d:%04d", cmd.cmd1, cmd.cmd2, cmd.cmd3);
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
64 else
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
65 fprintf(stderr, "%s", it->second.descr);
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
66 fprintf(stderr, "[%d] (", cmd.cmd4);
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
67 int i;
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
68 for (i=0; i<cmd.args.size(); i++) {
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
69 VarInfo info = cmd.args[i];
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
70 if (info.type == TYPE_STR || info.type == TYPE_VARSTR)
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
71 fprintf(stderr, "\"%s\"", cmd.Str(info));
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
72 else
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
73 fprintf(stderr, "%d", info.value);
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
74 if (i < cmd.args.size()-1)
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
75 fprintf(stderr, ", ");
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
76 }
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
77 fprintf(stderr,");\n");
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
78 }
f1a27ee7e03c * started the same changes on scn2k_text.cc
thib
parents: 54
diff changeset
79
54
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
80 bool CommandHandler::Exec(Cmd &cmd) {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
81 CommandMap::iterator it;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
82
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
83 it = command_map.find(cmd);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
84 if (it == command_map.end()) {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
85 //printf("Unrecognized opcode %d %d %d\n", cmd.cmd1, cmd.cmd2, cmd.cmd3); //Debug
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
86 return false;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
87 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
88 else {
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
89 CmdImpl func = it->second.function;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
90 if (func == NULL) printf("Function %s not implemented yet\n", it->second.descr);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
91 else (this->*func)(cmd);
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
92 return true;
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
93 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
94 }
d7cde171a1de * scn2k_grp.cc now handles commands in a cleanier way \o/
thib
parents:
diff changeset
95