Allow reason strings to be used as workitem result string
[strongswan.git] / src / libimcv / imv / imv_reason_string.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imv_reason_string.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_imv_reason_string_t private_imv_reason_string_t;
21
22 /**
23 * Private data of an imv_reason_string_t object.
24 */
25 struct private_imv_reason_string_t {
26
27 /**
28 * Public members of imv_reason_string_t
29 */
30 imv_reason_string_t public;
31
32 /**
33 * Preferred language
34 */
35 char *lang;
36
37 /**
38 * Separator concatenating multiple reasons
39 */
40 char *separator;
41
42 /**
43 * Contains the concatenated reasons
44 */
45 chunk_t reasons;
46
47 };
48
49 METHOD(imv_reason_string_t, add_reason, void,
50 private_imv_reason_string_t *this, imv_lang_string_t reason[])
51 {
52 char *s_reason;
53
54 s_reason = imv_lang_string_select_string(reason, this->lang);
55
56 if (this->reasons.len)
57 {
58 /* append any further reasons */
59 this->reasons = chunk_cat("mcc", this->reasons,
60 chunk_from_str(this->separator),
61 chunk_create(s_reason, strlen(s_reason)));
62 }
63 else
64 {
65 /* add the first reason */
66 this->reasons = chunk_clone(chunk_create(s_reason, strlen(s_reason)));
67 }
68 }
69
70 METHOD(imv_reason_string_t, get_encoding, chunk_t,
71 private_imv_reason_string_t *this)
72 {
73 return this->reasons;
74 }
75
76 METHOD(imv_reason_string_t, destroy, void,
77 private_imv_reason_string_t *this)
78 {
79 free(this->reasons.ptr);
80 free(this);
81 }
82
83 /**
84 * Described in header.
85 */
86 imv_reason_string_t *imv_reason_string_create(char *lang, char *separator)
87 {
88 private_imv_reason_string_t *this;
89
90 INIT(this,
91 .public = {
92 .add_reason = _add_reason,
93 .get_encoding = _get_encoding,
94 .destroy = _destroy,
95 },
96 .lang = lang,
97 .separator = separator,
98 );
99
100 return &this->public;
101 }
102