/* vifm * Copyright (C) 2001 Ken Steen. * Copyright (C) 2011 xaizek. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include "str.h" #include <stdarg.h> /* va_list va_start() va_end() */ #include <stddef.h> /* NULL size_t wchar_t */ #include <string.h> /* strncmp() strlen() strchr() */ #include <wchar.h> /* vswprintf() */ #include "../compat/reallocarray.h" static size_t copy_substr(char dst[], size_t dst_len, const char src[], char terminator); wchar_t * vifm_wcsdup(const wchar_t ws[]) { const size_t len = wcslen(ws) + 1; wchar_t * const result = reallocarray(NULL, len, sizeof(wchar_t)); if(result == NULL) { return NULL; } wcsncpy(result, ws, len); return result; } int starts_with(const char str[], const char prefix[]) { const size_t prefix_len = strlen(prefix); return starts_withn(str, prefix, prefix_len); } int starts_withn(const char str[], const char prefix[], size_t prefix_len) { return strncmp(str, prefix, prefix_len) == 0; } int vifm_swprintf(wchar_t str[], size_t len, const wchar_t format[], ...) { int result; va_list ap; va_start(ap, format); #ifdef BROKEN_SWPRINTF result = vswprintf(str, format, ap); #else result = vswprintf(str, len, format, ap); #endif va_end(ap); return result; } size_t copy_str(char dst[], size_t dst_len, const char src[]) { /* XXX: shouldn't we return "strlen(src)" instead of "0U"? */ return (dst == src) ? 0U : copy_substr(dst, dst_len, src, '\0'); } /* Copies characters from the string pointed to by src and terminated by the * terminator to piece of memory of size dst_len pointed to by dst. Ensures * that copied string ends with null character. Does nothing for zero * dst_len. Returns number of characters written, including terminating null * character. */ static size_t copy_substr(char dst[], size_t dst_len, const char src[], char terminator) { char *past_end; if(dst_len == 0U) { return 0U; } past_end = memccpy(dst, src, terminator, dst_len); if(past_end == NULL) { dst[dst_len - 1] = '\0'; return dst_len; } else { past_end[-1] = '\0'; return past_end - dst; } } int char_is_one_of(const char *list, char c) { return c != '\0' && strchr(list, c) != NULL; } void unescape(char s[], int regexp) { char *p; p = s; while(s[0] != '\0') { if(s[0] == '\\' && (!regexp || s[1] == '/')) { ++s; } *p++ = s[0]; if(s[0] != '\0') { ++s; } } *p = '\0'; } /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ /* vim: set cinoptions+=t0 filetype=c : */