summaryrefslogtreecommitdiff
path: root/src/utils/utils.h
blob: 3f7fcd0e895c6e2fe1b7c3d43aa50a0796a639a5 (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
/*
 * This file is part of LibCSS.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007-8 John-Mark Bell <jmb@netsurf-browser.org>
 */

#ifndef css_utils_h_
#define css_utils_h_

#include <libwapcaplet/libwapcaplet.h>

#include <libcss/types.h>
#include <libcss/errors.h>

#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif

#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif

#ifndef SLEN
/* Calculate length of a string constant */
#define SLEN(s) (sizeof((s)) - 1) /* -1 for '\0' */
#endif

#ifndef UNUSED
#define UNUSED(x) ((x)=(x))
#endif

#ifndef N_ELEMENTS
#define N_ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))
#endif

css_fixed css__number_from_lwc_string(lwc_string *string, bool int_only,
		size_t *consumed);
css_fixed css__number_from_string(const uint8_t *data, size_t len,
		bool int_only, size_t *consumed);

static inline bool isDigit(uint8_t c)
{
	return '0' <= c && c <= '9';
}

static inline bool isHex(uint8_t c)
{
	return isDigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
}

static inline uint32_t charToHex(uint8_t c)
{
	c -= '0';

	if (c > 9)
		c -= 'A' - '9' - 1;

	if (c > 15)
		c -= 'a' - 'A';

	return c;
}

static inline css_error css_error_from_lwc_error(lwc_error err)
{
        switch (err) {
        case lwc_error_ok:
                return CSS_OK;
        case lwc_error_oom:
                return CSS_NOMEM;
        case lwc_error_range:
                return CSS_BADPARM;
        default:
                break;
        }
        return CSS_INVALID;
}


/* Allocator to pass to libparserutils */
#define CSS_ALLOC_PW NULL
void *css_alloc(void *data, size_t len, void *pw);

#endif