2 *******************************************************************************
4 * Copyright (C) 1999-2008, 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 * This file contains utility functions for ICU tools like genccode.
20 #include "unicode/utypes.h"
21 #include "unicode/putil.h"
25 #include "unicode/ucal.h"
29 # define WIN32_LEAN_AND_MEAN
37 # include <sys/stat.h>
38 # include <sys/types.h>
42 static int32_t currentYear
= -1;
44 U_CAPI
int32_t U_EXPORT2
getCurrentYear() {
45 #if !UCONFIG_NO_FORMATTING
46 UErrorCode status
=U_ZERO_ERROR
;
47 UCalendar
*cal
= NULL
;
49 if(currentYear
== -1) {
50 cal
= ucal_open(NULL
, -1, NULL
, UCAL_TRADITIONAL
, &status
);
51 ucal_setMillis(cal
, ucal_getNow(), &status
);
52 currentYear
= ucal_get(cal
, UCAL_YEAR
, &status
);
62 U_CAPI
const char * U_EXPORT2
63 getLongPathname(const char *pathname
) {
65 /* anticipate problems with "short" pathnames */
66 static WIN32_FIND_DATA info
;
67 HANDLE file
=FindFirstFileA(pathname
, &info
);
68 if(file
!=INVALID_HANDLE_VALUE
) {
69 if(info
.cAlternateFileName
[0]!=0) {
70 /* this file has a short name, get and use the long one */
71 const char *basename
=findBasename(pathname
);
72 if(basename
!=pathname
) {
73 /* prepend the long filename with the original path */
74 uprv_memmove(info
.cFileName
+(basename
-pathname
), info
.cFileName
, uprv_strlen(info
.cFileName
)+1);
75 uprv_memcpy(info
.cFileName
, pathname
, basename
-pathname
);
77 pathname
=info
.cFileName
;
85 U_CAPI
const char * U_EXPORT2
86 findBasename(const char *filename
) {
87 const char *basename
=uprv_strrchr(filename
, U_FILE_SEP_CHAR
);
89 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
91 /* Use lenient matching on Windows, which can accept either \ or /
92 This is useful for environments like Win32+CygWin which have both.
94 basename
=uprv_strrchr(filename
, U_FILE_ALT_SEP_CHAR
);
105 U_CAPI
void U_EXPORT2
106 uprv_mkdir(const char *pathname
, UErrorCode
*status
) {
108 #if defined(U_WINDOWS)
109 retVal
= _mkdir(pathname
);
111 retVal
= mkdir(pathname
, S_IRWXU
| (S_IROTH
| S_IXOTH
) | (S_IROTH
| S_IXOTH
));
113 if (retVal
&& errno
!= EEXIST
) {
114 *status
= U_FILE_ACCESS_ERROR
;
118 /* tool memory helper ------------------------------------------------------- */
122 int32_t capacity
, maxCapacity
, size
, index
;
124 UAlignedMemory staticArray
[1];
127 U_CAPI UToolMemory
* U_EXPORT2
128 utm_open(const char *name
, int32_t initialCapacity
, int32_t maxCapacity
, int32_t size
) {
131 if(maxCapacity
<initialCapacity
) {
132 maxCapacity
=initialCapacity
;
135 mem
=(UToolMemory
*)uprv_malloc(sizeof(UToolMemory
)+initialCapacity
*size
);
137 fprintf(stderr
, "error: %s - out of memory\n", name
);
138 exit(U_MEMORY_ALLOCATION_ERROR
);
140 mem
->array
=mem
->staticArray
;
142 uprv_strcpy(mem
->name
, name
);
143 mem
->capacity
=initialCapacity
;
144 mem
->maxCapacity
=maxCapacity
;
150 U_CAPI
void U_EXPORT2
151 utm_close(UToolMemory
*mem
) {
153 if(mem
->array
!=mem
->staticArray
) {
154 uprv_free(mem
->array
);
161 U_CAPI
void * U_EXPORT2
162 utm_getStart(UToolMemory
*mem
) {
163 return (char *)mem
->array
;
166 U_CAPI
int32_t U_EXPORT2
167 utm_countItems(UToolMemory
*mem
) {
173 utm_hasCapacity(UToolMemory
*mem
, int32_t capacity
) {
174 if(mem
->capacity
<capacity
) {
177 if(mem
->maxCapacity
<capacity
) {
178 fprintf(stderr
, "error: %s - trying to use more than maxCapacity=%ld units\n",
179 mem
->name
, (long)mem
->maxCapacity
);
180 exit(U_MEMORY_ALLOCATION_ERROR
);
183 /* try to allocate a larger array */
184 if(capacity
>=2*mem
->capacity
) {
185 newCapacity
=capacity
;
186 } else if(mem
->capacity
<=mem
->maxCapacity
/3) {
187 newCapacity
=2*mem
->capacity
;
189 newCapacity
=mem
->maxCapacity
;
192 if(mem
->array
==mem
->staticArray
) {
193 mem
->array
=uprv_malloc(newCapacity
*mem
->size
);
194 if(mem
->array
!=NULL
) {
195 uprv_memcpy(mem
->array
, mem
->staticArray
, mem
->index
*mem
->size
);
198 mem
->array
=uprv_realloc(mem
->array
, newCapacity
*mem
->size
);
201 if(mem
->array
==NULL
) {
202 fprintf(stderr
, "error: %s - out of memory\n", mem
->name
);
203 exit(U_MEMORY_ALLOCATION_ERROR
);
210 U_CAPI
void * U_EXPORT2
211 utm_alloc(UToolMemory
*mem
) {
212 char *p
=(char *)mem
->array
+mem
->index
*mem
->size
;
213 int32_t newIndex
=mem
->index
+1;
214 if(utm_hasCapacity(mem
, newIndex
)) {
216 uprv_memset(p
, 0, mem
->size
);
221 U_CAPI
void * U_EXPORT2
222 utm_allocN(UToolMemory
*mem
, int32_t n
) {
223 char *p
=(char *)mem
->array
+mem
->index
*mem
->size
;
224 int32_t newIndex
=mem
->index
+n
;
225 if(utm_hasCapacity(mem
, newIndex
)) {
227 uprv_memset(p
, 0, n
*mem
->size
);