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; 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 * Return the modification date for the specified file or directory. 131 * Return value is undefined if there was an error. 132 */ 133 /*U_CAPI UDate U_EXPORT2 134 uprv_getModificationDate(const char *pathname, UErrorCode *status); 135 */ 136 /* 137 * Returns the modification 138 * 139 * @param status Set to an error code when mkdir failed. 140 */ 141 142 /* 143 * UToolMemory is used for generic, custom memory management. 144 * It is allocated with enough space for count*size bytes starting 145 * at array. 146 * The array is declared with a union of large data types so 147 * that its base address is aligned for any types. 148 * If size is a multiple of a data type size, then such items 149 * can be safely allocated inside the array, at offsets that 150 * are themselves multiples of size. 151 */ 152 struct UToolMemory; 153 typedef struct UToolMemory UToolMemory; 154 155 /** 156 * Open a UToolMemory object for allocation of initialCapacity to maxCapacity 157 * items with size bytes each. 158 */ 159 U_CAPI UToolMemory * U_EXPORT2 160 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size); 161 162 /** 163 * Close a UToolMemory object. 164 */ 165 U_CAPI void U_EXPORT2 166 utm_close(UToolMemory *mem); 167 168 /** 169 * Get the pointer to the beginning of the array of items. 170 * The pointer becomes invalid after allocation of new items. 171 */ 172 U_CAPI void * U_EXPORT2 173 utm_getStart(UToolMemory *mem); 174 175 /** 176 * Get the current number of items. 177 */ 178 U_CAPI int32_t U_EXPORT2 179 utm_countItems(UToolMemory *mem); 180 181 /** 182 * Allocate one more item and return the pointer to its start in the array. 183 */ 184 U_CAPI void * U_EXPORT2 185 utm_alloc(UToolMemory *mem); 186 187 /** 188 * Allocate n items and return the pointer to the start of the first one in the array. 189 */ 190 U_CAPI void * U_EXPORT2 191 utm_allocN(UToolMemory *mem, int32_t n); 192 193 #endif 194