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