diff options
Diffstat (limited to 'src/core/commandbuffer.cc')
-rw-r--r-- | src/core/commandbuffer.cc | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc index d1c9451..40517e1 100644 --- a/src/core/commandbuffer.cc +++ b/src/core/commandbuffer.cc @@ -20,6 +20,21 @@ namespace core { +void func_print(std::string const &args) +{ + con_print << args << "\n"; +} + +void func_print_file(std::string const &args) +{ + std::istringstream argstream(args); + std::string filename; + if (!(argstream >> filename)) + return; + + CommandBuffer::print_file(filename); +} + void func_list_func(std::string const &args) { Func::list(); @@ -75,7 +90,6 @@ std::stringstream CommandBuffer::cmdbuf(std::stringstream::in | std::stringstrea void CommandBuffer::init() { //con_debug << "Initializing command buffer...\n"; - Func *func = 0; func = Func::add("list_ent", (FuncPtr)func_list_ent); func->set_info("list entities"); @@ -89,6 +103,12 @@ void CommandBuffer::init() func = Func::add("set", (FuncPtr)func_set); func->set_info("[variable] [str] set variable value"); + func = Func::add("print", func_print); + func->set_info("[str] print a message on the console"); + + func = Func::add("print_file", func_print_file); + func->set_info("[filename] print messages from file"); + func = Func::add("exec", (FuncPtr)func_exec); func->set_info("[filename] execute commands from file"); } @@ -101,6 +121,9 @@ void CommandBuffer::shutdown() Func::remove("list_var"); Func::remove("list_func"); Func::remove("list_ent"); + Func::remove("print"); + Func::remove("print_file"); + Func::remove("exec"); } void CommandBuffer::exec(std::string const &cmdline) @@ -263,5 +286,32 @@ void CommandBuffer::exec_file(std::string const & filename) ifs.close(); } +void CommandBuffer::print_file(std::string const & filename) +{ + filesystem::File *f = filesystem::open(filename.c_str()); + if (!f) { + con_warn << "Could not open " << filename << std::endl; + return; + } + + std::string fn = f->path(); + fn.append(f->name()); + filesystem::close(f); + + std::ifstream ifs; + ifs.open(fn.c_str()); + if (!ifs.is_open()) { + con_warn << "Could not stream " << fn << "!\n"; + return; + } + + char line[MAXCMDSIZE]; + while (ifs.getline(line, MAXCMDSIZE-1)) { + con_print << line << "\n"; + } + + ifs.close(); +} + } |