Skip to content

Commit

Permalink
- Update C and C++ interfaces and examples to match the Delphi ones
Browse files Browse the repository at this point in the history
- add a TClassIDOrFactoryFunc cast to the C example to prevent compiler warning
  • Loading branch information
maelh committed Nov 17, 2021
1 parent 9437b27 commit 1fd3ef0
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ EXPORTS
AssignConverter @4
ChangeByteOrder @5
BytesToStr @6
StrToBytes @7
StrToBytes @7
AsInt64 @8
AsUInt64 @9
26 changes: 26 additions & 0 deletions Examples/C++/DataInspectorPluginExample/Int32Converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TInt32Converter::TInt32Converter()
{
FTypeName = L"C++ - Int32";
FFriendlyTypeName = FTypeName;
FCategory = tcSignedInteger;
FWidth = dtwFixed;
FMaxTypeSize = sizeof(int32_t);
FSupportedByteOrders = 1 << boLittleEndian | 1 << boBigEndian;
Expand Down Expand Up @@ -145,3 +146,28 @@ TStrToBytesError TInt32Converter::StrToBytes(std::wstring Str,

return result;
}

TBytesToIntError TInt32Converter::AsInt64(uint8_t* Bytes, int ByteCount,
int& ConvertedByteCount, int64_t& ConvertedInt)
{
if (ByteCount >= sizeof(int32_t))
{
ConvertedByteCount = sizeof(int32_t);
ConvertedInt = *(int32_t*)(Bytes);
return btieNone;
}
else
{
ConvertedByteCount = 0;
ConvertedInt = 0;

return btieBytesTooShort;
}
}

// not supported, just a dummy function to satisfy class requirements
TBytesToIntError TInt32Converter::AsUInt64(uint8_t* Bytes, int ByteCount,
int& ConvertedByteCount, uint64_t& ConvertedInt)
{
return btieNone;
}
5 changes: 5 additions & 0 deletions Examples/C++/DataInspectorPluginExample/Int32Converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ class TInt32Converter : public TExternalDataTypeConverter
TStrToBytesError StrToBytes(std::wstring Str,
TFormattingOptions FormattingOptions,
std::vector<uint8_t>& ConvertedBytes) override;

TBytesToIntError AsInt64(uint8_t* Bytes, int ByteCount,
int& ConvertedByteCount, int64_t& ConvertedInt) override;
TBytesToIntError AsUInt64(uint8_t* Bytes, int ByteCount,
int& ConvertedByteCount, uint64_t& ConvertedInt) override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ EXPORTS
AssignConverter @4
ChangeByteOrder @5
BytesToStr @6
StrToBytes @7
StrToBytes @7
AsInt64 @8
AsUInt64 @9
30 changes: 28 additions & 2 deletions Examples/C/DataInspectorPluginExample/Int32Converter.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ typedef struct TInt32ConverterInstance {

void* __stdcall CreateConverter(TConverterClassID ClassIDOrFactoryFunc,
const wchar_t** TypeName, const wchar_t** FriendlyTypeName,
TDataTypeWidth* Width, int* MaxTypeSize, TByteOrders* SupportedByteOrders,
BOOL* SupportsStrToBytes)
TTypeCategory* Category, TDataTypeWidth* Width, int* MaxTypeSize,
TByteOrders* SupportedByteOrders, BOOL* SupportsStrToBytes)
{
// ClassIDOrFactoryFunc can be used to delegate creation to constructor
// functions as needed. See the C++ plugin for an example.
Expand All @@ -59,6 +59,7 @@ void* __stdcall CreateConverter(TConverterClassID ClassIDOrFactoryFunc,

*TypeName = L"C - Int32";
*FriendlyTypeName = *TypeName;
*Category = tcSignedInteger;
*Width = dtwFixed;
*MaxTypeSize = sizeof(int32_t);
*SupportedByteOrders = 1 << boLittleEndian | 1 << boBigEndian;
Expand Down Expand Up @@ -226,6 +227,31 @@ TStrToBytesError __stdcall StrToBytes(void* ThisPtr, const wchar_t* Str,
return result;
}

TBytesToIntError __stdcall AsInt64(void* ThisPtr, uint8_t* Bytes, int ByteCount,
int* ConvertedByteCount, int64_t* ConvertedInt)
{
if (ByteCount >= sizeof(int32_t))
{
*ConvertedByteCount = sizeof(int32_t);
*ConvertedInt = *(int32_t*)(Bytes);
return btieNone;
}
else
{
*ConvertedByteCount = 0;
*ConvertedInt = 0;

return btieBytesTooShort;
}
}

// not supported, just a dummy function to satisfy export requirements
TBytesToIntError __stdcall AsUInt64(void* ThisPtr, uint8_t* Bytes,
int ByteCount, int* ConvertedByteCount, uint64_t* ConvertedInt)
{
return btieNone;
}


// Actual value does not matter (so assign 0), we only need the address to this
// constant storage, see RegisterDataTypeConverter() call in dllmain.c.
Expand Down
2 changes: 1 addition & 1 deletion Examples/C/DataInspectorPluginExample/dllmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
RegisterDataTypeConverter(&Int32ConverterClassID);
RegisterDataTypeConverter((TClassIDOrFactoryFunc)&Int32ConverterClassID);
break;

case DLL_THREAD_ATTACH:
Expand Down
24 changes: 22 additions & 2 deletions Interface/C++/DataInspectorPluginServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void TExternalDataTypeConverter::Assign(TExternalDataTypeConverter* Source)
{
FTypeName = Source->FTypeName;
FFriendlyTypeName = Source->FFriendlyTypeName;
FCategory = Source->FCategory;
FWidth = Source->FWidth;
FMaxTypeSize = Source->FMaxTypeSize;
FSupportedByteOrders = Source->FSupportedByteOrders;
Expand All @@ -19,8 +20,8 @@ void TExternalDataTypeConverter::Assign(TExternalDataTypeConverter* Source)

void* __stdcall CreateConverter(TConverterClassID ClassIDOrFactoryFunc,
const wchar_t** TypeName, const wchar_t** FriendlyTypeName,
TDataTypeWidth* Width, int* MaxTypeSize, TByteOrders* SupportedByteOrders,
BOOL* SupportsStrToBytes)
TTypeCategory* Category, TDataTypeWidth* Width, int* MaxTypeSize,
TByteOrders* SupportedByteOrders, BOOL* SupportsStrToBytes)
{
TExternalDataTypeConverterFactoryFunction FactoryFunc =
(TExternalDataTypeConverterFactoryFunction)ClassIDOrFactoryFunc;
Expand All @@ -29,6 +30,7 @@ void* __stdcall CreateConverter(TConverterClassID ClassIDOrFactoryFunc,

*TypeName = Converter->GetTypeName().c_str();
*FriendlyTypeName = Converter->GetFriendlyTypeName().c_str();
*Category = Converter->GetCategory();
*Width = Converter->GetWidth();
*MaxTypeSize = Converter->GetMaxTypeSize();
*SupportedByteOrders = Converter->GetSupportedByteOrders();
Expand Down Expand Up @@ -91,6 +93,24 @@ TStrToBytesError __stdcall StrToBytes(void* ThisPtr, const wchar_t* Str,
return result;
}

TBytesToIntError __stdcall AsInt64(void* ThisPtr, uint8_t* Bytes,
int ByteCount, int* ConvertedByteCount, int64_t* ConvertedInt)
{
TExternalDataTypeConverter* Converter = (TExternalDataTypeConverter*)ThisPtr;

return Converter->AsInt64(Bytes, ByteCount, *ConvertedByteCount,
*ConvertedInt);
}

TBytesToIntError __stdcall AsUInt64(void* ThisPtr, uint8_t* Bytes,
int ByteCount, int* ConvertedByteCount, uint64_t* ConvertedInt)
{
TExternalDataTypeConverter* Converter = (TExternalDataTypeConverter*)ThisPtr;

return Converter->AsUInt64(Bytes, ByteCount, *ConvertedByteCount,
*ConvertedInt);
}

std::vector<TConverterClassID> InternalClassIDsOrFactoryFuncs;

void RegisterDataTypeConverter(
Expand Down
7 changes: 7 additions & 0 deletions Interface/C++/DataInspectorPluginServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ class TExternalDataTypeConverter
TFormattingOptions FormattingOptions,
std::vector<uint8_t>& ConvertedBytes) = 0;

virtual TBytesToIntError AsInt64(uint8_t* Bytes, int ByteCount,
int& ConvertedByteCount, int64_t& ConvertedInt) = 0;
virtual TBytesToIntError AsUInt64(uint8_t* Bytes, int ByteCount,
int& ConvertedByteCount, uint64_t& ConvertedInt) = 0;

protected:
std::wstring FTypeName;
std::wstring FFriendlyTypeName;
TTypeCategory FCategory;
TDataTypeWidth FWidth;
int FMaxTypeSize;
TByteOrders FSupportedByteOrders;
Expand All @@ -42,6 +48,7 @@ class TExternalDataTypeConverter
public:
const std::wstring& GetTypeName() { return FTypeName; }
const std::wstring& GetFriendlyTypeName() { return FFriendlyTypeName; }
const TTypeCategory& GetCategory() { return FCategory; }
const TDataTypeWidth& GetWidth() { return FWidth; }
const int& GetMaxTypeSize() { return FMaxTypeSize; }
const TByteOrders& GetSupportedByteOrders() { return FSupportedByteOrders; }
Expand Down
15 changes: 15 additions & 0 deletions Interface/C/DataInspectorPluginInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void* __stdcall CreateConverter(
TConverterClassID ClassIDOrFactoryFunc,
const wchar_t** TypeName,
const wchar_t** FriendlyTypeName,
TTypeCategory* Category,
TDataTypeWidth* Width,
int* MaxTypeSize,
TByteOrders* SupportedByteOrders,
Expand Down Expand Up @@ -50,6 +51,20 @@ TStrToBytesError __stdcall StrToBytes(
uint8_t** ConvertedBytes,
int* ConvertedByteCount);

TBytesToIntError __stdcall AsInt64(
void* ThisPtr,
uint8_t* Bytes,
int ByteCount,
int* ConvertedByteCount,
int64_t* ConvertedInt);

TBytesToIntError __stdcall AsUInt64(
void* ThisPtr,
uint8_t* Bytes,
int ByteCount,
int* ConvertedByteCount,
uint64_t* ConvertedInt);

#ifdef __cplusplus
}
#endif
34 changes: 22 additions & 12 deletions Interface/C/DataInspectorShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ extern "C" {

#include "inttypes.h"

typedef enum TTypeCategory {
tcUnknown, tcBitSequence, tcSignedInteger, tcUnsignedInteger, tcFloat,
tcCharacter, tcCharacterString, tcDate, tcTime, tcDateTime, tcAssembly
} TTypeCategory;

typedef enum TByteOrder {
boLittleEndian, boBigEndian
boLittleEndian, boBigEndian
} TByteOrder;

// TByteOrders = set of TByteOrder // a set of flags
Expand All @@ -21,37 +26,42 @@ typedef enum TByteOrder {
typedef uint8_t TByteOrders;

typedef enum TDataTypeWidth {
dtwVariable, dtwFixed
dtwVariable, dtwFixed
} TDataTypeWidth;

typedef enum TIntegerBase {
ibDecimal, ibHexadecimal
ibDecimal, ibHexadecimal
} TIntegerBase;

typedef enum THexBaseIndication {
hbiPascalAndMotorola, hbiC, hbiIntelNoLeadingZero, hbiIntelLeadingZero
hbiNone, hbiPascalAndMotorola, hbiC, hbiIntelNoLeadingZero,
hbiIntelLeadingZero
} THexBaseIndication;

typedef enum TLetterCase {
lcUpperCase, lcLowerCase
lcUpperCase, lcLowerCase
} TLetterCase;

typedef struct TFormattingOptions {
TIntegerBase IntegerBase;
THexBaseIndication HexBaseIndication;
TLetterCase HexCasing;
TLetterCase InstructionCasing;
TIntegerBase IntegerBase;
THexBaseIndication HexBaseIndication;
TLetterCase HexCasing;
TLetterCase InstructionCasing;
} TFormattingOptions;

typedef enum TBytesToStrError {
btseNone, btseInvalidBytes, btseBytesTooShort
btseNone, btseInvalidBytes, btseBytesTooShort
} TBytesToStrError;

typedef enum TStrToBytesError {
stbeNone, stbeInvalidString, stbeUnderflow, stbeOverflow,
stbeOutOfRange // if unclear whether underflow or overflow
stbeNone, stbeInvalidString, stbeUnderflow, stbeOverflow,
stbeOutOfRange // if unclear whether underflow or overflow
} TStrToBytesError;

typedef enum TBytesToIntError {
btieNone, btieBytesTooShort
} TBytesToIntError;

typedef void* TConverterClassID;
typedef TConverterClassID* PConverterClassID;

Expand Down

0 comments on commit 1fd3ef0

Please sign in to comment.