1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1999-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: toolutil.c
12 * tab size: 8 (not used)
15 * created on: 1999nov19
16 * created by: Markus W. Scherer
18 * 6/25/08 - Added Cygwin specific code in uprv_mkdir - Brian Rower
20 * This file contains utility functions for ICU tools like genccode.
23 #include "unicode/platform.h"
24 #if U_PLATFORM == U_PF_MINGW
25 // *cough* - for struct stat
26 #ifdef __STRICT_ANSI__
27 #undef __STRICT_ANSI__
33 #include "unicode/utypes.h"
35 #ifndef U_TOOLUTIL_IMPLEMENTATION
36 #error U_TOOLUTIL_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see http://userguide.icu-project.org/howtouseicu
39 #if U_PLATFORM_USES_ONLY_WIN32_API
41 # define WIN32_LEAN_AND_MEAN
46 # if U_PLATFORM == U_PF_MINGW
47 # define __NO_MINGW_LFS /* gets around missing 'off64_t' */
52 # include <sys/stat.h>
53 # include <sys/types.h>
56 /* In MinGW environment, io.h needs to be included for _mkdir() */
57 #if U_PLATFORM == U_PF_MINGW
63 #include "unicode/errorcode.h"
64 #include "unicode/putil.h"
68 #include "unicode/ucal.h"
72 IcuToolErrorCode::~IcuToolErrorCode() {
73 // Safe because our handleFailure() does not throw exceptions.
74 if(isFailure()) { handleFailure(); }
77 void IcuToolErrorCode::handleFailure() const {
78 fprintf(stderr, "error at %s: %s\n", location, errorName());
84 static int32_t currentYear = -1;
86 U_CAPI int32_t U_EXPORT2 getCurrentYear() {
87 #if !UCONFIG_NO_FORMATTING
88 UErrorCode status=U_ZERO_ERROR;
89 UCalendar *cal = NULL;
91 if(currentYear == -1) {
92 cal = ucal_open(NULL, -1, NULL, UCAL_TRADITIONAL, &status);
93 ucal_setMillis(cal, ucal_getNow(), &status);
94 currentYear = ucal_get(cal, UCAL_YEAR, &status);
98 /* No formatting- no way to set the current year. */
104 U_CAPI const char * U_EXPORT2
105 getLongPathname(const char *pathname) {
106 #if U_PLATFORM_USES_ONLY_WIN32_API
107 /* anticipate problems with "short" pathnames */
108 static WIN32_FIND_DATAA info;
109 HANDLE file=FindFirstFileA(pathname, &info);
110 if(file!=INVALID_HANDLE_VALUE) {
111 if(info.cAlternateFileName[0]!=0) {
112 /* this file has a short name, get and use the long one */
113 const char *basename=findBasename(pathname);
114 if(basename!=pathname) {
115 /* prepend the long filename with the original path */
116 uprv_memmove(info.cFileName+(basename-pathname), info.cFileName, uprv_strlen(info.cFileName)+1);
117 uprv_memcpy(info.cFileName, pathname, basename-pathname);
119 pathname=info.cFileName;
127 U_CAPI const char * U_EXPORT2
128 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) {
129 if(U_FAILURE(*status)) return NULL;
130 const char *resultPtr = NULL;
131 int32_t resultLen = 0;
133 const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
134 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
135 const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR);
136 if(basenameAlt && (!basename || basename<basenameAlt)) {
137 basename = basenameAlt;
141 /* no basename - return ''. */
146 resultLen = basename - path;
148 resultLen = 1; /* '/' or '/a' -> '/' */
152 if((resultLen+1) <= bufLen) {
153 uprv_strncpy(buffer, resultPtr, resultLen);
157 *status = U_BUFFER_OVERFLOW_ERROR;
162 U_CAPI const char * U_EXPORT2
163 findBasename(const char *filename) {
164 const char *basename=uprv_strrchr(filename, U_FILE_SEP_CHAR);
166 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
167 #if !(U_PLATFORM == U_PF_CYGWIN && U_PLATFORM_USES_ONLY_WIN32_API)
171 /* Use lenient matching on Windows, which can accept either \ or /
172 This is useful for environments like Win32+CygWin which have both.
174 basename=uprv_strrchr(filename, U_FILE_ALT_SEP_CHAR);
185 U_CAPI void U_EXPORT2
186 uprv_mkdir(const char *pathname, UErrorCode *status) {
189 #if U_PLATFORM_USES_ONLY_WIN32_API
190 retVal = _mkdir(pathname);
192 retVal = mkdir(pathname, S_IRWXU | (S_IROTH | S_IXOTH) | (S_IROTH | S_IXOTH));
194 if (retVal && errno != EEXIST) {
195 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
196 /*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
197 /* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
200 if(stat(pathname,&st) != 0)
202 *status = U_FILE_ACCESS_ERROR;
205 *status = U_FILE_ACCESS_ERROR;
210 #if !UCONFIG_NO_FILE_IO
211 U_CAPI UBool U_EXPORT2
212 uprv_fileExists(const char *file) {
213 struct stat stat_buf;
214 if (stat(file, &stat_buf) == 0) {
222 /*U_CAPI UDate U_EXPORT2
223 uprv_getModificationDate(const char *pathname, UErrorCode *status)
225 if(U_FAILURE(*status)) {
228 // TODO: handle case where stat is not available
231 if(stat(pathname,&st) != 0)
233 *status = U_FILE_ACCESS_ERROR;
240 /* tool memory helper ------------------------------------------------------- */
244 int32_t capacity, maxCapacity, size, idx;
246 UAlignedMemory staticArray[1];
249 U_CAPI UToolMemory * U_EXPORT2
250 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size) {
253 if(maxCapacity<initialCapacity) {
254 maxCapacity=initialCapacity;
257 mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+initialCapacity*size);
259 fprintf(stderr, "error: %s - out of memory\n", name);
260 exit(U_MEMORY_ALLOCATION_ERROR);
262 mem->array=mem->staticArray;
264 uprv_strcpy(mem->name, name);
265 mem->capacity=initialCapacity;
266 mem->maxCapacity=maxCapacity;
272 U_CAPI void U_EXPORT2
273 utm_close(UToolMemory *mem) {
275 if(mem->array!=mem->staticArray) {
276 uprv_free(mem->array);
283 U_CAPI void * U_EXPORT2
284 utm_getStart(UToolMemory *mem) {
285 return (char *)mem->array;
288 U_CAPI int32_t U_EXPORT2
289 utm_countItems(UToolMemory *mem) {
295 utm_hasCapacity(UToolMemory *mem, int32_t capacity) {
296 if(mem->capacity<capacity) {
299 if(mem->maxCapacity<capacity) {
300 fprintf(stderr, "error: %s - trying to use more than maxCapacity=%ld units\n",
301 mem->name, (long)mem->maxCapacity);
302 exit(U_MEMORY_ALLOCATION_ERROR);
305 /* try to allocate a larger array */
306 if(capacity>=2*mem->capacity) {
307 newCapacity=capacity;
308 } else if(mem->capacity<=mem->maxCapacity/3) {
309 newCapacity=2*mem->capacity;
311 newCapacity=mem->maxCapacity;
314 if(mem->array==mem->staticArray) {
315 mem->array=uprv_malloc(newCapacity*mem->size);
316 if(mem->array!=NULL) {
317 uprv_memcpy(mem->array, mem->staticArray, (size_t)mem->idx*mem->size);
320 mem->array=uprv_realloc(mem->array, newCapacity*mem->size);
323 if(mem->array==NULL) {
324 fprintf(stderr, "error: %s - out of memory\n", mem->name);
325 exit(U_MEMORY_ALLOCATION_ERROR);
327 mem->capacity=newCapacity;
333 U_CAPI void * U_EXPORT2
334 utm_alloc(UToolMemory *mem) {
336 int32_t oldIndex=mem->idx;
337 int32_t newIndex=oldIndex+1;
338 if(utm_hasCapacity(mem, newIndex)) {
339 p=(char *)mem->array+oldIndex*mem->size;
341 uprv_memset(p, 0, mem->size);
346 U_CAPI void * U_EXPORT2
347 utm_allocN(UToolMemory *mem, int32_t n) {
349 int32_t oldIndex=mem->idx;
350 int32_t newIndex=oldIndex+n;
351 if(utm_hasCapacity(mem, newIndex)) {
352 p=(char *)mem->array+oldIndex*mem->size;
354 uprv_memset(p, 0, n*mem->size);