53 topo_hdl_strfree(topo_hdl_t *thp, char *s)
54 {
55 if (s != NULL)
56 topo_hdl_free(thp, s, strlen(s) + 1);
57 }
58
59 char *
60 topo_hdl_strsplit(topo_hdl_t *hdl, const char *input, const char *sep,
61 char **lastp)
62 {
63 size_t seplen = strlen(sep);
64 const char *scanstart;
65 char *token;
66 char *ret;
67
68 if (input != NULL) {
69 /*
70 * Start scanning at beginning of input:
71 */
72 scanstart = input;
73 } else {
74 /*
75 * If we have already finished scanning, return NULL.
76 */
77 if (*lastp == NULL)
78 return (NULL);
79
80 /*
81 * Otherwise, start scanning where we left off:
82 */
83 scanstart = *lastp;
84 }
85
86 token = strstr(scanstart, sep);
87 if (token != NULL) {
88 /*
89 * We still have a separator, so advance the next-start
90 * pointer past it:
91 */
92 *lastp = token + seplen;
93 /*
94 * Copy out this element:
95 */
96 ret = topo_hdl_alloc(hdl, token - scanstart + 1);
97 (void) strncpy(ret, scanstart, token - scanstart);
98 ret[token - scanstart] = '\0';
99 } else {
100 /*
101 * We have no separator, so this is the last element:
102 */
103 *lastp = NULL;
104 ret = topo_hdl_strdup(hdl, scanstart);
105 }
106
107 return (ret);
108 }
109
110 char *
111 topo_mod_strdup(topo_mod_t *mod, const char *s)
112 {
113 return (topo_hdl_strdup(mod->tm_hdl, s));
114 }
|
53 topo_hdl_strfree(topo_hdl_t *thp, char *s)
54 {
55 if (s != NULL)
56 topo_hdl_free(thp, s, strlen(s) + 1);
57 }
58
59 char *
60 topo_hdl_strsplit(topo_hdl_t *hdl, const char *input, const char *sep,
61 char **lastp)
62 {
63 size_t seplen = strlen(sep);
64 const char *scanstart;
65 char *token;
66 char *ret;
67
68 if (input != NULL) {
69 /*
70 * Start scanning at beginning of input:
71 */
72 scanstart = input;
73 } else if (*lastp == NULL) {
74 /*
75 * If we have already finished scanning, return NULL.
76 */
77 return (NULL);
78 } else {
79 /*
80 * Otherwise, start scanning where we left off:
81 */
82 scanstart = *lastp;
83 }
84
85 token = strstr(scanstart, sep);
86 if (token != NULL) {
87 /*
88 * We still have a separator, so advance the next-start
89 * pointer past it:
90 */
91 *lastp = token + seplen;
92 /*
93 * Copy out this element. The buffer must fit the string
94 * exactly, so that topo_hdl_strfree() can determine its
95 * size with strlen().
96 */
97 ret = topo_hdl_alloc(hdl, token - scanstart + 1);
98 (void) strncpy(ret, scanstart, token - scanstart);
99 ret[token - scanstart] = '\0';
100 } else {
101 /*
102 * We have no separator, so this is the last element:
103 */
104 *lastp = NULL;
105 ret = topo_hdl_strdup(hdl, scanstart);
106 }
107
108 return (ret);
109 }
110
111 char *
112 topo_mod_strdup(topo_mod_t *mod, const char *s)
113 {
114 return (topo_hdl_strdup(mod->tm_hdl, s));
115 }
|