]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/filetools.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /******************************************************************************
4 * Copyright (C) 2009-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *******************************************************************************
9 #include "unicode/platform.h"
10 #if U_PLATFORM == U_PF_MINGW
11 // *cough* - for struct stat
12 #ifdef __STRICT_ANSI__
13 #undef __STRICT_ANSI__
17 #include "filetools.h"
21 #include "unicode/putil.h"
32 typedef struct dirent DIRENT
;
38 static int32_t whichFileModTimeIsLater(const char *file1
, const char *file2
);
41 * Goes through the given directory recursive to compare each file's modification time with that of the file given.
42 * Also can be given just one file to check against. Default value for isDir is FALSE.
44 U_CAPI UBool U_EXPORT2
45 isFileModTimeLater(const char *filePath
, const char *checkAgainst
, UBool isDir
) {
46 UBool isLatest
= TRUE
;
48 if (filePath
== NULL
|| checkAgainst
== NULL
) {
55 if ((pDir
= opendir(checkAgainst
)) != NULL
) {
57 DIRENT
*dirEntry
= NULL
;
59 while ((dirEntry
= readdir(pDir
)) != NULL
) {
60 if (uprv_strcmp(dirEntry
->d_name
, SKIP1
) != 0 && uprv_strcmp(dirEntry
->d_name
, SKIP2
) != 0) {
61 UErrorCode status
= U_ZERO_ERROR
;
62 icu::CharString
newpath(checkAgainst
, -1, status
);
63 newpath
.append(U_FILE_SEP_STRING
, -1, status
);
64 newpath
.append(dirEntry
->d_name
, -1, status
);
65 if (U_FAILURE(status
)) {
66 fprintf(stderr
, "%s:%d: %s\n", __FILE__
, __LINE__
, u_errorName(status
));
70 if ((subDirp
= opendir(newpath
.data())) != NULL
) {
71 /* If this new path is a directory, make a recursive call with the newpath. */
73 isLatest
= isFileModTimeLater(filePath
, newpath
.data(), isDir
);
78 int32_t latest
= whichFileModTimeIsLater(filePath
, newpath
.data());
79 if (latest
< 0 || latest
== 2) {
89 fprintf(stderr
, "Unable to open directory: %s\n", checkAgainst
);
94 if (T_FileStream_file_exists(checkAgainst
)) {
95 int32_t latest
= whichFileModTimeIsLater(filePath
, checkAgainst
);
96 if (latest
< 0 || latest
== 2) {
107 /* Compares the mod time of both files returning a number indicating which one is later. -1 if error ocurs. */
108 static int32_t whichFileModTimeIsLater(const char *file1
, const char *file2
) {
110 struct stat stbuf1
, stbuf2
;
112 if (stat(file1
, &stbuf1
) == 0 && stat(file2
, &stbuf2
) == 0) {
113 time_t modtime1
, modtime2
;
116 modtime1
= stbuf1
.st_mtime
;
117 modtime2
= stbuf2
.st_mtime
;
119 diff
= difftime(modtime1
, modtime2
);
122 } else if (diff
> 0.0) {
127 fprintf(stderr
, "Unable to get stats from file: %s or %s\n", file1
, file2
);
134 /* Swap the file separater character given with the new one in the file path. */
135 U_CAPI
void U_EXPORT2
136 swapFileSepChar(char *filePath
, const char oldFileSepChar
, const char newFileSepChar
) {
137 for (int32_t i
= 0, length
= static_cast<int32_t>(uprv_strlen(filePath
)); i
< length
; i
++) {
138 filePath
[i
] = (filePath
[i
] == oldFileSepChar
) ? newFileSepChar
: filePath
[i
];