FreeRDP
Loading...
Searching...
No Matches
TestFileFindNextFile.c
1
2#include <stdio.h>
3#include <winpr/crt.h>
4#include <winpr/file.h>
5#include <winpr/path.h>
6#include <winpr/tchar.h>
7#include <winpr/windows.h>
8
9static TCHAR testDirectory2File1[] = _T("TestDirectory2File1");
10static TCHAR testDirectory2File2[] = _T("TestDirectory2File2");
11
12int TestFileFindNextFile(int argc, char* argv[])
13{
14 char* str = nullptr;
15 size_t length = 0;
16 BOOL status = 0;
17 HANDLE hFind = nullptr;
18 LPTSTR BasePath = nullptr;
19 WIN32_FIND_DATA FindData;
20 TCHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
21 WINPR_UNUSED(argc);
22 str = argv[1];
23#ifdef UNICODE
24 BasePath = ConvertUtf8ToWChar(str, &length);
25
26 if (!BasePath)
27 {
28 _tprintf(_T("Unable to allocate memory"));
29 return -1;
30 }
31#else
32 BasePath = _strdup(str);
33
34 if (!BasePath)
35 {
36 printf("Unable to allocate memory");
37 return -1;
38 }
39
40 length = strlen(BasePath);
41#endif
42 /* Simple filter matching all files inside current directory */
43 CopyMemory(FilePath, BasePath, length * sizeof(TCHAR));
44 FilePath[length] = 0;
45 if (FAILED(PathCchConvertStyle(BasePath, length, PATH_STYLE_WINDOWS)))
46 return -1;
47 if (FAILED(NativePathCchAppend(FilePath, PATHCCH_MAX_CCH, _T("TestDirectory2"))))
48 return -1;
49 if (FAILED(NativePathCchAppend(FilePath, PATHCCH_MAX_CCH, _T("TestDirectory2File*"))))
50 return -1;
51 free(BasePath);
52 _tprintf(_T("Finding file: %s\n"), FilePath);
53 hFind = FindFirstFile(FilePath, &FindData);
54
55 if (hFind == INVALID_HANDLE_VALUE)
56 {
57 _tprintf(_T("FindFirstFile failure: %s\n"), FilePath);
58 return -1;
59 }
60
61 _tprintf(_T("FindFirstFile: %s"), FindData.cFileName);
62
67 if ((_tcsncmp(FindData.cFileName, testDirectory2File1, ARRAYSIZE(testDirectory2File1)) != 0) &&
68 (_tcsncmp(FindData.cFileName, testDirectory2File2, ARRAYSIZE(testDirectory2File2)) != 0))
69 {
70 _tprintf(_T("FindFirstFile failure: Expected: %s, Actual: %s\n"), testDirectory2File1,
71 FindData.cFileName);
72 return -1;
73 }
74
75 status = FindNextFile(hFind, &FindData);
76
77 if (!status)
78 {
79 _tprintf(_T("FindNextFile failure: Expected: TRUE, Actual: %") _T(PRId32) _T("\n"), status);
80 return -1;
81 }
82
83 if ((_tcsncmp(FindData.cFileName, testDirectory2File1, ARRAYSIZE(testDirectory2File1)) != 0) &&
84 (_tcsncmp(FindData.cFileName, testDirectory2File2, ARRAYSIZE(testDirectory2File2)) != 0))
85 {
86 _tprintf(_T("FindNextFile failure: Expected: %s, Actual: %s\n"), testDirectory2File2,
87 FindData.cFileName);
88 return -1;
89 }
90
91 status = FindNextFile(hFind, &FindData);
92
93 if (status)
94 {
95 _tprintf(_T("FindNextFile failure: Expected: FALSE, Actual: %") _T(PRId32) _T("\n"),
96 status);
97 return -1;
98 }
99
100 FindClose(hFind);
101 return 0;
102}