summaryrefslogtreecommitdiffstats
path: root/src/client/misc.c
blob: 827bd90abb4d3ccb157a1b1f48be50307f513090 (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
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "client.h"
#include <string.h>
#include <ctype.h>

/**
 * Check if an element is present in a comma-separated list.
 *
 * @param list    Comma-separated list of elements.
 * @param element Element we want to check for.
 * @return 0 if the element was not found, 1 otherwise.
 */
int
contains(const char *list, const char *element)
{
	int len;
	if (element == NULL || list == NULL) return 0;
	while (list) {
		len = strlen(element);
		if (!strncmp(list, element, len) &&
		    (list[len] == '\0' || list[len] == ','))
			return 1;
		list = strchr(list, ',');
		if (list) list++;
	}
	return 0;
}

/**
 * Transform a string to a tag. This puts the string into lower space and
 * replace spaces with '-'. The result is statically allocated.
 *
 * @param value String to transform to a tag.
 * @return The tagged value or the string "none" if @c value is @c NULL
 */
const char *
totag(const char *value)
{
	int i;
	static char *result = NULL;
	free(result);
	result = NULL;
	if (!value) return "none";
	result = calloc(1, strlen(value) + 1);
	if (!result) return "none";
	for (i = 0; i < strlen(value); i++) {
		switch (value[i]) {
		case ' ':
			result[i] = '-';
			break;
		default:
			result[i] = tolower((int)value[i]);
			break;
		}
	}
	return result;
}