1/*
2*******************************************************************************
3*
4*   Copyright (C) 1999-2010, International Business Machines
5*   Corporation and others.  All Rights Reserved.
6*
7*******************************************************************************
8*   file name:  toolutil.h
9*   encoding:   US-ASCII
10*   tab size:   8 (not used)
11*   indentation:4
12*
13*   created on: 1999nov19
14*   created by: Markus W. Scherer
15*
16*   This file defines utility functions for ICU tools like genccode.
17*/
18
19#ifndef __TOOLUTIL_H__
20#define __TOOLUTIL_H__
21
22#include "unicode/utypes.h"
23
24#ifdef XP_CPLUSPLUS
25
26#include "unicode/errorcode.h"
27
28U_NAMESPACE_BEGIN
29
30/**
31 * ErrorCode subclass for use in ICU command-line tools.
32 * The destructor calls handleFailure() which calls exit(errorCode) when isFailure().
33 */
34class U_TOOLUTIL_API IcuToolErrorCode : public ErrorCode {
35public:
36    /**
37     * @param loc A short string describing where the IcuToolErrorCode is used.
38     */
39    IcuToolErrorCode(const char *loc) : location(loc) {}
40    virtual ~IcuToolErrorCode();
41protected:
42    virtual void handleFailure() const;
43private:
44    const char *location;
45};
46
47U_NAMESPACE_END
48
49#endif
50
51/*
52 * For Windows, a path/filename may be the short (8.3) version
53 * of the "real", long one. In this case, the short one
54 * is abbreviated and contains a tilde etc.
55 * This function returns a pointer to the original pathname
56 * if it is the "real" one itself, and a pointer to a static
57 * buffer (not thread-safe) containing the long version
58 * if the pathname is indeed abbreviated.
59 *
60 * On platforms other than Windows, this function always returns
61 * the input pathname pointer.
62 *
63 * This function is especially useful in tools that are called
64 * by a batch file for loop, which yields short pathnames on Win9x.
65 */
66U_CAPI const char * U_EXPORT2
67getLongPathname(const char *pathname);
68
69/**
70 * Find the basename at the end of a pathname, i.e., the part
71 * after the last file separator, and return a pointer
72 * to this part of the pathname.
73 * If the pathname only contains a basename and no file separator,
74 * then the pathname pointer itself is returned.
75 **/
76U_CAPI const char * U_EXPORT2
77findBasename(const char *filename);
78
79/**
80 * Find the directory name of a pathname, that is, everything
81 * up to but not including the last file separator.
82 *
83 * If successful, copies the directory name into the output buffer along with
84 * a terminating NULL.
85 *
86 * If there isn't a directory name in the path, it returns the current directory string ('.').
87 * @param path the full pathname to inspect.
88 * @param buffer the output buffer
89 * @param bufLen the output buffer length
90 * @param status error code- may return U_BUFFER_OVERFLOW_ERROR if bufLen is too small.
91 * @return If successful, a pointer to the output buffer. If failure or bufLen is too small, NULL.
92 **/
93U_CAPI const char * U_EXPORT2
94findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status);
95
96/*
97 * Return the current year in the Gregorian calendar. Used for copyright generation.
98 */
99U_CAPI int32_t U_EXPORT2
100getCurrentYear(void);
101
102/*
103 * Creates a directory with pathname.
104 *
105 * @param status Set to an error code when mkdir failed.
106 */
107U_CAPI void U_EXPORT2
108uprv_mkdir(const char *pathname, UErrorCode *status);
109
110/**
111 * Return the modification date for the specified file or directory.
112 * Return value is undefined if there was an error.
113 */
114/*U_CAPI UDate U_EXPORT2
115uprv_getModificationDate(const char *pathname, UErrorCode *status);
116*/
117/*
118 * Returns the modification
119 *
120 * @param status Set to an error code when mkdir failed.
121 */
122
123/*
124 * UToolMemory is used for generic, custom memory management.
125 * It is allocated with enough space for count*size bytes starting
126 * at array.
127 * The array is declared with a union of large data types so
128 * that its base address is aligned for any types.
129 * If size is a multiple of a data type size, then such items
130 * can be safely allocated inside the array, at offsets that
131 * are themselves multiples of size.
132 */
133struct UToolMemory;
134typedef struct UToolMemory UToolMemory;
135
136/**
137 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity
138 * items with size bytes each.
139 */
140U_CAPI UToolMemory * U_EXPORT2
141utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size);
142
143/**
144 * Close a UToolMemory object.
145 */
146U_CAPI void U_EXPORT2
147utm_close(UToolMemory *mem);
148
149/**
150 * Get the pointer to the beginning of the array of items.
151 * The pointer becomes invalid after allocation of new items.
152 */
153U_CAPI void * U_EXPORT2
154utm_getStart(UToolMemory *mem);
155
156/**
157 * Get the current number of items.
158 */
159U_CAPI int32_t U_EXPORT2
160utm_countItems(UToolMemory *mem);
161
162/**
163 * Allocate one more item and return the pointer to its start in the array.
164 */
165U_CAPI void * U_EXPORT2
166utm_alloc(UToolMemory *mem);
167
168/**
169 * Allocate n items and return the pointer to the start of the first one in the array.
170 */
171U_CAPI void * U_EXPORT2
172utm_allocN(UToolMemory *mem, int32_t n);
173
174#endif
175