]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/filetools.cpp
1 /******************************************************************************
2 * Copyright (C) 2009-2013, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *******************************************************************************
7 #if U_PLATFORM == U_PF_MINGW
8 // *cough* - for struct stat
10 #undef __STRICT_ANSI__
14 #include "filetools.h"
17 #include "unicode/putil.h"
28 typedef struct dirent DIRENT
;
30 #define MAX_PATH_SIZE 4096 /* Set the limit for the size of the path. */
36 static int32_t whichFileModTimeIsLater(const char *file1
, const char *file2
);
39 * Goes through the given directory recursive to compare each file's modification time with that of the file given.
40 * Also can be given just one file to check against. Default value for isDir is FALSE.
42 U_CAPI UBool U_EXPORT2
43 isFileModTimeLater(const char *filePath
, const char *checkAgainst
, UBool isDir
) {
44 UBool isLatest
= TRUE
;
46 if (filePath
== NULL
|| checkAgainst
== NULL
) {
53 if ((pDir
= opendir(checkAgainst
)) != NULL
) {
55 DIRENT
*dirEntry
= NULL
;
57 while ((dirEntry
= readdir(pDir
)) != NULL
) {
58 if (uprv_strcmp(dirEntry
->d_name
, SKIP1
) != 0 && uprv_strcmp(dirEntry
->d_name
, SKIP2
) != 0) {
59 char newpath
[MAX_PATH_SIZE
] = "";
60 uprv_strcpy(newpath
, checkAgainst
);
61 uprv_strcat(newpath
, U_FILE_SEP_STRING
);
62 uprv_strcat(newpath
, dirEntry
->d_name
);
64 if ((subDirp
= opendir(newpath
)) != NULL
) {
65 /* If this new path is a directory, make a recursive call with the newpath. */
67 isLatest
= isFileModTimeLater(filePath
, newpath
, isDir
);
72 int32_t latest
= whichFileModTimeIsLater(filePath
, newpath
);
73 if (latest
< 0 || latest
== 2) {
83 fprintf(stderr
, "Unable to open directory: %s\n", checkAgainst
);
88 if (T_FileStream_file_exists(checkAgainst
)) {
89 int32_t latest
= whichFileModTimeIsLater(filePath
, checkAgainst
);
90 if (latest
< 0 || latest
== 2) {
101 /* Compares the mod time of both files returning a number indicating which one is later. -1 if error ocurs. */
102 static int32_t whichFileModTimeIsLater(const char *file1
, const char *file2
) {
104 struct stat stbuf1
, stbuf2
;
106 if (stat(file1
, &stbuf1
) == 0 && stat(file2
, &stbuf2
) == 0) {
107 time_t modtime1
, modtime2
;
110 modtime1
= stbuf1
.st_mtime
;
111 modtime2
= stbuf2
.st_mtime
;
113 diff
= difftime(modtime1
, modtime2
);
116 } else if (diff
> 0.0) {
121 fprintf(stderr
, "Unable to get stats from file: %s or %s\n", file1
, file2
);
128 /* Swap the file separater character given with the new one in the file path. */
129 U_CAPI
void U_EXPORT2
130 swapFileSepChar(char *filePath
, const char oldFileSepChar
, const char newFileSepChar
) {
131 for (int32_t i
= 0, length
= uprv_strlen(filePath
); i
< length
; i
++) {
132 filePath
[i
] = (filePath
[i
] == oldFileSepChar
) ? newFileSepChar
: filePath
[i
];