• 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 <fstream>
34 #include <time.h>
35 #include "unicode/utypes.h"
36 
37 #ifndef U_TOOLUTIL_IMPLEMENTATION
38 #error U_TOOLUTIL_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see https://unicode-org.github.io/icu/userguide/howtouseicu
39 #endif
40 
41 #if U_PLATFORM_USES_ONLY_WIN32_API
42 #   define VC_EXTRALEAN
43 #   define WIN32_LEAN_AND_MEAN
44 #   define NOUSER
45 #   define NOSERVICE
46 #   define NOIME
47 #   define NOMCX
48 #   if U_PLATFORM == U_PF_MINGW
49 #     define __NO_MINGW_LFS /* gets around missing 'off64_t' */
50 #   endif
51 #   include <windows.h>
52 #   include <direct.h>
53 #else
54 #   include <sys/stat.h>
55 #   include <sys/types.h>
56 #endif
57 
58 /* In MinGW environment, io.h needs to be included for _mkdir() */
59 #if U_PLATFORM == U_PF_MINGW
60 #include <io.h>
61 #endif
62 
63 #include <errno.h>
64 
65 #include <cstddef>
66 
67 #include "unicode/errorcode.h"
68 #include "unicode/putil.h"
69 #include "cmemory.h"
70 #include "cstring.h"
71 #include "toolutil.h"
72 
73 U_NAMESPACE_BEGIN
74 
~IcuToolErrorCode()75 IcuToolErrorCode::~IcuToolErrorCode() {
76     // Safe because our handleFailure() does not throw exceptions.
77     if(isFailure()) { handleFailure(); }
78 }
79 
handleFailure() const80 void IcuToolErrorCode::handleFailure() const {
81     fprintf(stderr, "error at %s: %s\n", location, errorName());
82     exit(errorCode);
83 }
84 
85 U_NAMESPACE_END
86 
87 static int32_t currentYear = -1;
88 
getCurrentYear()89 U_CAPI int32_t U_EXPORT2 getCurrentYear() {
90     if(currentYear == -1) {
91         time_t now = time(nullptr);
92         tm *fields = gmtime(&now);
93         currentYear = 1900 + fields->tm_year;
94     }
95     return currentYear;
96 }
97 
98 
99 U_CAPI const char * U_EXPORT2
getLongPathname(const char * pathname)100 getLongPathname(const char *pathname) {
101 #if U_PLATFORM_USES_ONLY_WIN32_API
102     /* anticipate problems with "short" pathnames */
103     static WIN32_FIND_DATAA info;
104     HANDLE file=FindFirstFileA(pathname, &info);
105     if(file!=INVALID_HANDLE_VALUE) {
106         if(info.cAlternateFileName[0]!=0) {
107             /* this file has a short name, get and use the long one */
108             const char *basename=findBasename(pathname);
109             if(basename!=pathname) {
110                 /* prepend the long filename with the original path */
111                 uprv_memmove(info.cFileName+(basename-pathname), info.cFileName, uprv_strlen(info.cFileName)+1);
112                 uprv_memcpy(info.cFileName, pathname, basename-pathname);
113             }
114             pathname=info.cFileName;
115         }
116         FindClose(file);
117     }
118 #endif
119     return pathname;
120 }
121 
122 U_CAPI const char * U_EXPORT2
findDirname(const char * path,char * buffer,int32_t bufLen,UErrorCode * status)123 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) {
124   if(U_FAILURE(*status)) return NULL;
125   const char *resultPtr = NULL;
126   int32_t resultLen = 0;
127 
128   const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
129 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
130   const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR);
131   if(basenameAlt && (!basename || basename<basenameAlt)) {
132     basename = basenameAlt;
133   }
134 #endif
135   if(!basename) {
136     /* no basename - return ''. */
137     resultPtr = "";
138     resultLen = 0;
139   } else {
140     resultPtr = path;
141     resultLen = static_cast<int32_t>(basename - path);
142     if(resultLen<1) {
143       resultLen = 1; /* '/' or '/a' -> '/' */
144     }
145   }
146 
147   if((resultLen+1) <= bufLen) {
148     uprv_strncpy(buffer, resultPtr, resultLen);
149     buffer[resultLen]=0;
150     return buffer;
151   } else {
152     *status = U_BUFFER_OVERFLOW_ERROR;
153     return NULL;
154   }
155 }
156 
157 U_CAPI const char * U_EXPORT2
findBasename(const char * filename)158 findBasename(const char *filename) {
159     const char *basename=uprv_strrchr(filename, U_FILE_SEP_CHAR);
160 
161 #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR
162     //be lenient about pathname separators on Windows, like official implementation of C++17 std::filesystem in MSVC
163     //would be convenient to merge this loop with the one above, but alas, there is no such solution in the standard library
164     const char *alt_basename=uprv_strrchr(filename, U_FILE_ALT_SEP_CHAR);
165     if(alt_basename>basename) {
166         basename=alt_basename;
167     }
168 #endif
169 
170     if(basename!=NULL) {
171         return basename+1;
172     } else {
173         return filename;
174     }
175 }
176 
177 U_CAPI void U_EXPORT2
uprv_mkdir(const char * pathname,UErrorCode * status)178 uprv_mkdir(const char *pathname, UErrorCode *status) {
179 
180     int retVal = 0;
181 #if U_PLATFORM_USES_ONLY_WIN32_API
182     retVal = _mkdir(pathname);
183 #else
184     retVal = mkdir(pathname, S_IRWXU | (S_IROTH | S_IXOTH) | (S_IROTH | S_IXOTH));
185 #endif
186     if (retVal && errno != EEXIST) {
187 #if U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
188         /*if using Cygwin and the mkdir says it failed...check if the directory already exists..*/
189         /* if it does...don't give the error, if it does not...give the error - Brian Rower - 6/25/08 */
190         struct stat st;
191 
192         if(stat(pathname,&st) != 0)
193         {
194             *status = U_FILE_ACCESS_ERROR;
195         }
196 #else
197         *status = U_FILE_ACCESS_ERROR;
198 #endif
199     }
200 }
201 
202 #if !UCONFIG_NO_FILE_IO
203 U_CAPI UBool U_EXPORT2
uprv_fileExists(const char * file)204 uprv_fileExists(const char *file) {
205   struct stat stat_buf;
206   if (stat(file, &stat_buf) == 0) {
207     return true;
208   } else {
209     return false;
210   }
211 }
212 #endif
213 
214 U_CAPI int32_t U_EXPORT2
uprv_compareGoldenFiles(const char * buffer,int32_t bufferLen,const char * goldenFilePath,bool overwrite)215 uprv_compareGoldenFiles(
216         const char* buffer, int32_t bufferLen,
217         const char* goldenFilePath,
218         bool overwrite) {
219 
220     if (overwrite) {
221         std::ofstream ofs;
222         ofs.open(goldenFilePath);
223         ofs.write(buffer, bufferLen);
224         ofs.close();
225         return -1;
226     }
227 
228     std::ifstream ifs(goldenFilePath, std::ifstream::in);
229     int32_t pos = 0;
230     char c;
231     while (ifs.get(c) && pos < bufferLen) {
232         if (c != buffer[pos]) {
233             // Files differ at this position
234             break;
235         }
236         pos++;
237     }
238     if (pos == bufferLen && ifs.eof()) {
239         // Files are same lengths
240         pos = -1;
241     }
242     ifs.close();
243     return pos;
244 }
245 
246 /*U_CAPI UDate U_EXPORT2
247 uprv_getModificationDate(const char *pathname, UErrorCode *status)
248 {
249     if(U_FAILURE(*status)) {
250         return;
251     }
252     //  TODO: handle case where stat is not available
253     struct stat st;
254 
255     if(stat(pathname,&st) != 0)
256     {
257         *status = U_FILE_ACCESS_ERROR;
258     } else {
259         return st.st_mtime;
260     }
261 }
262 */
263 
264 /* tool memory helper ------------------------------------------------------- */
265 
266 struct UToolMemory {
267     char name[64];
268     int32_t capacity, maxCapacity, size, idx;
269     void *array;
270     alignas(std::max_align_t) char staticArray[1];
271 };
272 
273 U_CAPI UToolMemory * U_EXPORT2
utm_open(const char * name,int32_t initialCapacity,int32_t maxCapacity,int32_t size)274 utm_open(const char *name, int32_t initialCapacity, int32_t maxCapacity, int32_t size) {
275     UToolMemory *mem;
276 
277     if(maxCapacity<initialCapacity) {
278         maxCapacity=initialCapacity;
279     }
280 
281     mem=(UToolMemory *)uprv_malloc(sizeof(UToolMemory)+initialCapacity*size);
282     if(mem==NULL) {
283         fprintf(stderr, "error: %s - out of memory\n", name);
284         exit(U_MEMORY_ALLOCATION_ERROR);
285     }
286     mem->array=mem->staticArray;
287 
288     uprv_strcpy(mem->name, name);
289     mem->capacity=initialCapacity;
290     mem->maxCapacity=maxCapacity;
291     mem->size=size;
292     mem->idx=0;
293     return mem;
294 }
295 
296 U_CAPI void U_EXPORT2
utm_close(UToolMemory * mem)297 utm_close(UToolMemory *mem) {
298     if(mem!=NULL) {
299         if(mem->array!=mem->staticArray) {
300             uprv_free(mem->array);
301         }
302         uprv_free(mem);
303     }
304 }
305 
306 
307 U_CAPI void * U_EXPORT2
utm_getStart(UToolMemory * mem)308 utm_getStart(UToolMemory *mem) {
309     return (char *)mem->array;
310 }
311 
312 U_CAPI int32_t U_EXPORT2
utm_countItems(UToolMemory * mem)313 utm_countItems(UToolMemory *mem) {
314     return mem->idx;
315 }
316 
317 
318 static UBool
utm_hasCapacity(UToolMemory * mem,int32_t capacity)319 utm_hasCapacity(UToolMemory *mem, int32_t capacity) {
320     if(mem->capacity<capacity) {
321         int32_t newCapacity;
322 
323         if(mem->maxCapacity<capacity) {
324             fprintf(stderr, "error: %s - trying to use more than maxCapacity=%ld units\n",
325                     mem->name, (long)mem->maxCapacity);
326             exit(U_MEMORY_ALLOCATION_ERROR);
327         }
328 
329         /* try to allocate a larger array */
330         if(capacity>=2*mem->capacity) {
331             newCapacity=capacity;
332         } else if(mem->capacity<=mem->maxCapacity/3) {
333             newCapacity=2*mem->capacity;
334         } else {
335             newCapacity=mem->maxCapacity;
336         }
337 
338         if(mem->array==mem->staticArray) {
339             mem->array=uprv_malloc(newCapacity*mem->size);
340             if(mem->array!=NULL) {
341                 uprv_memcpy(mem->array, mem->staticArray, (size_t)mem->idx*mem->size);
342             }
343         } else {
344             mem->array=uprv_realloc(mem->array, newCapacity*mem->size);
345         }
346 
347         if(mem->array==NULL) {
348             fprintf(stderr, "error: %s - out of memory\n", mem->name);
349             exit(U_MEMORY_ALLOCATION_ERROR);
350         }
351         mem->capacity=newCapacity;
352     }
353 
354     return true;
355 }
356 
357 U_CAPI void * U_EXPORT2
utm_alloc(UToolMemory * mem)358 utm_alloc(UToolMemory *mem) {
359     char *p=NULL;
360     int32_t oldIndex=mem->idx;
361     int32_t newIndex=oldIndex+1;
362     if(utm_hasCapacity(mem, newIndex)) {
363         p=(char *)mem->array+oldIndex*mem->size;
364         mem->idx=newIndex;
365         uprv_memset(p, 0, mem->size);
366     }
367     return p;
368 }
369 
370 U_CAPI void * U_EXPORT2
utm_allocN(UToolMemory * mem,int32_t n)371 utm_allocN(UToolMemory *mem, int32_t n) {
372     char *p=NULL;
373     int32_t oldIndex=mem->idx;
374     int32_t newIndex=oldIndex+n;
375     if(utm_hasCapacity(mem, newIndex)) {
376         p=(char *)mem->array+oldIndex*mem->size;
377         mem->idx=newIndex;
378         uprv_memset(p, 0, n*mem->size);
379     }
380     return p;
381 }
382