Skip to content

Commit

Permalink
Add more extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Craftplacer committed Nov 22, 2024
1 parent cd76013 commit 89d9a05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
7 changes: 2 additions & 5 deletions packages/sane/lib/src/impl/sane_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SyncSane implements Sane {
ffi.Pointer<SANE_Char> username,
ffi.Pointer<SANE_Char> password,
) {
final credentials = authCallback!(dartStringFromSaneString(resource)!);
final credentials = authCallback!(resource.toDartString());
for (var i = 0;
i < credentials.username.length && i < SANE_MAX_USERNAME_LEN;
i++) {
Expand Down Expand Up @@ -368,10 +368,7 @@ class SyncSaneDevice implements SaneDevice, ffi.Finalizable {
saneFixedToDouble((valuePointer as ffi.Pointer<SANE_Word>).value);

case SaneOptionValueType.string:
result = dartStringFromSaneString(
valuePointer as ffi.Pointer<SANE_Char>,
) ??
'';
result = (valuePointer as ffi.Pointer<SANE_Char>).toDartString();

case SaneOptionValueType.button:
result = null;
Expand Down
19 changes: 4 additions & 15 deletions packages/sane/lib/src/type_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ SaneOptionConstraint? saneConstraintFromNative(
case SANE_Constraint_Type.CONSTRAINT_STRING_LIST:
final stringList = <String>[];
for (var i = 0; constraint.string_list[i] != ffi.nullptr; i++) {
final string = dartStringFromSaneString(constraint.string_list[i])!;
final string = constraint.string_list[i].toDartString();
stringList.add(string);
}
return SaneOptionConstraintStringList(stringList: stringList);
Expand All @@ -156,9 +156,9 @@ SaneOptionDescriptor saneOptionDescriptorFromNative(
) {
return SaneOptionDescriptor(
index: index,
name: dartStringFromSaneString(optionDescriptor.name) ?? '',
title: dartStringFromSaneString(optionDescriptor.title) ?? '',
desc: dartStringFromSaneString(optionDescriptor.desc) ?? '',
name: optionDescriptor.name.toDartString(),
title: optionDescriptor.title.toDartString(),
desc: optionDescriptor.desc.toDartString(),
type: saneOptionValueTypeFromNative(optionDescriptor.type),
unit: saneOptionUnitFromNative(optionDescriptor.unit),
size: optionDescriptor.size,
Expand Down Expand Up @@ -198,17 +198,6 @@ bool dartBoolFromSaneBool(int bool) {
}
}

@internal
String? dartStringFromSaneString(SANE_String_Const stringPointer) {
if (stringPointer == ffi.nullptr) return null;
return stringPointer.cast<ffi.Utf8>().toDartString();
}

@Deprecated('Use extension')
SANE_String_Const saneStringFromDartString(String string) {
return string.toSaneString();
}

@internal
extension SaneStringExtensions on SANE_String_Const {
String toDartString() => cast<ffi.Utf8>().toDartString();
Expand Down

0 comments on commit 89d9a05

Please sign in to comment.