2 *******************************************************************************
4 * Copyright (C) 1999-2014, 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 #include "unicode/platform.h"
22 #if U_PLATFORM == U_PF_MINGW
23 // *cough* - for struct stat
24 #ifdef __STRICT_ANSI__
25 #undef __STRICT_ANSI__
31 #include "unicode/utypes.h"
33 #ifndef U_TOOLUTIL_IMPLEMENTATION
34 #error U_TOOLUTIL_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see http://userguide.icu-project.org/howtouseicu
37 #if U_PLATFORM_USES_ONLY_WIN32_API
39 # define WIN32_LEAN_AND_MEAN
44 # if U_PLATFORM == U_PF_MINGW
45 # define __NO_MINGW_LFS /* gets around missing 'off64_t' */
50 # include <sys/stat.h>
51 # include <sys/types.h>
54 /* In MinGW environment, io.h needs to be included for _mkdir() */
55 #if U_PLATFORM == U_PF_MINGW
61 #include "unicode/errorcode.h"
62 #include "unicode/putil.h"
66 #include "unicode/ucal.h"
70 IcuToolErrorCode::~IcuToolErrorCode() {
71 // Safe because our handleFailure() does not throw exceptions.
72 if(isFailure()) { handleFailure(); }
75 void IcuToolErrorCode::handleFailure() const {
76 fprintf(stderr
, "error at %s: %s\n", location
, errorName());
82 static int32_t currentYear
= -1;
84 U_CAPI
int32_t U_EXPORT2
getCurrentYear() {
85 #if !UCONFIG_NO_FORMATTING
86 UErrorCode status
=U_ZERO_ERROR
;
87 UCalendar
*cal
= NULL
;
89 if(currentYear
== -1) {
90 cal
= ucal_open(NULL
, -1, NULL
, UCAL_TRADITIONAL
, &status
);
91 ucal_setMillis(cal
, ucal_getNow(), &status
);
92 currentYear
= ucal_get(cal
, UCAL_YEAR
, &status
);
96 /* No formatting- no way to set the current year. */
102 U_CAPI
const char * U_EXPORT2
103 getLongPathname(const char *pathname
) {
104 #if U_PLATFORM_USES_ONLY_WIN32_API
105 /* anticipate problems with "short" pathnames */
106 static WIN32_FIND_DATAA info
;
107 HANDLE file
=FindFirstFileA(pathname
, &info
);
108 if(file
!=INVALID_HANDLE_VALUE
) {
109 if(info
.cAlternateFileName
[0]!=0) {
110 /* this file has a short name, get and use the long one */
111 const char *basename
=findBasename(pathname
);
112 if(basename
!=pathname
) {
113 /* prepend the long filename with the original path */
114 uprv_memmove(info
.cFileName
+(basename
-pathname
), info
.cFileName
, uprv_strlen(info
.cFileName
)+1);
115 uprv_memcpy(info
.cFileName
, pathname
, basename
-pathname
);
117 pathname
=info
.cFileName
;
125 U_CAPI
const char * U_EXPORT2
126 findDirname(const char *path
, char *buffer
, int32_t bufLen
, UErrorCode
* status
) {
127 if(U_FAILURE(*status
)) return NULL
;
128 const char *resultPtr
= NULL
;
129 int32_t resultLen
= 0;
131 const char *basename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
132 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
133 const char *basenameAlt
=uprv_strrchr(path
, U_FILE_ALT_SEP_CHAR
);
134 if(basenameAlt
&& (!basename
|| basename
<basenameAlt
)) {
135 basename
= basenameAlt
;
139 /* no basename - return ''. */
144 resultLen
= basename
- path
;
146 resultLen
= 1; /* '/' or '/a' -> '/' */
150 if((resultLen
+1) <= bufLen
) {
151 uprv_strncpy(buffer
, resultPtr
, resultLen
);
155 *status
= U_BUFFER_OVERFLOW_ERROR
;
160 U_CAPI
const char * U_EXPORT2
161 findBasename(const char *filename
) {
162 const char *basename
=uprv_strrchr(filename
, U_FILE_SEP_CHAR
);
164 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
165 #if !(U_PLATFORM == U_PF_CYGWIN && U_PLATFORM_USES_ONLY_WIN32_API)
169 /* Use lenient matching on Windows, which can accept either \ or /
170 This is useful for environments like Win32+CygWin which have both.
172 basename
=uprv_strrchr(filename
, U_FILE_ALT_SEP_CHAR
);
183 U_CAPI
void U_EXPORT2
184 uprv_mkdir(const char *pathname
, UErrorCode
*status
) {
187 #if U_PLATFORM_USES_ONLY_WIN32_API
188 retVal
= _mkdir(pathname
);
190 retVal
= mkdir(pathname
, S_IRWXU
| (S_IROTH
| S_IXOTH
) | (S_IROTH
| S_IXOTH
));
192 if (retVal
&& errno
!= EEXIST
) {
193 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
194 /*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
195 /* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
198 if(stat(pathname
,&st
) != 0)
200 *status
= U_FILE_ACCESS_ERROR
;
203 *status
= U_FILE_ACCESS_ERROR
;
208 #if !UCONFIG_NO_FILE_IO
209 U_CAPI UBool U_EXPORT2
210 uprv_fileExists(const char *file
) {
211 struct stat stat_buf
;
212 if (stat(file
, &stat_buf
) == 0) {
220 /*U_CAPI UDate U_EXPORT2
221 uprv_getModificationDate(const char *pathname, UErrorCode *status)
223 if(U_FAILURE(*status)) {
226 // TODO: handle case where stat is not available
229 if(stat(pathname,&st) != 0)
231 *status = U_FILE_ACCESS_ERROR;
238 /* tool memory helper ------------------------------------------------------- */
242 int32_t capacity
, maxCapacity
, size
, idx
;
244 UAlignedMemory staticArray
[1];
247 U_CAPI UToolMemory
* U_EXPORT2
248 utm_open(const char *name
, int32_t initialCapacity
, int32_t maxCapacity
, int32_t size
) {
251 if(maxCapacity
<initialCapacity
) {
252 maxCapacity
=initialCapacity
;
255 mem
=(UToolMemory
*)uprv_malloc(sizeof(UToolMemory
)+initialCapacity
*size
);
257 fprintf(stderr
, "error: %s - out of memory\n", name
);
258 exit(U_MEMORY_ALLOCATION_ERROR
);
260 mem
->array
=mem
->staticArray
;
262 uprv_strcpy(mem
->name
, name
);
263 mem
->capacity
=initialCapacity
;
264 mem
->maxCapacity
=maxCapacity
;
270 U_CAPI
void U_EXPORT2
271 utm_close(UToolMemory
*mem
) {
273 if(mem
->array
!=mem
->staticArray
) {
274 uprv_free(mem
->array
);
281 U_CAPI
void * U_EXPORT2
282 utm_getStart(UToolMemory
*mem
) {
283 return (char *)mem
->array
;
286 U_CAPI
int32_t U_EXPORT2
287 utm_countItems(UToolMemory
*mem
) {
293 utm_hasCapacity(UToolMemory
*mem
, int32_t capacity
) {
294 if(mem
->capacity
<capacity
) {
297 if(mem
->maxCapacity
<capacity
) {
298 fprintf(stderr
, "error: %s - trying to use more than maxCapacity=%ld units\n",
299 mem
->name
, (long)mem
->maxCapacity
);
300 exit(U_MEMORY_ALLOCATION_ERROR
);
303 /* try to allocate a larger array */
304 if(capacity
>=2*mem
->capacity
) {
305 newCapacity
=capacity
;
306 } else if(mem
->capacity
<=mem
->maxCapacity
/3) {
307 newCapacity
=2*mem
->capacity
;
309 newCapacity
=mem
->maxCapacity
;
312 if(mem
->array
==mem
->staticArray
) {
313 mem
->array
=uprv_malloc(newCapacity
*mem
->size
);
314 if(mem
->array
!=NULL
) {
315 uprv_memcpy(mem
->array
, mem
->staticArray
, mem
->idx
*mem
->size
);
318 mem
->array
=uprv_realloc(mem
->array
, newCapacity
*mem
->size
);
321 if(mem
->array
==NULL
) {
322 fprintf(stderr
, "error: %s - out of memory\n", mem
->name
);
323 exit(U_MEMORY_ALLOCATION_ERROR
);
325 mem
->capacity
=newCapacity
;
331 U_CAPI
void * U_EXPORT2
332 utm_alloc(UToolMemory
*mem
) {
334 int32_t oldIndex
=mem
->idx
;
335 int32_t newIndex
=oldIndex
+1;
336 if(utm_hasCapacity(mem
, newIndex
)) {
337 p
=(char *)mem
->array
+oldIndex
*mem
->size
;
339 uprv_memset(p
, 0, mem
->size
);
344 U_CAPI
void * U_EXPORT2
345 utm_allocN(UToolMemory
*mem
, int32_t n
) {
347 int32_t oldIndex
=mem
->idx
;
348 int32_t newIndex
=oldIndex
+n
;
349 if(utm_hasCapacity(mem
, newIndex
)) {
350 p
=(char *)mem
->array
+oldIndex
*mem
->size
;
352 uprv_memset(p
, 0, n
*mem
->size
);