github.com/ckxng/wakeup@v0.0.0-20190105202853-90356a5f5a15/include/internal/cef_string_types.h (about)

     1  // Copyright (c) 2010 Marshall A. Greenblatt. All rights reserved.
     2  //
     3  // Redistribution and use in source and binary forms, with or without
     4  // modification, are permitted provided that the following conditions are
     5  // met:
     6  //
     7  //    * Redistributions of source code must retain the above copyright
     8  // notice, this list of conditions and the following disclaimer.
     9  //    * Redistributions in binary form must reproduce the above
    10  // copyright notice, this list of conditions and the following disclaimer
    11  // in the documentation and/or other materials provided with the
    12  // distribution.
    13  //    * Neither the name of Google Inc. nor the name Chromium Embedded
    14  // Framework nor the names of its contributors may be used to endorse
    15  // or promote products derived from this software without specific prior
    16  // written permission.
    17  //
    18  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29  
    30  #ifndef CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_
    31  #define CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_
    32  #pragma once
    33  
    34  // CEF provides functions for converting between UTF-8, -16 and -32 strings.
    35  // CEF string types are safe for reading from multiple threads but not for
    36  // modification. It is the user's responsibility to provide synchronization if
    37  // modifying CEF strings from multiple threads.
    38  
    39  #ifdef __cplusplus
    40  extern "C" {
    41  #endif
    42  
    43  #include "include/internal/cef_build.h"
    44  #include "include/internal/cef_export.h"
    45  #include <stddef.h>
    46  
    47  // CEF character type definitions. wchar_t is 2 bytes on Windows and 4 bytes on
    48  // most other platforms.
    49  
    50  #if defined(OS_WIN)
    51  typedef wchar_t char16;
    52  #else  // !OS_WIN
    53  typedef unsigned short char16;  // NOLINT (runtime/int)
    54  #ifndef WCHAR_T_IS_UTF32
    55  #define WCHAR_T_IS_UTF32
    56  #endif  // WCHAR_T_IS_UTF32
    57  #endif  // !OS_WIN
    58  
    59  
    60  // CEF string type definitions. Whomever allocates |str| is responsible for
    61  // providing an appropriate |dtor| implementation that will free the string in
    62  // the same memory space. When reusing an existing string structure make sure
    63  // to call |dtor| for the old value before assigning new |str| and |dtor|
    64  // values. Static strings will have a NULL |dtor| value. Using the below
    65  // functions if you want this managed for you.
    66  
    67  typedef struct _cef_string_wide_t {
    68    wchar_t* str;
    69    size_t length;
    70    void (*dtor)(wchar_t* str);
    71  } cef_string_wide_t;
    72  
    73  typedef struct _cef_string_utf8_t {
    74    char* str;
    75    size_t length;
    76    void (*dtor)(char* str);
    77  } cef_string_utf8_t;
    78  
    79  typedef struct _cef_string_utf16_t {
    80    char16* str;
    81    size_t length;
    82    void (*dtor)(char16* str);
    83  } cef_string_utf16_t;
    84  
    85  
    86  ///
    87  // These functions set string values. If |copy| is true (1) the value will be
    88  // copied instead of referenced. It is up to the user to properly manage
    89  // the lifespan of references.
    90  ///
    91  
    92  CEF_EXPORT int cef_string_wide_set(const wchar_t* src, size_t src_len,
    93                                     cef_string_wide_t* output, int copy);
    94  CEF_EXPORT int cef_string_utf8_set(const char* src, size_t src_len,
    95                                     cef_string_utf8_t* output, int copy);
    96  CEF_EXPORT int cef_string_utf16_set(const char16* src, size_t src_len,
    97                                      cef_string_utf16_t* output, int copy);
    98  
    99  
   100  ///
   101  // Convenience macros for copying values.
   102  ///
   103  
   104  #define cef_string_wide_copy(src, src_len, output)  \
   105      cef_string_wide_set(src, src_len, output, true)
   106  #define cef_string_utf8_copy(src, src_len, output)  \
   107      cef_string_utf8_set(src, src_len, output, true)
   108  #define cef_string_utf16_copy(src, src_len, output)  \
   109      cef_string_utf16_set(src, src_len, output, true)
   110  
   111  
   112  ///
   113  // These functions clear string values. The structure itself is not freed.
   114  ///
   115  
   116  CEF_EXPORT void cef_string_wide_clear(cef_string_wide_t* str);
   117  CEF_EXPORT void cef_string_utf8_clear(cef_string_utf8_t* str);
   118  CEF_EXPORT void cef_string_utf16_clear(cef_string_utf16_t* str);
   119  
   120  
   121  ///
   122  // These functions compare two string values with the same results as strcmp().
   123  ///
   124  
   125  CEF_EXPORT int cef_string_wide_cmp(const cef_string_wide_t* str1,
   126                                     const cef_string_wide_t* str2);
   127  CEF_EXPORT int cef_string_utf8_cmp(const cef_string_utf8_t* str1,
   128                                     const cef_string_utf8_t* str2);
   129  CEF_EXPORT int cef_string_utf16_cmp(const cef_string_utf16_t* str1,
   130                                      const cef_string_utf16_t* str2);
   131  
   132  
   133  ///
   134  // These functions convert between UTF-8, -16, and -32 strings. They are
   135  // potentially slow so unnecessary conversions should be avoided. The best
   136  // possible result will always be written to |output| with the boolean return
   137  // value indicating whether the conversion is 100% valid.
   138  ///
   139  
   140  CEF_EXPORT int cef_string_wide_to_utf8(const wchar_t* src, size_t src_len,
   141                                         cef_string_utf8_t* output);
   142  CEF_EXPORT int cef_string_utf8_to_wide(const char* src, size_t src_len,
   143                                         cef_string_wide_t* output);
   144  
   145  CEF_EXPORT int cef_string_wide_to_utf16(const wchar_t* src, size_t src_len,
   146                                          cef_string_utf16_t* output);
   147  CEF_EXPORT int cef_string_utf16_to_wide(const char16* src, size_t src_len,
   148                                          cef_string_wide_t* output);
   149  
   150  CEF_EXPORT int cef_string_utf8_to_utf16(const char* src, size_t src_len,
   151                                          cef_string_utf16_t* output);
   152  CEF_EXPORT int cef_string_utf16_to_utf8(const char16* src, size_t src_len,
   153                                          cef_string_utf8_t* output);
   154  
   155  
   156  ///
   157  // These functions convert an ASCII string, typically a hardcoded constant, to a
   158  // Wide/UTF16 string. Use instead of the UTF8 conversion routines if you know
   159  // the string is ASCII.
   160  ///
   161  
   162  CEF_EXPORT int cef_string_ascii_to_wide(const char* src, size_t src_len,
   163                                          cef_string_wide_t* output);
   164  CEF_EXPORT int cef_string_ascii_to_utf16(const char* src, size_t src_len,
   165                                           cef_string_utf16_t* output);
   166  
   167  
   168  
   169  ///
   170  // It is sometimes necessary for the system to allocate string structures with
   171  // the expectation that the user will free them. The userfree types act as a
   172  // hint that the user is responsible for freeing the structure.
   173  ///
   174  
   175  typedef cef_string_wide_t* cef_string_userfree_wide_t;
   176  typedef cef_string_utf8_t* cef_string_userfree_utf8_t;
   177  typedef cef_string_utf16_t* cef_string_userfree_utf16_t;
   178  
   179  
   180  ///
   181  // These functions allocate a new string structure. They must be freed by
   182  // calling the associated free function.
   183  ///
   184  
   185  CEF_EXPORT cef_string_userfree_wide_t cef_string_userfree_wide_alloc();
   186  CEF_EXPORT cef_string_userfree_utf8_t cef_string_userfree_utf8_alloc();
   187  CEF_EXPORT cef_string_userfree_utf16_t cef_string_userfree_utf16_alloc();
   188  
   189  
   190  ///
   191  // These functions free the string structure allocated by the associated
   192  // alloc function. Any string contents will first be cleared.
   193  ///
   194  
   195  CEF_EXPORT void cef_string_userfree_wide_free(cef_string_userfree_wide_t str);
   196  CEF_EXPORT void cef_string_userfree_utf8_free(cef_string_userfree_utf8_t str);
   197  CEF_EXPORT void cef_string_userfree_utf16_free(cef_string_userfree_utf16_t str);
   198  
   199  
   200  #ifdef __cplusplus
   201  }
   202  #endif
   203  
   204  #endif  // CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_