Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with 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 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | #include "bcmolt_string.h" |
| 31 | #include "bcmolt_math.h" |
| 32 | |
| 33 | struct bcmolt_string |
| 34 | { |
| 35 | char *str; |
| 36 | uint32_t max_len; |
| 37 | char *curr; |
| 38 | int32_t remaining; |
| 39 | }; |
| 40 | |
| 41 | int 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 | |
| 51 | int 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 | |
| 72 | const char *bcmolt_string_get(bcmolt_string *str) |
| 73 | { |
| 74 | return str->str; |
| 75 | } |
| 76 | |
| 77 | void 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 | |
| 84 | bcmos_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 | |
| 98 | void bcmolt_string_destroy(bcmolt_string *str) |
| 99 | { |
| 100 | bcmos_free(str); |
| 101 | } |
| 102 | |