1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-10-06 17:54:03 -04:00

Add the function chbufdup() to duplicate a chtype array

This commit is contained in:
John Zaitseff 2011-08-13 07:59:20 +10:00
parent 7d9bf097b9
commit 06b66716cf
2 changed files with 40 additions and 0 deletions

View File

@ -1235,6 +1235,32 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
}
/***********************************************************************/
// chbufdup: Duplicate a chtype buffer
chtype *chbufdup (const chtype *restrict chbuf, int chbufsize)
{
const chtype *p;
int len;
chtype *ret;
// Determine chbuf length, including ending NUL
for (len = 1, p = chbuf; *p != '\0' && len <= chbufsize; p++, len++)
;
ret = malloc(len * sizeof(chtype));
if (ret == NULL) {
err_exit_nomem();
}
memcpy(ret, chbuf, len * sizeof(chtype));
ret[len - 1] = '\0'; // Terminating NUL, just in case not present
return ret;
}
/***********************************************************************/
// pr_left: Print strings in chbuf left-aligned

View File

@ -385,6 +385,20 @@ extern int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
const char *restrict format, va_list args);
/*
Function: chbufdup - Duplicate a chtype buffer
Parameters: chbuf - Buffer to duplicate
chbufsize - Number of chtype elements in chbuf
Returns: chtype * - Pointer to new duplicated buffer
This function returns a new buffer of type chtype * that contains a
copy of the string in chbuf. No errors are returned: if sufficient
memory is not available, the program terminates with an "Out of memory"
message.
*/
extern chtype *chbufdup (const chtype *restrict chbuf, int chbufsize);
/*
Function: pr_left - Print strings in chbuf left-aligned
Parameters: win - Window to use (should be curwin)