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