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