<commit_before>#include "USBNotifier.h"
#include "AppController.h"
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/usb/USB.h>

extern void NSLog(CFStringRef format, ...);

static IONotificationPortRef	ioKitNotificationPort;
static CFRunLoopSourceRef		notificationRunLoopSource;
static Boolean					notificationsArePrimed = false;

#pragma mark C Callbacks

static void usbDeviceAdded(void *refCon, io_iterator_t iterator) {
#pragma unused(refCon)
//	NSLog(@"USB Device Added Notification.");
	io_object_t	thisObject;
	while ((thisObject = IOIteratorNext(iterator))) {
		Boolean keyExistsAndHasValidFormat;
		if (notificationsArePrimed || CFPreferencesGetAppBooleanValue(CFSTR("ShowExisting"), CFSTR("com.growl.hardwaregrowler"), &keyExistsAndHasValidFormat)) {
			kern_return_t	nameResult;
			io_name_t		deviceNameChars;

			//	This works with USB devices...
			//	but apparently not firewire
			nameResult = IORegistryEntryGetName(thisObject, deviceNameChars);

			CFStringRef deviceName = CFStringCreateWithCString(kCFAllocatorDefault,
															   deviceNameChars,
															   kCFStringEncodingASCII);
			if ((CFStringCompare(deviceName, CFSTR("OHCI Root Hub Simulation"), 0) == kCFCompareEqualTo) ||
				(CFStringCompare(deviceName, CFSTR("UHCI Root Hub Simulation"), 0) == kCFCompareEqualTo)) {
				CFRelease(deviceName);
				deviceName = CFCopyLocalizedString(CFSTR("USB Bus"), "");
			} else if (CFStringCompare(deviceName, CFSTR("EHCI Root Hub Simulation"), 0) == kCFCompareEqualTo) {
				CFRelease(deviceName);
				deviceName = CFCopyLocalizedString(CFSTR("USB 2.0 Bus"), "");
			}

			// NSLog(@"USB Device Attached: %@" , deviceName);
			AppController_usbDidConnect(deviceName);
			CFRelease(deviceName);
		}

		IOObjectRelease(thisObject);
	}
}

static void usbDeviceRemoved(void *refCon, io_iterator_t iterator) {
#pragma unused(refCon)
//	NSLog(@"USB Device Removed Notification.");
	io_object_t thisObject;
	while ((thisObject = IOIteratorNext(iterator))) {
		kern_return_t	nameResult;
		io_name_t		deviceNameChars;

		//	This works with USB devices...
		//	but apparently not firewire
		nameResult = IORegistryEntryGetName(thisObject, deviceNameChars);
		CFStringRef deviceName = CFStringCreateWithCString(kCFAllocatorDefault,
														   deviceNameChars,
														   kCFStringEncodingASCII);
		if (CFStringCompare(deviceName, CFSTR("OHCI Root Hub Simulation"), 0) == kCFCompareEqualTo)
			deviceName = CFCopyLocalizedString(CFSTR("USB Bus"), "");
		else if (CFStringCompare(deviceName, CFSTR("EHCI Root Hub Simulation"), 0) == kCFCompareEqualTo)
			deviceName = CFCopyLocalizedString(CFSTR("USB 2.0 Bus"), "");

		// NSLog(@"USB Device Detached: %@" , deviceName);
		AppController_usbDidDisconnect(deviceName);
		CFRelease(deviceName);

		IOObjectRelease(thisObject);
	}
}

#pragma mark -

static void registerForUSBNotifications(void) {
	//http://developer.apple.com/documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Finding_Devices/chapter_4_section_2.html#//apple_ref/doc/uid/TP30000379/BABEACCJ
	kern_return_t	matchingResult;
	kern_return_t	removeNoteResult;
	io_iterator_t	addedIterator;
	io_iterator_t	removedIterator;

//	NSLog(@"registerForUSBNotifications");

	//	Setup a matching Dictionary.
	CFDictionaryRef myMatchDictionary;<commit_msg>Fixed two more failures to report and handle errors, found by the Clang Static Analyzer.<commit_after>#include "USBNotifier.h"
#include "AppController.h"
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/usb/USB.h>

extern void NSLog(CFStringRef format, ...);

static IONotificationPortRef	ioKitNotificationPort;
static CFRunLoopSourceRef		notificationRunLoopSource;
static Boolean					notificationsArePrimed = false;

#pragma mark C Callbacks

static void usbDeviceAdded(void *refCon, io_iterator_t iterator) {
#pragma unused(refCon)
//	NSLog(@"USB Device Added Notification.");
	io_object_t	thisObject;
	while ((thisObject = IOIteratorNext(iterator))) {
		Boolean keyExistsAndHasValidFormat;
		if (notificationsArePrimed || CFPreferencesGetAppBooleanValue(CFSTR("ShowExisting"), CFSTR("com.growl.hardwaregrowler"), &keyExistsAndHasValidFormat)) {
			kern_return_t	nameResult;
			io_name_t		deviceNameChars;

			//	This works with USB devices...
			//	but apparently not firewire
			nameResult = IORegistryEntryGetName(thisObject, deviceNameChars);
			if (nameResult != KERN_SUCCESS) {
				NSLog(CFSTR("%s: Could not get name for USB object %p: IORegistryEntryGetName returned 0x%x"), __PRETTY_FUNCTION__, thisObject, nameResult);
				continue;
			}

			CFStringRef deviceName = CFStringCreateWithCString(kCFAllocatorDefault,
															   deviceNameChars,
															   kCFStringEncodingASCII);
			if ((CFStringCompare(deviceName, CFSTR("OHCI Root Hub Simulation"), 0) == kCFCompareEqualTo) ||
				(CFStringCompare(deviceName, CFSTR("UHCI Root Hub Simulation"), 0) == kCFCompareEqualTo)) {
				CFRelease(deviceName);
				deviceName = CFCopyLocalizedString(CFSTR("USB Bus"), "");
			} else if (CFStringCompare(deviceName, CFSTR("EHCI Root Hub Simulation"), 0) == kCFCompareEqualTo) {
				CFRelease(deviceName);
				deviceName = CFCopyLocalizedString(CFSTR("USB 2.0 Bus"), "");
			}

			// NSLog(@"USB Device Attached: %@" , deviceName);
			AppController_usbDidConnect(deviceName);
			CFRelease(deviceName);
		}

		IOObjectRelease(thisObject);
	}
}

static void usbDeviceRemoved(void *refCon, io_iterator_t iterator) {
#pragma unused(refCon)
//	NSLog(@"USB Device Removed Notification.");
	io_object_t thisObject;
	while ((thisObject = IOIteratorNext(iterator))) {
		kern_return_t	nameResult;
		io_name_t		deviceNameChars;

		//	This works with USB devices...
		//	but apparently not firewire
		nameResult = IORegistryEntryGetName(thisObject, deviceNameChars);
		if (nameResult != KERN_SUCCESS) {
			NSLog(CFSTR("%s: Could not get name for USB object %p: IORegistryEntryGetName returned 0x%x"), __PRETTY_FUNCTION__, thisObject, nameResult);
			continue;
		}

		CFStringRef deviceName = CFStringCreateWithCString(kCFAllocatorDefault,
														   deviceNameChars,
														   kCFStringEncodingASCII);
		if (CFStringCompare(deviceName, CFSTR("OHCI Root Hub Simulation"), 0) == kCFCompareEqualTo)
			deviceName = CFCopyLocalizedString(CFSTR("USB Bus"), "");
		else if (CFStringCompare(deviceName, CFSTR("EHCI Root Hub Simulation"), 0) == kCFCompareEqualTo)
			deviceName = CFCopyLocalizedString(CFSTR("USB 2.0 Bus"), "");

		// NSLog(@"USB Device Detached: %@" , deviceName);
		AppController_usbDidDisconnect(deviceName);
		CFRelease(deviceName);

		IOObjectRelease(thisObject);
	}
}

#pragma mark -

static void registerForUSBNotifications(void) {
	//http://developer.apple.com/documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Finding_Devices/chapter_4_section_2.html#//apple_ref/doc/uid/TP30000379/BABEACCJ
	kern_return_t	matchingResult;
	kern_return_t	removeNoteResult;
	io_iterator_t	addedIterator;
	io_iterator_t	removedIterator;

//	NSLog(@"registerForUSBNotifications");

	//	Setup a matching Dictionary.
	CFDictionaryRef myMatchDictionary;
{'fraction_non_alphanumeric': 0.1086048454469507, 'fraction_numerical': 0.00556947925368978, 'mean_word_length': 5.3286343612334806, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3293941', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2276, 'n_words': 461}