lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
strings/utility.h
00001 /*****************************************************/
00002 /* lean Strings                 (c) Tobias Zirr 2011 */
00003 /*****************************************************/
00004 
00005 #ifndef LEAN_STRINGS_UTILITY
00006 #define LEAN_STRINGS_UTILITY
00007 
00008 #include "../lean.h"
00009 #include <cstring>
00010 #include <cwchar>
00011 
00012 namespace lean
00013 {
00014 namespace strings
00015 {
00017     inline size_t strmcpy(char *dest, const char *source, size_t maxChars)
00018     {
00019         if (maxChars > 0)
00020         {
00021             size_t len = min(::strlen(source), maxChars - 1);
00022             ::memcpy(dest, source, len);
00023             dest[len] = 0;
00024             return len;
00025         }
00026         else
00027             return 0;
00028     }
00029 
00031     inline size_t wcsmcpy(wchar_t *dest, const wchar_t *source, size_t maxChars)
00032     {
00033         if (maxChars > 0)
00034         {
00035             size_t len = min(::wcslen(source), maxChars - 1);
00036             ::memcpy(dest, source, len * sizeof(wchar_t));
00037             dest[len] = 0;
00038             return len;
00039         }
00040         else
00041             return 0;
00042     }
00043 
00044 } // namespace
00045 
00046 using strings::strmcpy;
00047 using strings::wcsmcpy;
00048 
00049 } // namespace
00050 
00051 #endif