/
githubmirror
/
newt
Обзор
Документация
Войти
/
githubmirror
/
newt
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
label.c
98 строк
2 KB
Robert Gill
improve handling of malloc failures
19 июн 2025, 09:48
19 июн 2025, 09:48
d92e05d
Код
Авторство
О чём код?
#include <slang.h> #include <stdlib.h> #include <string.h> #include "newt.h" #include "newt_pr.h" struct label { char * text; int length; int cs; }; static void labelDraw(newtComponent co); static void labelDestroy(newtComponent co); static struct componentOps labelOps = { labelDraw, newtDefaultEventHandler, labelDestroy, newtDefaultPlaceHandler, newtDefaultMappedHandler, } ; newtComponent newtLabel(int left, int top, const char * text) { newtComponent co; struct label * la; co = malloc(sizeof(*co)); if (co == NULL) return NULL; la = malloc(sizeof(struct label)); if (la == NULL) { free(co); return NULL; } co->data = la; co->destroyCallback = NULL; co->ops = &labelOps; co->height = 1; co->width = wstrlen(text, -1); co->top = top; co->left = left; co->takesFocus = 0; co->isMapped = 0; la->length = strlen(text); la->text = strdup(text); la->cs = COLORSET_LABEL; return co; } void newtLabelSetText(newtComponent co, const char * text) { int newLength; struct label * la = co->data; co->width = wstrlen(text,-1); newLength = strlen(text); if (newLength <= la->length) { memset(la->text, ' ', la->length); memcpy(la->text, text, newLength); } else { free(la->text); la->text = strdup(text); la->length = newLength; } labelDraw(co); } void newtLabelSetColors(newtComponent co, int colorset) { struct label * la = co->data; la->cs = colorset; labelDraw(co); } static void labelDraw(newtComponent co) { struct label * la = co->data; if (!co->isMapped) return; SLsmg_set_color(la->cs); newtGotorc(co->top, co->left); SLsmg_write_string(la->text); } static void labelDestroy(newtComponent co) { struct label * la = co->data; free(la->text); free(la); free(co); }