From 2c31f9fc35b78f28875f5545f59adc17bc5bd43b Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Wed, 14 Aug 2024 14:41:12 +0200 Subject: [PATCH] Use toGenericString and return a Pathlib.Path instead of a string --- src/EnergyPlus/api/datatransfer.cc | 10 ++++++---- src/EnergyPlus/api/datatransfer.py | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/EnergyPlus/api/datatransfer.cc b/src/EnergyPlus/api/datatransfer.cc index 26017f46c0a..258b60c791e 100644 --- a/src/EnergyPlus/api/datatransfer.cc +++ b/src/EnergyPlus/api/datatransfer.cc @@ -243,16 +243,18 @@ void resetErrorFlag(EnergyPlusState state) char *inputFilePath(EnergyPlusState state) { const auto *thisState = static_cast(state); - char *p = new char[std::strlen(thisState->dataStrGlobals->inputFilePath.string().c_str()) + 1]; - std::strcpy(p, thisState->dataStrGlobals->inputFilePath.string().c_str()); + std::string const path_utf8 = EnergyPlus::FileSystem::toGenericString(thisState->dataStrGlobals->inputFilePath); + char *p = new char[std::strlen(path_utf8.c_str()) + 1]; + std::strcpy(p, path_utf8.c_str()); return p; } char *epwFilePath(EnergyPlusState state) { const auto *thisState = static_cast(state); - char *p = new char[std::strlen(thisState->files.inputWeatherFilePath.filePath.c_str()) + 1]; - std::strcpy(p, thisState->files.inputWeatherFilePath.filePath.c_str()); + std::string const path_utf8 = EnergyPlus::FileSystem::toGenericString(thisState->files.inputWeatherFilePath.filePath); + char *p = new char[std::strlen(path_utf8.c_str()) + 1]; + std::strcpy(p, path_utf8.c_str()); return p; } diff --git a/src/EnergyPlus/api/datatransfer.py b/src/EnergyPlus/api/datatransfer.py index 7f7985b9721..0a1c9112d28 100644 --- a/src/EnergyPlus/api/datatransfer.py +++ b/src/EnergyPlus/api/datatransfer.py @@ -56,7 +56,7 @@ from ctypes import cdll, c_int, c_char_p, c_void_p, POINTER, Structure, byref from pyenergyplus.common import RealEP, EnergyPlusException, is_number from typing import List, Union - +from pathlib import Path class DataExchange: """ @@ -363,27 +363,32 @@ def reset_api_error_flag(self, state: c_void_p) -> None: """ self.api.resetErrorFlag(state) - def get_input_file_path(self, state: c_void_p) -> bytes: + def get_input_file_path(self, state: c_void_p) -> Path: """ Provides the input file path back to the client. In most circumstances the client will know the path to the input file, but there are some cases where code is generalized in unexpected workflows. Users have requested a way to get the input file path back from the running instance. :param state: An active EnergyPlus "state" that is returned from a call to `api.state_manager.new_state()`. - :return: Returns a raw bytes representation of the input file path + :return: A pathlib.Path of the input file path """ - return self.api.inputFilePath(state) # TODO: Need to call free for the underlying char *? + c_string = self.api.inputFilePath(state) + res = Path(c_string.decode('utf-8')) + return res - def get_weather_file_path(self, state: c_void_p) -> bytes: + def get_weather_file_path(self, state: c_void_p) -> Path: """ Provides the weather file path back to the client. In most circumstances the client will know the path to the weather file, but there are some cases where code is generalized in unexpected workflows. Users have requested a way to get the weather file path back from the running instance. :param state: An active EnergyPlus "state" that is returned from a call to `api.state_manager.new_state()`. - :return: Returns a raw bytes representation of the weather file path + :return: A pathlib.Path of the weather file """ - return self.api.epwFilePath(state) + c_string = self.api.epwFilePath(state) + res = Path(c_string.decode('utf-8')) + return res + def get_object_names(self, state: c_void_p, object_type_name: Union[str, bytes]) -> List[str]: """