1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 *   Copyright (C) 1998-2011, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * File ufmt_cmn.h
12 *
13 * Modification History:
14 *
15 *   Date        Name        Description
16 *   12/02/98    stephen        Creation.
17 *   03/12/99    stephen     Modified for new C API.
18 *   03/15/99    stephen     Added defaultCPToUnicode, unicodeToDefaultCP
19 ******************************************************************************
20 */
21 
22 #ifndef UFMT_CMN_H
23 #define UFMT_CMN_H
24 
25 #include "unicode/utypes.h"
26 #include "unicode/utf16.h"
27 
28 #define UFMT_DEFAULT_BUFFER_SIZE 128
29 #define MAX_UCHAR_BUFFER_SIZE(buffer) ((int32_t)(sizeof(buffer)/(U16_MAX_LENGTH*sizeof(UChar))))
30 #define MAX_UCHAR_BUFFER_NEEDED(strLen) ((strLen+1)*U16_MAX_LENGTH*sizeof(UChar))
31 
32 /**
33  * Enum representing the possible argument types for uprintf/uscanf
34  */
35 typedef enum ufmt_type_info {
36     ufmt_empty = 0,
37     ufmt_simple_percent, /* %% do nothing */
38     ufmt_count,      /* special flag for count */
39     ufmt_int,        /* int */
40     ufmt_char,       /* int, cast to char */
41     ufmt_string,     /* char* */
42     ufmt_pointer,    /* void* */
43     ufmt_float,      /* float */
44     ufmt_double,     /* double */
45     ufmt_uchar,      /* int, cast to UChar */
46     ufmt_ustring     /* UChar* */
47     /*ufmt_wchar,*/      /* wchar_t */
48     /*ufmt_wstring,*/    /* wchar_t* */
49     /*ufmt_date,*/       /* Date */
50     /*ufmt_last*/
51 } ufmt_type_info;
52 
53 /**
54  * Union representing a uprintf/uscanf argument
55  */
56 typedef union ufmt_args {
57     int64_t int64Value;    /* int, UChar */
58     float   floatValue;    /* float */
59     double  doubleValue;   /* double */
60     void    *ptrValue;     /* any pointer - void*, char*, wchar_t*, UChar* */
61     /*wchar_t wcharValue;*/    /* wchar_t */    /* TODO: Should wchar_t be used? */
62     /*UDate dateValue;*/     /* Date */
63 } ufmt_args;
64 
65 /**
66  * Macro for determining the minimum of two numbers.
67  * @param a An integer
68  * @param b An integer
69  * @return <TT>a</TT> if </TT>a < b</TT>, <TT>b</TT> otherwise
70  */
71 #define ufmt_min(a,b) ((a) < (b) ? (a) : (b))
72 
73 /**
74  * Convert a UChar in hex radix to an integer value.
75  * @param c The UChar to convert.
76  * @return The integer value of <TT>c</TT>.
77  */
78 int
79 ufmt_digitvalue(UChar c);
80 
81 /**
82  * Determine if a UChar is a digit for a specified radix.
83  * @param c The UChar to check.
84  * @param radix The desired radix.
85  * @return true if <TT>c</TT> is a digit in <TT>radix</TT>, false otherwise.
86  */
87 UBool
88 ufmt_isdigit(UChar     c,
89          int32_t     radix);
90 
91 /**
92  * Convert an int64_t to a UChar* in a specified radix
93  * @param buffer The target buffer
94  * @param len On input, the size of <TT>buffer</TT>.  On output,
95  * the number of UChars written to <TT>buffer</TT>.
96  * @param value The value to be converted
97  * @param radix The desired radix
98  * @param uselower true means lower case will be used, false means upper case
99  * @param minDigits The minimum number of digits for for the formatted number,
100  * which will be padded with zeroes. -1 means do not pad.
101  */
102 void
103 ufmt_64tou(UChar     *buffer,
104       int32_t     *len,
105       uint64_t     value,
106       uint8_t     radix,
107       UBool    uselower,
108       int32_t    minDigits);
109 
110 /**
111  * It's like ufmt_64tou, but with a pointer.
112  * This functions avoids size constraints of 64-bit types.
113  * Pointers can be at 32-128 bits in size.
114  */
115 void
116 ufmt_ptou(UChar    *buffer,
117           int32_t   *len,
118           void      *value,
119           UBool     uselower);
120 
121 /**
122  * Convert a UChar* in a specified radix to an int64_t.
123  * @param buffer The target buffer
124  * @param len On input, the size of <TT>buffer</TT>.  On output,
125  * the number of UChars read from <TT>buffer</TT>.
126  * @param radix The desired radix
127  * @return The numeric value.
128  */
129 int64_t
130 ufmt_uto64(const UChar     *buffer,
131       int32_t     *len,
132       int8_t     radix);
133 
134 /**
135  * Convert a UChar* in a specified radix to a pointer,
136  * @param buffer The target buffer
137  * @param len On input, the size of <TT>buffer</TT>.  On output,
138  * the number of UChars read from <TT>buffer</TT>.
139  * @param radix The desired radix
140  * @return The pointer value.
141  */
142 void *
143 ufmt_utop(const UChar     *buffer,
144       int32_t     *len);
145 
146 /**
147  * Convert a string from the default codepage to Unicode.
148  * @param s The string to convert, in the default codepage.
149  * @param sSize The size of s to convert.
150  * @param target The buffer to convert to.
151  * @param tSize The size of target
152  * @return A pointer to a newly allocated converted version of s, or 0
153  * on error.
154  */
155 UChar*
156 ufmt_defaultCPToUnicode(const char *s, int32_t sSize,
157                         UChar *target, int32_t tSize);
158 
159 
160 
161 #endif
162 
163