blob: 98b2155551e439649283dac3b33c0f7954604e3d [file] [log] [blame]
Jungshik Shin87232d82017-05-13 21:10:13 -07001// © 2016 and later: Unicode, Inc. and others.
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07002// License & terms of use: http://www.unicode.org/copyright.html
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00003/*
4*******************************************************************************
5*
6* Copyright (C) 1999-2013, International Business Machines
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: toolutil.h
Jungshik Shin87232d82017-05-13 21:10:13 -070011* encoding: UTF-8
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000012* tab size: 8 (not used)
13* indentation:4
14*
15* created on: 1999nov19
16* created by: Markus W. Scherer
17*
18* This file defines utility functions for ICU tools like genccode.
19*/
20
21#ifndef __TOOLUTIL_H__
22#define __TOOLUTIL_H__
23
24#include "unicode/utypes.h"
25
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000026#ifdef __cplusplus
27
28#include "unicode/errorcode.h"
29
30U_NAMESPACE_BEGIN
31
32/**
33 * ErrorCode subclass for use in ICU command-line tools.
34 * The destructor calls handleFailure() which calls exit(errorCode) when isFailure().
35 */
36class U_TOOLUTIL_API IcuToolErrorCode : public ErrorCode {
37public:
38 /**
39 * @param loc A short string describing where the IcuToolErrorCode is used.
40 */
41 IcuToolErrorCode(const char *loc) : location(loc) {}
42 virtual ~IcuToolErrorCode();
43protected:
Frank Tang3e05d9d2021-11-08 14:04:04 -080044 virtual void handleFailure() const override;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000045private:
46 const char *location;
47};
48
49U_NAMESPACE_END
50
51#endif
52
53/*
54 * For Windows, a path/filename may be the short (8.3) version
55 * of the "real", long one. In this case, the short one
56 * is abbreviated and contains a tilde etc.
57 * This function returns a pointer to the original pathname
58 * if it is the "real" one itself, and a pointer to a static
59 * buffer (not thread-safe) containing the long version
60 * if the pathname is indeed abbreviated.
61 *
62 * On platforms other than Windows, this function always returns
63 * the input pathname pointer.
64 *
65 * This function is especially useful in tools that are called
66 * by a batch file for loop, which yields short pathnames on Win9x.
67 */
68U_CAPI const char * U_EXPORT2
69getLongPathname(const char *pathname);
70
71/**
72 * Find the basename at the end of a pathname, i.e., the part
73 * after the last file separator, and return a pointer
74 * to this part of the pathname.
75 * If the pathname only contains a basename and no file separator,
76 * then the pathname pointer itself is returned.
77 **/
78U_CAPI const char * U_EXPORT2
79findBasename(const char *filename);
80
81/**
82 * Find the directory name of a pathname, that is, everything
83 * up to but not including the last file separator.
84 *
85 * If successful, copies the directory name into the output buffer along with
86 * a terminating NULL.
87 *
88 * If there isn't a directory name in the path, it returns an empty string.
89 * @param path the full pathname to inspect.
90 * @param buffer the output buffer
91 * @param bufLen the output buffer length
92 * @param status error code- may return U_BUFFER_OVERFLOW_ERROR if bufLen is too small.
93 * @return If successful, a pointer to the output buffer. If failure or bufLen is too small, NULL.
94 **/
95U_CAPI const char * U_EXPORT2
96findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status);
97
98/*
99 * Return the current year in the Gregorian calendar. Used for copyright generation.
100 */
101U_CAPI int32_t U_EXPORT2
102getCurrentYear(void);
103
104/*
105 * Creates a directory with pathname.
106 *
107 * @param status Set to an error code when mkdir failed.
108 */
109U_CAPI void U_EXPORT2
110uprv_mkdir(const char *pathname, UErrorCode *status);
111
112#if !UCONFIG_NO_FILE_IO
113/**
Frank Tang1f164ee2022-11-08 12:31:27 -0800114 * Return true if the named item exists
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000115 * @param file filename
Frank Tang1f164ee2022-11-08 12:31:27 -0800116 * @return true if named item (file, dir, etc) exists, false otherwise
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000117 */
118U_CAPI UBool U_EXPORT2
119uprv_fileExists(const char *file);
120#endif
121
122/**
Frank Tang3e05d9d2021-11-08 14:04:04 -0800123 * Performs a golden data test. Asserts that the contents of the buffer is equal
124 * to the data in goldenFilePath.
125 *
126 * Pass the value of the -G flag to "overwrite"; if true, new goldens will be
127 * written to the filesystem.
128 *
129 * @return The first index at which the files differ, or -1 if they are the same.
130 */
131U_CAPI int32_t U_EXPORT2
132uprv_compareGoldenFiles(
133 const char* buffer, int32_t bufferLen,
134 const char* goldenFilePath,
135 bool overwrite);
136
137/**
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000138 * Return the modification date for the specified file or directory.
139 * Return value is undefined if there was an error.
140 */
141/*U_CAPI UDate U_EXPORT2
142uprv_getModificationDate(const char *pathname, UErrorCode *status);
143*/
144/*
145 * Returns the modification
146 *
147 * @param status Set to an error code when mkdir failed.
148 */
149
150/*
151 * UToolMemory is used for generic, custom memory management.
152 * It is allocated with enough space for count*size bytes starting
153 * at array.
154 * The array is declared with a union of large data types so
155 * that its base address is aligned for any types.
156 * If size is a multiple of a data type size, then such items
157 * can be safely allocated inside the array, at offsets that
158 * are themselves multiples of size.
159 */
160struct UToolMemory;
161typedef struct UToolMemory UToolMemory;
162
163/**
164 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity
165 * items with size bytes each.
166 */
167U_CAPI UToolMemory * U_EXPORT2
168utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size);
169
170/**
171 * Close a UToolMemory object.
172 */
173U_CAPI void U_EXPORT2
174utm_close(UToolMemory *mem);
175
176/**
177 * Get the pointer to the beginning of the array of items.
178 * The pointer becomes invalid after allocation of new items.
179 */
180U_CAPI void * U_EXPORT2
181utm_getStart(UToolMemory *mem);
182
183/**
184 * Get the current number of items.
185 */
186U_CAPI int32_t U_EXPORT2
187utm_countItems(UToolMemory *mem);
188
189/**
190 * Allocate one more item and return the pointer to its start in the array.
191 */
192U_CAPI void * U_EXPORT2
193utm_alloc(UToolMemory *mem);
194
195/**
196 * Allocate n items and return the pointer to the start of the first one in the array.
197 */
198U_CAPI void * U_EXPORT2
199utm_allocN(UToolMemory *mem, int32_t n);
200
201#endif