diff --git a/library/Core.cpp b/library/Core.cpp index 1482aac82..04b885fa7 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -91,9 +91,35 @@ struct Core::Cond void cheap_tokenise(string const& input, vector &output) { - istringstream str(input); - istream_iterator cur(str), end; - output.assign(cur, end); + string *cur = NULL; + + for (unsigned i = 0; i < input.size(); i++) { + char c = input[i]; + if (isspace(c)) { + cur = NULL; + } else { + if (!cur) { + output.push_back(""); + cur = &output.back(); + } + + if (c == '"') { + for (i++; i < input.size(); i++) { + c = input[i]; + if (c == '"') + break; + else if (c == '\\') { + if (++i < input.size()) + cur->push_back(input[i]); + } + else + cur->push_back(c); + } + } else { + cur->push_back(c); + } + } + } } struct IODATA diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index ab7e039a4..783c6507c 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -59,6 +59,7 @@ DFHACK_PLUGIN(filltraffic filltraffic.cpp) DFHACK_PLUGIN(seedwatch seedwatch.cpp) DFHACK_PLUGIN(initflags initflags.cpp) DFHACK_PLUGIN(stockpiles stockpiles.cpp) +DFHACK_PLUGIN(rename rename.cpp) #DFHACK_PLUGIN(versionosd versionosd.cpp) # this is the skeleton plugin. If you want to make your own, make a copy and then change it diff --git a/plugins/rename.cpp b/plugins/rename.cpp new file mode 100644 index 000000000..3b6951061 --- /dev/null +++ b/plugins/rename.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +using std::vector; +using std::string; +using std::endl; +using namespace DFHack; +using namespace df::enums; + +using df::global::ui; +using df::global::world; + +static command_result rename(Core * c, vector & parameters); + +DFhackCExport const char * plugin_name ( void ) +{ + return "rename"; +} + +DFhackCExport command_result plugin_init (Core *c, std::vector &commands) +{ + commands.clear(); + if (world && ui) { + commands.push_back(PluginCommand("rename", "Rename various things.", rename)); + } + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +static command_result usage(Core *c) +{ + c->con << "Usage:" << endl + << " rename squad \"name\"" << endl + << " rename hotkey \"name\"" << endl; + return CR_OK; +} + +static command_result rename(Core * c, vector ¶meters) +{ + CoreSuspender suspend(c); + + string cmd; + if (!parameters.empty()) + cmd = parameters[0]; + + if (cmd == "squad") { + if (parameters.size() != 3) + return usage(c); + + std::vector &squads = world->squads.all; + + int id = atoi(parameters[1].c_str()); + if (id < 1 || id > squads.size()) { + c->con.printerr("Invalid squad index\n"); + return usage(c); + } + + squads[id-1]->alias = parameters[2]; + } else if (cmd == "hotkey") { + if (parameters.size() != 3) + return usage(c); + + int id = atoi(parameters[1].c_str()); + if (id < 1 || id > 16) { + c->con.printerr("Invalid hotkey index\n"); + return usage(c); + } + + ui->main.hotkeys[id-1].name = parameters[2]; + } else { + if (!parameters.empty() && cmd != "?") + c->con.printerr("Invalid command: %s\n", cmd.c_str()); + return usage(c); + } + + return CR_OK; +}