1*8af74909SZhong Yang // Scintilla source code edit control 2*8af74909SZhong Yang /** @file StringCopy.h 3*8af74909SZhong Yang ** Safe string copy function which always NUL terminates. 4*8af74909SZhong Yang ** ELEMENTS macro for determining array sizes. 5*8af74909SZhong Yang **/ 6*8af74909SZhong Yang // Copyright 2013 by Neil Hodgson <[email protected]> 7*8af74909SZhong Yang // The License.txt file describes the conditions under which this software may be distributed. 8*8af74909SZhong Yang 9*8af74909SZhong Yang #ifndef STRINGCOPY_H 10*8af74909SZhong Yang #define STRINGCOPY_H 11*8af74909SZhong Yang 12*8af74909SZhong Yang namespace Scintilla { 13*8af74909SZhong Yang 14*8af74909SZhong Yang // Safer version of string copy functions like strcpy, wcsncpy, etc. 15*8af74909SZhong Yang // Instantiate over fixed length strings of both char and wchar_t. 16*8af74909SZhong Yang // May truncate if source doesn't fit into dest with room for NUL. 17*8af74909SZhong Yang 18*8af74909SZhong Yang template <typename T, size_t count> StringCopy(T (& dest)[count],const T * source)19*8af74909SZhong Yangvoid StringCopy(T (&dest)[count], const T* source) { 20*8af74909SZhong Yang for (size_t i=0; i<count; i++) { 21*8af74909SZhong Yang dest[i] = source[i]; 22*8af74909SZhong Yang if (!source[i]) 23*8af74909SZhong Yang break; 24*8af74909SZhong Yang } 25*8af74909SZhong Yang dest[count-1] = 0; 26*8af74909SZhong Yang } 27*8af74909SZhong Yang 28*8af74909SZhong Yang #define ELEMENTS(a) (sizeof(a) / sizeof(a[0])) 29*8af74909SZhong Yang 30*8af74909SZhong Yang } 31*8af74909SZhong Yang 32*8af74909SZhong Yang #endif 33