Example #1
0
int CLuaFileDefs::fileRead ( lua_State* luaVM )
{
    // string fileRead ( file, count )

    // Grab the file pointer
    CScriptFile* pFile = NULL;
    unsigned long ulCount = 0;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pFile );
    argStream.ReadNumber ( ulCount );

    if ( !argStream.HasErrors ( ) )
    {
        if ( pFile )
        {
            if ( ulCount > 0 )
            {
                // Allocate a buffer to read the stuff into and read some shit into it
                char* pReadContent = new char [ulCount + 1];
                long lBytesRead = pFile->Read ( ulCount, pReadContent );

                if ( lBytesRead != -1 )
                {
                    // Push the string onto the lua stack. Use pushlstring so we are binary
                    // compatible. Normal push string takes zero terminated strings.
                    lua_pushlstring ( luaVM, pReadContent, lBytesRead );
                }
                else
                {
                    m_pScriptDebugging->LogBadPointer ( luaVM, "file", 1 );
                    lua_pushnil ( luaVM );
                }

                // Delete our read content. Lua should've stored it
                delete[] pReadContent;

                // We're returning the result string
                return 1;
            }
            else
            {
                // Reading zero bytes from a file results in an empty string
                lua_pushstring ( luaVM, "" );
                return 1;
            }
        }
        else
            m_pScriptDebugging->LogBadPointer ( luaVM, "file", 1 );
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // Error
    lua_pushnil ( luaVM );
    return 1;
}
Example #2
0
int CLuaFileDefs::fileRead ( lua_State* luaVM )
{
    // string fileRead ( file, count )
    CScriptFile* pFile;
    unsigned long ulCount = 0;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pFile );
    argStream.ReadNumber ( ulCount );

    if ( !argStream.HasErrors ( ) )
    {
        if ( ulCount > 0 )
        {
            // Allocate a buffer to read the stuff into and read some shit into it
            CBuffer buffer;
            long lBytesRead = pFile->Read ( ulCount, buffer );

            if ( lBytesRead != -1 )
            {
                // Push the string onto the lua stack. Use pushlstring so we are binary
                // compatible. Normal push string takes zero terminated strings.
                lua_pushlstring ( luaVM, buffer.GetData(), lBytesRead );
            }
            else
            {
                m_pScriptDebugging->LogBadPointer ( luaVM, "file", 1 );
                lua_pushnil ( luaVM );
            }

            // We're returning the result string
            return 1;
        }
        else
        {
            // Reading zero bytes from a file results in an empty string
            lua_pushstring ( luaVM, "" );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    lua_pushnil ( luaVM );
    return 1;
}