101e04c3fSmrg/************************************************************************** 201e04c3fSmrg * 301e04c3fSmrg * Copyright 2008 VMware, Inc. 401e04c3fSmrg * All Rights Reserved. 501e04c3fSmrg * 601e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 701e04c3fSmrg * copy of this software and associated documentation files (the 801e04c3fSmrg * "Software"), to deal in the Software without restriction, including 901e04c3fSmrg * without limitation the rights to use, copy, modify, merge, publish, 1001e04c3fSmrg * distribute, sub license, and/or sell copies of the Software, and to 1101e04c3fSmrg * permit persons to whom the Software is furnished to do so, subject to 1201e04c3fSmrg * the following conditions: 1301e04c3fSmrg * 1401e04c3fSmrg * The above copyright notice and this permission notice (including the 1501e04c3fSmrg * next paragraph) shall be included in all copies or substantial portions 1601e04c3fSmrg * of the Software. 1701e04c3fSmrg * 1801e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1901e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2001e04c3fSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 2101e04c3fSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 2201e04c3fSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 2301e04c3fSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 2401e04c3fSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2501e04c3fSmrg * 2601e04c3fSmrg **************************************************************************/ 2701e04c3fSmrg 2801e04c3fSmrg/** 2901e04c3fSmrg * @file 3001e04c3fSmrg * Platform independent functions for string manipulation. 3101e04c3fSmrg * 3201e04c3fSmrg * @author Jose Fonseca <jfonseca@vmware.com> 3301e04c3fSmrg */ 3401e04c3fSmrg 3501e04c3fSmrg#ifndef U_STRING_H_ 3601e04c3fSmrg#define U_STRING_H_ 3701e04c3fSmrg 3801e04c3fSmrg#if !defined(XF86_LIBC_H) 3901e04c3fSmrg#include <stdio.h> 4001e04c3fSmrg#endif 4101e04c3fSmrg#include <stdlib.h> 4201e04c3fSmrg#include <stddef.h> 4301e04c3fSmrg#include <stdarg.h> 4401e04c3fSmrg#include <string.h> 457ec681f3Smrg#include <limits.h> 4601e04c3fSmrg 4701e04c3fSmrg#include "util/macros.h" // PRINTFLIKE 4801e04c3fSmrg 4901e04c3fSmrg 5001e04c3fSmrg#ifdef __cplusplus 5101e04c3fSmrgextern "C" { 5201e04c3fSmrg#endif 5301e04c3fSmrg 547ec681f3Smrg#if !defined(_GNU_SOURCE) || defined(__APPLE__) 5501e04c3fSmrg 567ec681f3Smrg#define strchrnul util_strchrnul 5701e04c3fSmrgstatic inline char * 5801e04c3fSmrgutil_strchrnul(const char *s, char c) 5901e04c3fSmrg{ 6001e04c3fSmrg for (; *s && *s != c; ++s); 6101e04c3fSmrg 6201e04c3fSmrg return (char *)s; 6301e04c3fSmrg} 6401e04c3fSmrg 6501e04c3fSmrg#endif 6601e04c3fSmrg 6701e04c3fSmrg#ifdef _WIN32 6801e04c3fSmrg 697ec681f3Smrg#define sprintf util_sprintf 7001e04c3fSmrgstatic inline void 7101e04c3fSmrg PRINTFLIKE(2, 3) 7201e04c3fSmrgutil_sprintf(char *str, const char *format, ...) 7301e04c3fSmrg{ 7401e04c3fSmrg va_list ap; 7501e04c3fSmrg va_start(ap, format); 767ec681f3Smrg vsnprintf(str, INT_MAX, format, ap); 7701e04c3fSmrg va_end(ap); 7801e04c3fSmrg} 7901e04c3fSmrg 807ec681f3Smrg#define vasprintf util_vasprintf 8101e04c3fSmrgstatic inline int 8201e04c3fSmrgutil_vasprintf(char **ret, const char *format, va_list ap) 8301e04c3fSmrg{ 8401e04c3fSmrg va_list ap_copy; 8501e04c3fSmrg 8601e04c3fSmrg /* Compute length of output string first */ 8701e04c3fSmrg va_copy(ap_copy, ap); 887ec681f3Smrg int r = vsnprintf(NULL, 0, format, ap_copy); 8901e04c3fSmrg va_end(ap_copy); 9001e04c3fSmrg 9101e04c3fSmrg if (r < 0) 9201e04c3fSmrg return -1; 9301e04c3fSmrg 9401e04c3fSmrg *ret = (char *) malloc(r + 1); 9501e04c3fSmrg if (!*ret) 9601e04c3fSmrg return -1; 9701e04c3fSmrg 9801e04c3fSmrg /* Print to buffer */ 997ec681f3Smrg return vsnprintf(*ret, r + 1, format, ap); 10001e04c3fSmrg} 10101e04c3fSmrg 1027ec681f3Smrg#define asprintf util_asprintf 10301e04c3fSmrgstatic inline int 1047ec681f3Smrgutil_asprintf(char **str, const char *fmt, ...) 10501e04c3fSmrg{ 1067ec681f3Smrg int ret; 1077ec681f3Smrg va_list args; 1087ec681f3Smrg va_start(args, fmt); 1097ec681f3Smrg ret = vasprintf(str, fmt, args); 1107ec681f3Smrg va_end(args); 1117ec681f3Smrg return ret; 11201e04c3fSmrg} 11301e04c3fSmrg 1147ec681f3Smrg#ifndef strcasecmp 1157ec681f3Smrg#define strcasecmp stricmp 1167ec681f3Smrg#endif 11701e04c3fSmrg 1187ec681f3Smrg#define strdup _strdup 11901e04c3fSmrg 1207ec681f3Smrg#if defined(_WIN32) && !defined(HAVE_STRTOK_R) 1217ec681f3Smrg#define strtok_r strtok_s 1227ec681f3Smrg#endif 12301e04c3fSmrg 12401e04c3fSmrg#endif 12501e04c3fSmrg 12601e04c3fSmrg 12701e04c3fSmrg#ifdef __cplusplus 12801e04c3fSmrg} 12901e04c3fSmrg#endif 13001e04c3fSmrg 13101e04c3fSmrg#endif /* U_STRING_H_ */ 132