summaryrefslogtreecommitdiff
path: root/src/utils.c
Commit message (Collapse)AuthorAgeFilesLines
* build: include config.h manuallyRan Benita2019-12-271-0/+2
| | | | | | | | | Previously we included it with an `-include` compiler directive. But that's not portable. And it's better to be explicit anyway. Every .c file should have `include "config.h"` first thing. Signed-off-by: Ran Benita <ran@unusedvar.com>
* Provide a fallback implementation of [v]asprintf()Adrian Perez de Castro2019-12-271-0/+45
| | | | | | Some environments (e.g. Windows + MSVC) do not provide asprintf() or vasprintf(). This tries to detect their presence, and provides suitable fallback implementations when not available.
* keysym: fix locale dependence in xkb_keysym_from_name()Ran Benita2016-12-021-0/+53
| | | | | | | | | | | | | | | | | | | | | | | We currently use strcasecmp, which is locale-dependent. In particular, one well-known surprise even if restricted just ASCII input is found in the tr_TR (Turkish) locale, see e.g. https://msdn.microsoft.com/en-us/library/ms973919.aspx#stringsinnet20_topic5 We have known to avoid locale-dependent functions before, but in this case, we forgot. Fix it by implementing our own simple ASCII-only strcasecmp/strncasecmp. Might have been possible to use strcasecmp_l() with the C locale, but went the easy route. Side advantage is that even this non-optimized version is faster than the optimized libc one (__strcasecmp_l_sse42) since it doesn't need to do the locale stuff. xkb_keysym_from_name(), which uses strcasecmp heavily, becomes faster, and so for example Compose file parsing, which uses xkb_keysym_from_name() heavily, becomes ~20% faster. Resolves https://github.com/xkbcommon/libxkbcommon/issues/42 Signed-off-by: Ran Benita <ran234@gmail.com>
* src/utils: check if fileno() failed in map_fileRan Benita2016-03-131-1/+4
| | | | | | | | | | | | | | | | fileno() can fail, if called on e.g. fmemopen() FILEs which are not backed by a file descriptor. This functions uses mmap to map the entire file to memory, so using such FILEs will not work. (There is actually no change of behavior here, since the following fstat would have already failed with EBADF. But lets make it clear.) Another possibility is to fall back to the !HAVE_MMAP case; but it sounds like a better idea to leave it to the programmer to use the new_from_string/new_from_buffer functions instead, instead of doing double allocation behind their back. Signed-off-by: Ran Benita <ran234@gmail.com>
* src/utils: change map_file to not take const string argumentRan Benita2015-11-191-6/+6
| | | | | | | | | map_file() uses PROT_READ, so const seems fitting; however unmap_file calls munmap/free, which do not take const, so an UNCONSTIFY is needed. To avoid the UNCONSTIFY hack, which is likely undefined behavior or some such, just remove the const. Signed-off-by: Ran Benita <ran234@gmail.com>
* utils: add {un,}map_file to read an entire fileRan Benita2013-04-011-0/+107
| | | | | | | This wraps the current mmap call and adds a fallback implementation for systems which do not have mmap (e.g. mingw). Signed-off-by: Ran Benita <ran234@gmail.com>
* Add logging APIRan Benita2012-07-231-154/+0
| | | | | | | | | | | | | | | | | | | | | | | | | Add new public API to provide the library users with some options to control and customize the logging output from the library. It is based upon the skeleton from the libabc demo libray: https://git.kernel.org/?p=linux/kernel/git/kay/libabc.git which is public domain and works pretty well. This requires passing in the context object in every logging call, and thus the conversion is done file by file. We also remove the global warningLevel variable in favor of a verbosity level in the context, which can be set by the user and is silent by default. One issue is the ACTION calls, which, while nice, do not play very well with line- and priority-based logging, and would require some line continuation handling or keeping state or some other compromise. So instead remove these and just inline them with their respective warning/error. So instead of: ERROR("Memory allocation failed\n") ACTION("Removing all files on hardisk\n") its something like that: log_err("Memory allocation failed; Removing all files on harddisk\n") Signed-off-by: Ran Benita <ran234@gmail.com>
* utils: replace FATAL by malloc_or_dieRan Benita2012-07-231-25/+0
| | | | | | | "Out of memory" is enough in this case. If we want to be OOM-safe this makes it clear where to begin. Signed-off-by: Ran Benita <ran234@gmail.com>
* Run source tree through uncrustifyDaniel Stone2012-07-171-30/+27
| | | | | | | .uncrustify.cfg committed for future reference also, but had to manually fix up a few things: it really likes justifying struct initialisers. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
* utils: remove unused recalloc and related macrosRan Benita2012-06-091-15/+0
| | | | | | Their use is superseded by darray everywhere now. Signed-off-by: Ran Benita <ran234@gmail.com>
* Rename XKBcommonint.h to xkb-priv.h and use itRan Benita2012-05-081-3/+2
| | | | | | | | | | Make the files in the src/* directory use their own header or a consilidated private header. This makes the file dependencies clearer. Also drop the pointless "xkb" file name prefix, add split a few declarations to their own files (atom.h and text.h). Signed-off-by: Ran Benita <ran234@gmail.com>
* Use stdbool.hRan Benita2012-04-301-3/+3
| | | | | | | | 'Cause defining your own True and False is so 1990's. Signed-off-by: Ran Benita <ran234@gmail.com> [daniels: Fixed for xkb_desc -> xkb_keymap changes.]
* Rewrite recalloc to the correct typeRan Benita2012-03-271-11/+8
| | | | | | | The recalloc function should be expressed in terms of bytes to match its name. However uTypedRecalloc retains its type so nothing is changed. Signed-off-by: Ran Benita <ran234@gmail.com>
* Remove useless stuff from utilsRan Benita2012-03-271-6/+9
| | | | | | | Signed-off-by: Ran Benita <ran234@gmail.com> [daniels: fixed conflicts from strcasecmp, added includes to make filecomp build again]
* Remove fallback strcasecmp/strncasecmpDaniel Stone2012-03-271-54/+0
| | | | | | | Sorry if your libc doesn't have this, but it's not my problem. Signed-off-by: Daniel Stone <daniel@fooishbar.org> Reported-by: Ran Benita <ran234@gmail.com>
* Move utils.{c,h} to be used by the entire projectRan Benita2012-03-271-0/+252
This is a first step for making consistent use of utils.h also outside of xkbcomp/ . Signed-off-by: Ran Benita <ran234@gmail.com>