Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Replaced manual NSAutoreleasePool handing with @autorelease

  • Loading branch information
stopiccot committed Jan 6, 2013
1 parent 68ee601 commit 612b38b7776d6f6e33000827a3582e595061faff
@@ -20,25 +20,22 @@
return fopen(file, mode);
}

NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];


NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
if([file_manager fileExistsAtPath:full_path_with_file_to_try])
{
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
}
else
{
fp = fopen(file, mode);
}

[autorelease_pool drain];
@autoreleasepool {
NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
if([file_manager fileExistsAtPath:full_path_with_file_to_try])
{
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
}
else
{
fp = fopen(file, mode);
}
}

return fp;
}
@@ -45,51 +45,46 @@
Cocoa_SetClipboardText(_THIS, const char *text)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool;

NSPasteboard *pasteboard;
NSString *format = GetTextFormat(_this);

pool = [[NSAutoreleasePool alloc] init];

pasteboard = [NSPasteboard generalPasteboard];
data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
[pasteboard setString:[NSString stringWithUTF8String:text] forType:format];

[pool release];

@autoreleasepool {
pasteboard = [NSPasteboard generalPasteboard];
data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
[pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
}

return 0;
}

char *
Cocoa_GetClipboardText(_THIS)
{
NSAutoreleasePool *pool;
NSPasteboard *pasteboard;
NSString *format = GetTextFormat(_this);
NSString *available;
char *text;

pool = [[NSAutoreleasePool alloc] init];

pasteboard = [NSPasteboard generalPasteboard];
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
if ([available isEqualToString:format]) {
NSString* string;
const char *utf8;

string = [pasteboard stringForType:format];
if (string == nil) {
utf8 = "";
@autoreleasepool {
pasteboard = [NSPasteboard generalPasteboard];
available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
if ([available isEqualToString:format]) {
NSString* string;
const char *utf8;

string = [pasteboard stringForType:format];
if (string == nil) {
utf8 = "";
} else {
utf8 = [string UTF8String];
}
text = SDL_strdup(utf8);
} else {
utf8 = [string UTF8String];
text = SDL_strdup("");
}
text = SDL_strdup(utf8);
} else {
text = SDL_strdup("");
}

[pool release];


return text;
}

@@ -108,22 +103,19 @@
void
Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
{
NSAutoreleasePool *pool;
NSPasteboard *pasteboard;
NSInteger count;

pool = [[NSAutoreleasePool alloc] init];

pasteboard = [NSPasteboard generalPasteboard];
count = [pasteboard changeCount];
if (count != data->clipboard_count) {
if (data->clipboard_count) {
SDL_SendClipboardUpdate();
@autoreleasepool {
pasteboard = [NSPasteboard generalPasteboard];
count = [pasteboard changeCount];
if (count != data->clipboard_count) {
if (data->clipboard_count) {
SDL_SendClipboardUpdate();
}
data->clipboard_count = count;
}
data->clipboard_count = count;
}

[pool release];
}

#endif /* SDL_VIDEO_DRIVER_COCOA */
@@ -158,33 +158,30 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
{
/* This can get called more than once! Be careful what you initialize! */
ProcessSerialNumber psn;
NSAutoreleasePool *pool;

if (!GetCurrentProcess(&psn)) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
}

pool = [[NSAutoreleasePool alloc] init];
if (NSApp == nil) {
[NSApplication sharedApplication];
@autoreleasepool {
if (NSApp == nil) {
[NSApplication sharedApplication];

if ([NSApp mainMenu] == nil) {
CreateApplicationMenus();
if ([NSApp mainMenu] == nil) {
CreateApplicationMenus();
}
[NSApp finishLaunching];
}
if ([NSApp delegate] == nil) {
[NSApp setDelegate:[[SDLAppDelegate alloc] init]];
}
[NSApp finishLaunching];
}
if ([NSApp delegate] == nil) {
[NSApp setDelegate:[[SDLAppDelegate alloc] init]];
}
[pool release];
}

void
Cocoa_PumpEvents(_THIS)
{
NSAutoreleasePool *pool;

/* Update activity every 30 seconds to prevent screensaver */
if (_this->suspend_screensaver) {
SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
@@ -196,39 +193,39 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
}
}

pool = [[NSAutoreleasePool alloc] init];
for ( ; ; ) {
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
if ( event == nil ) {
break;
}

switch ([event type]) {
case NSLeftMouseDown:
case NSOtherMouseDown:
case NSRightMouseDown:
case NSLeftMouseUp:
case NSOtherMouseUp:
case NSRightMouseUp:
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged: /* usually middle mouse dragged */
case NSMouseMoved:
case NSScrollWheel:
Cocoa_HandleMouseEvent(_this, event);
break;
case NSKeyDown:
case NSKeyUp:
case NSFlagsChanged:
Cocoa_HandleKeyEvent(_this, event);
break;
default:
break;
@autoreleasepool {
for ( ; ; ) {
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
if ( event == nil ) {
break;
}

switch ([event type]) {
case NSLeftMouseDown:
case NSOtherMouseDown:
case NSRightMouseDown:
case NSLeftMouseUp:
case NSOtherMouseUp:
case NSRightMouseUp:
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged: /* usually middle mouse dragged */
case NSMouseMoved:
case NSScrollWheel:
Cocoa_HandleMouseEvent(_this, event);
break;
case NSKeyDown:
case NSKeyUp:
case NSFlagsChanged:
Cocoa_HandleKeyEvent(_this, event);
break;
default:
break;
}
/* Pass through to NSApp to make sure everything stays in sync */
[NSApp sendEvent:event];
}
/* Pass through to NSApp to make sure everything stays in sync */
[NSApp sendEvent:event];
}
[pool release];
}

#endif /* SDL_VIDEO_DRIVER_COCOA */
@@ -625,28 +625,27 @@ - (NSArray *) validAttributesForMarkedText
Cocoa_StartTextInput(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSView *parentView = [[NSApp keyWindow] contentView];

/* We only keep one field editor per process, since only the front most
* window can receive text input events, so it make no sense to keep more
* than one copy. When we switched to another window and requesting for
* text input, simply remove the field editor from its superview then add
* it to the front most window's content view */
if (!data->fieldEdit) {
data->fieldEdit =
[[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
}
@autoreleasepool {
NSView *parentView = [[NSApp keyWindow] contentView];

/* We only keep one field editor per process, since only the front most
* window can receive text input events, so it make no sense to keep more
* than one copy. When we switched to another window and requesting for
* text input, simply remove the field editor from its superview then add
* it to the front most window's content view */
if (!data->fieldEdit) {
data->fieldEdit =
[[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
}

if (![[data->fieldEdit superview] isEqual: parentView])
{
// DEBUG_IME(@"add fieldEdit to window contentView");
[data->fieldEdit removeFromSuperview];
[parentView addSubview: data->fieldEdit];
[[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
if (![[data->fieldEdit superview] isEqual: parentView])
{
// DEBUG_IME(@"add fieldEdit to window contentView");
[data->fieldEdit removeFromSuperview];
[parentView addSubview: data->fieldEdit];
[[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
}
}

[pool release];
}

void
@@ -655,11 +654,11 @@ - (NSArray *) validAttributesForMarkedText
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

if (data && data->fieldEdit) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[data->fieldEdit removeFromSuperview];
[data->fieldEdit release];
data->fieldEdit = nil;
[pool release];
@autoreleasepool {
[data->fieldEdit removeFromSuperview];
[data->fieldEdit release];
data->fieldEdit = nil;
}
}
}

@@ -39,40 +39,38 @@
{
Cocoa_RegisterApp();

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
NSAlert* alert = [[NSAlert alloc] init];

NSAlert* alert = [[NSAlert alloc] init];

if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
[alert setAlertStyle:NSCriticalAlertStyle];
} else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
[alert setAlertStyle:NSWarningAlertStyle];
} else {
[alert setAlertStyle:NSInformationalAlertStyle];
}

[alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
[alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];

const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
int i;
for (i = 0; i < messageboxdata->numbuttons; ++i) {
NSButton *button = [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
[button setKeyEquivalent:@"\r"];
} else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
[button setKeyEquivalent:@"\033"];
if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
[alert setAlertStyle:NSCriticalAlertStyle];
} else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
[alert setAlertStyle:NSWarningAlertStyle];
} else {
[button setKeyEquivalent:@""];
[alert setAlertStyle:NSInformationalAlertStyle];
}
}

NSInteger clicked = [alert runModal];
clicked -= NSAlertFirstButtonReturn;
*buttonid = buttons[clicked].buttonid;
[alert release];
[alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
[alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];

const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
int i;
for (i = 0; i < messageboxdata->numbuttons; ++i) {
NSButton *button = [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
[button setKeyEquivalent:@"\r"];
} else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
[button setKeyEquivalent:@"\033"];
} else {
[button setKeyEquivalent:@""];
}
}

[pool release];
NSInteger clicked = [alert runModal];
clicked -= NSAlertFirstButtonReturn;
*buttonid = buttons[clicked].buttonid;
[alert release];
}

return 0;
}

0 comments on commit 612b38b

Please sign in to comment.