From cf77acfa0fab7b3dd42781b483835a85b12308f2 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Mon, 29 Aug 2022 12:40:04 -0400 Subject: Rewrite file header peeking. --- tests/CMakeLists.txt | 3 +++ tests/testdrive.cpp | 23 +++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2190875..5990374 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,3 +29,6 @@ target_link_libraries(testdrive ${CMAKE_THREAD_LIBS_INIT} xsig ) +if(WIN32) + target_link_libraries(testdrive shell32 kernel32) +endif() diff --git a/tests/testdrive.cpp b/tests/testdrive.cpp index 2d8b0eb..b6d7436 100644 --- a/tests/testdrive.cpp +++ b/tests/testdrive.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -16,6 +17,8 @@ #ifdef _WIN32 //for the superior operating system #include +#define WIN32_LEAN_AND_MEAN +#include #include #include #endif @@ -168,18 +171,18 @@ void build_file_list(fs::path path, bool recursive, std::vector &out) auto dirit = fs::recursive_directory_iterator(path); for (auto &p : dirit) { - FILE* fp = fopen(p.path().c_str(),"r"); + std::fstream st(p.path(), std::ios::binary | std::ios::in); char c[8]; - size_t sz = fread((void*)c,1,6,fp); - if (sz < 6) continue; - if(!memcmp(c,"\x89PNG\r\n",6)||!memcmp(c,"\xff\xd8\xff",3)) + st.read(c, 6); + if (st.gcount() < 6) continue; + if(!memcmp(c,"\x89PNG\r\n", 6) || !memcmp(c,"\xff\xd8\xff", 3)) { out.push_back(p.path().string()); #if DEBUG > 0 printf("%ld, %s\n", out.size() - 1, out.back().c_str()); #endif } - fclose(fp); + st.close(); } } else @@ -187,18 +190,18 @@ void build_file_list(fs::path path, bool recursive, std::vector &out) auto dirit = fs::directory_iterator(path); for(auto &p : dirit) { - FILE* fp = fopen(p.path().c_str(),"r"); + std::fstream st(p.path(), std::ios::binary | std::ios::in); char c[8]; - size_t sz = fread((void*)c,1,6,fp); - if (sz < 6) continue; - if(!memcmp(c,"\x89PNG\r\n",6)||!memcmp(c,"\xff\xd8\xff",3)) + st.read(c, 6); + if (st.gcount() < 6) continue; + if(!memcmp(c,"\x89PNG\r\n", 6) || !memcmp(c,"\xff\xd8\xff", 3)) { out.push_back(p.path().string()); #if DEBUG > 0 printf("%ld, %s\n", out.size() - 1, out.back().c_str()); #endif } - fclose(fp); + st.close(); } } } -- cgit v1.2.3