From 6af21e6a4f2251e71353562d5df7f376fdffc270 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 25 Mar 2025 17:36:52 -0400 Subject: initial checkout from wrc --- lib/padstr.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/padstr.c (limited to 'lib/padstr.c') diff --git a/lib/padstr.c b/lib/padstr.c new file mode 100644 index 0000000..b83229f --- /dev/null +++ b/lib/padstr.c @@ -0,0 +1,61 @@ +/** +** @file padstr.c +** +** @author Numerous CSCI-452 classes +** +** @brief C implementations of common library functions +*/ + +#ifndef PADSTR_SRC_INC +#define PADSTR_SRC_INC + +#include + +#include + +/** +** padstr(dst,str,len,width,leftadjust,padchar - add padding characters +** to a string +** +** @param dst The destination buffer +** @param str The string to be padded +** @param len The string length, or -1 +** @param width The desired final length of the string +** @param leftadjust Should the string be left-justified? +** @param padchar What character to pad with +** +** @return Pointer to the first byte after the padded string +** +** NOTE: does NOT NUL-terminate the buffer +*/ +char *padstr( char *dst, char *str, int len, int width, + int leftadjust, int padchar ) { + int extra; + + // determine the length of the string if we need to + if( len < 0 ){ + len = strlen( str ); + } + + // how much filler must we add? + extra = width - len; + + // add filler on the left if we're not left-justifying + if( extra > 0 && !leftadjust ){ + dst = pad( dst, extra, padchar ); + } + + // copy the string itself + for( int i = 0; i < len; ++i ) { + *dst++ = str[i]; + } + + // add filler on the right if we are left-justifying + if( extra > 0 && leftadjust ){ + dst = pad( dst, extra, padchar ); + } + + return dst; +} + +#endif -- cgit v1.2.3-freya