toolutil.h 5.2 KB

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