X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/2b1e3b1eda0915dede0146e8ea5c183a0483acee..f3b7e5f4b4d7c87ee3e8827313ec966ea8fc8387:/src/bindings/lua/lua_utils.cpp diff --git a/src/bindings/lua/lua_utils.cpp b/src/bindings/lua/lua_utils.cpp index 82a3689fb6..758da4083c 100644 --- a/src/bindings/lua/lua_utils.cpp +++ b/src/bindings/lua/lua_utils.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2010-2019. The SimGrid Team. +/* Copyright (c) 2010-2020. The SimGrid Team. * All rights reserved. * * This program is free software; you can redistribute it and/or modify it @@ -13,63 +13,58 @@ /* SimGrid Lua helper functions */ #include "lua_utils.hpp" #include +#include +#include /** * @brief Returns a string representation of a value in the Lua stack. * * This function is for debugging purposes. - * It always returns the same pointer. * * @param L the Lua state * @param index index in the stack * @return a string representation of the value at this index */ -const char* sglua_tostring(lua_State* L, int index) +std::string sglua_tostring(lua_State* L, int index) { - - static char buff[64]; + std::string buff; switch (lua_type(L, index)) { - case LUA_TNIL: - snprintf(buff, 4, "nil"); + buff = "nil"; break; case LUA_TNUMBER: - snprintf(buff, 64, "%.3f", lua_tonumber(L, index)); + buff = simgrid::xbt::string_printf("%.3f", lua_tonumber(L, index)); break; case LUA_TBOOLEAN: - snprintf(buff, 64, "%s", lua_toboolean(L, index) ? "true" : "false"); + buff = lua_toboolean(L, index) ? "true" : "false"; break; case LUA_TSTRING: - snprintf(buff, 63, "'%s'", lua_tostring(L, index)); + buff = simgrid::xbt::string_printf("'%s'", lua_tostring(L, index)); break; case LUA_TFUNCTION: - if (lua_iscfunction(L, index)) { - snprintf(buff, 11, "C-function"); - } else { - snprintf(buff, 9, "function"); - } + buff = lua_iscfunction(L, index) ? "C-function" : "function"; break; case LUA_TTABLE: - snprintf(buff, 64, "table(%p)", lua_topointer(L, index)); + buff = simgrid::xbt::string_printf("table(%p)", lua_topointer(L, index)); break; case LUA_TLIGHTUSERDATA: case LUA_TUSERDATA: - snprintf(buff, 64, "userdata(%p)", lua_touserdata(L, index)); + buff = simgrid::xbt::string_printf("userdata(%p)", lua_touserdata(L, index)); break; case LUA_TTHREAD: - snprintf(buff, 7, "thread"); + buff = "thread"; break; default: - snprintf(buff, 64, "unknown(%d)", lua_type(L, index)); + buff = simgrid::xbt::string_printf("unknown(%d)", lua_type(L, index)); break; } return buff; @@ -78,19 +73,12 @@ const char* sglua_tostring(lua_State* L, int index) /** * @brief Returns a string representation of a key-value pair. * - * It always returns the same pointer. - * * @param L the Lua state * @param key_index index of the key (in the lua stack) * @param value_index index of the value (in the lua stack) * @return a string representation of the key-value pair */ -const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) +std::string sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) { - - static char buff[64]; - /* value_tostring also always returns the same pointer */ - int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index)); - snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index)); - return buff; + return std::string("[") + sglua_tostring(L, key_index) + "] -> " + sglua_tostring(L, value_index); }