How can I get the corresponding error string from an FT_Error code? - freetype2

Given an FT_Error, is there a macro/function to return the corresponding error message string?

The answer is No, as far as I know.
You should create your own macro or function,
using macros defined in fterrors.h.
For example, the following code will work:
#include <ft2build.h>
#include FT_FREETYPE_H
const char* getErrorMessage(FT_Error err)
{
#undef FTERRORS_H_
#define FT_ERRORDEF( e, v, s ) case e: return s;
#define FT_ERROR_START_LIST switch (err) {
#define FT_ERROR_END_LIST }
#include FT_ERRORS_H
return "(Unknown error)";
}
Read fterrors.h (found in /usr/local/include/freetype2/ in my environment) for more detail and another example.

In case anyone else stumbles upon this question, FT_Error_String was introduced in FreeType2 version 2.10.1 to retrieve the error string of an FT_Error code.
FT_Error error = ...
if (error != 0)
{
puts(FT_Error_String(error), stderr);
}
Important: You must define FT_CONFIG_OPTION_ERROR_STRINGS when compiling FreeType otherwise the function will return NULL.

Related

pcap_getnonblock() returns -3

I am quite new to using pcap lib, so please bear with me.
I am trying to use pcap_getnonblock function, the documentation says the following:
pcap_getnonblock() returns the current 'non-blocking' state of
the capture descriptor; it always returns 0 on 'savefiles' . If
there is an error, PCAP_ERROR is returned and errbuf is filled in
with an appropriate error message.
errbuf is assumed to be able to hold at least PCAP_ERRBUF_SIZE
chars.
I got -3 returned and the errbuf is an empty string, I couldn't understand the meaning of such result.
I believe this caused a socket error: 10065.
This problem happened only once and I could not reproduce it, but still it would be great to find its causing to prevent it in future executions.
Thanks in advance.
pcap_getnonblock() can return -3 - that's PCAP_ERROR_NOT_ACTIVATED. Unfortunately, that's not documented; I'll fix that.
Here's a minimal reproducible example that demonstrates this:
#include <pcap/pcap.h>
#include <stdio.h>
int
main(int argc, char **argv)
{
pcap_t *pcap;
char errbuf[PCAP_ERRBUF_SIZE];
if (argc != 2) {
fprintf(stderr, "Usage: this_program <interface_name>\n");
return 1;
}
pcap = pcap_create(argv[1], errbuf);
if (pcap == NULL) {
fprintf(stderr, "this_program: pcap_create(%s) failed: %s\n",
argv[1], errbuf);
return 2;
}
printf("pcap_getnonblock() returns %d on non-activated pcap_t\n",
pcap_getnonblock(pcap, errbuf));
return 0;
}
(yes, that's minimal, as 1) names of interfaces are OS-dependent, so it has to be a command-line argument and 2) if you don't run the program correctly, it should let you know what's happening, so you know what you have to do in order to reproduce the problem).
Perhaps pcap_getnonblock() and pcap_setnonblock() should be changed so that you can set non-blocking mode before activating the pcap_t, so that, when activated, it will be in non-blocking mode. It doesn't work that way currently, however.
I.e., you're allocating a pcap_t with pcap_create(), but you're not activating it with pcap_activate(). You need to do both in order to have a pcap_t on which you can capture.

SWIG wrong encoded string crashes Python

I've a problem where all my SWIG wrappers that deals with strings crashes If I pass a wrong encoded string inside a std::string, I mean strings that contains èé and so on, characters valid for the current locale, but not UTF-8 valid.
On my code side, I have solved parsing the input as wide strings and convert them to UTF-8, but I would like to catch those kind of errors with an Exception rather than a crash, isn't supposed PyUnicode_Check to fail with those strings ?
Swig actually crashes in SWIG_AsCharPtrAndSize() when calling PyString_AsStringAndSize(), this is the swig generated code:
SWIGINTERN int
SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
{
#if PY_VERSION_HEX>=0x03000000
#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
if (PyBytes_Check(obj))
#else
if (PyUnicode_Check(obj))
#endif
#else
if (PyString_Check(obj))
#endif
{
char *cstr; Py_ssize_t len;
#if PY_VERSION_HEX>=0x03000000
#if !defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
if (!alloc && cptr) {
/* We can't allow converting without allocation, since the internal
representation of string in Python 3 is UCS-2/UCS-4 but we require
a UTF-8 representation.
TODO(bhy) More detailed explanation */
return SWIG_RuntimeError;
}
obj = PyUnicode_AsUTF8String(obj);
if(alloc) *alloc = SWIG_NEWOBJ;
#endif
PyBytes_AsStringAndSize(obj, &cstr, &len);
#else
PyString_AsStringAndSize(obj, &cstr, &len);
#endif
if (cptr) {
Crash happens to into the last PyString_AsStringAndSize visible.
I remark that strings are passed as std::string but in happens also with const char* without any kind of difference.
Thanks in advice !
Cannot reproduce. Edit your question and add a Minimal, Complete, Verifable Example if this example doesn't solve your issue and need further help:
test.i
%module test
%include <std_string.i>
%inline %{
#include <string>
std::string func(std::string s)
{
return '[' + s + ']';
}
%}
Demo:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.func('ábc')
'[ábc]'
Problem was with 3.3.0 version we were still using, updating to 3.3.7 solved the problem, in the Python release notes there's several bug fixed in regards to PyUnicode_Check

Firebreath plugin, how to read <param > values

I want to read the <param> values of custom plugin
I could not find answer on the internet, what I found was:
https://github.com/firebreath/FireBreath/blob/master/src/NpapiCore/NpapiPlugin.cpp#L76
I see params are stored in pluginMain->setParams(paramList);
Can you point how can I access this paramList later? or pluginMain
Is there pluginMain->getParams()? I could not find reference
Nor I could locate the source for setParams().
The question is, how do I get that parameters from PluginWindowXXX or FB::NpapiPluginXXX ?
I exported m_npHost to PluginWindowXXX, set breakpoint in it with gdb but still no success.
All I can think of was:
(gdb) p ((FB::Npapi::NpapiBrowserHost)this->m_npHost)->GetValue
$17 = {NPError (const FB::Npapi::NpapiBrowserHost * const, NPNVariable, void *)} 0x7fe435adeff8 <FB::Npapi::NpapiBrowserHost::GetValue(NPNVariable, void*) const>
Obviously what I do is wrong but I am stuck,
I am passing this host from NpapiPluginX11.cpp
pluginWin->setHost(m_npHost);
taxilian's answer is the most correct one as always but I'll give a try. I'm reading params in my MyPluginAPI constructor.
MyPluginAPI::MyPluginAPI(const MyPluginPtr& plugin, const FB::BrowserHostPtr& host) : m_plugin(plugin), m_host(host)
{
string settings; //<param name="settings" value="{'foo':'bar'}">
settings = plugin->getParam("settings");
}
Inside your PluginCore-derived class, you can use either the getParam method or the getParamVariant method.
From the FireBreath Source:
boost::optional<std::string> PluginCore::getParam(const std::string& key) {
boost::optional<std::string> rval;
FB::VariantMap::const_iterator fnd = m_params.find(key.c_str());
if (fnd != m_params.end())
rval.reset(fnd->second.convert_cast<std::string>());
return rval;
}
FB::variant FB::PluginCore::getParamVariant( const std::string& key )
{
FB::VariantMap::const_iterator fnd = m_params.find(key.c_str());
if (fnd != m_params.end())
return fnd->second;
return FB::variant();
}
So if it's for sure a string (which it pretty much is, unless it starts with on, in which case it might have been converted to a referenced function), you can use:
boost::optional<std::string> mystr = getParam("mystr");
if (mystr) {
call_fn_with_string(*mystr);
}
Alternately, you can get it as a variant and convert it:
FB::variant mystrVal = getParamVariant("mystr");
try {
call_fn_with_string(mystrVal.convert_cast<std::string>());
} catch (FB::bad_variant_cast &err) {
// What to do if the cast to string fails
}

Problems using libpng in iPhone project

I am trying to add libpng to my iPhone project.
I copied the .c and .h files to their own directory "thirdparty/libpng/" and I included png.h in my texture class:
#ifndef PNG_H
#include "thirdparty/libpng/png.h"
#endif
At this point my project compiles great with no warnings and errors.
Next I tried adding a function to check if a texture is a png, and I get a compile error on png_sig_cmp, even though png.h is included:
#define PNG_BYTES_TO_CHECK 4
int GETexture::CheckIfValidPNGTexture( const char* pTextureName, FILE **ppFp )
{
char buf[PNG_BYTES_TO_CHECK];
/* Open the prospective PNG file. */
if ((*ppFp = fopen(pTextureName, "rb")) == NULL)
return 0;
/* Read in some of the signature bytes */
if (fread(buf, 1, PNG_BYTES_TO_CHECK, *ppFp) != PNG_BYTES_TO_CHECK)
return 0;
/* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
Return nonzero (true) if they match */
return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); // <- COMPILE ERROR
}
The error I get is: No matching function for call to 'png_sig_cmp'
The header is definitely getting included. If I try to type something random in it like "sdfdd" I get a compile error, showing it is parsing that header file.
Any ideas?
I had exactly the same problem once and what helped me was simple casting - because if you look into png.h header file , png_sig_cmp definition :
png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
First parameter is png_const_bytep - which is defined as :
PNG_CONST png_byte FAR * png_const_bytep
which translated really as :
const unsigned char*
I would simple typecast :
return(!png_sig_cmp((png_const_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
Hope it helps!

I get no output from xgettext

I will try to use gnu-gettext for localization of a semi-big software project, so now I'm trying to learn the basics. Problem is that I got stuck on a pretty fundamental function. When I try to extract the strings from the sourcecode using xgettext I get nothing. When dll's were missing it complained, and when a parameter is wrong it complains, but now it just silently returns without producing any pot-file or anything else.
So, my question is:
Is there anybody out there recognizing this problem?
Is there any way to make xgettext more verbose about what it is doing?
I have tried putting xgettext among the source-files and putting the sourcefiles in the gettext\bin directory, but to no avail.
I should mention that I am working on a Win7-machine and I use gettext-tools-dev_0.18.1.1-2_win32. I have installed MinGW.
My testcode locks like this:
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "libintl.h"
#include "locale.h"
#include "helper.h"
#define _(String) gettext(String)
#define N_(String) String
//#define textdomain(Domain)
//#define bindtextdomain(Package, Directory)
int main(void)
{
printf( "setlocale returns %s\n", setlocale( LC_ALL, "" ) );
bindtextdomain( "hello", "locale" );
textdomain( "hello" );
int a = 1;
int b = 2;
/// First string
printf( _( "Hello, world!\n" ) );
std::string multiline =
/* Another string */
_( "This is a " \
"multi line string." );
// A string that contains another
printf( _( "A string: %s\n" ), multiline.c_str() );
printf( N_( "An untranslatable string!\n" ));
int foo = 42;
/// Playing with positions; int before string in original...
printf( _( "int: %1$d, string: %2$s\n" ), foo, _( "Fubar" ));
printf( _( "%1$s %2$s\n" ), Helper::String1().c_str(), Helper::String2().c_str() );
exit(0);
}
If somebody could help me out on this, I would be thankful.
/Robert
Ok, I solved it. I somehow thought that xgettext would understand the redefinition:
#define _(String) gettext(String)
Well, obviously it didn't and when I read the manual carefully it kind of said that as well.
So when I added -k_ to the xgettext options it all worked.
/Robert