Click here to Skip to main content
15,897,334 members
Articles / Desktop Programming / MFC

Tiny C Runtime Library

Rate me:
Please Sign up or sign in to vote.
4.86/5 (60 votes)
25 Mar 20079 min read 334.9K   5.7K   123  
Reduce code bloat for those simple utility programs by using a streamlined C runtime - now with Unicode support!
// wstring.cpp

// based on:
// LIBCTINY - Matt Pietrek 2001
// MSDN Magazine, January 2001

// 08/12/06 (mv)
// 08/13/06 (mv)

#include <windows.h>
#include <string.h>
#include "libct.h"

BEGIN_EXTERN_C

int _wcsicmp(const wchar_t *s1, const wchar_t *s2) {return wcsicmp(s1, s2);}
int wcsicmp(const wchar_t *s1, const wchar_t *s2)
{
    return lstrcmpiW(s1, s2);
}

int wcscmp(const wchar_t *s1, const wchar_t *s2)
{
	return lstrcmpW(s1, s2);
}

int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
{
	if (!n)
		return 0;

	for (size_t i = 0; i < n; i++)
	{
		if (!s1[i] || s1[i] != s2[i])
			return s1[i] - s2[i];
	}

	return 0;
}

int wcsnicmp(const wchar_t *s1, const wchar_t *s2, size_t n)
{
	return CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
}

wchar_t *wcsdup(const wchar_t *src)
{
	if (!src)
		return 0;

	wchar_t *dst = (wchar_t*)malloc((wcslen(src)+1)*sizeof(wchar_t));
	wcscpy(dst, src);
	return dst;
}

wchar_t *_wcsdup(const wchar_t *src)
{
	return wcsdup(src);
}

wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
{
	return lstrcpyW(dest, src);
}

wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t n)
{
	memcpy(dest, src, n*sizeof(wchar_t));
	size_t len = wcslen(src);
	if (n > len)
		memset(&dest[len], 0, (n-len)*sizeof(wchar_t));
	return dest;
}

size_t wcslen(const wchar_t *str)
{
	return lstrlenW(str);
}

const wchar_t *wcschr(const wchar_t *str, wchar_t ch)
{
	while (*str)
	{
		if (*str == ch)
			return str;
		str++;
	}
	return 0;
}

const wchar_t *wcsrchr(const wchar_t *str, wchar_t ch)
{
	const wchar_t *end = str + wcslen(str) + 1;
	while (end != str)
	{
		end--;
		if (*end == ch)
			return end;
	}
	return 0;
}

wchar_t *wcscat(wchar_t *dst, const wchar_t *src)
{
	return lstrcatW(dst, src);
}

const wchar_t *wcsstr(const wchar_t *str, const wchar_t *substr)
{
	int str_len = wcslen(str);
	int substr_len = wcslen(substr);

	if (substr_len == 0)
		return str;
	if (str_len < substr_len)
		return 0;
	for (int i = 0; i < (int)(str_len-substr_len+1); i++)
	{
		if (!wcscmp(&str[i], substr))
			return (const wchar_t *)(&str[i]);
	}
	return 0;
}

END_EXTERN_C

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Mike_V is currently a student at UCLA.

After a few years on the Dark Side, he reformed and now chants "Death to VB." His computer-related interests include C++, C#, and ASP.NET (in C#, of course). He writes operating systems in C++ and assembler as a hobby.

Comments and Discussions