blob: 03f59c24e9adb7346f27bc614d105b381dd8c458 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2023 Thomas E. Dickey *
3 * Copyright 2000-2012,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
33
34/*
35** lib_mvcur.c
36**/
37
38#include <curses.priv.h>
micky3879b9f5e72025-07-08 18:04:53 -040039#include <tic.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053040
micky3879b9f5e72025-07-08 18:04:53 -040041MODULE_ID("$Id: strings.c,v 1.11 2023/05/27 20:13:10 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042
43/****************************************************************************
44 * Useful string functions (especially for mvcur)
45 ****************************************************************************/
46
47#if !HAVE_STRSTR
48NCURSES_EXPORT(char *)
49_nc_strstr(const char *haystack, const char *needle)
50{
51 size_t len1 = strlen(haystack);
52 size_t len2 = strlen(needle);
53 char *result = 0;
54
55 while ((len1 != 0) && (len1-- >= len2)) {
56 if (!strncmp(haystack, needle, len2)) {
57 result = (char *) haystack;
58 break;
59 }
60 haystack++;
61 }
62 return result;
63}
64#endif
65
66/*
67 * Initialize the descriptor so we can append to it. Note that 'src' may
68 * be a null pointer (see _nc_str_null), so the corresponding strcat and
69 * strcpy calls have to allow for this.
70 */
71NCURSES_EXPORT(string_desc *)
72_nc_str_init(string_desc * dst, char *src, size_t len)
73{
74 if (dst != 0) {
75 dst->s_head = src;
76 dst->s_tail = src;
77 dst->s_size = len - 1;
78 dst->s_init = dst->s_size;
79 if (src != 0)
80 *src = 0;
81 }
82 return dst;
83}
84
85/*
86 * Initialize the descriptor for only tracking the amount of memory used.
87 */
88NCURSES_EXPORT(string_desc *)
89_nc_str_null(string_desc * dst, size_t len)
90{
91 return _nc_str_init(dst, 0, len);
92}
93
94/*
95 * Copy a descriptor
96 */
97NCURSES_EXPORT(string_desc *)
micky3879b9f5e72025-07-08 18:04:53 -040098_nc_str_copy(string_desc * dst, const string_desc * const src)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053099{
100 *dst = *src;
101 return dst;
102}
103
104/*
105 * Replaces strcat into a fixed buffer, returning false on failure.
106 */
107NCURSES_EXPORT(bool)
108_nc_safe_strcat(string_desc * dst, const char *src)
109{
micky3879b9f5e72025-07-08 18:04:53 -0400110 if (PRESENT(src)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111 size_t len = strlen(src);
112
113 if (len < dst->s_size) {
114 if (dst->s_tail != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100115 _nc_STRCPY(dst->s_tail, src, dst->s_size);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530116 dst->s_tail += len;
117 }
118 dst->s_size -= len;
119 return TRUE;
120 }
121 }
122 return FALSE;
123}
124
125/*
126 * Replaces strcpy into a fixed buffer, returning false on failure.
127 */
128NCURSES_EXPORT(bool)
129_nc_safe_strcpy(string_desc * dst, const char *src)
130{
micky3879b9f5e72025-07-08 18:04:53 -0400131 if (PRESENT(src)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132 size_t len = strlen(src);
133
134 if (len < dst->s_size) {
135 if (dst->s_head != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100136 _nc_STRCPY(dst->s_head, src, dst->s_size);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530137 dst->s_tail = dst->s_head + len;
138 }
139 dst->s_size = dst->s_init - len;
140 return TRUE;
141 }
142 }
143 return FALSE;
144}