📄 sflstr.c
字号:
error. Returns NULL if there was insufficient memory to allocate the
descriptor block.
---------------------------------------------------------------------[>]-*/
DESCR *
strt2descr (
char **table)
{
DESCR
*descr; /* Allocated descriptor */
char
*descr_ptr; /* Pointer into block */
size_t
descr_size; /* Size of table */
int
string_nbr; /* Index into string table */
ASSERT (table);
/* Calculate the size of the descriptor */
descr_size = 1; /* Allow for final null byte */
for (string_nbr = 0; table [string_nbr]; string_nbr++)
descr_size += strlen (table [string_nbr]) + 1;
/* Allocate a descriptor and fill it with the strings */
descr = mem_alloc (descr_size + sizeof (DESCR));
if (descr)
{
descr-> size = descr_size;
descr-> data = (byte *) descr + sizeof (DESCR);
descr_ptr = (char *) descr-> data;
for (string_nbr = 0; table [string_nbr]; string_nbr++)
{
size_t descr_len = strlen (table [string_nbr]) + 1;
strncpy (descr_ptr, table [string_nbr], descr_len);
descr_ptr += descr_len;
}
*descr_ptr = '\0'; /* Add a null string */
}
return (descr);
}
/* ---------------------------------------------------------------------[<]-
Function: descr2strt
Synopsis: Takes a descriptor prepared by strt2descr() and returns an
array of strings pointers, terminated in a null pointer. The array is
allocated using the mem_alloc() function. Each string is individually
allocated. Thus, to free the string table you must call mem_free() for
each entry in the table, except the last one, and then for the table.
You can also call strtfree() to destroy the table in a single operation.
Returns NULL if there was insufficient memory to allocate the table of
strings.
---------------------------------------------------------------------[>]-*/
char **
descr2strt (
const DESCR *descr)
{
char
**table;
int
string_count,
string_nbr; /* Index into string table */
char
*descr_ptr; /* Pointer into block */
ASSERT (descr);
/* Count the number of strings in the table */
descr_ptr = (char *) descr-> data;
string_count = 0;
while (*descr_ptr) /* Loop until we hit null string */
{
string_count++;
descr_ptr += strlen (descr_ptr) + 1;
}
/* Allocate a table and fill it with the strings */
table = mem_alloc ((string_count + 1) * sizeof (char *));
if (table)
{
descr_ptr = (char *) descr-> data;
for (string_nbr = 0; string_nbr < string_count; string_nbr++)
{
table [string_nbr] = mem_strdup (descr_ptr);
descr_ptr += strlen (descr_ptr) + 1;
}
table [string_count] = NULL; /* Store final null pointer */
}
return (table);
}
/* ---------------------------------------------------------------------[<]-
Function: strtfree
Synopsis: Releases a table of strings as created by descr2strt() or a
similar function. If the argument is null, does nothing.
---------------------------------------------------------------------[>]-*/
void
strtfree (
char **table)
{
int
string_nbr; /* Index into string array */
if (table)
{
for (string_nbr = 0; table [string_nbr]; string_nbr++)
mem_free (table [string_nbr]);
mem_free (table);
}
}
/* ---------------------------------------------------------------------[<]-
Function: strcntch
Synopsis: Returns number of instances of a character in a string.
---------------------------------------------------------------------[>]-*/
int
strcntch (const char *string, char value)
{
int
count = 0;
ASSERT (string);
while (*string)
if (*string++ == value)
count++;
return (count);
}
/* ---------------------------------------------------------------------[<]-
Function: strlookup
Synopsis: Searches the specified lookup table, defined as an array of
LOOKUP items, for the specified string key, and returns a lookup value.
You are REQUIRED to terminate the table with a null key: if the key is
not found in the table, returns the value for the last, null key.
---------------------------------------------------------------------[>]-*/
int
strlookup (const LOOKUP *lookup, const char *key)
{
int
index;
ASSERT (lookup);
ASSERT (key);
for (index = 0; lookup [index].key; index++)
if (streq (lookup [index].key, key))
break;
return (lookup [index].value);
}
/* ---------------------------------------------------------------------[<]-
Function: strreformat
Synopsis: Reformats a string to fit within lines of the specified width.
Prefixes each line with some optional text (included in the width).
Allocates a fresh block of memory to contain the newly formatted line.
---------------------------------------------------------------------[>]-*/
char *
strreformat (const char *source, size_t width, const char *prefix)
{
size_t
total_size, /* Total size of buffer */
prefix_len, /* Size of prefix string */
token_len; /* Size of current token */
char
**tokens, /* String broken into words */
*token, /* Current token */
*buffer, /* Target multiline buffer */
*bufptr; /* Next position in buffer */
int
cur_width, /* Current line width incl. prefix */
token_nbr; /* Token number, 0..n */
ASSERT (source);
if (source == NULL)
return NULL;
/* Ignore prefix if NULL */
if (prefix == NULL)
prefix = "";
/* Calculate maximum size of resulting buffer, which is difficult to
* predict accurately. We allow for 8 wasted characters on each line
* plus the line ending.
*/
prefix_len = strlen (prefix);
total_size = strlen (source) / (width - prefix_len) + 1;
total_size = total_size * (width + 9);
buffer = mem_alloc (total_size);
tokens = tok_split (source);
ASSERT (strlen (prefix) < width);
ASSERT (total_size > tok_text_size (tokens));
cur_width = 0;
bufptr = buffer;
for (token_nbr = 0; tokens [token_nbr]; token_nbr++)
{
token = tokens [token_nbr];
token_len = strlen (token);
/* First decide if next token will fit on line or not */
if (token_len + cur_width > width)
{
*bufptr++ = '\n'; /* Start new line */
cur_width = 0;
}
/* Put prefix at at start of line if necessary */
if (cur_width == 0)
{
/* If prefix would overflow buffer, we quit */
if ((bufptr - buffer) + prefix_len >= total_size)
break;
memcpy (bufptr, prefix, prefix_len);
bufptr += prefix_len;
cur_width = prefix_len;
}
/* If token would overflow buffer, we quit */
if ((bufptr - buffer) + token_len + 1 >= total_size)
break;
/* Now append token to line and add a space */
memcpy (bufptr, token, token_len);
bufptr += token_len;
cur_width += token_len + 1;
*bufptr++ = ' ';
}
*bufptr = '\0'; /* Terminate the last line */
tok_free (tokens);
return (buffer);
}
/* ---------------------------------------------------------------------[<]-
Function: removechars
Synopsis: Removes known chars from a string. Returns pointer to head of
the buffer. Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
removechars (
char *strbuf,
char *chrstorm)
{
char *offset;
ASSERT (strbuf);
ASSERT (chrstorm);
offset = (char *)NULL;
while (*strbuf)
{
offset = strpbrk (strbuf, chrstorm);
if (offset)
strcpy (offset, (offset + 1)); /* NO OVERRUN */
else
break;
}
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: replacechrswith
Synopsis: Subsitutes known char(s)in a string with another. Returns
pointer to head of the buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
replacechrswith (
char *strbuf,
char *chrstorm,
char chartorlcwith)
{
char *offset;
ASSERT (strbuf);
ASSERT (chrstorm);
offset = (char *)NULL;
while (*strbuf)
{
offset = strpbrk (strbuf, chrstorm);
if (offset)
{
*(offset)= chartorlcwith;
}
else
break;
}
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: insertstring
Synopsis: Inserts a string into another string. Returns a pointer
to head of the buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
insertstring (
char *strbuf,
char *chrstoins,
int pos)
{
ASSERT (strbuf);
ASSERT (chrstoins);
memmove (((strbuf + pos) + strlen (chrstoins)),
(strbuf + pos), (strlen ((strbuf + pos)) + 1));
memcpy ((strbuf + pos), chrstoins, strlen (chrstoins));
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: insertchar
Synopsis: Inserts a char into a string. Returns a pointer to head of
the buffer. Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
insertchar (
char *strbuf,
char chrtoins,
int pos)
{
ASSERT (strbuf);
memmove ((strbuf + pos)+ 1, (strbuf + pos), strlen ((strbuf + pos))+ 1);
*(strbuf + pos)= chrtoins;
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: leftfill
Synopsis: Pads a string to the left, to a know length, with the
given char value. Returns a pointer to head of the buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
leftfill (
char *strbuf,
char chrtofill,
unsigned len)
{
ASSERT (strbuf);
while (strlen (strbuf)< len)
{
insertchar (strbuf, chrtofill, 0);
}
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: rightfill
Synopsis: Pads a string to the right, to a known length, with the
given char value. Returns a pointer to head of the buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
rightfill (
char *strbuf,
char chrtofill,
unsigned len)
{
ASSERT (strbuf);
while (strlen (strbuf)< len)
{
insertchar (strbuf, chrtofill, strlen (strbuf));
}
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: trim
Synopsis: Eats the whitespace's from the left and right side of a
string. This function maintains a proper pointer head. Returns a
pointer to head of the buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
trim (
char *strin)
{
ASSERT (strin);
ltrim (strin);
strcrop (strin);
return strin;
}
/* ---------------------------------------------------------------------[<]-
Function: ltrim
Synopsis: Deletes leading white spaces in string, and returns a
pointer to the first non-blank character. If this is a null, the
end of the string was reached.
---------------------------------------------------------------------[>]-*/
char *
ltrim (
char *string)
{
ASSERT (string);
while (isspace(*string))
deletechar(string,0);
return string;
}
/* ---------------------------------------------------------------------[<]-
Function: searchreplace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -