2 *******************************************************************************
4 * Copyright (C) 1999-2005, 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"
28 # define WIN32_LEAN_AND_MEAN
36 # include <sys/stat.h>
37 # include <sys/types.h>
41 U_CAPI
const char * U_EXPORT2
42 getLongPathname(const char *pathname
) {
44 /* anticipate problems with "short" pathnames */
45 static WIN32_FIND_DATA info
;
46 HANDLE file
=FindFirstFile(pathname
, &info
);
47 if(file
!=INVALID_HANDLE_VALUE
) {
48 if(info
.cAlternateFileName
[0]!=0) {
49 /* this file has a short name, get and use the long one */
50 const char *basename
=findBasename(pathname
);
51 if(basename
!=pathname
) {
52 /* prepend the long filename with the original path */
53 uprv_memmove(info
.cFileName
+(basename
-pathname
), info
.cFileName
, uprv_strlen(info
.cFileName
)+1);
54 uprv_memcpy(info
.cFileName
, pathname
, basename
-pathname
);
56 pathname
=info
.cFileName
;
64 U_CAPI
const char * U_EXPORT2
65 findBasename(const char *filename
) {
66 const char *basename
=uprv_strrchr(filename
, U_FILE_SEP_CHAR
);
68 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
70 /* Use lenient matching on Windows, which can accept either \ or /
71 This is useful for environments like Win32+CygWin which have both.
73 basename
=uprv_strrchr(filename
, U_FILE_ALT_SEP_CHAR
);
85 uprv_mkdir(const char *pathname
, UErrorCode
*status
) {
87 #if defined(U_WINDOWS)
88 retVal
= _mkdir(pathname
);
90 retVal
= mkdir(pathname
, S_IRWXU
| (S_IROTH
| S_IXOTH
) | (S_IROTH
| S_IXOTH
));
92 if (retVal
&& errno
!= EEXIST
) {
93 *status
= U_FILE_ACCESS_ERROR
;
97 /* tool memory helper ------------------------------------------------------- */
101 int32_t capacity
, maxCapacity
, size
, index
;
103 UAlignedMemory staticArray
[1];
106 U_CAPI UToolMemory
* U_EXPORT2
107 utm_open(const char *name
, int32_t initialCapacity
, int32_t maxCapacity
, int32_t size
) {
110 if(maxCapacity
<initialCapacity
) {
111 maxCapacity
=initialCapacity
;
114 mem
=(UToolMemory
*)uprv_malloc(sizeof(UToolMemory
)+initialCapacity
*size
);
116 fprintf(stderr
, "error: %s - out of memory\n", name
);
117 exit(U_MEMORY_ALLOCATION_ERROR
);
119 mem
->array
=mem
->staticArray
;
121 uprv_strcpy(mem
->name
, name
);
122 mem
->capacity
=initialCapacity
;
123 mem
->maxCapacity
=maxCapacity
;
129 U_CAPI
void U_EXPORT2
130 utm_close(UToolMemory
*mem
) {
132 if(mem
->array
!=mem
->staticArray
) {
133 uprv_free(mem
->array
);
140 U_CAPI
void * U_EXPORT2
141 utm_getStart(UToolMemory
*mem
) {
142 return (char *)mem
->array
;
145 U_CAPI
int32_t U_EXPORT2
146 utm_countItems(UToolMemory
*mem
) {
152 utm_hasCapacity(UToolMemory
*mem
, int32_t capacity
) {
153 if(mem
->capacity
<capacity
) {
156 if(mem
->maxCapacity
<capacity
) {
157 fprintf(stderr
, "error: %s - trying to use more than maxCapacity=%ld units\n",
158 mem
->name
, (long)mem
->maxCapacity
);
159 exit(U_MEMORY_ALLOCATION_ERROR
);
162 /* try to allocate a larger array */
163 if(capacity
>=2*mem
->capacity
) {
164 newCapacity
=capacity
;
165 } else if(mem
->capacity
<=mem
->maxCapacity
/3) {
166 newCapacity
=2*mem
->capacity
;
168 newCapacity
=mem
->maxCapacity
;
171 if(mem
->array
==mem
->staticArray
) {
172 mem
->array
=uprv_malloc(newCapacity
*mem
->size
);
173 if(mem
->array
!=NULL
) {
174 uprv_memcpy(mem
->array
, mem
->staticArray
, mem
->index
*mem
->size
);
177 mem
->array
=uprv_realloc(mem
->array
, newCapacity
*mem
->size
);
180 if(mem
->array
==NULL
) {
181 fprintf(stderr
, "error: %s - out of memory\n", mem
->name
);
182 exit(U_MEMORY_ALLOCATION_ERROR
);
189 U_CAPI
void * U_EXPORT2
190 utm_alloc(UToolMemory
*mem
) {
191 char *p
=(char *)mem
->array
+mem
->index
*mem
->size
;
192 int32_t newIndex
=mem
->index
+1;
193 if(utm_hasCapacity(mem
, newIndex
)) {
195 uprv_memset(p
, 0, mem
->size
);
200 U_CAPI
void * U_EXPORT2
201 utm_allocN(UToolMemory
*mem
, int32_t n
) {
202 char *p
=(char *)mem
->array
+mem
->index
*mem
->size
;
203 int32_t newIndex
=mem
->index
+n
;
204 if(utm_hasCapacity(mem
, newIndex
)) {
206 uprv_memset(p
, 0, n
*mem
->size
);