utf-conv.h 4.23 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8
#ifndef INCLUDE_win32_utf_conv_h__
#define INCLUDE_win32_utf_conv_h__
9

10
#include "git2_util.h"
11

12 13
#include <wchar.h>

14 15 16 17
#ifndef WC_ERR_INVALID_CHARS
# define WC_ERR_INVALID_CHARS	0x80
#endif

18
/**
19 20 21 22 23 24 25 26 27 28 29 30
 * Converts a NUL-terminated UTF-8 string to wide characters. This is a
 * convenience function for `git_utf8_to_16_with_len`.
 *
 * @param dest The buffer to receive the wide string.
 * @param dest_size The size of the buffer, in characters.
 * @param src The UTF-8 string to convert.
 * @return The length of the wide string, in characters
 *         (not counting the NULL terminator), or < 0 for failure
 */
int git_utf8_to_16(wchar_t *dest, size_t dest_size, const char *src);

/**
31 32 33 34 35
 * Converts a UTF-8 string to wide characters.
 *
 * @param dest The buffer to receive the wide string.
 * @param dest_size The size of the buffer, in characters.
 * @param src The UTF-8 string to convert.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 * @param src_len The length of the string to convert.
 * @return The length of the wide string, in characters
 *         (not counting the NULL terminator), or < 0 for failure
 */
int git_utf8_to_16_with_len(
	wchar_t *dest,
	size_t dest_size,
	const char *src,
	int src_len);

/**
 * Converts a NUL-terminated wide string to UTF-8. This is a convenience
 * function for `git_utf8_from_16_with_len`.
 *
 * @param dest The buffer to receive the UTF-8 string.
 * @param dest_size The size of the buffer, in bytes.
 * @param src The wide string to convert.
 * @param src_len The length of the string to convert.
 * @return The length of the UTF-8 string, in bytes
 *         (not counting the NULL terminator), or < 0 for failure
56
 */
57
int git_utf8_from_16(char *dest, size_t dest_size, const wchar_t *src);
58

59 60 61 62 63 64
/**
 * Converts a wide string to UTF-8.
 *
 * @param dest The buffer to receive the UTF-8 string.
 * @param dest_size The size of the buffer, in bytes.
 * @param src The wide string to convert.
65 66 67
 * @param src_len The length of the string to convert.
 * @return The length of the UTF-8 string, in bytes
 *         (not counting the NULL terminator), or < 0 for failure
68
 */
69
int git_utf8_from_16_with_len(char *dest, size_t dest_size, const wchar_t *src, int src_len);
70

71
/**
72 73 74
 * Converts a UTF-8 string to wide characters. Memory is allocated to hold
 * the converted string. The caller is responsible for freeing the string
 * with git__free.
75 76 77
 *
 * @param dest Receives a pointer to the wide string.
 * @param src The UTF-8 string to convert.
78 79
 * @return The length of the wide string, in characters
 *         (not counting the NULL terminator), or < 0 for failure
80
 */
81
int git_utf8_to_16_alloc(wchar_t **dest, const char *src);
82 83

/**
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
 * Converts a UTF-8 string to wide characters. Memory is allocated to hold
 * the converted string. The caller is responsible for freeing the string
 * with git__free.
 *
 * @param dest Receives a pointer to the wide string.
 * @param src The UTF-8 string to convert.
 * @param src_len The length of the string.
 * @return The length of the wide string, in characters
 *         (not counting the NULL terminator), or < 0 for failure
 */
int git_utf8_to_16_alloc_with_len(
	wchar_t **dest,
	const char *src,
	int src_len);

/**
 * Converts a wide string to UTF-8. Memory is allocated to hold the
 * converted string. The caller is responsible for freeing the string
 * with git__free.
 *
 * @param dest Receives a pointer to the UTF-8 string.
 * @param src The wide string to convert.
 * @return The length of the UTF-8 string, in bytes
 *         (not counting the NULL terminator), or < 0 for failure
 */
int git_utf8_from_16_alloc(char **dest, const wchar_t *src);

/**
 * Converts a wide string to UTF-8. Memory is allocated to hold the
 * converted string. The caller is responsible for freeing the string
 * with git__free.
115 116 117
 *
 * @param dest Receives a pointer to the UTF-8 string.
 * @param src The wide string to convert.
118 119 120
 * @param src_len The length of the wide string.
 * @return The length of the UTF-8 string, in bytes
 *         (not counting the NULL terminator), or < 0 for failure
121
 */
122 123 124 125
int git_utf8_from_16_alloc_with_len(
	char **dest,
	const wchar_t *src,
	int src_len);
126

127
#endif