blob: a10fa206f762f05bf0811299b1127ab45c31a53b [file] [log] [blame]
Shad Ansari2f7f9be2017-06-07 13:34:53 -07001/*
2<:copyright-BRCM:2016:DUAL/GPL:standard
3
4 Broadcom Proprietary and Confidential.(c) 2016 Broadcom
5 All Rights Reserved
6
7Unless you and Broadcom execute a separate written software license
8agreement governing use of this software, this software is licensed
9to you under the terms of the GNU General Public License version 2
10(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
11with the following added to such license:
12
13 As a special exception, the copyright holders of this software give
14 you permission to link this software with independent modules, and
15 to copy and distribute the resulting executable under terms of your
16 choice, provided that you also meet, for each linked independent
17 module, the terms and conditions of the license of that module.
18 An independent module is a module which is not derived from this
19 software. The special exception does not apply to any modifications
20 of the software.
21
22Not withstanding the above, under no circumstances may you combine
23this software in any way with any other Broadcom software provided
24under a license other than the GPL, without Broadcom's express prior
25written consent.
26
27:>
28 */
29
30#include "bcmolt_string.h"
31#include "bcmolt_math.h"
32
33struct bcmolt_string
34{
35 char *str;
36 uint32_t max_len;
37 char *curr;
38 int32_t remaining;
39};
40
41int bcmolt_string_copy(bcmolt_string *str, const char *buf, uint32_t size)
42{
43 int to_copy = MIN(size, str->remaining);
44 memcpy(str->curr, buf, to_copy);
45 str->remaining -= to_copy;
46 str->curr += to_copy;
47 str->curr[0] = '\0';
48 return to_copy;
49}
50
51int bcmolt_string_append(bcmolt_string *str, const char *fmt, ...)
52{
53 int n;
54 va_list args;
55
56 va_start(args, fmt);
57 n = vsnprintf(str->curr, str->remaining, fmt, args);
58 va_end(args);
59 if (n > 0)
60 {
61 if (n > str->remaining)
62 {
63 n = str->remaining;
64 }
65 str->remaining -= n;
66 str->curr += n;
67 }
68
69 return n;
70}
71
72const char *bcmolt_string_get(bcmolt_string *str)
73{
74 return str->str;
75}
76
77void bcmolt_string_reset(bcmolt_string *str)
78{
79 str->str[0] = '\0';
80 str->curr = str->str;
81 str->remaining = str->max_len;
82}
83
84bcmos_errno bcmolt_string_create(bcmolt_string **str, uint32_t max_len)
85{
86 *str = bcmos_calloc(sizeof(bcmolt_string) + max_len + 1);
87 if (*str != NULL)
88 {
89 (*str)->str = (char*)((*str) + 1);
90 (*str)->max_len = max_len;
91 bcmolt_string_reset(*str);
92 return BCM_ERR_OK;
93 }
94
95 return BCM_ERR_NOMEM;
96}
97
98void bcmolt_string_destroy(bcmolt_string *str)
99{
100 bcmos_free(str);
101}
102