/* * Copyright (c) 2018-2020, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include extern "C" { size_t wcslen(const wchar_t* str) { size_t len = 0; while (*(str++)) ++len; return len; } wchar_t* wcscpy(wchar_t* dest, const wchar_t* src) { wchar_t* originalDest = dest; while ((*dest++ = *src++) != '\0') ; return originalDest; } wchar_t* wcsncpy(wchar_t* dest, const wchar_t* src, size_t num) { wchar_t* originalDest = dest; while (((*dest++ = *src++) != '\0') && ((size_t)(dest - originalDest) < num)) ; return originalDest; } int wcscmp(const wchar_t* s1, const wchar_t* s2) { while (*s1 == *s2++) if (*s1++ == 0) return 0; return *(const wchar_t*)s1 - *(const wchar_t*)--s2; } wchar_t* wcschr(const wchar_t* str, int c) { wchar_t ch = c; for (;; ++str) { if (*str == ch) return const_cast(str); if (!*str) return nullptr; } } const wchar_t* wcsrchr(const wchar_t* str, wchar_t wc) { wchar_t* last = nullptr; wchar_t c; for (; (c = *str); ++str) { if (c == wc) last = const_cast(str); } return last; } wchar_t* wcscat(wchar_t* dest, const wchar_t* src) { size_t dest_length = wcslen(dest); size_t i; for (i = 0; src[i] != '\0'; i++) dest[dest_length + i] = src[i]; dest[dest_length + i] = '\0'; return dest; } wchar_t* wcstok(wchar_t* str, const wchar_t* delim, wchar_t** ptr) { wchar_t* used_str = str; if (!used_str) { used_str = *ptr; } size_t token_start = 0; size_t token_end = 0; size_t str_len = wcslen(used_str); size_t delim_len = wcslen(delim); for (size_t i = 0; i < str_len; ++i) { bool is_proper_delim = false; for (size_t j = 0; j < delim_len; ++j) { if (used_str[i] == delim[j]) { // Skip beginning delimiters if (token_end - token_start == 0) { ++token_start; break; } is_proper_delim = true; } } ++token_end; if (is_proper_delim && token_end > 0) { --token_end; break; } } if (used_str[token_start] == '\0') return nullptr; if (token_end == 0) { return &used_str[token_start]; } used_str[token_end] = '\0'; return &used_str[token_start]; } wchar_t* wcsncat(wchar_t* dest, const wchar_t* src, size_t n) { size_t dest_length = wcslen(dest); size_t i; for (i = 0; i < n && src[i] != '\0'; i++) dest[dest_length + i] = src[i]; dest[dest_length + i] = '\0'; return dest; } }