• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /**************************************************************************
2   *
3   * Copyright 2008 VMware, Inc.
4   * All Rights Reserved.
5   *
6   * Permission is hereby granted, free of charge, to any person obtaining a
7   * copy of this software and associated documentation files (the
8   * "Software"), to deal in the Software without restriction, including
9   * without limitation the rights to use, copy, modify, merge, publish,
10   * distribute, sub license, and/or sell copies of the Software, and to
11   * permit persons to whom the Software is furnished to do so, subject to
12   * the following conditions:
13   *
14   * The above copyright notice and this permission notice (including the
15   * next paragraph) shall be included in all copies or substantial portions
16   * of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19   * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21   * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22   * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25   *
26   **************************************************************************/
27  
28  /**
29   * @file
30   * Platform independent functions for string manipulation.
31   *
32   * @author Jose Fonseca <jfonseca@vmware.com>
33   */
34  
35  #ifndef U_STRING_H_
36  #define U_STRING_H_
37  
38  #if !defined(XF86_LIBC_H)
39  #include <stdio.h>
40  #endif
41  #include <stdlib.h>
42  #include <stddef.h>
43  #include <stdarg.h>
44  #include <string.h>
45  #include <limits.h>
46  
47  #include "util/macros.h" // PRINTFLIKE
48  
49  
50  #ifdef __cplusplus
51  extern "C" {
52  #endif
53  
54  #if !defined(_GNU_SOURCE) || defined(__APPLE__)
55  
56  #define strchrnul util_strchrnul
57  static inline char *
util_strchrnul(const char * s,char c)58  util_strchrnul(const char *s, char c)
59  {
60     for (; *s && *s != c; ++s);
61  
62     return (char *)s;
63  }
64  
65  #endif
66  
67  #ifdef _WIN32
68  
69  #define sprintf util_sprintf
70  static inline void
71     PRINTFLIKE(2, 3)
util_sprintf(char * str,const char * format,...)72  util_sprintf(char *str, const char *format, ...)
73  {
74     va_list ap;
75     va_start(ap, format);
76     vsnprintf(str, INT_MAX, format, ap);
77     va_end(ap);
78  }
79  
80  #define vasprintf util_vasprintf
81  static inline int
util_vasprintf(char ** ret,const char * format,va_list ap)82  util_vasprintf(char **ret, const char *format, va_list ap)
83  {
84     va_list ap_copy;
85  
86     /* Compute length of output string first */
87     va_copy(ap_copy, ap);
88     int r = vsnprintf(NULL, 0, format, ap_copy);
89     va_end(ap_copy);
90  
91     if (r < 0)
92        return -1;
93  
94     *ret = (char *) malloc(r + 1);
95     if (!*ret)
96        return -1;
97  
98     /* Print to buffer */
99     return vsnprintf(*ret, r + 1, format, ap);
100  }
101  
102  #define asprintf util_asprintf
103  static inline int
util_asprintf(char ** str,const char * fmt,...)104  util_asprintf(char **str, const char *fmt, ...)
105  {
106     int ret;
107     va_list args;
108     va_start(args, fmt);
109     ret = vasprintf(str, fmt, args);
110     va_end(args);
111     return ret;
112  }
113  
114  #ifndef strcasecmp
115  #define strcasecmp stricmp
116  #endif
117  
118  #define strdup _strdup
119  
120  #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
121  #define strtok_r strtok_s
122  #endif
123  
124  #endif
125  
126  
127  #ifdef __cplusplus
128  }
129  #endif
130  
131  #endif /* U_STRING_H_ */
132