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 27 #ifdef __cplusplus 28 29 #include "unicode/errorcode.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; 46 private: 47 const char *location; 48 }; 49 50 U_NAMESPACE_END 51 52 #endif 53 54 /* 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. 62 * 63 * On platforms other than Windows, this function always returns 64 * the input pathname pointer. 65 * 66 * This function is especially useful in tools that are called 67 * by a batch file for loop, which yields short pathnames on Win9x. 68 */ 69 U_CAPI const char * U_EXPORT2 70 getLongPathname(const char *pathname); 71 72 /** 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. 78 **/ 79 U_CAPI const char * U_EXPORT2 80 findBasename(const char *filename); 81 82 /** 83 * Find the directory name of a pathname, that is, everything 84 * up to but not including the last file separator. 85 * 86 * If successful, copies the directory name into the output buffer along with 87 * a terminating NULL. 88 * 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. 95 **/ 96 U_CAPI const char * U_EXPORT2 97 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status); 98 99 /* 100 * Return the current year in the Gregorian calendar. Used for copyright generation. 101 */ 102 U_CAPI int32_t U_EXPORT2 103 getCurrentYear(void); 104 105 /* 106 * Creates a directory with pathname. 107 * 108 * @param status Set to an error code when mkdir failed. 109 */ 110 U_CAPI void U_EXPORT2 111 uprv_mkdir(const char *pathname, UErrorCode *status); 112 113 #if !UCONFIG_NO_FILE_IO 114 /** 115 * Return TRUE if the named item exists 116 * @param file filename 117 * @return TRUE if named item (file, dir, etc) exists, FALSE otherwise 118 */ 119 U_CAPI UBool U_EXPORT2 120 uprv_fileExists(const char *file); 121 #endif 122 123 /** 124 * Return the modification date for the specified file or directory. 125 * Return value is undefined if there was an error. 126 */ 127 /*U_CAPI UDate U_EXPORT2 128 uprv_getModificationDate(const char *pathname, UErrorCode *status); 129 */ 130 /* 131 * Returns the modification 132 * 133 * @param status Set to an error code when mkdir failed. 134 */ 135 136 /* 137 * UToolMemory is used for generic, custom memory management. 138 * It is allocated with enough space for count*size bytes starting 139 * at array. 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. 145 */ 146 struct UToolMemory; 147 typedef struct UToolMemory UToolMemory; 148 149 /** 150 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity 151 * items with size bytes each. 152 */ 153 U_CAPI UToolMemory * U_EXPORT2 154 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size); 155 156 /** 157 * Close a UToolMemory object. 158 */ 159 U_CAPI void U_EXPORT2 160 utm_close(UToolMemory *mem); 161 162 /** 163 * Get the pointer to the beginning of the array of items. 164 * The pointer becomes invalid after allocation of new items. 165 */ 166 U_CAPI void * U_EXPORT2 167 utm_getStart(UToolMemory *mem); 168 169 /** 170 * Get the current number of items. 171 */ 172 U_CAPI int32_t U_EXPORT2 173 utm_countItems(UToolMemory *mem); 174 175 /** 176 * Allocate one more item and return the pointer to its start in the array. 177 */ 178 U_CAPI void * U_EXPORT2 179 utm_alloc(UToolMemory *mem); 180 181 /** 182 * Allocate n items and return the pointer to the start of the first one in the array. 183 */ 184 U_CAPI void * U_EXPORT2 185 utm_allocN(UToolMemory *mem, int32_t n); 186 187 #endif 188