Skip to content

Commit

Permalink
Fix Boolean, CFIndex, and Pointer typing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Sep 4, 2019
1 parent cd00036 commit be63808
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
35 changes: 15 additions & 20 deletions contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,8 @@ public interface CoreFoundation extends Library {

int kCFNotFound = -1;

int kCFStringEncodingMacRoman = 0;
int kCFStringEncodingWindowsLatin1 = 0x0500;
int kCFStringEncodingISOLatin1 = 0x0201;
int kCFStringEncodingNextStepLatin = 0x0B01;
int kCFStringEncodingASCII = 0x0600;
int kCFStringEncodingUnicode = 0x0100;
int kCFStringEncodingUTF8 = 0x08000100;
int kCFStringEncodingNonLossyASCII = 0x0BFF;

/**
* The {@code CFTypeRef} type is the base type defined in Core Foundation. It is
Expand Down Expand Up @@ -267,7 +261,7 @@ public static CFStringRef toCFString(String s) {
* A pointer to the value for the returned number object.
* @return A new number with the value specified by {@code valuePtr}.
*/
CFNumberRef CFNumberCreate(CFAllocatorRef alloc, int theType, PointerType valuePtr);
CFNumberRef CFNumberCreate(CFAllocatorRef alloc, long theType, PointerType valuePtr);

/**
* Creates a new immutable array with the given values.
Expand Down Expand Up @@ -475,9 +469,9 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac
* value from the dictionary is not returned (but the return value of
* this function still indicates whether or not the key-value pair
* was present).
* @return {@code true} if a matching key was found, otherwise {@code false}.
* @return 1 if a matching key was found, otherwise 0.
*/
boolean CFDictionaryGetValueIfPresent(CFDictionaryRef theDict, PointerType key, PointerByReference value);
byte CFDictionaryGetValueIfPresent(CFDictionaryRef theDict, PointerType key, PointerByReference value);

/**
* Sets the value corresponding to a given key.
Expand Down Expand Up @@ -525,19 +519,19 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac
* The string encoding to which the character contents of
* {@code theString} should be converted. The encoding must specify
* an 8-bit encoding.
* @return {@code true} upon success or {@code false} if the conversion fails or
* the provided buffer is too small.
* @return 1 upon success or 0 if the conversion fails or the provided buffer is
* too small.
*/
boolean CFStringGetCString(CFTypeRef theString, Pointer bufferToFill, long bufferSize, int encoding);
byte CFStringGetCString(CFTypeRef theString, Pointer bufferToFill, long bufferSize, int encoding);

/**
* Returns the value of a {@code CFBoolean} object.
*
* @param bool
* The boolean to examine.
* @return The value of {@code bool}.
* @return 1 if the value of {@code bool} is {@code true}, 0 otherwise.
*/
boolean CFBooleanGetValue(CFTypeRef bool);
byte CFBooleanGetValue(CFTypeRef bool);

/**
* Retrieves a value at a given index.
Expand All @@ -560,7 +554,7 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac
* @return A constant that indicates the data type of the value contained in
* number. See {@link CFNumberType} for a list of possible values.
*/
int CFNumberGetType(CFNumberRef number);
long CFNumberGetType(CFNumberRef number);

/**
* Obtains the value of a {@code CFNumber} object cast to a specified type.
Expand All @@ -572,10 +566,9 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac
* {@link CFNumberType} for a list of possible values.
* @param On
* return, contains the value of {@code number}.
* @return {@code true} if the operation was successful, otherwise
* {@code false}.
* @return 1 if the operation was successful, otherwise 0.
*/
boolean CFNumberGetValue(CFNumberRef number, int theType, ByReference valuePtr);
byte CFNumberGetValue(CFNumberRef number, long theType, ByReference valuePtr);

/**
* Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a
Expand All @@ -597,7 +590,9 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac
* @param encoding
* The string encoding for the number of characters specified by
* length.
* @return a long.
* @return The maximum number of bytes that could be needed to represent length
* number of Unicode characters with the string encoding encoding, or
* {@link #kCFNotFound} if the number exceeds {@link Long#MAX_VALUE}.
*/
long CFStringGetMaximumSizeForEncoding(long length, int encoding);

Expand Down Expand Up @@ -628,5 +623,5 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac
* The {@code CFData} object to examine.
* @return A read-only pointer to the bytes associated with {@code theData}.
*/
PointerByReference CFDataGetBytePtr(CFDataRef theData);
Pointer CFDataGetBytePtr(CFDataRef theData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public static int cfPointerToInt(CFNumberRef theInt) {
* @return The corresponding {@code boolean}
*/
public static boolean cfPointerToBoolean(CFBooleanRef theBoolean) {
return CF.CFBooleanGetValue(theBoolean);
System.out.println("BOOL=" + CF.CFBooleanGetValue(theBoolean));
return 0 != CF.CFBooleanGetValue(theBoolean);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static com.sun.jna.platform.mac.CoreFoundationUtil.release;
import static com.sun.jna.platform.mac.CoreFoundationUtil.releaseAll;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -65,7 +65,7 @@ public void testCFStringRef() {

Memory mem = new Memory(awesome.getBytes().length + 1);
mem.clear();
assertTrue(CF.CFStringGetCString(cfAwesome, mem, mem.size(), CoreFoundation.kCFStringEncodingUTF8));
assertNotEquals(0, CF.CFStringGetCString(cfAwesome, mem, mem.size(), CoreFoundation.kCFStringEncodingUTF8));
byte[] awesomeBytes = mem.getByteArray(0, (int) mem.size() - 1);
byte[] awesomeArr = awesome.getBytes();
for (int i = 0; i < awesomeArr.length; i++) {
Expand Down Expand Up @@ -160,8 +160,8 @@ public void testCFData() {
CFDataRef cfBug = CF.CFDataCreate(null, bugBytes, bugBytes.size());
assertEquals(bugBytes.size(), CF.CFDataGetLength(cfBug));

PointerByReference bytes = CF.CFDataGetBytePtr(cfBug);
assertEquals(deadBug, bytes.getPointer().getString(0));
Pointer bytes = CF.CFDataGetBytePtr(cfBug);
assertEquals(deadBug, bytes.getString(0));

release(cfBug);
}
Expand All @@ -173,13 +173,13 @@ public void testCFDictionary() {
CFStringRef oneStr = CFStringRef.toCFString("one");

// Key does not exist, returns null
assertFalse(CF.CFDictionaryGetValueIfPresent(dict, oneStr, null));
assertEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, null));
Pointer cfNull = CF.CFDictionaryGetValue(dict, oneStr);
assertNull(cfNull);

// Store and retrieve null value
CF.CFDictionarySetValue(dict, oneStr, null);
assertTrue(CF.CFDictionaryGetValueIfPresent(dict, oneStr, null));
assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, null));
Pointer cfNullValue = CF.CFDictionaryGetValue(dict, oneStr);
assertNull(cfNullValue);

Expand All @@ -188,13 +188,13 @@ public void testCFDictionary() {
CFNumberRef cfOne = CF.CFNumberCreate(null, CFNumberType.kCFNumberIntType.ordinal(), one);
CF.CFDictionarySetValue(dict, oneStr, cfOne);

assertTrue(CF.CFDictionaryGetValueIfPresent(dict, oneStr, null));
assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, null));
Pointer result = CF.CFDictionaryGetValue(dict, oneStr);
CFNumberRef numRef = new CFNumberRef(result);
assertEquals(1, CoreFoundationUtil.cfPointerToInt(numRef));

PointerByReference resultPtr = new PointerByReference();
assertTrue(CF.CFDictionaryGetValueIfPresent(dict, oneStr, resultPtr));
assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, resultPtr));
numRef = new CFNumberRef(resultPtr.getValue());
assertEquals(1, CoreFoundationUtil.cfPointerToInt(numRef));

Expand Down
10 changes: 5 additions & 5 deletions contrib/platform/test/com/sun/jna/platform/mac/IOKitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testMatching() {
IO.IORegistryEntryCreateCFProperties(platformExpert, properties, CF.CFAllocatorGetDefault(), 0));
dict = new CFMutableDictionaryRef();
dict.setPointer(properties.getValue());
assertTrue(CF.CFDictionaryGetValueIfPresent(dict, serialKey, null));
assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, serialKey, null));
result = CF.CFDictionaryGetValue(dict, serialKey);
cfSerial = new CFStringRef(result);
assertEquals(serialNumber, CoreFoundationUtil.cfPointerToString(cfSerial));
Expand Down Expand Up @@ -250,16 +250,16 @@ public void testPowerSources() {
// Get values from dictionary (See IOPSKeys.h)
// Skip if not present
PointerByReference result = new PointerByReference();
if (CF.CFDictionaryGetValueIfPresent(dictionary, isPresentKey, result)) {
if (0 != CF.CFDictionaryGetValueIfPresent(dictionary, isPresentKey, result)) {
CFBooleanRef isPresentRef = new CFBooleanRef(result.getValue());
if (CF.CFBooleanGetValue(isPresentRef)) {
if (CoreFoundationUtil.cfPointerToBoolean(isPresentRef)) {
int currentCapacity = 0;
if (CF.CFDictionaryGetValueIfPresent(dictionary, currentCapacityKey, result)) {
if (0 != CF.CFDictionaryGetValueIfPresent(dictionary, currentCapacityKey, result)) {
CFNumberRef cap = new CFNumberRef(result.getValue());
currentCapacity = CoreFoundationUtil.cfPointerToInt(cap);
}
int maxCapacity = 100;
if (CF.CFDictionaryGetValueIfPresent(dictionary, maxCapacityKey, result)) {
if (0 != CF.CFDictionaryGetValueIfPresent(dictionary, maxCapacityKey, result)) {
CFNumberRef cap = new CFNumberRef(result.getValue());
maxCapacity = CoreFoundationUtil.cfPointerToInt(cap);
}
Expand Down

0 comments on commit be63808

Please sign in to comment.