Eliminated direct use of strdup(); introduced uiDarwinNSStringToText() to do that work for us (and abort on failure).

This commit is contained in:
Pietro Gagliardi 2015-04-10 13:24:34 -04:00
parent 512819def7
commit 016a05aebf
7 changed files with 23 additions and 7 deletions

View File

@ -7,7 +7,7 @@ void *uiAlloc(size_t size, const char *type)
void *out;
out = malloc(size);
if (out != NULL) {
if (out == NULL) {
fprintf(stderr, "memory exhausted in uiAlloc() allocating %s\n", type);
abort();
}
@ -24,7 +24,7 @@ void *uiRealloc(void *p, size_t size, const char *type)
if (p == NULL)
return uiAlloc(size, type);
out = realloc(p, size);
if (out != NULL) {
if (out == NULL) {
fprintf(stderr, "memory exhausted in uiRealloc() reallocating %s\n", type);
abort();
}

View File

@ -58,7 +58,7 @@ char *uiButtonText(uiControl *c)
uiNSButton *b;
b = (uiNSButton *) uiControlHandle(c);
return strdup(fromNSString([b title]));
return uiDarwinNSStringToText([b title]);
}
void uiButtonSetText(uiControl *c, const char *text)

View File

@ -57,7 +57,7 @@ char *uiCheckboxText(uiControl *c)
uiCheckboxNSButton *cc;
cc = (uiCheckboxNSButton *) uiControlHandle(c);
return strdup(fromNSString([cc title]));
return uiDarwinNSStringToText([cc title]);
}
void uiCheckboxSetText(uiControl *c, const char *text)

View File

@ -55,8 +55,7 @@ char *uiEntryText(uiControl *c)
uiNSTextField *t;
t = (uiNSTextField *) uiControlHandle(c);
// TODO wrap all strdup calls
return strdup(fromNSString([t stringValue]));
return uiDarwinNSStringToText([t stringValue]);
}
void uiEntrySetText(uiControl *c, const char *text)

14
text_darwin.m Normal file
View File

@ -0,0 +1,14 @@
// 10 april 2015
#import "uipriv_darwin.h"
char *uiDarwinNSStringToText(NSString *s)
{
char *out;
out = strdup([s UTF8String]);
if (out == NULL) {
fprintf(stderr, "memory exhausted in uiDarwinNSStringToText()\n");
abort();
}
return out;
}

View File

@ -15,6 +15,9 @@ This file assumes that you have imported <Cocoa/Cocoa.h> and "ui.h" beforehand.
extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder);
extern BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview);
// You can use this function from within your control implementations to return text strings that can be freed with uiTextFree().
extern char *uiDarwinNSStringToText(NSString *);
struct uiSizingSys {
// this structure currently left blank
};

View File

@ -81,7 +81,7 @@ uintptr_t uiWindowHandle(uiWindow *w)
char *uiWindowTitle(uiWindow *w)
{
return strdup(fromNSString([D.w title]));
return uiDarwinNSStringToText([D.w title]);
}
void uiWindowSetTitle(uiWindow *w, const char *title)