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