2021-09-16, 09:46
Your explanations seemed reasonable to me too and they set me on the right track to find the solution: thanks again Roman!
For future reference one solution is:
In a nutshell the problems were:
For future reference one solution is:
cpp:
ssize_t Read(void* context, void* buffer, size_t uiBufSize) override
{
std:tring my_string = "my test string";
std::memcpy(buffer, my_string.c_str(), my_string.length());
return my_string.length();
}
int64_t GetLength(void* context) override
{
std:tring my_string = "my test string";
int64_t length = my_string.length();
return length;
}
In a nutshell the problems were:
- the size of the string returned by xbmcvfs.File(...).read() is defined by the returned value of the C method GetLength(). I was returning a dummy value in this method which explains why there were some extra bytes which python complained about (including null bytes). For instance changing return length; to return 1; in the code above will make the string returned by xbmcvfs.File(...).read() equal to "m".
- assigning the void* buffer to another pointer was not working as expected: I think it was assigning the pointer somehow to a local/temporary value. Using memcpy copies the data in the out pointer correctly.
- std:: string seems to be a rather useful C++ class which comes with many methods but it is not possible to use the reference of a std:: string directly because as you said the data at this address contains all the bells and whistles that make the class. This class provides the method c_str() which returns an equivalent char* which is actually only the string and can be used in memcpy.