libk  scp.fn.c at tip

File mod/kstr/scp.fn.c from the latest check-in


#include <k/core.h>
#include <k/str.h>

kscond kscp(ksraw str, ksmut dest, sz* len) {
	if (str.size == 0) { /* calculate length on the fly */
		for(; str.ptr[str.size] != 0; ++ str.size) {
			if (str.size >= dest.size) return kscond_no_room;

			dest.ptr[str.size] = str.ptr[str.size];
		}
	} else {
		if (dest.size < str.size) return kscond_no_room;

		if (dest.ptr == null || str.ptr == null) return kscond_null;

		for(sz idx = 0; idx < str.size; ++ idx) {
			dest.ptr[idx] = str.ptr[idx];
		}
	}

	/* did we copy a nul? */
	if (str.ptr[str.size - 1] != 0)
		/* we didn't, can we? */
		if (dest.size > str.size)
			/* set a final nul */
			dest.ptr[str.size] = 0;

	if (len != null) *len = str.size;
	return kscond_ok;
}