summaryrefslogtreecommitdiff
path: root/src/libimcv/imv/imv_reason_string.c
blob: d1447ec35345884189a02ef9c9b721d24b40a737 (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
/*
 * Copyright (C) 2012 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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 General Public License
 * for more details.
 */

#include "imv_reason_string.h"

#include <utils/debug.h>

typedef struct private_imv_reason_string_t private_imv_reason_string_t;

/**
 * Private data of an imv_reason_string_t object.
 */
struct private_imv_reason_string_t {

	/**
	 * Public members of imv_reason_string_t
	 */
	imv_reason_string_t public;

	/**
	 * Preferred language
	 */
	char *lang;

	/**
	 * Contains the concatenated reasons
	 */
	chunk_t reasons;

};

METHOD(imv_reason_string_t, add_reason, void,
	private_imv_reason_string_t *this, imv_lang_string_t reason[])
{
	char *s_reason;

	s_reason = imv_lang_string_select_string(reason, this->lang);

	if (this->reasons.len)
	{
		/* append any further reasons */
		this->reasons = chunk_cat("mcc", this->reasons, chunk_from_chars('\n'),
								  chunk_create(s_reason, strlen(s_reason)));
	}
	else
	{
		/* add the first reason */
		this->reasons = chunk_clone(chunk_create(s_reason, strlen(s_reason)));
	}
}

METHOD(imv_reason_string_t, get_encoding, chunk_t,
	private_imv_reason_string_t *this)
{
	return this->reasons;
}

METHOD(imv_reason_string_t, destroy, void,
	private_imv_reason_string_t *this)
{
	free(this->reasons.ptr);
	free(this);
}

/**
 * Described in header.
 */
imv_reason_string_t *imv_reason_string_create(char *lang)
{
	private_imv_reason_string_t *this;

	INIT(this,
		.public = {
			.add_reason = _add_reason,
			.get_encoding = _get_encoding,
			.destroy = _destroy,
		},
		.lang = lang,
	);

	return &this->public;
}