]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/filetools.cpp
1 /******************************************************************************
2 * Copyright (C) 2009, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *******************************************************************************
10 #include "unicode/putil.h"
20 typedef struct dirent DIRENT
;
22 #define MAX_PATH_SIZE 4096 /* Set the limit for the size of the path. */
28 static int32_t whichFileModTimeIsLater(const char *file1
, const char *file2
);
31 * Goes through the given directory recursive to compare each file's modification time with that of the file given.
32 * Also can be given just one file to check against. Default value for isDir is FALSE.
34 U_CAPI UBool U_EXPORT2
35 isFileModTimeLater(const char *filePath
, const char *checkAgainst
, UBool isDir
) {
36 UBool isLatest
= TRUE
;
38 if (filePath
== NULL
|| checkAgainst
== NULL
) {
45 if ((pDir
= opendir(checkAgainst
)) != NULL
) {
47 DIRENT
*dirEntry
= NULL
;
49 while ((dirEntry
= readdir(pDir
)) != NULL
) {
50 if (uprv_strcmp(dirEntry
->d_name
, SKIP1
) != 0 && uprv_strcmp(dirEntry
->d_name
, SKIP2
) != 0) {
51 char newpath
[MAX_PATH_SIZE
] = "";
52 uprv_strcpy(newpath
, checkAgainst
);
53 uprv_strcat(newpath
, U_FILE_SEP_STRING
);
54 uprv_strcat(newpath
, dirEntry
->d_name
);
56 if ((subDirp
= opendir(newpath
)) != NULL
) {
57 /* If this new path is a directory, make a recursive call with the newpath. */
59 isLatest
= isFileModTimeLater(filePath
, newpath
, isDir
);
64 int32_t latest
= whichFileModTimeIsLater(filePath
, newpath
);
65 if (latest
< 0 || latest
== 2) {
75 fprintf(stderr
, "Unable to open directory: %s\n", checkAgainst
);
80 if (T_FileStream_file_exists(checkAgainst
)) {
81 int32_t latest
= whichFileModTimeIsLater(filePath
, checkAgainst
);
82 if (latest
< 0 || latest
== 2) {
93 /* Compares the mod time of both files returning a number indicating which one is later. -1 if error ocurs. */
94 static int32_t whichFileModTimeIsLater(const char *file1
, const char *file2
) {
96 struct stat stbuf1
, stbuf2
;
98 if (stat(file1
, &stbuf1
) == 0 && stat(file2
, &stbuf2
) == 0) {
99 time_t modtime1
, modtime2
;
102 modtime1
= stbuf1
.st_mtime
;
103 modtime2
= stbuf2
.st_mtime
;
105 diff
= difftime(modtime1
, modtime2
);
108 } else if (diff
> 0.0) {
113 fprintf(stderr
, "Unable to get stats from file: %s or %s\n", file1
, file2
);
120 /* Swap the file separater character given with the new one in the file path. */
121 U_CAPI
void U_EXPORT2
122 swapFileSepChar(char *filePath
, const char oldFileSepChar
, const char newFileSepChar
) {
123 for (int32_t i
= 0, length
= uprv_strlen(filePath
); i
< length
; i
++) {
124 filePath
[i
] = (filePath
[i
] == oldFileSepChar
) ? newFileSepChar
: filePath
[i
];