OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CORE] OV file utils file_size use std file path (#28462)
barnasm1e6be681e4b7e
diff --git a/src/common/util/include/openvino/util/file_path.hpp b/src/common/util/include/openvino/util/file_path.hppindex 34c326e67ec..f5816956d68 100644--- a/src/common/util/include/openvino/util/file_path.hpp+++ b/src/common/util/include/openvino/util/file_path.hpp@@ -7,10 +7,14 @@#include <cstdio>#include "openvino/util/filesystem.hpp"-namespace ov {-namespace util {++namespace ov::util {#if defined(OPENVINO_HAS_FILESYSTEM)+// There are known issues with usage of std::filesystem::path unicode represenataion:+// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95048+// * https://stackoverflow.com/questions/58521857/cross-platform-way-to-handle-stdstring-stdwstring-with-stdfilesystempath+// Working compiler versions has been designated with godbolt.using Path = std::filesystem::path;#elif defined(OPENVINO_HAS_EXP_FILESYSTEM)// Known issues:@@ -30,5 +34,27 @@ using Path = std::filesystem::path;using Path = std::experimental::filesystem::path;#endif-} // namespace util-} // namespace ov+#if !defined(__GNUC__) || (__GNUC__ > 12 || __GNUC__ == 12 && __GNUC_MINOR__ >= 3)+# define GCC_NOT_USED_OR_VER_AT_LEAST_12_3+#endif++#if !defined(__clang__) || defined(__clang__) && __clang_major__ >= 17+# define CLANG_NOT_USED_OR_VER_AT_LEAST_17+#endif++#if defined(__GNUC__) && (__GNUC__ < 12 || __GNUC__ == 12 && __GNUC_MINOR__ < 3)+# define GCC_VER_LESS_THAN_12_3+#endif++#if defined(__clang__) && __clang_major__ < 17+# define CLANG_VER_LESS_THAN_17+#endif++} // namespace ov::util++#if defined(GCC_VER_LESS_THAN_12_3) || defined(CLANG_VER_LESS_THAN_17)++template <>+ov::util::Path::path(const std::wstring& source, ov::util::Path::format fmt);++#endifdiff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hppindex b4db4df774b..928a8fd1635 100644--- a/src/common/util/include/openvino/util/file_util.hpp+++ b/src/common/util/include/openvino/util/file_util.hpp@@ -10,7 +10,9 @@#include <string>#include <vector>+#include "openvino/util/file_path.hpp"#include "openvino/util/util.hpp"+#include "openvino/util/wstring_convert_util.hpp"namespace ov {namespace util {@@ -89,19 +91,6 @@ const std::string& path_to_string(const Path& path) {}#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT-/**- * @brief Conversion from wide character string to a single-byte chain.- * @param wstr A wide-char string- * @return A multi-byte string- */-std::string wstring_to_string(const std::wstring& wstr);-/**- * @brief Conversion from single-byte chain to wide character string.- * @param str A null-terminated string- * @return A wide-char string- */-std::wstring string_to_wstring(const std::string& str);-/*** @brief Convert path as wide character string to a single-byte chain.* @param path Path as wide-char string.@@ -177,22 +166,18 @@ bool directory_exists(const std::wstring& path);* @param[in] path The file name* @return file size*/++inline int64_t file_size(const ov::util::Path& path) {+ std::error_code ec;+ const auto size = std::filesystem::file_size(path, ec);+ return ec ? -1 : static_cast<int64_t>(size);+}++#ifdef _MSC_VERinline int64_t file_size(const char* path) {-#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- std::wstring widefilename = ov::util::string_to_wstring(path);- const wchar_t* file_name = widefilename.c_str();-#elif defined(__ANDROID__) || defined(ANDROID)- std::string file_name = path;- std::string::size_type pos = file_name.find('!');- if (pos != std::string::npos) {- file_name = file_name.substr(0, pos);- }-#else- const char* file_name = path;-#endif- std::ifstream in(file_name, std::ios_base::binary | std::ios_base::ate);- return in.tellg();+ return file_size(ov::util::string_to_wstring(path));}+#endif/*** @brief Returns file size for file@@ -218,15 +203,6 @@ inline bool file_exists(const char* path) {#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT-/**- * @brief Returns file size for file- * @param[in] path The file name- * @return file size- */-inline int64_t file_size(const std::wstring& path) {- return file_size(wstring_to_string(path).c_str());-}-/*** @brief Returns true if file exists* @param[in] path The file name@@ -237,15 +213,6 @@ inline bool file_exists(const std::wstring& path) {}#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT-/**- * @brief Returns file size for file- * @param[in] path The file name- * @return file size- */-inline int64_t file_size(const std::string& path) {- return file_size(path.c_str());-}-/*** @brief Returns true if file exists* @param[in] path The file name@@ -397,5 +364,9 @@ inline std::basic_string<C> make_path(const std::basic_string<C>& folder, constreturn folder + ov::util::FileTraits<C>::file_separator + file;}+inline ov::util::Path make_path(const wchar_t* file) {+ return {std::wstring(file)};+}+} // namespace util} // namespace ovdiff --git a/src/common/util/include/openvino/util/wstring_convert_util.hpp b/src/common/util/include/openvino/util/wstring_convert_util.hppnew file mode 100644index 00000000000..f5f8512bcf8--- /dev/null+++ b/src/common/util/include/openvino/util/wstring_convert_util.hpp@@ -0,0 +1,29 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <string>++#include "openvino/util/util.hpp"++namespace ov::util {++#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+/**+ * @brief Conversion from wide character string to a single-byte chain.+ * @param wstr A wide-char string+ * @return A multi-byte string+ */+std::string wstring_to_string(const std::wstring& wstr);+/**+ * @brief Conversion from single-byte chain to wide character string.+ * @param str A null-terminated string+ * @return A wide-char string+ */+std::wstring string_to_wstring(const std::string& str);++#endif++} // namespace ov::utildiff --git a/src/common/util/src/file_path.cpp b/src/common/util/src/file_path.cppnew file mode 100644index 00000000000..8a0d8154dfb--- /dev/null+++ b/src/common/util/src/file_path.cpp@@ -0,0 +1,15 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/util/file_path.hpp"++#include "openvino/util/wstring_convert_util.hpp"++#if defined(GCC_VER_LESS_THAN_12_3) || defined(CLANG_VER_LESS_THAN_17)++template <>+ov::util::Path::path(const std::wstring& source, ov::util::Path::format fmt)+ : path(ov::util::wstring_to_string(source), fmt) {}++#endifdiff --git a/src/common/util/src/file_util.cpp b/src/common/util/src/file_util.cppindex 9e1c0c41cea..c454eacba7d 100644--- a/src/common/util/src/file_util.cpp+++ b/src/common/util/src/file_util.cpp@@ -338,46 +338,6 @@ void ov::util::convert_path_win_style(std::string& path) {std::replace(path.begin(), path.end(), '/', '\\');}-#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT--# ifdef __clang__-# pragma clang diagnostic push-# pragma clang diagnostic ignored "-Wdeprecated-declarations"-# endif--std::string ov::util::wstring_to_string(const std::wstring& wstr) {-# ifdef _WIN32- int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);- std::string strTo(size_needed, 0);- WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);- return strTo;-# else- std::wstring_convert<std::codecvt_utf8<wchar_t>> wstring_decoder;- return wstring_decoder.to_bytes(wstr);-# endif-}--std::wstring ov::util::string_to_wstring(const std::string& string) {- const char* str = string.c_str();-# ifdef _WIN32- int strSize = static_cast<int>(std::strlen(str));- int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, strSize, NULL, 0);- std::wstring wstrTo(size_needed, 0);- MultiByteToWideChar(CP_UTF8, 0, str, strSize, &wstrTo[0], size_needed);- return wstrTo;-# else- std::wstring_convert<std::codecvt_utf8<wchar_t>> wstring_encoder;- std::wstring result = wstring_encoder.from_bytes(str);- return result;-# endif-}--# ifdef __clang__-# pragma clang diagnostic pop-# endif--#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT-std::string ov::util::get_absolute_file_path(const std::string& path) {std::string absolutePath;absolutePath.resize(MAX_ABS_PATH);diff --git a/src/common/util/src/wstring_convert_util.cpp b/src/common/util/src/wstring_convert_util.cppnew file mode 100644index 00000000000..de76e1134e1--- /dev/null+++ b/src/common/util/src/wstring_convert_util.cpp@@ -0,0 +1,52 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/util/wstring_convert_util.hpp"++#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT++# include <codecvt>+# include <locale>++# ifdef _WIN32+# include <windows.h>+# endif++# ifdef __clang__+# pragma clang diagnostic push+# pragma clang diagnostic ignored "-Wdeprecated-declarations"+# endif++std::string ov::util::wstring_to_string(const std::wstring& wstr) {+# ifdef _WIN32+ int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);+ std::string strTo(size_needed, 0);+ WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);+ return strTo;+# else+ std::wstring_convert<std::codecvt_utf8<wchar_t>> wstring_decoder;+ return wstring_decoder.to_bytes(wstr);+# endif+}++std::wstring ov::util::string_to_wstring(const std::string& string) {+ const char* str = string.c_str();+# ifdef _WIN32+ int strSize = static_cast<int>(std::strlen(str));+ int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, strSize, NULL, 0);+ std::wstring wstrTo(size_needed, 0);+ MultiByteToWideChar(CP_UTF8, 0, str, strSize, &wstrTo[0], size_needed);+ return wstrTo;+# else+ std::wstring_convert<std::codecvt_utf8<wchar_t>> wstring_encoder;+ std::wstring result = wstring_encoder.from_bytes(str);+ return result;+# endif+}++# ifdef __clang__+# pragma clang diagnostic pop+# endif++#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORTdiff --git a/src/core/tests/file_util.cpp b/src/core/tests/file_util.cppindex 33d5d7f9db2..8841a297f47 100644--- a/src/core/tests/file_util.cpp+++ b/src/core/tests/file_util.cpp@@ -9,6 +9,7 @@#include <random>#include <sstream>#include <string>+#include <string_view>#include <vector>#include "openvino/util/file_path.hpp"@@ -262,18 +263,6 @@ TEST(file_util, path_cast) {EXPECT_TRUE(std::u16string(u"C:\\Users\\file.txt") == ov::util::Path(U"C:\\Users\\file.txt").u16string());}-// There are known issues related with usage of std::filesystem::path unocode represenataion:-// https://jira.devtools.intel.com/browse/CVS-160477-// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95048-// * https://stackoverflow.com/questions/58521857/cross-platform-way-to-handle-stdstring-stdwstring-with-stdfilesystempath-#if !defined(__GNUC__) || (__GNUC__ > 12 || __GNUC__ == 12 && __GNUC_MINOR__ >= 3)-# define GCC_NOT_USED_OR_VER_AT_LEAST_12_3-#endif--#if !defined(__clang__) || defined(__clang__) && __clang_major__ >= 17-# define CLANG_NOT_USED_OR_VER_AT_LEAST_17-#endif-TEST(file_util, path_cast_unicode) {EXPECT_EQ("~/狗/ǡ୫ԩϗ/にほ/ąę/ど/௸ඊƷ/狗1.txt", ov::util::Path("~/狗/ǡ୫ԩϗ/にほ/ąę/ど/௸ඊƷ/狗1.txt").generic_string());EXPECT_TRUE(std::u16string(u"~/狗/ǡ୫ԩϗ/にほ/ąę/ど/௸ඊƷ/狗6.txt") ==@@ -469,3 +458,177 @@ TEST(file_util, unicode_path_cast_from_wstring_to_char8_t) {ov::util::Path(L"~/狗/ǡ୫ԩϗ/にほ/ąę/ど/௸ඊƷ/狗33.txt").generic_u8string());#endif}++class FileUtilTest : public ::testing::Test {+protected:+ void SetUp() override {+ // Create a temporary files for testing+ { std::ofstream outfile("test_file_0.txt"); }+ {+ std::ofstream outfile("test_file_20.txt");+ outfile << "This is a test file.";+ }+ {+ std::ofstream outfile("test_file_20x1000.txt");+ for (int i = 0; i < 1000; ++i) {+ outfile << "This is a test file.";+ }+ }+ {+ std::ofstream outfile("test_file_raw_bytes_746.txt", std::ios::binary);+ std::vector<char> buffer(746, 0);+ std::random_device rd;+ std::mt19937 gen(rd());+ std::uniform_int_distribution<> dis(0, 255);++ std::generate(buffer.begin(), buffer.end(), [&]() {+ return static_cast<char>(dis(gen));+ });++ outfile.write(buffer.data(), buffer.size());+ }++#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+# ifdef OPENVINO_CPP_VER_AT_LEAST_20+ {+ std::ofstream outfile(ov::util::Path(u8"这是_u8.txt"));+ outfile << "This is a test file.";+ }+# endif+ {+ std::ofstream outfile(ov::util::Path(u"这是_u16.txt"));+ outfile << "This is a test file.";+ }+ {+ std::ofstream outfile(ov::util::Path(U"这是_u32.txt"));+ outfile << "This is a test file.";+ }+ {+ std::ofstream outfile(ov::util::make_path(L"这是_wchar.txt"));+ outfile << "This is a test file.";+ }+#endif++#if defined(__ANDROID__) || defined(ANDROID)+ {+ std::ofstream outfile("android_test_file_20.txt");+ outfile << "This is a test file.";+ }+#endif+ }++ void TearDown() override {+ // Remove the temporary files after testing+ std::filesystem::remove("test_file_0.txt");+ std::filesystem::remove("test_file_20.txt");+ std::filesystem::remove("test_file_raw_bytes_746.txt");+ std::filesystem::remove("test_file_20x1000.txt");+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+ std::filesystem::remove(u8"这是_u8.txt");+ std::filesystem::remove(u"这是_u16.txt");+ std::filesystem::remove(U"这是_u32.txt");+ std::filesystem::remove(ov::util::make_path(L"这是_wchar.txt"));+#endif+#if defined(__ANDROID__) || defined(ANDROID)+ std::filesystem::remove("android_test_file_20.txt");+#endif+ }+};++TEST_F(FileUtilTest, FileSizeNonExistentFileTest) {+ EXPECT_EQ(ov::util::file_size("non_existent_file.txt"), -1);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("non_existent_file.txt")), -1);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"non_existent_file.txt")), -1);+}++TEST_F(FileUtilTest, EmptyFileSizeTest) {+#ifdef OPENVINO_CPP_VER_AT_LEAST_20+ EXPECT_EQ(ov::util::file_size(u8"test_file_0.txt"), 0);+#endif+ EXPECT_EQ(ov::util::file_size("test_file_0.txt"), 0);+ EXPECT_EQ(ov::util::file_size(u"test_file_0.txt"), 0);+ EXPECT_EQ(ov::util::file_size(U"test_file_0.txt"), 0);+ EXPECT_EQ(ov::util::file_size(std::wstring(L"test_file_0.txt")), 0);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("test_file_0.txt")), 0);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(u8"test_file_0.txt")), 0);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(u"test_file_0.txt")), 0);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(U"test_file_0.txt")), 0);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(L"test_file_0.txt")), 0);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"test_file_0.txt")), 0);+}++TEST_F(FileUtilTest, FileSizeTest) {+ EXPECT_EQ(ov::util::file_size("test_file_20.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"test_file_20.txt")), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("test_file_20.txt")), 20);+}++TEST_F(FileUtilTest, FileSizeRawBytesTest) {+ EXPECT_EQ(ov::util::file_size("test_file_raw_bytes_746.txt"), 746);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"test_file_raw_bytes_746.txt")), 746);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("test_file_raw_bytes_746.txt")), 746);+}++TEST_F(FileUtilTest, LargeFileSizeTest) {+ EXPECT_EQ(ov::util::file_size("test_file_20x1000.txt"), 20 * 1000);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"test_file_20x1000.txt")), 20 * 1000);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("test_file_20x1000.txt")), 20 * 1000);+}++#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT++# ifdef OPENVINO_CPP_VER_AT_LEAST_20+TEST_F(FileUtilTest, u8FileSizeTest) {+ EXPECT_EQ(ov::util::file_size(u8"这是_u8.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(u8"这是_u8.txt")), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"这是_u8.txt")), 20);+}+# endif++TEST_F(FileUtilTest, u16FileSizeTest) {+ EXPECT_EQ(ov::util::file_size("这是_u16.txt"), 20);+# ifdef OPENVINO_CPP_VER_AT_LEAST_20+ EXPECT_EQ(ov::util::file_size(u8"这是_u16.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(u8"这是_u16.txt")), 20);+# endif+ EXPECT_EQ(ov::util::file_size(u"这是_u16.txt"), 20);+ EXPECT_EQ(ov::util::file_size(U"这是_u16.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"这是_u16.txt")), 20);+ EXPECT_EQ(ov::util::file_size(std::wstring(L"这是_u16.txt")), 20);++ EXPECT_EQ(ov::util::file_size(ov::util::Path(u"这是_u16.txt")), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(U"这是_u16.txt")), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(std::wstring(L"这是_u16.txt"))), 20);+}++TEST_F(FileUtilTest, u32FileSizeTest) {+ EXPECT_EQ(ov::util::file_size(U"这是_u32.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(U"这是_u32.txt")), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"这是_u32.txt")), 20);+}++TEST_F(FileUtilTest, wcharFileSizeTest) {+# ifdef _MSC_VER+ EXPECT_EQ(ov::util::file_size(L"这是_wchar.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path(L"这是_wchar.txt")), 20);+# else+ EXPECT_EQ(ov::util::file_size("这是_wchar.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("这是_wchar.txt")), 20);+# endif+ EXPECT_EQ(ov::util::file_size("这是_wchar.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::make_path(L"这是_wchar.txt")), 20);+}+#endif++#if defined(__ANDROID__) || defined(ANDROID)+TEST_F(FileUtilTest, androidFileSizeTest) {+ EXPECT_EQ(ov::util::file_size("android_test_file_20.txt"), 20);+ EXPECT_EQ(ov::util::file_size(L"android_test_file_20.txt"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("android_test_file_20.txt")), 20);+}+TEST_F(FileUtilTest, androidWithCutFileSizeTest) {+ EXPECT_EQ(ov::util::file_size("android_test_file_20.txt!_to_cut.jar"), 20);+ EXPECT_EQ(ov::util::file_size(L"android_test_file_20.txt!_to_cut.jar"), 20);+ EXPECT_EQ(ov::util::file_size(ov::util::Path("android_test_file_20.txt!_to_cut.jar")), 20);+}+#endif