1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1999-2013, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: toolutil.h
12 * tab size: 8 (not used)
15 * created on: 1999nov19
16 * created by: Markus W. Scherer
18 * This file defines utility functions for ICU tools like genccode.
21 #ifndef __TOOLUTIL_H__
22 #define __TOOLUTIL_H__
24 #include "unicode/utypes.h"
29 #include "unicode/errorcode.h"
34 * ErrorCode subclass for use in ICU command-line tools.
35 * The destructor calls handleFailure() which calls exit(errorCode) when isFailure().
37 class U_TOOLUTIL_API IcuToolErrorCode
: public ErrorCode
{
40 * @param loc A short string describing where the IcuToolErrorCode is used.
42 IcuToolErrorCode(const char *loc
) : location(loc
) {}
43 virtual ~IcuToolErrorCode();
45 virtual void handleFailure() const;
55 * For Windows, a path/filename may be the short (8.3) version
56 * of the "real", long one. In this case, the short one
57 * is abbreviated and contains a tilde etc.
58 * This function returns a pointer to the original pathname
59 * if it is the "real" one itself, and a pointer to a static
60 * buffer (not thread-safe) containing the long version
61 * if the pathname is indeed abbreviated.
63 * On platforms other than Windows, this function always returns
64 * the input pathname pointer.
66 * This function is especially useful in tools that are called
67 * by a batch file for loop, which yields short pathnames on Win9x.
69 U_CAPI
const char * U_EXPORT2
70 getLongPathname(const char *pathname
);
73 * Find the basename at the end of a pathname, i.e., the part
74 * after the last file separator, and return a pointer
75 * to this part of the pathname.
76 * If the pathname only contains a basename and no file separator,
77 * then the pathname pointer itself is returned.
79 U_CAPI
const char * U_EXPORT2
80 findBasename(const char *filename
);
83 * Find the directory name of a pathname, that is, everything
84 * up to but not including the last file separator.
86 * If successful, copies the directory name into the output buffer along with
89 * If there isn't a directory name in the path, it returns an empty string.
90 * @param path the full pathname to inspect.
91 * @param buffer the output buffer
92 * @param bufLen the output buffer length
93 * @param status error code- may return U_BUFFER_OVERFLOW_ERROR if bufLen is too small.
94 * @return If successful, a pointer to the output buffer. If failure or bufLen is too small, NULL.
96 U_CAPI
const char * U_EXPORT2
97 findDirname(const char *path
, char *buffer
, int32_t bufLen
, UErrorCode
* status
);
100 * Return the current year in the Gregorian calendar. Used for copyright generation.
102 U_CAPI
int32_t U_EXPORT2
103 getCurrentYear(void);
106 * Creates a directory with pathname.
108 * @param status Set to an error code when mkdir failed.
110 U_CAPI
void U_EXPORT2
111 uprv_mkdir(const char *pathname
, UErrorCode
*status
);
113 #if !UCONFIG_NO_FILE_IO
115 * Return TRUE if the named item exists
116 * @param file filename
117 * @return TRUE if named item (file, dir, etc) exists, FALSE otherwise
119 U_CAPI UBool U_EXPORT2
120 uprv_fileExists(const char *file
);
124 * Return the modification date for the specified file or directory.
125 * Return value is undefined if there was an error.
127 /*U_CAPI UDate U_EXPORT2
128 uprv_getModificationDate(const char *pathname, UErrorCode *status);
131 * Returns the modification
133 * @param status Set to an error code when mkdir failed.
137 * UToolMemory is used for generic, custom memory management.
138 * It is allocated with enough space for count*size bytes starting
140 * The array is declared with a union of large data types so
141 * that its base address is aligned for any types.
142 * If size is a multiple of a data type size, then such items
143 * can be safely allocated inside the array, at offsets that
144 * are themselves multiples of size.
147 typedef struct UToolMemory UToolMemory
;
150 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity
151 * items with size bytes each.
153 U_CAPI UToolMemory
* U_EXPORT2
154 utm_open(const char *name
, int32_t initialCapacity
, int32_t maxCapacity
, int32_t size
);
157 * Close a UToolMemory object.
159 U_CAPI
void U_EXPORT2
160 utm_close(UToolMemory
*mem
);
163 * Get the pointer to the beginning of the array of items.
164 * The pointer becomes invalid after allocation of new items.
166 U_CAPI
void * U_EXPORT2
167 utm_getStart(UToolMemory
*mem
);
170 * Get the current number of items.
172 U_CAPI
int32_t U_EXPORT2
173 utm_countItems(UToolMemory
*mem
);
176 * Allocate one more item and return the pointer to its start in the array.
178 U_CAPI
void * U_EXPORT2
179 utm_alloc(UToolMemory
*mem
);
182 * Allocate n items and return the pointer to the start of the first one in the array.
184 U_CAPI
void * U_EXPORT2
185 utm_allocN(UToolMemory
*mem
, int32_t n
);