Example #1
0
File: rtti.c Project: ifzz/exlibs-c
void ex_rtti_deinit () {
    if ( __initialized ) {
        // free all allocated string
        ex_hashmap_each ( __typeid_to_rtti, ex_rtti_t *, _rtti ) {
            ex_free(_rtti->props);
            ex_free(_rtti);
        } ex_hashmap_each_end;
        ex_hashmap_free(__typeid_to_rtti);
        __initialized = false;
    }
Example #2
0
static int __draw_poly ( lua_State *_l ) {
    int nargs = lua_gettop(_l);
    int i, num;
    ex_vec2f_t *verts;
    const ex_color4f_t *c;
    bool close;

    // check if the first arguments is table
    luaL_checktype(_l, 1, LUA_TTABLE);
    num = luaL_getn(_l,1);
    verts = (ex_vec2f_t *)ex_malloc( sizeof(ex_vec2f_t) * num );

    for ( i = 0; i < num; ++i ) {
        lua_rawgeti(_l, 1, i+1);
        verts[i] = *ex_lua_tovec2f(_l,-1);
        lua_remove(_l, -1);
    }

    //
    c = &ex_color4f_white;
    close = true;
    if ( nargs == 2 ) {
        c = ex_lua_checkcolor4f(_l,2);
    }
    else if ( nargs == 3 ) {
        c = ex_lua_checkcolor4f(_l,2);
        close = lua_toboolean(_l,3);
    }

    //
    ex_draw_poly ( verts, num, c, close );
    ex_free( verts );

    return 0;
}
Example #3
0
int main (void) {
    int i = 0;
    void *buffer[BUF_SIZE];

    ex_sdk_init ();

    i = 0;
    while ( i < BUF_SIZE ) {
        buffer[i] = ex_malloc( EX_MB(1) );
        ++i;
    }

    i = 0;
    while ( i < BUF_SIZE ) {
        buffer[i] = ex_realloc( buffer[i], EX_KB(1) * rand() % 2048  );
        ++i;
    }

    i = 0;
    while ( i < BUF_SIZE ) {
        ex_free(buffer[i]);
        ++i;
    }

    ex_sdk_deinit ();
    return 0;
}
Example #4
0
File: cstr.c Project: ifzz/exlibs-c
static void normal () {
    // strcpy
    {
        char to[128];
        const char *text = "hello world";

        // right
        memset ( to, 1, 128 );
        strcpy( to, text );
        printf("for break");
    }

    // strncpy
    {
        char to[128];
        const char *text = "hello world";
        int len = strlen(text);

        // wrong
        memset ( to, 1, 128 );
        strncpy( to, text, len );

        // right
        memset ( to, 1, 128 );
        strncpy( to, text, len );
        to[len] = 0;
        printf("for break");
    }

    // strlen
    {
        char *to;
        const char *text = "hello world";
        int len = strlen(text);

        // wrong
        to = ex_malloc( len );
        strcpy( to, text );
        ex_free(to);

        // right
        to = ex_malloc( len+1 );
        strcpy( to, text );
        ex_free(to);
        printf("for break");
    }
}
Example #5
0
void ex_bitarray_free ( ex_bitarray_t *_bitarray )
{
    ex_assert( _bitarray != NULL );

    ex_bitarray_deinit(_bitarray);
    _bitarray->count = 0;
    ex_free(_bitarray);
}
Example #6
0
// make a UDP server/client socket
// When *sServerPort = 0, it is to create an even port.
// When *sServerPort = 1, it is to create an odd port.
amt_sock_t * amt_makeUDPSock(u32 localIP, u16 *sServerPort, u32 remoteIP, u16 remotePort, int reuse)
{
    ex_sock_t *pExSock;
    amt_sock_t *pSock =  (amt_sock_t *) malloc(sizeof(amt_sock_t));
    AMT_ERR_CHECK(pSock!=NULL, NULL, "No Memory");
    pExSock = ex_makeUDPSock(localIP, sServerPort, remoteIP, remotePort, reuse);
    AMT_ERR_CHECK(pExSock!=NULL, NULL, "Failed to make a UDP socket");
    *pSock = *((amt_sock_t *)pExSock);
    ex_free(pExSock);
    return pSock;
}
Example #7
0
TEST( MemMng, alloc, off )
{
    const int BUFFER_SIZE = 5;
    const int MAX_COUNT = 256;

    ClassProbe* pBuffer = ex_new_array( ClassProbe, BUFFER_SIZE );
    for ( int i = 0; i < MAX_COUNT; ++i )
    {
        char* pTestBuffer = (char*)ex_malloc( sizeof(char)*BUFFER_SIZE ); 
        ex_free(pTestBuffer);
    }
    ex_delete_array(pBuffer);
}
Example #8
0
void XmlParser::OnCharacterData ( const tchar* _pData, int _len )
{
    // get white space
    const tchar* white_space_list;
    uint list_count = 0;
    ex::str::getWSList( white_space_list, list_count );

    // skip empty content
    bool validContent = false;
    for ( int i = 0; i < _len; ++i )
    {
        if ( !str::isInTheList( _pData[i], white_space_list, list_count ) )
        {
            validContent = true;
            break;
        }
    }

    // retrun if invalid content ( empty content )
    if ( !validContent )
        return;

    // TODO: implement temp string allocator
    // 
    char* pMallocBuffer = NULL;
    char* pStrBuffer = NULL;
    char str[EX_INT16_MAX+1];

    if ( _len < EX_INT16_MAX+1 )
    {
        pStrBuffer = str;
    }
    else
    {
        pStrBuffer = pMallocBuffer = (char*)ex_malloc( _len + 1 );
    }
    memcpy( pStrBuffer, _pData, _len );
    pStrBuffer[_len] = 0;

    // set contents
    ex_assert( !m_NodeStack.empty(), "Invalid Node Stack" );
    if ( !m_NodeStack.empty() )
    {
        m_NodeStack.top()->SetContent( pStrBuffer );
    }
    ex_free(pMallocBuffer);
}
Example #9
0
int ex_lua_dofile ( lua_State *_l, const char *_filepath, const char *_modname ) {
    int status;
    ex_file_t *file;
    size_t buf_size;
    void *buffer;

    // open the file
    file = ex_fopen_r(_filepath);
    if ( file == NULL ) {
        ex_error ( "can't find the file %s", _filepath );
        return -1;
    }

    // get the file to the buffer we allocated.
    buf_size = ex_fsize (file);
    buffer = ex_malloc (buf_size);
    ex_fread (file, buffer, buf_size );
    ex_fclose(file);

    // parse the buffer by lua interpreter
    status = luaL_loadbuffer( _l, (const char *)buffer, buf_size, _filepath );
    if ( status ) {
        ex_lua_alert(_l);
        goto PARSE_FAILED;
    }

    // call the script 
    if ( _modname ) {
        lua_pushstring(_l,_modname);
        status = lua_pcall(_l, 1, LUA_MULTRET, 0);
    }
    else {
        status = lua_pcall(_l, 0, LUA_MULTRET, 0);
    }
    if ( status ) {
        ex_lua_alert(_l);
        goto PARSE_FAILED;
    }

PARSE_FAILED:
    ex_free(buffer);
    return -1;
}
Example #10
0
File: wiz.c Project: ifzz/exsdk
void wiz_destroy_window ( lua_State *_l, int _refID ) {
    int idx = -1;
    size_t i = 0;
    __window_info_t *win_info;

    // get window
    while ( i < ex_array_count(__window_list) ) {
        win_info = *(__window_info_t **)ex_array_get(__window_list, i);
        if ( win_info->refID == _refID ) {
            break;
        }
        ++i;
    }

    // call win_info.refID.onDestroy
    lua_pushcfunction( _l, ex_lua_trace_back ); // push traceback function
    idx = lua_gettop(_l);
    lua_rawgeti( _l, LUA_REGISTRYINDEX, win_info->refID );
    lua_getfield( _l, -1, "onDestroy" );
    if ( lua_isnil(_l,-1) == 0 && lua_isfunction(_l,-1) ) {
        lua_pushvalue(_l,-2);
        ex_lua_pcall( _l, 1, 0, idx );
        lua_pop ( _l, 1 ); // pop win_info.refID
    }
    else {
        lua_pop ( _l, 2 ); // pop win_info.refID
    }
    lua_pop ( _l, 1 ); // pop traceback function

    //
    ex_array_remove_at_fast ( __window_list, i );
    luaL_unref( _l, LUA_REGISTRYINDEX, win_info->refID );
    SDL_DestroyRenderer( win_info->sdl_renderer );
    SDL_DestroyWindow( win_info->sdl_window );
    ex_free(win_info);
}
Example #11
0
void ex_mempool_free ( ex_mempool_t *_pool ) {
    ex_assert( _pool != NULL );

    ex_mempool_deinit(_pool);
    ex_free(_pool);
}
Example #12
0
File: list.c Project: exdev/exsdk
void ex_list_free ( ex_list_t *_list )
{
    ex_list_deinit(_list);
    ex_free(_list);
}
Example #13
0
static int __lua_font_wrap_text ( lua_State *_l ) {
    ex_font_t *font;
    const char *text, *whitespace;
    int maxWidth;

    const char *str, *nextstr, *word_start;
    char *newtext, *newtext_p, *last_newtext;
    int ch, next_ch, len, newlen, cpylen;
    uint ft_index, prev_ft_index;
    int cur_x, word_start_x;
    ex_glyph_t *glyph;
    bool linebreak, beginningOfLine, trimWhitespace, skipcpy; 
    bool wrapword, collapseSpace, collapseLinebreak;

    // get lua arguments
    ex_lua_check_nargs(_l,5);
    text = luaL_checkstring(_l,1);
    luaL_checktype( _l, 2, LUA_TLIGHTUSERDATA );
    font = lua_touserdata(_l,2);
    whitespace = luaL_checkstring(_l,3);
    maxWidth = luaL_checkint(_l,4);
    trimWhitespace = beginningOfLine = (luaL_checkint(_l,5) == 1);

    //
    len = strlen(text);
    str = nextstr = word_start = text;
    newtext_p = newtext = last_newtext = ex_malloc( len * sizeof(char) );
    prev_ft_index = -1;

    // get wrapMode
    wrapword = false;
    collapseSpace = collapseLinebreak = false;
    if ( !strncmp( whitespace, "pre-wrap", 8 ) ) {
        wrapword = true;
        collapseSpace = false;
        collapseLinebreak = false;
    }
    else if ( !strncmp( whitespace, "pre-line", 8 ) ) {
        wrapword = true;
        collapseSpace = true;
        collapseLinebreak = false;
    }
    else if ( !strncmp( whitespace, "normal", 6 ) ) {
        wrapword = true;
        collapseSpace = true;
        collapseLinebreak = true;
    }
    else if ( !strncmp( whitespace, "nowrap", 6 ) ) {
        wrapword = false;
        collapseSpace = true;
        collapseLinebreak = true;
    }
    else if ( !strncmp( whitespace, "pre", 3 ) ) {
        wrapword = false;
        collapseSpace = false;
        collapseLinebreak = false;
    }

    // process text
    cur_x = word_start_x = 0;
    linebreak = false;
    while ( *str ) {
        skipcpy = false;
        nextstr += utf8proc_iterate ((const uint8_t *)str, -1, &ch);

        // if this is line-break
        if ( ch == '\n' || ch == '\r' ) {
            if ( collapseLinebreak ) {
                ch = ' '; // turn it to space
            }
            else {
                linebreak = true;
            }
        }

        // if this is space 
        if ( ch == ' ' || ch == '\t' || ch == '\f' ) {
            if ( collapseSpace ) {
                const char * nextnextstr = nextstr;
                while ( *nextnextstr ) {
                    nextstr = nextnextstr;
                    nextnextstr += utf8proc_iterate ((const uint8_t *)nextnextstr, -1, &next_ch);

                    // if next_ch is white-space, then collapse this char
                    if ( next_ch == ' ' || next_ch == '\t' || next_ch == '\f' ) {
                        str = nextstr;
                        continue;
                    }

                    // if next_ch is line-break and collapseLinebreak is true, then collapse this char
                    if ( next_ch == '\n' || next_ch == '\r' ) {
                        if ( collapseLinebreak ) {
                            str = nextstr;
                            continue;
                        }
                    }

                    //
                    break;
                }

                // skip first-time collapse
                if ( trimWhitespace ) {
                    trimWhitespace = false;
                    str = nextstr;
                    continue;
                }

                // yes, must turn it to space to make sure only one space
                ch = ' ';
            }
        }

        //
        trimWhitespace = false;

        // process word-break, word-wrap
        if ( wrapword ) {
            word_start = str;
            word_start_x = cur_x;

            // if this character can break
            if ( nextstr == NULL || __can_word_break (ch) ) {
                // advanced character
                ft_index = ex_font_get_index ( font, ch );
                glyph = ex_font_get_glyph ( font, ft_index );
                cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
                cur_x += glyph->advance_x;
                prev_ft_index = ft_index;

                // check if the word exceed content width
                if ( cur_x > maxWidth ) {
                    if ( !beginningOfLine ) {
                        linebreak = true;

                        // skip copy the white-space if it is at the end of the wrap
                        if ( ch == ' ' || ch == '\t' || ch == '\f' ) {
                            skipcpy = true;
                        }
                        else {
                            nextstr = word_start;
                            cur_x = word_start_x;
                        }
                    }
                }

                beginningOfLine = false;
            }
            else {
                // advanced current character
                ft_index = ex_font_get_index ( font, ch );
                glyph = ex_font_get_glyph ( font, ft_index );
                cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
                cur_x += glyph->advance_x;
                prev_ft_index = ft_index;

                const char * nextnextstr = nextstr;
                while ( *nextnextstr ) {
                    nextstr = nextnextstr;
                    nextnextstr += utf8proc_iterate ((const uint8_t *)nextnextstr, -1, &next_ch);

                    // if this character can break
                    if ( __can_word_break (next_ch) ) {
                        break;
                    }

                    // advanced character
                    ft_index = ex_font_get_index ( font, next_ch );
                    glyph = ex_font_get_glyph ( font, ft_index );
                    cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
                    cur_x += glyph->advance_x;
                    prev_ft_index = ft_index;

                    // TODO: process word-break
                    // check if the word exceed content width
                    if ( cur_x > maxWidth ) {
                        if ( !beginningOfLine ) {
                            linebreak = true;

                            nextstr = word_start;
                            cur_x = word_start_x;
                            skipcpy = true;
                            break;
                        }
                    }
                }
            }
        }
        else {
            // advanced character
            ft_index = ex_font_get_index ( font, ch );
            glyph = ex_font_get_glyph ( font, ft_index );
            cur_x += ex_font_get_kerning( font, prev_ft_index, ft_index );
            cur_x += glyph->advance_x;
            prev_ft_index = ft_index;
        } 

        // copy character to newtext_p
        if ( !skipcpy ) {
            cpylen = nextstr - str;
            if ( cpylen > 0 ) {
                strncpy( newtext_p, str, cpylen);
                newtext_p += cpylen;
            }
        }

        // step
        str = nextstr;
        if ( linebreak ) {
            break;
        }
    }

    // text1
    newlen = newtext_p-newtext;
    if ( newlen > 0 ) {
        lua_pushlstring(_l, newtext, newlen);
    }
    else {
        lua_pushnil(_l);
    }

    // text2
    if ( linebreak && *str ) {
        lua_pushstring(_l, str );
    }
    else {
        lua_pushnil(_l);
    }
    lua_pushinteger(_l,cur_x); // width
    lua_pushboolean(_l,linebreak); // line-break

    //
    ex_free(newtext);

    return 4; // text1(can be nil), text2(can be nil), width of text1, linebreak
}
Example #14
0
BinarySerializeNode::~BinarySerializeNode ()
{
    ex_free(m_pData);
}