Skip to main content
Inspiring
April 26, 2024
Answered

SDK Optimal string conversions and error handling

  • April 26, 2024
  • 1 reply
  • 409 views

Hi

 

After working with the SDK for a while I found myself still struggling with string conversions. So I decided to create a small class for it. Together with my good friend ChatGPT I have ended up with the following class:

#ifndef AE_STRING_CONVERTER
#define AE_STRING_CONVERTER

#include <locale>
#include <codecvt>
#include <string>

class AEStringConverter
{
public:

	static PF_Err StringToAChar(const std::string& inString, A_char* outAChar, size_t bufferSize)
	{
		PF_Err err = PF_Err_NONE;

		errno_t copyResult = strcpy_s(outAChar, bufferSize, inString.c_str());

		if (copyResult != 0)
		{
			err = PF_Err_INTERNAL_STRUCT_DAMAGED;
		}

		return err;
	}

	static PF_Err StringToAUTF16Char(const std::string& inString, A_UTF16Char* outAChar, size_t maxOutChars)
	{
		PF_Err err = PF_Err_NONE;

		std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
		std::wstring utf16String = converter.from_bytes(inString);

		if (utf16String.size() + 1 > maxOutChars)
		{
			return PF_Err_OUT_OF_MEMORY;
		}

		std::copy(utf16String.begin(), utf16String.end(), outAChar);
		outAChar[utf16String.size()] = L'\0';  // Ensure null-terminated

		return err;
	}

	static PF_Err ACharToString(const A_char* inAChar, std::string* outString)
	{
		PF_Err err = PF_Err_NONE;

		*outString = std::string(inAChar);

		return err;
	}

	static PF_Err AUTF16CharToString(const A_UTF16Char* inAUTF16Char, std::string* outString)
	{
		PF_Err err = PF_Err_NONE;
		try
		{
			std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
			*outString = converter.to_bytes((wchar_t*)inAUTF16Char);
		}
		catch (const std::range_error&)
		{
			err = PF_Err_INTERNAL_STRUCT_DAMAGED;
		}
		catch (...)
		{
			err = PF_Err_OUT_OF_MEMORY;
		}

		return err;
	}
};

#endif /* AE_STRING_CONVERTER */

To test the class. I call the class like this, to convert a string to A_Char and an A_UTF16Char and then back to string again:

const size_t	BUFFER_SIZE = 1024;
std::string		myString = "This is my string";
A_char			myAChar[BUFFER_SIZE];
A_UTF16Char		myAUTF16Char[BUFFER_SIZE];
std::string		stringFromAChar;
std::string		stringFromAUTF16Char;

ERR(AEStringConverter::StringToAChar(myString, myAChar, BUFFER_SIZE));
ERR(AEStringConverter::StringToAUTF16Char(myString, myAUTF16Char, BUFFER_SIZE));
ERR(AEStringConverter::ACharToString(myAChar, &stringFromAChar));
ERR(AEStringConverter::AUTF16CharToString(myAUTF16Char, &stringFromAUTF16Char));

 I have three questions:

1. How do I actually know what BUFFER_SIZE should be? Is it simply the number of characters in a string? Could I simply use myString.size()+1? Or would that fail in some cases?

 

2. The error handling. Is it correct to use those errors (PF_Err_OUT_OF_MEMORY etc.) or should/could I create my own?

 

3. Is there anything you would change in the AEStringConverter class or do differently? Any fallpits as you see it?

 

Once the class is perfect I'll put it on github for others to find. I greatly appreciate any inputs.

 

Thanks,

Jakob

This topic has been closed for replies.
Correct answer shachar carmi

you can predict the buffer size using MultiByteToWideChar with a null fr the last paramter.
https://stackoverflow.com/questions/6693010/how-do-i-use-multibytetowidechar

 

as for the returned errors, i'd only use PF_Err_OUT_OF_MEMORY, as PF_Err_INTERNAL_STRUCT_DAMAGED tells AE this instance of the effect is bad and AE won't talk to it again. (if i recall correctly...)
in any case, PF_Err_OUT_OF_MEMORY is the polite way of telling AE a cetain operation has failed and it's results should be ignored.

1 reply

shachar carmiCommunity ExpertCorrect answer
Community Expert
April 26, 2024

you can predict the buffer size using MultiByteToWideChar with a null fr the last paramter.
https://stackoverflow.com/questions/6693010/how-do-i-use-multibytetowidechar

 

as for the returned errors, i'd only use PF_Err_OUT_OF_MEMORY, as PF_Err_INTERNAL_STRUCT_DAMAGED tells AE this instance of the effect is bad and AE won't talk to it again. (if i recall correctly...)
in any case, PF_Err_OUT_OF_MEMORY is the polite way of telling AE a cetain operation has failed and it's results should be ignored.

Inspiring
April 29, 2024

Thanks! 🙂