From 06b66716cf5e11bb9e046c66092f926a6fabe9ee Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Sat, 13 Aug 2011 07:59:20 +1000 Subject: [PATCH] Add the function chbufdup() to duplicate a chtype array --- src/intf.c | 26 ++++++++++++++++++++++++++ src/intf.h | 14 ++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/intf.c b/src/intf.c index 8dab9fc..238c756 100644 --- a/src/intf.c +++ b/src/intf.c @@ -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 diff --git a/src/intf.h b/src/intf.h index 1809355..8d594d0 100644 --- a/src/intf.h +++ b/src/intf.h @@ -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)