• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 1999-2010, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  toolutil.c
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 1999nov19
14 *   created by: Markus W. Scherer
15 *
16 *	6/25/08 - Added Cygwin specific code in uprv_mkdir - Brian Rower
17 *
18 *   This file contains utility functions for ICU tools like genccode.
19 */
20 
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include "unicode/utypes.h"
24 
25 #ifdef U_WINDOWS
26 #   define VC_EXTRALEAN
27 #   define WIN32_LEAN_AND_MEAN
28 #   define NOUSER
29 #   define NOSERVICE
30 #   define NOIME
31 #   define NOMCX
32 #   include <windows.h>
33 #   include <direct.h>
34 #else
35 #   include <sys/stat.h>
36 #   include <sys/types.h>
37 #endif
38 #include <errno.h>
39 
40 #include "unicode/errorcode.h"
41 #include "unicode/putil.h"
42 #include "cmemory.h"
43 #include "cstring.h"
44 #include "toolutil.h"
45 #include "unicode/ucal.h"
46 
47 U_NAMESPACE_BEGIN
48 
~IcuToolErrorCode()49 IcuToolErrorCode::~IcuToolErrorCode() {
50     // Safe because our handleFailure() does not throw exceptions.
51     if(isFailure()) { handleFailure(); }
52 }
53 
handleFailure() const54 void IcuToolErrorCode::handleFailure() const {
55     fprintf(stderr, "error at %s: %s\n", location, errorName());
56     exit(errorCode);
57 }
58 
59 U_NAMESPACE_END
60 
61 static int32_t currentYear = -1;
62 
getCurrentYear()63 U_CAPI int32_t U_EXPORT2 getCurrentYear() {
64 #if !UCONFIG_NO_FORMATTING
65     UErrorCode status=U_ZERO_ERROR;
66     UCalendar *cal = NULL;
67 
68     if(currentYear == -1) {
69         cal = ucal_open(NULL, -1, NULL, UCAL_TRADITIONAL, &status);
70         ucal_setMillis(cal, ucal_getNow(), &status);
71         currentYear = ucal_get(cal, UCAL_YEAR, &status);
72         ucal_close(cal);
73     }
74 #else
75     /* No formatting- no way to set the current year. */
76 #endif
77     return currentYear;
78 }
79 
80 
81 U_CAPI const char * U_EXPORT2
getLongPathname(const char * pathname)82 getLongPathname(const char *pathname) {
83 #ifdef U_WINDOWS
84     /* anticipate problems with "short" pathnames */
85     static WIN32_FIND_DATAA info;
86     HANDLE file=FindFirstFileA(pathname, &info);
87     if(file!=INVALID_HANDLE_VALUE) {
88         if(info.cAlternateFileName[0]!=0) {
89             /* this file has a short name, get and use the long one */
90             const char *basename=findBasename(pathname);
91             if(basename!=pathname) {
92                 /* prepend the long filename with the original path */
93                 uprv_memmove(info.cFileName+(basename-pathname), info.cFileName, uprv_strlen(info.cFileName)+1);
94                 uprv_memcpy(info.cFileName, pathname, basename-pathname);
95             }
96             pathname=info.cFileName;
97         }
98         FindClose(file);
99     }
100 #endif
101     return pathname;
102 }
103 
104 U_CAPI const char * U_EXPORT2
findDirname(const char * path,char * buffer,int32_t bufLen,UErrorCode * status)105 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) {
106   if(U_FAILURE(*status)) return NULL;
107   const char *resultPtr = NULL;
108   int32_t resultLen = 0;
109 
110   const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
111 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
112   const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR);
113   if(basenameAlt && (!basename || basename<basenameAlt)) {
114     basename = basenameAlt;
115   }
116 #endif
117   if(!basename) {
118     /* no basename - return '.'. */
119     resultPtr = ".";
120     resultLen = 1;
121   } else {
122     resultPtr = path;
123     resultLen = basename - path;
124     if(resultLen<1) {
125       resultLen = 1; /* '/' or '/a' -> '/' */
126     }
127   }
128 
129   if((resultLen+1) <= bufLen) {
130     uprv_strncpy(buffer, resultPtr, resultLen);
131     buffer[resultLen]=0;
132     return buffer;
133   } else {
134     *status = U_BUFFER_OVERFLOW_ERROR;
135     return NULL;
136   }
137 }
138 
139 U_CAPI const char * U_EXPORT2
findBasename(const char * filename)140 findBasename(const char *filename) {
141     const char *basename=uprv_strrchr(filename, U_FILE_SEP_CHAR);
142 
143 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
144     if(basename==NULL) {
145         /* Use lenient matching on Windows, which can accept either \ or /
146            This is useful for environments like Win32+CygWin which have both.
147         */
148         basename=uprv_strrchr(filename, U_FILE_ALT_SEP_CHAR);
149     }
150 #endif
151 
152     if(basename!=NULL) {
153         return basename+1;
154     } else {
155         return filename;
156     }
157 }
158 
159 U_CAPI void U_EXPORT2
uprv_mkdir(const char * pathname,UErrorCode * status)160 uprv_mkdir(const char *pathname, UErrorCode *status) {
161 
162     int retVal = 0;
163 #if defined(U_WINDOWS)
164     retVal = _mkdir(pathname);
165 #else
166     retVal = mkdir(pathname, S_IRWXU | (S_IROTH | S_IXOTH) | (S_IROTH | S_IXOTH));
167 #endif
168     if (retVal && errno != EEXIST) {
169 #if defined(U_CYGWIN)
170 		/*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
171 		/* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
172 		struct stat st;
173 
174 		if(stat(pathname,&st) != 0)
175 		{
176 			*status = U_FILE_ACCESS_ERROR;
177 		}
178 #else
179         *status = U_FILE_ACCESS_ERROR;
180 #endif
181     }
182 }
183 
184 /*U_CAPI UDate U_EXPORT2
185 uprv_getModificationDate(const char *pathname, UErrorCode *status)
186 {
187     if(U_FAILURE(*status)) {
188         return;
189     }
190     //  TODO: handle case where stat is not available
191     struct stat st;
192 
193     if(stat(pathname,&st) != 0)
194     {
195         *status = U_FILE_ACCESS_ERROR;
196     } else {
197         return st.st_mtime;
198     }
199 }
200 */
201 
202 /* tool memory helper ------------------------------------------------------- */
203 
204 struct UToolMemory {
205     char name[64];
206     int32_t capacity, maxCapacity, size, idx;
207     void *array;
208     UAlignedMemory staticArray[1];
209 };
210 
211 U_CAPI UToolMemory * U_EXPORT2
utm_open(const char * name,int32_t initialCapacity,int32_t maxCapacity,int32_t size)212 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size) {
213     UToolMemory *mem;
214 
215     if(maxCapacity<initialCapacity) {
216         maxCapacity=initialCapacity;
217     }
218 
219     mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+initialCapacity*size);
220     if(mem==NULL) {
221         fprintf(stderr, "error: %s - out of memory\n", name);
222         exit(U_MEMORY_ALLOCATION_ERROR);
223     }
224     mem->array=mem->staticArray;
225 
226     uprv_strcpy(mem->name, name);
227     mem->capacity=initialCapacity;
228     mem->maxCapacity=maxCapacity;
229     mem->size=size;
230     mem->idx=0;
231     return mem;
232 }
233 
234 U_CAPI void U_EXPORT2
utm_close(UToolMemory * mem)235 utm_close(UToolMemory *mem) {
236     if(mem!=NULL) {
237         if(mem->array!=mem->staticArray) {
238             uprv_free(mem->array);
239         }
240         uprv_free(mem);
241     }
242 }
243 
244 
245 U_CAPI void * U_EXPORT2
utm_getStart(UToolMemory * mem)246 utm_getStart(UToolMemory *mem) {
247     return (char *)mem->array;
248 }
249 
250 U_CAPI int32_t U_EXPORT2
utm_countItems(UToolMemory * mem)251 utm_countItems(UToolMemory *mem) {
252     return mem->idx;
253 }
254 
255 
256 static UBool
utm_hasCapacity(UToolMemory * mem,int32_t capacity)257 utm_hasCapacity(UToolMemory *mem, int32_t capacity) {
258     if(mem->capacity<capacity) {
259         int32_t newCapacity;
260 
261         if(mem->maxCapacity<capacity) {
262             fprintf(stderr, "error: %s - trying to use more than maxCapacity=%ld units\n",
263                     mem->name, (long)mem->maxCapacity);
264             exit(U_MEMORY_ALLOCATION_ERROR);
265         }
266 
267         /* try to allocate a larger array */
268         if(capacity>=2*mem->capacity) {
269             newCapacity=capacity;
270         } else if(mem->capacity<=mem->maxCapacity/3) {
271             newCapacity=2*mem->capacity;
272         } else {
273             newCapacity=mem->maxCapacity;
274         }
275 
276         if(mem->array==mem->staticArray) {
277             mem->array=uprv_malloc(newCapacity*mem->size);
278             if(mem->array!=NULL) {
279                 uprv_memcpy(mem->array, mem->staticArray, mem->idx*mem->size);
280             }
281         } else {
282             mem->array=uprv_realloc(mem->array, newCapacity*mem->size);
283         }
284 
285         if(mem->array==NULL) {
286             fprintf(stderr, "error: %s - out of memory\n", mem->name);
287             exit(U_MEMORY_ALLOCATION_ERROR);
288         }
289         mem->capacity=newCapacity;
290     }
291 
292     return TRUE;
293 }
294 
295 U_CAPI void * U_EXPORT2
utm_alloc(UToolMemory * mem)296 utm_alloc(UToolMemory *mem) {
297     char *p=NULL;
298     int32_t oldIndex=mem->idx;
299     int32_t newIndex=oldIndex+1;
300     if(utm_hasCapacity(mem, newIndex)) {
301         p=(char *)mem->array+oldIndex*mem->size;
302         mem->idx=newIndex;
303         uprv_memset(p, 0, mem->size);
304     }
305     return p;
306 }
307 
308 U_CAPI void * U_EXPORT2
utm_allocN(UToolMemory * mem,int32_t n)309 utm_allocN(UToolMemory *mem, int32_t n) {
310     char *p=NULL;
311     int32_t oldIndex=mem->idx;
312     int32_t newIndex=oldIndex+n;
313     if(utm_hasCapacity(mem, newIndex)) {
314         p=(char *)mem->array+oldIndex*mem->size;
315         mem->idx=newIndex;
316         uprv_memset(p, 0, n*mem->size);
317     }
318     return p;
319 }
320