1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

lib/util: Move next_token* functions to util_str.c

This is part of an attempt to untangle charset from util.c.

Most of this was moved from util_str.c in commit
55903e6f91.  next_token() was moved from
charset to util.c in commit 4be643d4ce
so that it would be alongside next_token_talloc().

Now that util_str.c is in shared lib/util it is no longer necessary to
have these in util.c.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Volker Lendecke <vl@samba.org>
This commit is contained in:
Martin Schwenke 2014-09-19 13:30:26 +10:00 committed by Volker Lendecke
parent 1bf36f177b
commit f3d50e5ff5
2 changed files with 145 additions and 146 deletions

View File

@ -1043,152 +1043,6 @@ _PUBLIC_ size_t utf16_len_n(const void *src, size_t n)
return len;
}
/**
* @file
* @brief String utilities.
**/
static bool next_token_internal_talloc(TALLOC_CTX *ctx,
const char **ptr,
char **pp_buff,
const char *sep,
bool ltrim)
{
const char *s;
const char *saved_s;
char *pbuf;
bool quoted;
size_t len=1;
*pp_buff = NULL;
if (!ptr) {
return(false);
}
s = *ptr;
/* default to simple separators */
if (!sep) {
sep = " \t\n\r";
}
/* find the first non sep char, if left-trimming is requested */
if (ltrim) {
while (*s && strchr_m(sep,*s)) {
s++;
}
}
/* nothing left? */
if (!*s) {
return false;
}
/* When restarting we need to go from here. */
saved_s = s;
/* Work out the length needed. */
for (quoted = false; *s &&
(quoted || !strchr_m(sep,*s)); s++) {
if (*s == '\"') {
quoted = !quoted;
} else {
len++;
}
}
/* We started with len = 1 so we have space for the nul. */
*pp_buff = talloc_array(ctx, char, len);
if (!*pp_buff) {
return false;
}
/* copy over the token */
pbuf = *pp_buff;
s = saved_s;
for (quoted = false; *s &&
(quoted || !strchr_m(sep,*s)); s++) {
if ( *s == '\"' ) {
quoted = !quoted;
} else {
*pbuf++ = *s;
}
}
*ptr = (*s) ? s+1 : s;
*pbuf = 0;
return true;
}
bool next_token_talloc(TALLOC_CTX *ctx,
const char **ptr,
char **pp_buff,
const char *sep)
{
return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
}
/*
* Get the next token from a string, return false if none found. Handles
* double-quotes. This version does not trim leading separator characters
* before looking for a token.
*/
bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
const char **ptr,
char **pp_buff,
const char *sep)
{
return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
}
/**
* Get the next token from a string, return False if none found.
* Handles double-quotes.
*
* Based on a routine by GJC@VILLAGE.COM.
* Extensively modified by Andrew.Tridgell@anu.edu.au
**/
_PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
{
const char *s;
bool quoted;
size_t len=1;
if (!ptr)
return false;
s = *ptr;
/* default to simple separators */
if (!sep)
sep = " \t\n\r";
/* find the first non sep char */
while (*s && strchr_m(sep,*s))
s++;
/* nothing left? */
if (!*s)
return false;
/* copy over the token */
for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
if (*s == '\"') {
quoted = !quoted;
} else {
len++;
*buff++ = *s;
}
}
*ptr = (*s) ? s+1 : s;
*buff = 0;
return true;
}
struct anonymous_shared_header {
union {
size_t length;

View File

@ -133,3 +133,148 @@ _PUBLIC_ bool strequal(const char *s1, const char *s2)
return strcasecmp_m(s1,s2) == 0;
}
/**
* @file
* @brief String utilities.
**/
static bool next_token_internal_talloc(TALLOC_CTX *ctx,
const char **ptr,
char **pp_buff,
const char *sep,
bool ltrim)
{
const char *s;
const char *saved_s;
char *pbuf;
bool quoted;
size_t len=1;
*pp_buff = NULL;
if (!ptr) {
return(false);
}
s = *ptr;
/* default to simple separators */
if (!sep) {
sep = " \t\n\r";
}
/* find the first non sep char, if left-trimming is requested */
if (ltrim) {
while (*s && strchr_m(sep,*s)) {
s++;
}
}
/* nothing left? */
if (!*s) {
return false;
}
/* When restarting we need to go from here. */
saved_s = s;
/* Work out the length needed. */
for (quoted = false; *s &&
(quoted || !strchr_m(sep,*s)); s++) {
if (*s == '\"') {
quoted = !quoted;
} else {
len++;
}
}
/* We started with len = 1 so we have space for the nul. */
*pp_buff = talloc_array(ctx, char, len);
if (!*pp_buff) {
return false;
}
/* copy over the token */
pbuf = *pp_buff;
s = saved_s;
for (quoted = false; *s &&
(quoted || !strchr_m(sep,*s)); s++) {
if ( *s == '\"' ) {
quoted = !quoted;
} else {
*pbuf++ = *s;
}
}
*ptr = (*s) ? s+1 : s;
*pbuf = 0;
return true;
}
bool next_token_talloc(TALLOC_CTX *ctx,
const char **ptr,
char **pp_buff,
const char *sep)
{
return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
}
/*
* Get the next token from a string, return false if none found. Handles
* double-quotes. This version does not trim leading separator characters
* before looking for a token.
*/
bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
const char **ptr,
char **pp_buff,
const char *sep)
{
return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
}
/**
* Get the next token from a string, return False if none found.
* Handles double-quotes.
*
* Based on a routine by GJC@VILLAGE.COM.
* Extensively modified by Andrew.Tridgell@anu.edu.au
**/
_PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
{
const char *s;
bool quoted;
size_t len=1;
if (!ptr)
return false;
s = *ptr;
/* default to simple separators */
if (!sep)
sep = " \t\n\r";
/* find the first non sep char */
while (*s && strchr_m(sep,*s))
s++;
/* nothing left? */
if (!*s)
return false;
/* copy over the token */
for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
if (*s == '\"') {
quoted = !quoted;
} else {
len++;
*buff++ = *s;
}
}
*ptr = (*s) ? s+1 : s;
*buff = 0;
return true;
}