2 *******************************************************************************
4 * Copyright (C) 1999-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: toolutil.h
10 * tab size: 8 (not used)
13 * created on: 1999nov19
14 * created by: Markus W. Scherer
16 * This file defines utility functions for ICU tools like genccode.
19 #ifndef __TOOLUTIL_H__
20 #define __TOOLUTIL_H__
22 #include "unicode/utypes.h"
27 #include "unicode/errorcode.h"
32 * ErrorCode subclass for use in ICU command-line tools.
33 * The destructor calls handleFailure() which calls exit(errorCode) when isFailure().
35 class U_TOOLUTIL_API IcuToolErrorCode
: public ErrorCode
{
38 * @param loc A short string describing where the IcuToolErrorCode is used.
40 IcuToolErrorCode(const char *loc
) : location(loc
) {}
41 virtual ~IcuToolErrorCode();
43 virtual void handleFailure() const;
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.
61 * On platforms other than Windows, this function always returns
62 * the input pathname pointer.
64 * This function is especially useful in tools that are called
65 * by a batch file for loop, which yields short pathnames on Win9x.
67 U_CAPI
const char * U_EXPORT2
68 getLongPathname(const char *pathname
);
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.
77 U_CAPI
const char * U_EXPORT2
78 findBasename(const char *filename
);
81 * Find the directory name of a pathname, that is, everything
82 * up to but not including the last file separator.
84 * If successful, copies the directory name into the output buffer along with
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.
94 U_CAPI
const char * U_EXPORT2
95 findDirname(const char *path
, char *buffer
, int32_t bufLen
, UErrorCode
* status
);
98 * Return the current year in the Gregorian calendar. Used for copyright generation.
100 U_CAPI
int32_t U_EXPORT2
101 getCurrentYear(void);
104 * Creates a directory with pathname.
106 * @param status Set to an error code when mkdir failed.
108 U_CAPI
void U_EXPORT2
109 uprv_mkdir(const char *pathname
, UErrorCode
*status
);
111 #if !UCONFIG_NO_FILE_IO
113 * Return TRUE if the named item exists
114 * @param file filename
115 * @return TRUE if named item (file, dir, etc) exists, FALSE otherwise
117 U_CAPI UBool U_EXPORT2
118 uprv_fileExists(const char *file
);
122 * Return the modification date for the specified file or directory.
123 * Return value is undefined if there was an error.
125 /*U_CAPI UDate U_EXPORT2
126 uprv_getModificationDate(const char *pathname, UErrorCode *status);
129 * Returns the modification
131 * @param status Set to an error code when mkdir failed.
135 * UToolMemory is used for generic, custom memory management.
136 * It is allocated with enough space for count*size bytes starting
138 * The array is declared with a union of large data types so
139 * that its base address is aligned for any types.
140 * If size is a multiple of a data type size, then such items
141 * can be safely allocated inside the array, at offsets that
142 * are themselves multiples of size.
145 typedef struct UToolMemory UToolMemory
;
148 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity
149 * items with size bytes each.
151 U_CAPI UToolMemory
* U_EXPORT2
152 utm_open(const char *name
, int32_t initialCapacity
, int32_t maxCapacity
, int32_t size
);
155 * Close a UToolMemory object.
157 U_CAPI
void U_EXPORT2
158 utm_close(UToolMemory
*mem
);
161 * Get the pointer to the beginning of the array of items.
162 * The pointer becomes invalid after allocation of new items.
164 U_CAPI
void * U_EXPORT2
165 utm_getStart(UToolMemory
*mem
);
168 * Get the current number of items.
170 U_CAPI
int32_t U_EXPORT2
171 utm_countItems(UToolMemory
*mem
);
174 * Allocate one more item and return the pointer to its start in the array.
176 U_CAPI
void * U_EXPORT2
177 utm_alloc(UToolMemory
*mem
);
180 * Allocate n items and return the pointer to the start of the first one in the array.
182 U_CAPI
void * U_EXPORT2
183 utm_allocN(UToolMemory
*mem
, int32_t n
);