2 *******************************************************************************
4 * Copyright (C) 1999-2004, 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.
21 # define WIN32_LEAN_AND_MEAN
29 #include "unicode/utypes.h"
30 #include "unicode/putil.h"
35 U_CAPI
const char * U_EXPORT2
36 getLongPathname(const char *pathname
) {
38 /* anticipate problems with "short" pathnames */
39 static WIN32_FIND_DATA info
;
40 HANDLE file
=FindFirstFile(pathname
, &info
);
41 if(file
!=INVALID_HANDLE_VALUE
) {
42 if(info
.cAlternateFileName
[0]!=0) {
43 /* this file has a short name, get and use the long one */
44 const char *basename
=findBasename(pathname
);
45 if(basename
!=pathname
) {
46 /* prepend the long filename with the original path */
47 uprv_memmove(info
.cFileName
+(basename
-pathname
), info
.cFileName
, uprv_strlen(info
.cFileName
)+1);
48 uprv_memcpy(info
.cFileName
, pathname
, basename
-pathname
);
50 pathname
=info
.cFileName
;
58 U_CAPI
const char * U_EXPORT2
59 findBasename(const char *filename
) {
60 const char *basename
=uprv_strrchr(filename
, U_FILE_SEP_CHAR
);
62 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
64 /* Use lenient matching on Windows, which can accept either \ or /
65 This is useful for environments like Win32+CygWin which have both.
67 basename
=uprv_strrchr(filename
, U_FILE_ALT_SEP_CHAR
);
78 /* tool memory helper ------------------------------------------------------- */
82 int32_t capacity
, maxCapacity
, size
, index
;
84 UAlignedMemory staticArray
[1];
87 U_CAPI UToolMemory
* U_EXPORT2
88 utm_open(const char *name
, int32_t initialCapacity
, int32_t maxCapacity
, int32_t size
) {
91 if(maxCapacity
<initialCapacity
) {
92 maxCapacity
=initialCapacity
;
95 mem
=(UToolMemory
*)uprv_malloc(sizeof(UToolMemory
)+initialCapacity
*size
);
97 fprintf(stderr
, "error: %s - out of memory\n", name
);
98 exit(U_MEMORY_ALLOCATION_ERROR
);
100 mem
->array
=mem
->staticArray
;
102 uprv_strcpy(mem
->name
, name
);
103 mem
->capacity
=initialCapacity
;
104 mem
->maxCapacity
=maxCapacity
;
110 U_CAPI
void U_EXPORT2
111 utm_close(UToolMemory
*mem
) {
113 if(mem
->array
!=mem
->staticArray
) {
114 uprv_free(mem
->array
);
121 U_CAPI
void * U_EXPORT2
122 utm_getStart(UToolMemory
*mem
) {
123 return (char *)mem
->array
;
126 U_CAPI
int32_t U_EXPORT2
127 utm_countItems(UToolMemory
*mem
) {
133 utm_hasCapacity(UToolMemory
*mem
, int32_t capacity
) {
134 if(mem
->capacity
<capacity
) {
137 if(mem
->maxCapacity
<capacity
) {
138 fprintf(stderr
, "error: %s - trying to use more than maxCapacity=%ld units\n",
139 mem
->name
, (long)mem
->maxCapacity
);
140 exit(U_MEMORY_ALLOCATION_ERROR
);
143 /* try to allocate a larger array */
144 if(capacity
>=2*mem
->capacity
) {
145 newCapacity
=capacity
;
146 } else if(mem
->capacity
<=mem
->maxCapacity
/3) {
147 newCapacity
=2*mem
->capacity
;
149 newCapacity
=mem
->maxCapacity
;
152 if(mem
->array
==mem
->staticArray
) {
153 mem
->array
=uprv_malloc(newCapacity
*mem
->size
);
154 if(mem
->array
!=NULL
) {
155 uprv_memcpy(mem
->array
, mem
->staticArray
, mem
->index
*mem
->size
);
158 mem
->array
=uprv_realloc(mem
->array
, newCapacity
*mem
->size
);
161 if(mem
->array
==NULL
) {
162 fprintf(stderr
, "error: %s - out of memory\n", mem
->name
);
163 exit(U_MEMORY_ALLOCATION_ERROR
);
170 U_CAPI
void * U_EXPORT2
171 utm_alloc(UToolMemory
*mem
) {
172 char *p
=(char *)mem
->array
+mem
->index
*mem
->size
;
173 int32_t newIndex
=mem
->index
+1;
174 if(utm_hasCapacity(mem
, newIndex
)) {
176 uprv_memset(p
, 0, mem
->size
);
181 U_CAPI
void * U_EXPORT2
182 utm_allocN(UToolMemory
*mem
, int32_t n
) {
183 char *p
=(char *)mem
->array
+mem
->index
*mem
->size
;
184 int32_t newIndex
=mem
->index
+n
;
185 if(utm_hasCapacity(mem
, newIndex
)) {
187 uprv_memset(p
, 0, n
*mem
->size
);