From 711a4a159bf3d41b36f5720df734e7cd61a78a63 Mon Sep 17 00:00:00 2001 From: Milan Toth Date: Wed, 15 Mar 2023 23:44:09 +0100 Subject: [PATCH] v0.91b, KineticUI update --- meson.build | 36 ++----- src/kinetic_ui/egl/ku_gl.c | 33 +++--- src/kinetic_ui/egl/ku_gl_floatbuffer.c | 2 +- src/kinetic_ui/egl/ku_gl_shader.c | 2 +- src/kinetic_ui/handler/vh_cv_evnt.c | 16 +++ src/kinetic_ui/handler/vh_tbl_evnt.c | 6 +- src/kinetic_ui/handler/vh_tbl_scrl.c | 9 +- src/kinetic_ui/ku_connector_wayland.c | 4 +- src/kinetic_ui/ku_draw.c | 11 +- src/kinetic_ui/ku_view.c | 13 ++- src/kinetic_ui/ku_window.c | 5 +- src/mt_core/mt_memory.c | 123 ++++----------------- src/mt_core/mt_path.c | 143 ++++++++++++++----------- src/mt_core/mt_vector.c | 30 ++++-- 14 files changed, 193 insertions(+), 240 deletions(-) diff --git a/meson.build b/meson.build index 20c3af9..73f6a82 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project( 'vmp', 'c', - version: '0.9b', + version: '0.91b', license: 'MIT', default_options: ['warning_level=3', 'c_std=gnu99'] @@ -9,17 +9,15 @@ project( cc = meson.get_compiler('c') -png = cc.find_library('png') +egl = dependency('egl') +png = dependency('libpng') +wegl = dependency('wayland-egl') math = cc.find_library('m') -sdl2 = cc.find_library('SDL2') -freetype = cc.find_library('freetype') +sdl2 = dependency('sdl2') +glesv2 = dependency('glesv2') +freetype = dependency('freetype2') xkbcommon = dependency('xkbcommon') -gl = cc.find_library('GL') -egl = cc.find_library('EGL') -wegl = cc.find_library('wayland-egl') -glew = cc.find_library('GLEW') - avutil = cc.find_library('avutil') avcodec = cc.find_library('avcodec') avdevice = cc.find_library('avdevice') @@ -71,9 +69,8 @@ vmp_dependencies = [ png, freetype, math, - gl, - glew, sdl2, + glesv2, avutil, avcodec, avdevice, @@ -109,25 +106,8 @@ vmp_inc = include_directories( 'src/mt_core_ext') if build_machine.system() == 'freebsd' - vmp_inc = [ - vmp_inc, - include_directories( - '/usr/local/include', - '/usr/local/include/GLES2', - '/usr/local/include/EGL', - '/usr/local/include/SDL2', - '/usr/local/include/freetype2')] epoll = cc.find_library('epoll-shim') vmp_dependencies += epoll -else - vmp_inc = [ - vmp_inc, - include_directories( - '/usr/include', - '/usr/include/GLES2', - '/usr/include/EGL', - '/usr/include/SDL2', - '/usr/include/freetype2')] endif com_sources = ['src/vmp/analyzer.c', diff --git a/src/kinetic_ui/egl/ku_gl.c b/src/kinetic_ui/egl/ku_gl.c index fab2a7f..01c6619 100644 --- a/src/kinetic_ui/egl/ku_gl.c +++ b/src/kinetic_ui/egl/ku_gl.c @@ -3,7 +3,6 @@ #include "ku_bitmap.c" #include "mt_vector.c" -/* #include */ #include #include #include @@ -31,7 +30,6 @@ void ku_gl_save_framebuffer(ku_bitmap_t* bitmap); typedef struct _glbuf_t { GLuint vbo; - GLuint vao; } glbuf_t; typedef struct _gltex_t @@ -76,7 +74,7 @@ glsha_t ku_gl_create_texture_shader() "{" " highp float alpha = vUv.w;" " highp vec4 col = texture2D(sampler[0], vUv.xy);" - " if (alpha < 1.0) col.w *= alpha;" + " if (alpha != 1.0) col.w = alpha;" " gl_FragColor = col;" "}"; @@ -102,13 +100,6 @@ glbuf_t ku_gl_create_vertex_buffer() glGenBuffers(1, &vb.vbo); // DEL 0 glBindBuffer(GL_ARRAY_BUFFER, vb.vbo); - glGenVertexArrays(1, &vb.vao); // DEL 1 - glBindVertexArray(vb.vao); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 24, 0); - glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 24, (const GLvoid*) 8); - glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); return vb; @@ -119,7 +110,6 @@ glbuf_t ku_gl_create_vertex_buffer() void ku_gl_delete_vertex_buffer(glbuf_t buf) { glDeleteBuffers(1, &buf.vbo); - glDeleteVertexArrays(1, &buf.vao); } /* create texture */ @@ -136,8 +126,9 @@ gltex_t ku_gl_create_texture(int index, uint32_t w, uint32_t h) glActiveTexture(GL_TEXTURE0 + tex.index); glBindTexture(GL_TEXTURE_2D, tex.tx); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + /* !!! GL_LINEAR breaks session record/replay determinism, interpolated values can somehow differ */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); @@ -167,10 +158,6 @@ void ku_gl_delete_texture(gltex_t tex) void ku_gl_init(int max_dev_width, int max_dev_height) { - GLenum err = glewInit(); - if (GLEW_OK != err) - mt_log_debug("GLEW Init error %s\n", glewGetErrorString(err)); - kugl.shader = ku_gl_create_texture_shader(); kugl.buffer = ku_gl_create_vertex_buffer(); @@ -430,7 +417,10 @@ void ku_gl_add_vertexes(mt_vector_t* views) void ku_gl_render(ku_bitmap_t* bitmap) { /* glBindFramebuffer(GL_FRAMEBUFFER, tex_frame.fb); */ - glBindVertexArray(kugl.buffer.vao); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 24, 0); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 24, (const GLvoid*) 8); glActiveTexture(GL_TEXTURE0 + kugl.texture.index); glBindTexture(GL_TEXTURE_2D, kugl.texture.tx); @@ -447,7 +437,6 @@ void ku_gl_render(ku_bitmap_t* bitmap) glViewport(0, 0, bitmap->w, bitmap->h); glDrawArrays(GL_TRIANGLES, 0, kugl.floatbuffer->pos); - glBindVertexArray(0); } /* render one quad */ @@ -455,7 +444,10 @@ void ku_gl_render(ku_bitmap_t* bitmap) void ku_gl_render_quad(ku_bitmap_t* bitmap, uint32_t index, bmr_t mask) { /* glBindFramebuffer(GL_FRAMEBUFFER, tex_frame.fb); */ - glBindVertexArray(kugl.buffer.vao); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 24, 0); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 24, (const GLvoid*) 8); glActiveTexture(GL_TEXTURE0 + kugl.texture.index); glBindTexture(GL_TEXTURE_2D, kugl.texture.tx); @@ -475,7 +467,6 @@ void ku_gl_render_quad(ku_bitmap_t* bitmap, uint32_t index, bmr_t mask) glDrawArrays(GL_TRIANGLES, index * 6, 6); - glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/kinetic_ui/egl/ku_gl_floatbuffer.c b/src/kinetic_ui/egl/ku_gl_floatbuffer.c index 52cff01..55f5be5 100644 --- a/src/kinetic_ui/egl/ku_gl_floatbuffer.c +++ b/src/kinetic_ui/egl/ku_gl_floatbuffer.c @@ -2,7 +2,7 @@ #define ku_floatbuffer_h #include "mt_memory.c" -#include +#include #include typedef struct ku_floatbuffer_t ku_floatbuffer_t; diff --git a/src/kinetic_ui/egl/ku_gl_shader.c b/src/kinetic_ui/egl/ku_gl_shader.c index 995cf8c..3e99c33 100644 --- a/src/kinetic_ui/egl/ku_gl_shader.c +++ b/src/kinetic_ui/egl/ku_gl_shader.c @@ -1,7 +1,7 @@ #ifndef ku_gl_shader_h #define ku_gl_shader_h -#include +#include typedef struct _glsha_t { diff --git a/src/kinetic_ui/handler/vh_cv_evnt.c b/src/kinetic_ui/handler/vh_cv_evnt.c index 0a7e7dc..a67f36d 100644 --- a/src/kinetic_ui/handler/vh_cv_evnt.c +++ b/src/kinetic_ui/handler/vh_cv_evnt.c @@ -11,11 +11,15 @@ enum vh_cv_evnt_event_id { VH_CV_EVENT_RESIZE, VH_CV_EVENT_CLICK, + VH_CV_EVENT_KEY_DOWN, + VH_CV_EVENT_KEY_UP }; typedef struct _vh_cv_evnt_event_t { enum vh_cv_evnt_event_id id; + ku_view_t* view; + ku_event_t ev; } vh_cv_evnt_event_t; typedef struct _vh_cv_evnt_t @@ -207,6 +211,18 @@ int vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) if (vh->tscrl_view) vh_cv_scrl_hide(vh->tscrl_view); } + else if (ev.type == KU_EVENT_KEY_DOWN) + { + vh_cv_evnt_event_t event = {.id = VH_CV_EVENT_KEY_DOWN, .view = view, .ev = ev}; + if (vh->on_event) + (*vh->on_event)(event); + } + else if (ev.type == KU_EVENT_KEY_UP) + { + vh_cv_evnt_event_t event = {.id = VH_CV_EVENT_KEY_UP, .view = view, .ev = ev}; + if (vh->on_event) + (*vh->on_event)(event); + } return 0; } diff --git a/src/kinetic_ui/handler/vh_tbl_evnt.c b/src/kinetic_ui/handler/vh_tbl_evnt.c index 2d3cde7..82ad8af 100644 --- a/src/kinetic_ui/handler/vh_tbl_evnt.c +++ b/src/kinetic_ui/handler/vh_tbl_evnt.c @@ -351,7 +351,11 @@ int vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) vh->inertia_y = 0; } - return (vh->tscrl_view != NULL); + /* TODO refactor ku_window's key and text handling */ + if (ev.type == KU_EVENT_KEY_DOWN || ev.type == KU_EVENT_KEY_UP) + return 0; + else + return (vh->tscrl_view != NULL); } void vh_tbl_evnt_del(void* p) diff --git a/src/kinetic_ui/handler/vh_tbl_scrl.c b/src/kinetic_ui/handler/vh_tbl_scrl.c index 62033e6..380767d 100644 --- a/src/kinetic_ui/handler/vh_tbl_scrl.c +++ b/src/kinetic_ui/handler/vh_tbl_scrl.c @@ -43,6 +43,8 @@ void vh_tbl_scrl_enable(ku_view_t* view, int flag); #if __INCLUDE_LEVEL__ == 0 +#include "mt_log.c" + #define SCROLLBAR 20.0 void vh_tbl_scrl_set_item_count(ku_view_t* view, size_t count) @@ -262,7 +264,12 @@ int vh_tbl_scrl_evt(ku_view_t* view, ku_event_t ev) { vh_tbl_scrl_t* vh = view->evt_han_data; - if (ev.type == KU_EVENT_MOUSE_MOVE) + if (ev.type == KU_EVENT_FRAME) + { + vh_tbl_scrl_update(view); + /* frame update stops if there is no change in the ui, scroller reaches final position or alpha */ + } + else if (ev.type == KU_EVENT_MOUSE_MOVE) { // show scroll if (vh->scroll_visible == 0) diff --git a/src/kinetic_ui/ku_connector_wayland.c b/src/kinetic_ui/ku_connector_wayland.c index 5ffae26..0d5db55 100644 --- a/src/kinetic_ui/ku_connector_wayland.c +++ b/src/kinetic_ui/ku_connector_wayland.c @@ -931,7 +931,7 @@ wl_window_t* ku_wayland_create_generic_layer(struct monitor_info* monitor, int w info->margin = margin; info->monitor = monitor; info->hidden = 1; - memcpy(info->anchor, anchor, 4); + memcpy(info->anchor, anchor, strlen(anchor) < 5 ? strlen(anchor) : 4); info->type = WL_WINDOW_LAYER; @@ -1210,8 +1210,6 @@ void ku_wayland_pointer_handle_button(void* data, struct wl_pointer* wl_pointer, if (window->wl_pointer == wl_pointer) { - window->pointer.drag = window->pointer.down; - ku_event_t event = init_event(); event.x = window->pointer.px; event.y = window->pointer.py; diff --git a/src/kinetic_ui/ku_draw.c b/src/kinetic_ui/ku_draw.c index 12f8c5d..a1c5bdb 100644 --- a/src/kinetic_ui/ku_draw.c +++ b/src/kinetic_ui/ku_draw.c @@ -36,6 +36,7 @@ void ku_draw_insert_bitmap(ku_bitmap_t* base, ku_bitmap_t* src, int sx, int sy); #if __INCLUDE_LEVEL__ == 0 +#include "mt_log.c" #include void ku_draw_circle(ku_bitmap_t* bitmap, float cx, float cy, float r, float edge, uint32_t c) @@ -934,9 +935,9 @@ void ku_draw_scale(ku_bitmap_t* srcbmp, ku_bitmap_t* dstbmp) int newWidth = dst->w; int newHeight = dst->h; int x, y; - for (x = 0, y = 0; y < newHeight; x++) + for (x = 0, y = 0; y < newHeight - 1; x++) { - if (x > newWidth) + if (x == newWidth) { x = 0; y++; @@ -944,10 +945,11 @@ void ku_draw_scale(ku_bitmap_t* srcbmp, ku_bitmap_t* dstbmp) // float gx = x / (float)(newWidth) * (src->w - 1); // float gy = y / (float)(newHeight) * (src->h - 1); // Image should be clamped at the edges and not scaled. - float gx = max(x / (float) (newWidth) * (src->w) - 0.5f, src->w - 1); - float gy = max(y / (float) (newHeight) * (src->h) - 0.5, src->h - 1); + float gx = max(x / (float) (newWidth) * (src->w) - 0.5f, src->w - 2); + float gy = max(y / (float) (newHeight) * (src->h) - 0.5, src->h - 2); int gxi = (int) gx; int gyi = (int) gy; + uint32_t result = 0; uint32_t c00 = getpixel(src, gxi, gyi); uint32_t c10 = getpixel(src, gxi + 1, gyi); @@ -959,6 +961,7 @@ void ku_draw_scale(ku_bitmap_t* srcbmp, ku_bitmap_t* dstbmp) //((uint8_t*)&result)[i] = blerp( ((uint8_t*)&c00)[i], ((uint8_t*)&c10)[i], ((uint8_t*)&c01)[i], ((uint8_t*)&c11)[i], gxi - gx, gyi - gy); // this is shady result |= (uint8_t) blerp(getByte(c00, i), getByte(c10, i), getByte(c01, i), getByte(c11, i), gx - gxi, gy - gyi) << (8 * i); } + putpixel(dst, x, y, result); } } diff --git a/src/kinetic_ui/ku_view.c b/src/kinetic_ui/ku_view.c index 074ed1d..36e3e6c 100644 --- a/src/kinetic_ui/ku_view.c +++ b/src/kinetic_ui/ku_view.c @@ -217,6 +217,13 @@ void ku_view_del(void* pointer) REL(view->script); REL(view->id); + + for (size_t index = 0; index < view->views->length; index++) + { + ku_view_t* sview = view->views->data[index]; + sview->parent = NULL; + } + REL(view->views); } @@ -332,6 +339,8 @@ void ku_view_insert_subview(ku_view_t* view, ku_view_t* subview, uint32_t index) void ku_view_remove_subview(ku_view_t* view, ku_view_t* subview) { + ku_view_set_parent(subview, NULL); + char success = VREM(view->views, subview); if (success == 1) @@ -340,8 +349,6 @@ void ku_view_remove_subview(ku_view_t* view, ku_view_t* subview) ku_view_t* tview = view; while (tview->parent != NULL) tview = tview->parent; tview->rearrange = 1; - - ku_view_set_parent(subview, NULL); } } @@ -352,7 +359,7 @@ void ku_view_remove_from_parent(ku_view_t* view) void ku_view_set_parent(ku_view_t* view, ku_view_t* parent) { - view->parent = parent; + if (view) view->parent = parent; } void ku_view_coll_touched(ku_view_t* view, ku_event_t ev, mt_vector_t* queue) diff --git a/src/kinetic_ui/ku_window.c b/src/kinetic_ui/ku_window.c index 7eb9c87..cb3e083 100644 --- a/src/kinetic_ui/ku_window.c +++ b/src/kinetic_ui/ku_window.c @@ -176,7 +176,10 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) if (ev.type == KU_EVENT_MOUSE_UP) outev.type = KU_EVENT_MOUSE_UP_OUT; - if (ev.type == KU_EVENT_MOUSE_DOWN) + /* binding this to only mouse down disables drop on different table */ + /* but enables out of mouse input field activation */ + if (ev.type == KU_EVENT_MOUSE_DOWN || + (ev.type == KU_EVENT_MOUSE_UP && ev.drag)) { for (size_t i = win->ptrqueue->length; i-- > 0;) { diff --git a/src/mt_core/mt_memory.c b/src/mt_core/mt_memory.c index f0b61a7..c7c7ca0 100644 --- a/src/mt_core/mt_memory.c +++ b/src/mt_core/mt_memory.c @@ -8,8 +8,6 @@ #include #include -#undef MT_MEMORY_DEBUG - #define CAL(X, Y, Z) mt_memory_calloc(X, Y, Z); #define RET(X) mt_memory_retain(X) #define REL(X) mt_memory_release(X) @@ -24,50 +22,18 @@ size_t mt_memory_retaincount(void* pointer); void* mt_memory_stack_to_heap(size_t size, void (*destructor)(void*), void (*descriptor)(void*, int), char* data); void mt_memory_describe(void* pointer, int level); -#ifdef MT_MEMORY_DEBUG -void mt_memory_stats(); -#endif - #endif #if __INCLUDE_LEVEL__ == 0 -#ifdef MT_MEMORY_DEBUG - #include -#endif - struct mt_memory_head { -#ifdef MT_MEMORY_DEBUG - size_t index; /* allocation index for debugging/statistics */ -#endif + char id[2]; void (*destructor)(void*); void (*descriptor)(void*, int); size_t retaincount; }; -#ifdef MT_MEMORY_DEBUG - - #define MT_MEMORY_DEBUG_SIZE 2000000 /* maximum areas to store */ - #define MT_MEMORY_DEBUG_INDEX 0 /* head index to stop at error */ - -struct mt_memory_head* mt_memory_heads[MT_MEMORY_DEBUG_SIZE] = {0}; -static size_t mt_memory_index = 1; /* live object counter for debugging */ - -void mt_memory_trace( - char* id, struct mt_memory_head* head) -{ - printf("*** %i %s : %i ***\n", head->index, id, head->retaincount); - - void* array[128]; - int size = backtrace(array, 128); - char** strings = backtrace_symbols(array, size); - for (int i = 0; i < size; ++i) printf("%s\n", strings[i]); - free(strings); -} - -#endif - void* mt_memory_alloc( size_t size, /* size of data to store */ void (*destructor)(void*), /* optional destructor */ @@ -78,18 +44,12 @@ void* mt_memory_alloc( { struct mt_memory_head* head = (struct mt_memory_head*) bytes; + head->id[0] = 'm'; + head->id[1] = 't'; head->destructor = destructor; head->descriptor = descriptor; head->retaincount = 1; -#ifdef MT_MEMORY_DEBUG - head->index = mt_memory_index; - mt_memory_heads[mt_memory_index] = head; - if (head->index == MT_MEMORY_DEBUG_INDEX) - mt_memory_trace("ALLOC", head); - mt_memory_index++; -#endif - return bytes + sizeof(struct mt_memory_head); } else @@ -102,22 +62,17 @@ void* mt_memory_calloc( void (*descriptor)(void*, int)) /* optional descriptor for describing memory area */ { char* bytes = calloc(1, sizeof(struct mt_memory_head) + size); + if (bytes != NULL) { struct mt_memory_head* head = (struct mt_memory_head*) bytes; + head->id[0] = 'm'; + head->id[1] = 't'; head->destructor = destructor; head->descriptor = descriptor; head->retaincount = 1; -#ifdef MT_MEMORY_DEBUG - head->index = mt_memory_index; - mt_memory_heads[mt_memory_index] = head; - if (head->index == MT_MEMORY_DEBUG_INDEX) - mt_memory_trace("CALLOC", head); - mt_memory_index++; -#endif - return bytes + sizeof(struct mt_memory_head); } else @@ -150,10 +105,6 @@ void* mt_memory_realloc(void* pointer, size_t size) bytes = realloc(bytes, sizeof(struct mt_memory_head) + size); if (bytes != NULL) { -#ifdef MT_MEMORY_DEBUG - struct mt_memory_head* head = (struct mt_memory_head*) bytes; - mt_memory_heads[head->index] = head; -#endif return bytes + sizeof(struct mt_memory_head); } else @@ -168,13 +119,12 @@ void* mt_memory_retain(void* pointer) bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + if (head->retaincount < SIZE_MAX) { head->retaincount += 1; -#ifdef MT_MEMORY_DEBUG - if (head->index == MT_MEMORY_DEBUG_INDEX) - mt_memory_trace("RETAIN", head); -#endif return pointer; } else @@ -189,25 +139,22 @@ char mt_memory_release(void* pointer) bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); assert(head->retaincount > 0); head->retaincount -= 1; -#ifdef MT_MEMORY_DEBUG - if (head->index == MT_MEMORY_DEBUG_INDEX) - mt_memory_trace("RELEASE", head); - if (head->retaincount == -1) - mt_memory_trace("RELEASE RETAINCOUNT -1!!!", head); -#endif - if (head->retaincount == 0) { if (head->destructor != NULL) head->destructor(pointer); - /* don't clean up to catch overrelease or leaks */ -#ifndef MT_MEMORY_DEBUG + + head->id[0] = '\0'; + head->id[1] = '\0'; + free(bytes); -#endif + return 1; } @@ -222,6 +169,9 @@ size_t mt_memory_retaincount(void* pointer) bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + return head->retaincount; } @@ -233,6 +183,9 @@ void mt_memory_describe(void* pointer, int level) bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + if (head->descriptor != NULL) { head->descriptor(pointer, ++level); @@ -243,36 +196,4 @@ void mt_memory_describe(void* pointer, int level) } } -#ifdef MT_MEMORY_DEBUG - -void mt_memory_stats() -{ - printf("\n***MEM STATS***\n"); - - // print block statistics - - int problem = 0; - - for (int index = 1; index < mt_memory_index; index++) - { - if (MT_MEMORY_DEBUG_INDEX == 0 || MT_MEMORY_DEBUG_INDEX == index) - { - if (mt_memory_heads[index]->retaincount < 0) - { - problem = 1; - printf("OVERRELEASE at %i : %i\n", index, mt_memory_heads[index]->retaincount); - } - if (mt_memory_heads[index]->retaincount > 0) - { - problem = 1; - printf("LEAK at %i : %i\n", index, mt_memory_heads[index]->retaincount); - } - } - } - - if (problem == 0) printf("Everything seems all right\n"); -} - -#endif - #endif diff --git a/src/mt_core/mt_path.c b/src/mt_core/mt_path.c index 3da00d2..4934fb5 100644 --- a/src/mt_core/mt_path.c +++ b/src/mt_core/mt_path.c @@ -15,6 +15,7 @@ char* mt_path_new_normalize(char* path); #include #include +#include #ifdef __linux__ #include #endif @@ -32,6 +33,7 @@ char* mt_path_new_append(char* root, char* component) char* mt_path_new_remove_last_component(char* path) { + /* TODO use POSIX dirname */ int index; for (index = strlen(path) - 2; index > 0; index--) { @@ -48,29 +50,37 @@ char* mt_path_new_remove_last_component(char* path) memcpy(str, path, index); return str; } - else return mt_string_new_cstring("/"); + else + return mt_string_new_cstring("/"); } char* mt_path_new_extension(char* path) { - int index; - for (index = strlen(path) - 1; index > -1; --index) - { - if (path[index] == '.') - { - index++; - break; - } - } - - int len = strlen(path) - index; - char* ext = CAL(len + 1, NULL, mt_string_describe); - memcpy(ext, path + index, len); - return ext; + char* ext = strrchr(path, '.'); + char* res = NULL; + if (ext) + res = mt_string_new_cstring(ext + 1); + + /* int index; */ + /* for (index = strlen(path) - 1; index > -1; --index) */ + /* { */ + /* if (path[index] == '.') */ + /* { */ + /* index++; */ + /* break; */ + /* } */ + /* } */ + + /* int len = strlen(path) - index; */ + /* char* ext = CAL(len + 1, NULL, mt_string_describe); */ + /* memcpy(ext, path + index, len); */ + return res; } char* mt_path_new_filename(char* path) { + /* TODO use POSIX basename */ + int dotindex; for (dotindex = strlen(path) - 1; dotindex > -1; --dotindex) { @@ -100,59 +110,64 @@ char* mt_path_new_filename(char* path) char* mt_path_new_normalize(char* path) { - mt_vector_t* tokens = mt_string_tokenize(path, "/"); - char* result = NULL; + char* extpath = mt_string_new_cstring(""); + char* newpath = CAL(PATH_MAX, NULL, NULL); - if (tokens->length > 0) + if (path[0] == '~') { - result = mt_string_new_cstring(""); - mt_vector_t* newtok = VNEW(); - - for (size_t index = 0; index < tokens->length; index++) - { - char* token = tokens->data[index]; - if (token[0] == '~') - { - // replace tilde with home - VADDR(newtok, mt_string_new_cstring(getenv("HOME"))); - } - else if (strlen(token) == 2 && token[0] == '.' && token[1] == '.') - { - // delete last token - mt_vector_rem_index(newtok, newtok->length - 1); - } - else if (strlen(token) == 1 && token[0] == '.') - { - // do nothing at current dir - } - else - { - VADD(newtok, token); - } - } - - /* assemble new tokens */ - - for (size_t index = 0; index < newtok->length; index++) - { - char* token = newtok->data[index]; - if (index > 0 || path[0] == '/' || path[0] == '"') - result = mt_string_append(result, "/"); - result = mt_string_append(result, token); - } - - if (newtok->length == 0) - result = mt_string_new_cstring("/"); - - REL(newtok); + /* replace tilde with home dir */ + extpath = mt_string_append(extpath, getenv("HOME")); + extpath = mt_string_append_sub(extpath, path, 1, strlen(path) - 1); } else - { - result = mt_string_new_cstring("/"); - } - - REL(tokens); - return result; + extpath = mt_string_append(extpath, path); + + realpath(extpath, newpath); + + REL(extpath); + + /* char cwd[PATH_MAX] = {"~"}; */ + /* getcwd(cwd, sizeof(cwd)); */ + + /* char* newpath = NULL; */ + + /* if (path[0] == '~') */ + /* { */ + /* /\* replace tilde with home dir *\/ */ + /* newpath = mt_string_new_cstring(getenv("HOME")); */ + /* newpath = mt_string_append_sub(newpath, path, 1, strlen(path) - 1); */ + /* } */ + /* else if (path[0] == '\0') */ + /* { */ + /* /\* empty path to root path *\/ */ + /* newpath = mt_string_new_cstring("/"); */ + /* } */ + /* else if (path[0] != '/') */ + /* { */ + /* /\* insert working directory in case of relative path *\/ */ + /* newpath = mt_string_new_format(PATH_MAX, "%s/%s", cwd, path); */ + /* } */ + /* else */ + /* { */ + /* newpath = mt_string_new_cstring(path); */ + /* } */ + + /* /\* remove last component in case of double dot *\/ */ + + /* size_t len = strlen(newpath); */ + /* if (len > 3 && newpath[len - 1] == '.' && newpath[len - 2] == '.') */ + /* { */ + /* for (size_t index = len - 3; index-- > 0;) */ + /* { */ + /* if (newpath[index] == '/') */ + /* { */ + /* newpath[index] = '\0'; */ + /* break; */ + /* } */ + /* } */ + /* } */ + + return newpath; } #endif diff --git a/src/mt_core/mt_vector.c b/src/mt_core/mt_vector.c index 80c4084..cd7c283 100644 --- a/src/mt_core/mt_vector.c +++ b/src/mt_core/mt_vector.c @@ -49,6 +49,8 @@ void mt_vector_rem_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_v #endif #if __INCLUDE_LEVEL__ == 0 +#include "mt_log.c" + void mt_vector_del(void* vector); void mt_vector_describe_data(void* p, int level); void mt_vector_describe_mtvn(void* p, int level); @@ -279,16 +281,18 @@ void mt_vector_sort_ins(mtvn_t* node, void* data, int (*comp)(void* left, void* } } -void mt_vector_sort_ord(mtvn_t* node, mt_vector_t* vector, int* index) +void mt_vector_sort_ord(mtvn_t* node, mt_vector_t* vector, size_t* index) { - if (node->l) mt_vector_sort_ord(node->l, vector, index); + if (node->l) + mt_vector_sort_ord(node->l, vector, index); vector->data[*index] = node->c; *index += 1; // cleanup node mtvn_t* right = node->r; - if (right) mt_vector_sort_ord(right, vector, index); + if (right) + mt_vector_sort_ord(right, vector, index); } // sorts values in vector, needs a comparator function @@ -296,18 +300,22 @@ void mt_vector_sort_ord(mtvn_t* node, mt_vector_t* vector, int* index) void mt_vector_sort(mt_vector_t* vector, int (*comp)(void* left, void* right)) { - /* create cache */ - /* TODO make it local to make it thread safe */ + if (vector->length > 1) + { + /* create cache */ + /* TODO make it local to make it thread safe */ - cache = CAL(sizeof(mtvn_t) * vector->length, NULL, NULL); - cachei = 1; + cache = CAL(sizeof(mtvn_t) * vector->length, NULL, NULL); + cachei = 1; - for (size_t index = 0; index < vector->length; index++) mt_vector_sort_ins(cache, vector->data[index], comp); - int index = 0; + for (size_t index = 0; index < vector->length; index++) mt_vector_sort_ins(cache, vector->data[index], comp); - mt_vector_sort_ord(cache, vector, &index); + size_t index = 0; - REL(cache); + mt_vector_sort_ord(cache, vector, &index); + + REL(cache); + } } void mt_vector_describe(void* pointer, int level)