Skip to content

Commit

Permalink
[CORE] OV file utils file_size use std file path (#28462)
Browse files Browse the repository at this point in the history
### Details:
- refactor file_size function to accept ov::util::Path /
std::filesystem::path. Be backward compatible with previous version.

### Tickets:
 - [*146659*](https://jira.devtools.intel.com/browse/CVS-146659)

---------

Co-authored-by: Michal Lukaszewski <[email protected]>
  • Loading branch information
barnasm1 and mlukasze authored Feb 5, 2025
1 parent d387020 commit e6be681
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 101 deletions.
34 changes: 30 additions & 4 deletions src/common/util/include/openvino/util/file_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);

#endif
61 changes: 16 additions & 45 deletions src/common/util/include/openvino/util/file_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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_VER
inline 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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -397,5 +364,9 @@ inline std::basic_string<C> make_path(const std::basic_string<C>& folder, const
return 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 ov
29 changes: 29 additions & 0 deletions src/common/util/include/openvino/util/wstring_convert_util.hpp
Original file line number Diff line number Diff line change
@@ -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::util
15 changes: 15 additions & 0 deletions src/common/util/src/file_path.cpp
Original file line number Diff line number Diff line change
@@ -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) {}

#endif
40 changes: 0 additions & 40 deletions src/common/util/src/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions src/common/util/src/wstring_convert_util.cpp
Original file line number Diff line number Diff line change
@@ -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_SUPPORT
Loading

0 comments on commit e6be681

Please sign in to comment.