Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /* |
| 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 Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 11 | * encoding: UTF-8 |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 12 | * 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 26 | #ifdef __cplusplus |
| 27 | |
| 28 | #include "unicode/errorcode.h" |
| 29 | |
| 30 | U_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 | */ |
| 36 | class U_TOOLUTIL_API IcuToolErrorCode : public ErrorCode { |
| 37 | public: |
| 38 | /** |
| 39 | * @param loc A short string describing where the IcuToolErrorCode is used. |
| 40 | */ |
| 41 | IcuToolErrorCode(const char *loc) : location(loc) {} |
| 42 | virtual ~IcuToolErrorCode(); |
| 43 | protected: |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 44 | virtual void handleFailure() const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 45 | private: |
| 46 | const char *location; |
| 47 | }; |
| 48 | |
| 49 | U_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 | */ |
| 68 | U_CAPI const char * U_EXPORT2 |
| 69 | getLongPathname(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 | **/ |
| 78 | U_CAPI const char * U_EXPORT2 |
| 79 | findBasename(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 | **/ |
| 95 | U_CAPI const char * U_EXPORT2 |
| 96 | findDirname(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 | */ |
| 101 | U_CAPI int32_t U_EXPORT2 |
| 102 | getCurrentYear(void); |
| 103 | |
| 104 | /* |
| 105 | * Creates a directory with pathname. |
| 106 | * |
| 107 | * @param status Set to an error code when mkdir failed. |
| 108 | */ |
| 109 | U_CAPI void U_EXPORT2 |
| 110 | uprv_mkdir(const char *pathname, UErrorCode *status); |
| 111 | |
| 112 | #if !UCONFIG_NO_FILE_IO |
| 113 | /** |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 114 | * Return true if the named item exists |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 115 | * @param file filename |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 116 | * @return true if named item (file, dir, etc) exists, false otherwise |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 117 | */ |
| 118 | U_CAPI UBool U_EXPORT2 |
| 119 | uprv_fileExists(const char *file); |
| 120 | #endif |
| 121 | |
| 122 | /** |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 123 | * 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 | */ |
| 131 | U_CAPI int32_t U_EXPORT2 |
| 132 | uprv_compareGoldenFiles( |
| 133 | const char* buffer, int32_t bufferLen, |
| 134 | const char* goldenFilePath, |
| 135 | bool overwrite); |
| 136 | |
| 137 | /** |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 138 | * 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 |
| 142 | uprv_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 | */ |
| 160 | struct UToolMemory; |
| 161 | typedef struct UToolMemory UToolMemory; |
| 162 | |
| 163 | /** |
| 164 | * Open a UToolMemory object for allocation of initialCapacity to maxCapacity |
| 165 | * items with size bytes each. |
| 166 | */ |
| 167 | U_CAPI UToolMemory * U_EXPORT2 |
| 168 | utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size); |
| 169 | |
| 170 | /** |
| 171 | * Close a UToolMemory object. |
| 172 | */ |
| 173 | U_CAPI void U_EXPORT2 |
| 174 | utm_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 | */ |
| 180 | U_CAPI void * U_EXPORT2 |
| 181 | utm_getStart(UToolMemory *mem); |
| 182 | |
| 183 | /** |
| 184 | * Get the current number of items. |
| 185 | */ |
| 186 | U_CAPI int32_t U_EXPORT2 |
| 187 | utm_countItems(UToolMemory *mem); |
| 188 | |
| 189 | /** |
| 190 | * Allocate one more item and return the pointer to its start in the array. |
| 191 | */ |
| 192 | U_CAPI void * U_EXPORT2 |
| 193 | utm_alloc(UToolMemory *mem); |
| 194 | |
| 195 | /** |
| 196 | * Allocate n items and return the pointer to the start of the first one in the array. |
| 197 | */ |
| 198 | U_CAPI void * U_EXPORT2 |
| 199 | utm_allocN(UToolMemory *mem, int32_t n); |
| 200 | |
| 201 | #endif |