summaryrefslogtreecommitdiff
path: root/src/libtracker-common/tracker-parser-utils.c
blob: 0394016ca98f909e962edb621e41a3f72c039a68 (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
/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "config.h"

#include <string.h>

#include <libtracker-common/tracker-utils.h>

#include "tracker-parser-utils.h"

/*
 * Definition of the possible reserved words.
 *  Length of word is explicitly given to avoid strlen() calls
 */
typedef struct {
	const gchar *word;
	gsize        word_length;
} TrackerParserReservedWord;

static const TrackerParserReservedWord reserved_words[] = {
	{ "and", 3 },
	{ "near", 4 },
	{ "not", 3 },
	{ "or", 2 },
	{ NULL, 0 }
};

gboolean
tracker_parser_is_reserved_word_utf8 (const gchar *word,
                                      gsize word_length)
{
	gint i = 0;

	/* Loop the array of predefined reserved words */
	while (reserved_words[i].word != NULL) {
		if (word_length == reserved_words[i].word_length &&
		    strncmp (word,
		             reserved_words[i].word,
		             word_length) == 0) {
			return TRUE;
		}
		i++;
	}

	return FALSE;
}


#if TRACKER_PARSER_DEBUG_HEX
void
tracker_parser_message_hex (const gchar  *message,
                            const gchar  *str,
                            gsize         str_length)
{
	gchar *hex_aux;
	gchar *str_aux;

	g_return_if_fail (message);
	g_return_if_fail (str);
	g_return_if_fail (str_length != 0);

	/* String may not come NIL-terminated */
	str_aux = g_malloc (str_length + 1);
	memcpy (str_aux, str, str_length);
	str_aux[str_length] = '\0';

	/* Get hexadecimal representation of the input string */
	hex_aux = tracker_strhex (str, str_length, ':');

	/* Log it */
	g_message ("%s: '%s' (%s)",
	           message, str_aux, hex_aux);

	g_free (str_aux);
	g_free (hex_aux);
}
#endif