1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 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 11 * encoding: UTF-8 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 26 #ifdef __cplusplus 27 28 #include "unicode/errorcode.h" 29 #include "unicode/umutablecptrie.h" 30 31 U_NAMESPACE_BEGIN 32 33 /** 34 * ErrorCode subclass for use in ICU command-line tools. 35 * The destructor calls handleFailure() which calls exit(errorCode) when isFailure(). 36 */ 37 class U_TOOLUTIL_API IcuToolErrorCode : public ErrorCode { 38 public: 39 /** 40 * @param loc A short string describing where the IcuToolErrorCode is used. 41 */ IcuToolErrorCode(const char * loc)42 IcuToolErrorCode(const char *loc) : location(loc) {} 43 virtual ~IcuToolErrorCode(); 44 protected: 45 virtual void handleFailure() const override; 46 private: 47 const char *location; 48 }; 49 50 namespace toolutil { 51 52 /** 53 * Sets one bit in the trie values of the start..end range, 54 * without changing the other bits in the trie values of that range. 55 */ 56 U_TOOLUTIL_API void 57 setCPTrieBit(UMutableCPTrie *mutableCPTrie, 58 UChar32 start, UChar32 end, int32_t shift, bool on, UErrorCode &errorCode); 59 60 /** 61 * Sets a bit set (defined by the mask) in the trie values of the start..end range, 62 * without changing the other bits in the trie values of that range. 63 * The given value must not have any bits set outside of the mask. 64 */ 65 U_TOOLUTIL_API void 66 setCPTrieBits(UMutableCPTrie *mutableCPTrie, 67 UChar32 start, UChar32 end, uint32_t mask, uint32_t value, UErrorCode &errorCode); 68 69 U_TOOLUTIL_API int32_t 70 getCPTrieSize(UMutableCPTrie *mt, UCPTrieType type, UCPTrieValueWidth valueWidth); 71 72 } // toolutil 73 74 U_NAMESPACE_END 75 76 #endif 77 78 /* 79 * For Windows, a path/filename may be the short (8.3) version 80 * of the "real", long one. In this case, the short one 81 * is abbreviated and contains a tilde etc. 82 * This function returns a pointer to the original pathname 83 * if it is the "real" one itself, and a pointer to a static 84 * buffer (not thread-safe) containing the long version 85 * if the pathname is indeed abbreviated. 86 * 87 * On platforms other than Windows, this function always returns 88 * the input pathname pointer. 89 * 90 * This function is especially useful in tools that are called 91 * by a batch file for loop, which yields short pathnames on Win9x. 92 */ 93 U_CAPI const char * U_EXPORT2 94 getLongPathname(const char *pathname); 95 96 /** 97 * Find the basename at the end of a pathname, i.e., the part 98 * after the last file separator, and return a pointer 99 * to this part of the pathname. 100 * If the pathname only contains a basename and no file separator, 101 * then the pathname pointer itself is returned. 102 **/ 103 U_CAPI const char * U_EXPORT2 104 findBasename(const char *filename); 105 106 /** 107 * Find the directory name of a pathname, that is, everything 108 * up to but not including the last file separator. 109 * 110 * If successful, copies the directory name into the output buffer along with 111 * a terminating NULL. 112 * 113 * If there isn't a directory name in the path, it returns an empty string. 114 * @param path the full pathname to inspect. 115 * @param buffer the output buffer 116 * @param bufLen the output buffer length 117 * @param status error code- may return U_BUFFER_OVERFLOW_ERROR if bufLen is too small. 118 * @return If successful, a pointer to the output buffer. If failure or bufLen is too small, NULL. 119 **/ 120 U_CAPI const char * U_EXPORT2 121 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status); 122 123 /* 124 * Return the current year in the Gregorian calendar. Used for copyright generation. 125 */ 126 U_CAPI int32_t U_EXPORT2 127 getCurrentYear(void); 128 129 /* 130 * Creates a directory with pathname. 131 * 132 * @param status Set to an error code when mkdir failed. 133 */ 134 U_CAPI void U_EXPORT2 135 uprv_mkdir(const char *pathname, UErrorCode *status); 136 137 #if !UCONFIG_NO_FILE_IO 138 /** 139 * Return true if the named item exists 140 * @param file filename 141 * @return true if named item (file, dir, etc) exists, false otherwise 142 */ 143 U_CAPI UBool U_EXPORT2 144 uprv_fileExists(const char *file); 145 #endif 146 147 /** 148 * Performs a golden data test. Asserts that the contents of the buffer is equal 149 * to the data in goldenFilePath. 150 * 151 * Pass the value of the -G flag to "overwrite"; if true, new goldens will be 152 * written to the filesystem. 153 * 154 * @return The first index at which the files differ, or -1 if they are the same. 155 */ 156 U_CAPI int32_t U_EXPORT2 157 uprv_compareGoldenFiles( 158 const char* buffer, int32_t bufferLen, 159 const char* goldenFilePath, 160 bool overwrite); 161 162 /** 163 * Return the modification date for the specified file or directory. 164 * Return value is undefined if there was an error. 165 */ 166 /*U_CAPI UDate U_EXPORT2 167 uprv_getModificationDate(const char *pathname, UErrorCode *status); 168 */ 169 /* 170 * Returns the modification 171 * 172 * @param status Set to an error code when mkdir failed. 173 */ 174 175 /* 176 * UToolMemory is used for generic, custom memory management. 177 * It is allocated with enough space for count*size bytes starting 178 * at array. 179 * The array is declared with a union of large data types so 180 * that its base address is aligned for any types. 181 * If size is a multiple of a data type size, then such items 182 * can be safely allocated inside the array, at offsets that 183 * are themselves multiples of size. 184 */ 185 struct UToolMemory; 186 typedef struct UToolMemory UToolMemory; 187 188 /** 189 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity 190 * items with size bytes each. 191 */ 192 U_CAPI UToolMemory * U_EXPORT2 193 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size); 194 195 /** 196 * Close a UToolMemory object. 197 */ 198 U_CAPI void U_EXPORT2 199 utm_close(UToolMemory *mem); 200 201 /** 202 * Get the pointer to the beginning of the array of items. 203 * The pointer becomes invalid after allocation of new items. 204 */ 205 U_CAPI void * U_EXPORT2 206 utm_getStart(UToolMemory *mem); 207 208 /** 209 * Get the current number of items. 210 */ 211 U_CAPI int32_t U_EXPORT2 212 utm_countItems(UToolMemory *mem); 213 214 /** 215 * Allocate one more item and return the pointer to its start in the array. 216 */ 217 U_CAPI void * U_EXPORT2 218 utm_alloc(UToolMemory *mem); 219 220 /** 221 * Allocate n items and return the pointer to the start of the first one in the array. 222 */ 223 U_CAPI void * U_EXPORT2 224 utm_allocN(UToolMemory *mem, int32_t n); 225 226 #endif 227