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