removed malloc from TimEditState_construct
This commit is contained in:
@@ -301,14 +301,10 @@ bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle
|
||||
TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style);
|
||||
|
||||
/// @param e uninitialized state
|
||||
/// @param capacity in bytes
|
||||
/// @param buffer an array
|
||||
/// @param capacity buffer size in bytes
|
||||
/// @param initial_content may be NULL
|
||||
void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content);
|
||||
|
||||
static inline void TimEditState_destroy(TimEditState* e) {
|
||||
if(!e) return;
|
||||
free(e->s);
|
||||
}
|
||||
void TimEditState_construct(TimEditState* e, char* buffer, i32 capacity, cstr initial_content);
|
||||
|
||||
void TimEditState_insert(TimEditState* e, cstr s);
|
||||
void TimEditState_delete(TimEditState* e);
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
#include "tim.h"
|
||||
|
||||
void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content){
|
||||
void TimEditState_construct(TimEditState* e, char* buf, i32 capacity, cstr initial_content){
|
||||
e->masked = false;
|
||||
e->length = initial_content ? tim_utf8_len(initial_content) : 0;
|
||||
e->cursor = e->length;
|
||||
e->capacity = capacity;
|
||||
e->s = (char*)malloc(capacity + 1);
|
||||
i32 byte_len = strlen(initial_content);
|
||||
e->s = buf;
|
||||
i32 byte_len = 0;
|
||||
if(e->length > 0){
|
||||
byte_len = strlen(initial_content);
|
||||
memcpy(e->s, initial_content, byte_len);
|
||||
}
|
||||
e->s[byte_len] = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -126,8 +126,10 @@ static inline void test_screen(TimEvent* e) {
|
||||
}
|
||||
|
||||
i32 main(void) {
|
||||
TimEditState_construct(&ed1, 32, "Edit 1");
|
||||
TimEditState_construct(&ed2, 32, "");
|
||||
char ed1_buf[32];
|
||||
char ed2_buf[32];
|
||||
TimEditState_construct(&ed1, ed1_buf, ARRAY_SIZE(ed1_buf), "Edit 1");
|
||||
TimEditState_construct(&ed2, ed2_buf, ARRAY_SIZE(ed2_buf), NULL);
|
||||
|
||||
while (tim_run(1.5)) {
|
||||
test_screen(&tim->event);
|
||||
@@ -136,8 +138,5 @@ i32 main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
TimEditState_destroy(&ed1);
|
||||
TimEditState_destroy(&ed2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user