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