"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/Diluculum/InternalUtils.cpp" (12 May 2020, 5506 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /******************************************************************************\
    2 * InternalUtils.cpp                                                            *
    3 * Miscellaneous utilities used internally by Diluculum.                        *
    4 *                                                                              *
    5 *                                                                              *
    6 * Copyright (C) 2005-2013 by Leandro Motta Barros.                             *
    7 *                                                                              *
    8 * Permission is hereby granted, free of charge, to any person obtaining a copy *
    9 * of this software and associated documentation files (the "Software"), to     *
   10 * deal in the Software without restriction, including without limitation the   *
   11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or  *
   12 * sell copies of the Software, and to permit persons to whom the Software is   *
   13 * furnished to do so, subject to the following conditions:                     *
   14 *                                                                              *
   15 * The above copyright notice and this permission notice shall be included in   *
   16 * all copies or substantial portions of the Software.                          *
   17 *                                                                              *
   18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
   19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
   20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE *
   21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
   22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
   23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS *
   24 * IN THE SOFTWARE.                                                             *
   25 \******************************************************************************/
   26 
   27 
   28 #include "InternalUtils.hpp"
   29 #include <Diluculum/LuaUtils.hpp>
   30 #include <cstring>
   31 #include <boost/lexical_cast.hpp>
   32 
   33 namespace Diluculum
   34 {
   35    namespace Impl
   36    {
   37       // - CallFunctionOnTop ---------------------------------------------------
   38       LuaValueList CallFunctionOnTop (lua_State* ls, const LuaValueList& params)
   39       {
   40          int topBefore = lua_gettop (ls);
   41 
   42          if (lua_type (ls, -1) != LUA_TFUNCTION)
   43             throw TypeMismatchError ("function", luaL_typename (ls, -1));
   44 
   45          typedef LuaValueList::const_iterator iter_t;
   46          for (iter_t p = params.begin(); p != params.end(); ++p)
   47             PushLuaValue (ls, *p);
   48 
   49          int status = lua_pcall (ls, params.size(), LUA_MULTRET, 0);
   50 
   51          ThrowOnLuaError (ls, status);
   52 
   53          int numResults = lua_gettop (ls) - topBefore + 1;
   54 
   55          LuaValueList results;
   56 
   57          for (int i = numResults; i > 0; --i)
   58             results.push_back (ToLuaValue (ls, -i));
   59 
   60          lua_pop (ls, numResults);
   61 
   62          return results;
   63       }
   64 
   65 
   66 
   67       // - ThrowOnLuaError -----------------------------------------------------
   68       void ThrowOnLuaError (lua_State* ls, int statusCode)
   69       {
   70          if (statusCode != 0)
   71          {
   72             std::string errorMessage;
   73             if (lua_isstring (ls, -1))
   74             {
   75                errorMessage = lua_tostring (ls, -1);
   76                lua_pop (ls, 1);
   77             }
   78             else
   79             {
   80                errorMessage =
   81                   "Sorry, there is no additional information about this error.";
   82             }
   83 
   84             switch (statusCode)
   85             {
   86                case LUA_ERRRUN:
   87                   throw LuaRunTimeError (errorMessage.c_str());
   88                case LUA_ERRFILE:
   89                   throw LuaFileError (errorMessage.c_str());
   90                case LUA_ERRSYNTAX:
   91                   throw LuaSyntaxError (errorMessage.c_str());
   92                case LUA_ERRMEM:
   93                   throw LuaMemoryError (errorMessage.c_str());
   94                case LUA_ERRERR:
   95                   throw LuaErrorError (errorMessage.c_str());
   96                default:
   97                   throw LuaError ("Unknown Lua return code passed "
   98                                   "to 'Diluculum::Impl::ThrowOnLuaError()'.");
   99             }
  100          }
  101       }
  102 
  103 
  104 
  105       // - LuaFunctionWriter ---------------------------------------------------
  106       int LuaFunctionWriter(lua_State* luaState, const void* data, size_t size,
  107                             void* func)
  108       {
  109          Diluculum::LuaFunction* f =
  110             reinterpret_cast<Diluculum::LuaFunction*>(func);
  111 
  112          size_t newSize = f->getSize() + size;
  113 
  114          boost::scoped_array<char> buff (new char[newSize]);
  115 
  116          memcpy (buff.get(), f->getData(), f->getSize());
  117          memcpy (buff.get() + f->getSize(), data, size);
  118 
  119          f->setData (buff.get(), newSize);
  120 
  121          return 0;
  122       }
  123 
  124 
  125 
  126       // - LuaFunctionReader ---------------------------------------------------
  127       const char* LuaFunctionReader(lua_State* luaState, void* func,
  128                                     size_t* size)
  129       {
  130          Diluculum::LuaFunction* f =
  131             reinterpret_cast<Diluculum::LuaFunction*>(func);
  132 
  133          if (f->getReaderFlag())
  134             return 0;
  135 
  136          f->setReaderFlag (true); // return 0 on the next call
  137 
  138          *size = f->getSize();
  139          return reinterpret_cast<const char*>(f->getData());
  140       }
  141 
  142    } // namespace Impl
  143 
  144 } // namespace Diluculum