2 *******************************************************************************
4 * Copyright (C) 1999-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: toolutil.c
10 * tab size: 8 (not used)
13 * created on: 1999nov19
14 * created by: Markus W. Scherer
16 * 6/25/08 - Added Cygwin specific code in uprv_mkdir - Brian Rower
18 * This file contains utility functions for ICU tools like genccode.
21 #if U_PLATFORM == U_PF_MINGW
22 // *cough* - for struct stat
23 #ifdef __STRICT_ANSI__
24 #undef __STRICT_ANSI__
30 #include "unicode/utypes.h"
32 #ifndef U_TOOLUTIL_IMPLEMENTATION
33 #error U_TOOLUTIL_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see http://userguide.icu-project.org/howtouseicu
36 #if U_PLATFORM_USES_ONLY_WIN32_API
38 # define WIN32_LEAN_AND_MEAN
43 # if U_PLATFORM == U_PF_MINGW
44 # define __NO_MINGW_LFS /* gets around missing 'off64_t' */
49 # include <sys/stat.h>
50 # include <sys/types.h>
53 /* In MinGW environment, io.h needs to be included for _mkdir() */
54 #if U_PLATFORM == U_PF_MINGW
60 #include "unicode/errorcode.h"
61 #include "unicode/putil.h"
65 #include "unicode/ucal.h"
69 IcuToolErrorCode::~IcuToolErrorCode() {
70 // Safe because our handleFailure() does not throw exceptions.
71 if(isFailure()) { handleFailure(); }
74 void IcuToolErrorCode::handleFailure() const {
75 fprintf(stderr, "error at %s: %s\n", location, errorName());
81 static int32_t currentYear = -1;
83 U_CAPI int32_t U_EXPORT2 getCurrentYear() {
84 #if !UCONFIG_NO_FORMATTING
85 UErrorCode status=U_ZERO_ERROR;
86 UCalendar *cal = NULL;
88 if(currentYear == -1) {
89 cal = ucal_open(NULL, -1, NULL, UCAL_TRADITIONAL, &status);
90 ucal_setMillis(cal, ucal_getNow(), &status);
91 currentYear = ucal_get(cal, UCAL_YEAR, &status);
95 /* No formatting- no way to set the current year. */
101 U_CAPI const char * U_EXPORT2
102 getLongPathname(const char *pathname) {
103 #if U_PLATFORM_USES_ONLY_WIN32_API
104 /* anticipate problems with "short" pathnames */
105 static WIN32_FIND_DATAA info;
106 HANDLE file=FindFirstFileA(pathname, &info);
107 if(file!=INVALID_HANDLE_VALUE) {
108 if(info.cAlternateFileName[0]!=0) {
109 /* this file has a short name, get and use the long one */
110 const char *basename=findBasename(pathname);
111 if(basename!=pathname) {
112 /* prepend the long filename with the original path */
113 uprv_memmove(info.cFileName+(basename-pathname), info.cFileName, uprv_strlen(info.cFileName)+1);
114 uprv_memcpy(info.cFileName, pathname, basename-pathname);
116 pathname=info.cFileName;
124 U_CAPI const char * U_EXPORT2
125 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) {
126 if(U_FAILURE(*status)) return NULL;
127 const char *resultPtr = NULL;
128 int32_t resultLen = 0;
130 const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
131 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
132 const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR);
133 if(basenameAlt && (!basename || basename<basenameAlt)) {
134 basename = basenameAlt;
138 /* no basename - return ''. */
143 resultLen = basename - path;
145 resultLen = 1; /* '/' or '/a' -> '/' */
149 if((resultLen+1) <= bufLen) {
150 uprv_strncpy(buffer, resultPtr, resultLen);
154 *status = U_BUFFER_OVERFLOW_ERROR;
159 U_CAPI const char * U_EXPORT2
160 findBasename(const char *filename) {
161 const char *basename=uprv_strrchr(filename, U_FILE_SEP_CHAR);
163 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
165 /* Use lenient matching on Windows, which can accept either \ or /
166 This is useful for environments like Win32+CygWin which have both.
168 basename=uprv_strrchr(filename, U_FILE_ALT_SEP_CHAR);
179 U_CAPI void U_EXPORT2
180 uprv_mkdir(const char *pathname, UErrorCode *status) {
183 #if U_PLATFORM_USES_ONLY_WIN32_API
184 retVal = _mkdir(pathname);
186 retVal = mkdir(pathname, S_IRWXU | (S_IROTH | S_IXOTH) | (S_IROTH | S_IXOTH));
188 if (retVal && errno != EEXIST) {
189 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
190 /*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
191 /* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
194 if(stat(pathname,&st) != 0)
196 *status = U_FILE_ACCESS_ERROR;
199 *status = U_FILE_ACCESS_ERROR;
204 #if !UCONFIG_NO_FILE_IO
205 U_CAPI UBool U_EXPORT2
206 uprv_fileExists(const char *file) {
207 struct stat stat_buf;
208 if (stat(file, &stat_buf) == 0) {
216 /*U_CAPI UDate U_EXPORT2
217 uprv_getModificationDate(const char *pathname, UErrorCode *status)
219 if(U_FAILURE(*status)) {
222 // TODO: handle case where stat is not available
225 if(stat(pathname,&st) != 0)
227 *status = U_FILE_ACCESS_ERROR;
234 /* tool memory helper ------------------------------------------------------- */
238 int32_t capacity, maxCapacity, size, idx;
240 UAlignedMemory staticArray[1];
243 U_CAPI UToolMemory * U_EXPORT2
244 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size) {
247 if(maxCapacity<initialCapacity) {
248 maxCapacity=initialCapacity;
251 mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+initialCapacity*size);
253 fprintf(stderr, "error: %s - out of memory\n", name);
254 exit(U_MEMORY_ALLOCATION_ERROR);
256 mem->array=mem->staticArray;
258 uprv_strcpy(mem->name, name);
259 mem->capacity=initialCapacity;
260 mem->maxCapacity=maxCapacity;
266 U_CAPI void U_EXPORT2
267 utm_close(UToolMemory *mem) {
269 if(mem->array!=mem->staticArray) {
270 uprv_free(mem->array);
277 U_CAPI void * U_EXPORT2
278 utm_getStart(UToolMemory *mem) {
279 return (char *)mem->array;
282 U_CAPI int32_t U_EXPORT2
283 utm_countItems(UToolMemory *mem) {
289 utm_hasCapacity(UToolMemory *mem, int32_t capacity) {
290 if(mem->capacity<capacity) {
293 if(mem->maxCapacity<capacity) {
294 fprintf(stderr, "error: %s - trying to use more than maxCapacity=%ld units\n",
295 mem->name, (long)mem->maxCapacity);
296 exit(U_MEMORY_ALLOCATION_ERROR);
299 /* try to allocate a larger array */
300 if(capacity>=2*mem->capacity) {
301 newCapacity=capacity;
302 } else if(mem->capacity<=mem->maxCapacity/3) {
303 newCapacity=2*mem->capacity;
305 newCapacity=mem->maxCapacity;
308 if(mem->array==mem->staticArray) {
309 mem->array=uprv_malloc(newCapacity*mem->size);
310 if(mem->array!=NULL) {
311 uprv_memcpy(mem->array, mem->staticArray, mem->idx*mem->size);
314 mem->array=uprv_realloc(mem->array, newCapacity*mem->size);
317 if(mem->array==NULL) {
318 fprintf(stderr, "error: %s - out of memory\n", mem->name);
319 exit(U_MEMORY_ALLOCATION_ERROR);
321 mem->capacity=newCapacity;
327 U_CAPI void * U_EXPORT2
328 utm_alloc(UToolMemory *mem) {
330 int32_t oldIndex=mem->idx;
331 int32_t newIndex=oldIndex+1;
332 if(utm_hasCapacity(mem, newIndex)) {
333 p=(char *)mem->array+oldIndex*mem->size;
335 uprv_memset(p, 0, mem->size);
340 U_CAPI void * U_EXPORT2
341 utm_allocN(UToolMemory *mem, int32_t n) {
343 int32_t oldIndex=mem->idx;
344 int32_t newIndex=oldIndex+n;
345 if(utm_hasCapacity(mem, newIndex)) {
346 p=(char *)mem->array+oldIndex*mem->size;
348 uprv_memset(p, 0, n*mem->size);