michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: toolutil.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 1999nov19 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * This file defines utility functions for ICU tools like genccode. michael@0: */ michael@0: michael@0: #ifndef __TOOLUTIL_H__ michael@0: #define __TOOLUTIL_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: michael@0: #ifdef __cplusplus michael@0: michael@0: #include "unicode/errorcode.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * ErrorCode subclass for use in ICU command-line tools. michael@0: * The destructor calls handleFailure() which calls exit(errorCode) when isFailure(). michael@0: */ michael@0: class U_TOOLUTIL_API IcuToolErrorCode : public ErrorCode { michael@0: public: michael@0: /** michael@0: * @param loc A short string describing where the IcuToolErrorCode is used. michael@0: */ michael@0: IcuToolErrorCode(const char *loc) : location(loc) {} michael@0: virtual ~IcuToolErrorCode(); michael@0: protected: michael@0: virtual void handleFailure() const; michael@0: private: michael@0: const char *location; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: * For Windows, a path/filename may be the short (8.3) version michael@0: * of the "real", long one. In this case, the short one michael@0: * is abbreviated and contains a tilde etc. michael@0: * This function returns a pointer to the original pathname michael@0: * if it is the "real" one itself, and a pointer to a static michael@0: * buffer (not thread-safe) containing the long version michael@0: * if the pathname is indeed abbreviated. michael@0: * michael@0: * On platforms other than Windows, this function always returns michael@0: * the input pathname pointer. michael@0: * michael@0: * This function is especially useful in tools that are called michael@0: * by a batch file for loop, which yields short pathnames on Win9x. michael@0: */ michael@0: U_CAPI const char * U_EXPORT2 michael@0: getLongPathname(const char *pathname); michael@0: michael@0: /** michael@0: * Find the basename at the end of a pathname, i.e., the part michael@0: * after the last file separator, and return a pointer michael@0: * to this part of the pathname. michael@0: * If the pathname only contains a basename and no file separator, michael@0: * then the pathname pointer itself is returned. michael@0: **/ michael@0: U_CAPI const char * U_EXPORT2 michael@0: findBasename(const char *filename); michael@0: michael@0: /** michael@0: * Find the directory name of a pathname, that is, everything michael@0: * up to but not including the last file separator. michael@0: * michael@0: * If successful, copies the directory name into the output buffer along with michael@0: * a terminating NULL. michael@0: * michael@0: * If there isn't a directory name in the path, it returns an empty string. michael@0: * @param path the full pathname to inspect. michael@0: * @param buffer the output buffer michael@0: * @param bufLen the output buffer length michael@0: * @param status error code- may return U_BUFFER_OVERFLOW_ERROR if bufLen is too small. michael@0: * @return If successful, a pointer to the output buffer. If failure or bufLen is too small, NULL. michael@0: **/ michael@0: U_CAPI const char * U_EXPORT2 michael@0: findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status); michael@0: michael@0: /* michael@0: * Return the current year in the Gregorian calendar. Used for copyright generation. michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: getCurrentYear(void); michael@0: michael@0: /* michael@0: * Creates a directory with pathname. michael@0: * michael@0: * @param status Set to an error code when mkdir failed. michael@0: */ michael@0: U_CAPI void U_EXPORT2 michael@0: uprv_mkdir(const char *pathname, UErrorCode *status); michael@0: michael@0: #if !UCONFIG_NO_FILE_IO michael@0: /** michael@0: * Return TRUE if the named item exists michael@0: * @param file filename michael@0: * @return TRUE if named item (file, dir, etc) exists, FALSE otherwise michael@0: */ michael@0: U_CAPI UBool U_EXPORT2 michael@0: uprv_fileExists(const char *file); michael@0: #endif michael@0: michael@0: /** michael@0: * Return the modification date for the specified file or directory. michael@0: * Return value is undefined if there was an error. michael@0: */ michael@0: /*U_CAPI UDate U_EXPORT2 michael@0: uprv_getModificationDate(const char *pathname, UErrorCode *status); michael@0: */ michael@0: /* michael@0: * Returns the modification michael@0: * michael@0: * @param status Set to an error code when mkdir failed. michael@0: */ michael@0: michael@0: /* michael@0: * UToolMemory is used for generic, custom memory management. michael@0: * It is allocated with enough space for count*size bytes starting michael@0: * at array. michael@0: * The array is declared with a union of large data types so michael@0: * that its base address is aligned for any types. michael@0: * If size is a multiple of a data type size, then such items michael@0: * can be safely allocated inside the array, at offsets that michael@0: * are themselves multiples of size. michael@0: */ michael@0: struct UToolMemory; michael@0: typedef struct UToolMemory UToolMemory; michael@0: michael@0: /** michael@0: * Open a UToolMemory object for allocation of initialCapacity to maxCapacity michael@0: * items with size bytes each. michael@0: */ michael@0: U_CAPI UToolMemory * U_EXPORT2 michael@0: utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size); michael@0: michael@0: /** michael@0: * Close a UToolMemory object. michael@0: */ michael@0: U_CAPI void U_EXPORT2 michael@0: utm_close(UToolMemory *mem); michael@0: michael@0: /** michael@0: * Get the pointer to the beginning of the array of items. michael@0: * The pointer becomes invalid after allocation of new items. michael@0: */ michael@0: U_CAPI void * U_EXPORT2 michael@0: utm_getStart(UToolMemory *mem); michael@0: michael@0: /** michael@0: * Get the current number of items. michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: utm_countItems(UToolMemory *mem); michael@0: michael@0: /** michael@0: * Allocate one more item and return the pointer to its start in the array. michael@0: */ michael@0: U_CAPI void * U_EXPORT2 michael@0: utm_alloc(UToolMemory *mem); michael@0: michael@0: /** michael@0: * Allocate n items and return the pointer to the start of the first one in the array. michael@0: */ michael@0: U_CAPI void * U_EXPORT2 michael@0: utm_allocN(UToolMemory *mem, int32_t n); michael@0: michael@0: #endif