summaryrefslogtreecommitdiff
path: root/doc/helper_headers.rst
blob: 7b4c083f2ae6909182298af372c9b819fd199d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
==============
Helper headers
==============

ctype helpers
=============

Functions from the ``<ctype.h>`` header, including, but not limited to,
``isalpha``, ``tolower``, and so forth, are defined to take an ``int`` as first
argument. C strings consist of ``char``s, which may either be ``signed`` or
``unsigned``. It does not matter for *characters_* but since ``char``s are
implicitly convertible to *numbers*, one needs to explicitly cast chars to
unsigned before feeding them to ``isalpha``.

.. code-block:: c

	/* “hyvää yötä”, UTF-8 encoded */

	const char h[] = {'h', 'y', 'v', 0xc3, 0xa4, 0xc3, 0xa4, ' ',
			  'y', 0xc3, 0xb6, 't', 0xc3, 0xa4};

libHX's ctype_helper.h therefore provides wrappers with a different function
signature that does the intended test. It does so by always taking ``unsigned
char``. The implication is that EOF cannot be passed to ``HX_isalpha`` — not
that there is a good reason to do so in the first place.

.. code-block:: c

	#include <libHX/ctype_helper.h>

	bool HX_isalnum(unsigned char c);
	bool HX_isalpha(unsigned char c);
	bool HX_isdigit(unsigned char c);
	bool HX_islower(unsigned char c);
	bool HX_isprint(unsigned char c);
	bool HX_isspace(unsigned char c);
	bool HX_isupper(unsigned char c);
	bool HX_isxdigit(unsigned char c);
	unsigned char HX_tolower(unsigned char c);
	unsigned char HX_toupper(unsigned char c);

The ``HX_is*`` functions also differ from ctype's in that they return ``bool``
instead of ``int``. Not all functions from ``ctype.h`` are present either;
``isascii``, ``isblank``, ``iscntrl``, ``isgraph``, ``ispunct`` and
``isxdigit`` have been omitted as the author has never needed them to this
date.


libxml2 helpers
===============

libxml2 defines a type called ``xmlChar`` for use within its strings.
``xmlChar`` is typedef'ed to ``unsigned char`` by libxml2, causing compiler warnings related to
differing signedness whenever interacting with strings from the
outside world, which are usually just a pointer to char. Because
casting is a real chore, ``libxml_helper.h`` will do it by
providing some wrappers with better argument types.

.. code-block:: c

	#include <libHX/libxml_helper.h>

	int xml_strcmp(const xmlChar *a, const char *b);
	int xml_strcasecmp(const xmlChar *a, const char *b);

	char *xml_getprop(xmlNode *node, const char *attr);
	char *xml_getnsprop(xmlNode *node, const char *nsprefix, const char *attr);
	xmlAttr *xml_newprop(xmlNode *node, const char *attr);
	xmlNode *xml_newnode(xmlNode *parent, const char *name, const char *value);
	xmlAttr *xml_setprop(xmlNode *node, const char *name, const char *value);

The functions map to ``strcmp``, ``strcasecmp``, ``xmlGetProp``,
``xmlNewPropxmlNewProp``, ``xmlNewTextNodexmlNewTextNode`` and
``xmlSetPropxmlSetProp``, respectively.

``xml_getnsprop`` works similar to ``xmlGetNsProp``, but instead of taking a
namespace URI, it does a lookup by namespace prefix. The argument order is also
different compared to ``xmlGetNsProp``.


wxWidgets
=========

.. code-block:: c++

	#include <libHX/wx_helper.hpp>

Shortcut macros
---------------

``wxACV``
	Expands to ``wxALIGN_CENTER_VERTICAL``.

``wxCDF``
	Expands to a set of common dialog flags for wxDialogs, which includes
	``wxDEFAULT_FRAME_STYLE`` and a flag such that the dialog does not
	create a new window in the task bar (``wxFRAME_NO_TASKBAR``).

``wxDPOS``
	Expands to ``wxDefaultPosition``.

``wxDSIZE``
	Expands to ``wxDefaultSize``.

``wxDSPAN``
	Expands to ``wxDefaultSpan``.

String conversion
-----------------

.. code-block:: c++

	wxString wxfu8(const char *);
	wxString wxfv8(const char *);
	const char *wxtu8(const wxString &);

``wxfu8``
	Converts a UTF-8 string to a ``wxString`` object.

``wxfv8``
	Converts a UTF-8 string to an entity usable by ``wxPrintf``.

``wxtu8``
	Converts a wxString to a pointer to char usable by ``printf``. Note
	that the validity of the pointer is very limited and usually does not
	extend beyond the statement in which it is used. Hence, storing the
	pointer in a variable (``const char *p = wxtu8(s);``) will make ``p``
	dangling as soon as the assignment has been completed.