Skip to content

Commit

Permalink
Add a helper to convert a C++ string to and a C string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diptorup Deb committed Oct 5, 2021
1 parent 38d5a1f commit a58afe9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 102 deletions.
61 changes: 61 additions & 0 deletions dpctl-capi/helper/include/dpctl_string_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===--- dpctl_string_utils.hpp - C++ to C string converted -*-C++-*- ===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Helper function to convert a C++ string to a C string.
//===----------------------------------------------------------------------===//
#include <cstring>
#include <iostream>
#include <string>

#pragma once

namespace dpctl
{
namespace helper
{
/*!
* @brief Convert a C++ std::string to a const char* and return the string to
* caller.
*
* @param str A C++ string that has to be converted to a C string.
* @return A const char* string representation of the C++ string.
*/
static inline __dpctl_give const char *
cstring_from_string(const std::string &str)
{
char *cstr = nullptr;
try {
auto cstr_len = str.length() + 1;
cstr = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr, cstr_len, str.c_str(), cstr_len);
#else
std::strncpy(cstr, str.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
}

return cstr;
}
} // namespace helper
} // namespace dpctl
16 changes: 2 additions & 14 deletions dpctl-capi/source/dpctl_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,13 @@
#include "dpctl_service.h"
#include "Config/dpctl_config.h"

#include "../helper/include/dpctl_string_utils.hpp"
#include <algorithm>
#include <cstring>
#include <iostream>

__dpctl_give const char *DPCTLService_GetDPCPPVersion(void)
{
std::string version = DPCTL_DPCPP_VERSION;
char *version_cstr = nullptr;
try {
auto cstr_len = version.length() + 1;
version_cstr = new char[cstr_len];
#ifdef _WIN32
strncpy_s(version_cstr, cstr_len, version.c_str(), cstr_len);
#else
std::strncpy(version_cstr, version.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
}
return version_cstr;
return dpctl::helper::cstring_from_string(version);
}
40 changes: 7 additions & 33 deletions dpctl-capi/source/dpctl_sycl_device_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//===----------------------------------------------------------------------===//

#include "dpctl_sycl_device_interface.h"
#include "../helper/include/dpctl_string_utils.hpp"
#include "../helper/include/dpctl_utils_helper.h"
#include "Support/CBindingWrapping.h"
#include "dpctl_sycl_device_manager.h"
Expand Down Expand Up @@ -307,21 +308,12 @@ DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef)
__dpctl_give const char *
DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
char *cstr_name = nullptr;
const char *cstr_name = nullptr;
auto D = unwrap(DRef);
if (D) {
try {
auto name = D->get_info<info::device::name>();
auto cstr_len = name.length() + 1;
cstr_name = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len);
#else
std::strncpy(cstr_name, name.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
cstr_name = dpctl::helper::cstring_from_string(name);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
Expand All @@ -333,21 +325,12 @@ DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef)
__dpctl_give const char *
DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
char *cstr_vendor = nullptr;
const char *cstr_vendor = nullptr;
auto D = unwrap(DRef);
if (D) {
try {
auto vendor = D->get_info<info::device::vendor>();
auto cstr_len = vendor.length() + 1;
cstr_vendor = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len);
#else
std::strncpy(cstr_vendor, vendor.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
cstr_vendor = dpctl::helper::cstring_from_string(vendor);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
Expand All @@ -359,21 +342,12 @@ DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef)
__dpctl_give const char *
DPCTLDevice_GetDriverVersion(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
char *cstr_driver = nullptr;
const char *cstr_driver = nullptr;
auto D = unwrap(DRef);
if (D) {
try {
auto driver = D->get_info<info::device::driver_version>();
auto cstr_len = driver.length() + 1;
cstr_driver = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len);
#else
std::strncpy(cstr_driver, driver.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
cstr_driver = dpctl::helper::cstring_from_string(driver);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
Expand Down
14 changes: 3 additions & 11 deletions dpctl-capi/source/dpctl_sycl_device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//===----------------------------------------------------------------------===//

#include "dpctl_sycl_device_manager.h"
#include "../helper/include/dpctl_string_utils.hpp"
#include "../helper/include/dpctl_utils_helper.h"
#include "Support/CBindingWrapping.h"
#include "dpctl_sycl_enum_types.h"
Expand Down Expand Up @@ -171,21 +172,12 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
__dpctl_give const char *
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
char *cstr_info = nullptr;
const char *cstr_info = nullptr;
auto D = unwrap(DRef);
if (D) {
try {
auto infostr = get_device_info_str(*D);
auto cstr_len = infostr.length() + 1;
cstr_info = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_info, cstr_len, infostr.c_str(), cstr_len);
#else
std::strncpy(cstr_info, infostr.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
cstr_info = dpctl::helper::cstring_from_string(infostr);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
Expand Down
10 changes: 2 additions & 8 deletions dpctl-capi/source/dpctl_sycl_kernel_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//===----------------------------------------------------------------------===//

#include "dpctl_sycl_kernel_interface.h"
#include "../helper/include/dpctl_string_utils.hpp"
#include "Support/CBindingWrapping.h"
#include <CL/sycl.hpp> /* Sycl headers */

Expand All @@ -49,14 +50,7 @@ DPCTLKernel_GetFunctionName(__dpctl_keep const DPCTLSyclKernelRef Kernel)
auto kernel_name = SyclKernel->get_info<info::kernel::function_name>();
if (kernel_name.empty())
return nullptr;
auto cstr_len = kernel_name.length() + 1;
auto cstr_name = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_name, cstr_len, kernel_name.c_str(), cstr_len);
#else
std::strncpy(cstr_name, kernel_name.c_str(), cstr_len);
#endif
return cstr_name;
return dpctl::helper::cstring_from_string(kernel_name);
}

size_t DPCTLKernel_GetNumArgs(__dpctl_keep const DPCTLSyclKernelRef Kernel)
Expand Down
46 changes: 10 additions & 36 deletions dpctl-capi/source/dpctl_sycl_platform_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//===----------------------------------------------------------------------===//

#include "dpctl_sycl_platform_interface.h"
#include "../helper/include/dpctl_string_utils.hpp"
#include "../helper/include/dpctl_utils_helper.h"
#include "Support/CBindingWrapping.h"
#include <CL/sycl.hpp>
Expand Down Expand Up @@ -121,88 +122,61 @@ DPCTLPlatform_GetBackend(__dpctl_keep const DPCTLSyclPlatformRef PRef)
__dpctl_give const char *
DPCTLPlatform_GetName(__dpctl_keep const DPCTLSyclPlatformRef PRef)
{
char *cstr_name = nullptr;
auto P = unwrap(PRef);
if (P) {
try {
auto name = P->get_info<info::platform::name>();
auto cstr_len = name.length() + 1;
cstr_name = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len);
#else
std::strncpy(cstr_name, name.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
return dpctl::helper::cstring_from_string(name);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
return nullptr;
}
}
else {
std::cerr << "Name cannot be looked up for a NULL platform\n";
return nullptr;
}
return cstr_name;
}

__dpctl_give const char *
DPCTLPlatform_GetVendor(__dpctl_keep const DPCTLSyclPlatformRef PRef)
{
char *cstr_vendor = nullptr;
auto P = unwrap(PRef);
if (P) {
try {
auto vendor = P->get_info<info::platform::vendor>();
auto cstr_len = vendor.length() + 1;
cstr_vendor = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len);
#else
std::strncpy(cstr_vendor, vendor.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
return dpctl::helper::cstring_from_string(vendor);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
return nullptr;
}
}
else {
std::cerr << "Vendor cannot be looked up for a NULL platform\n";
return nullptr;
}
return cstr_vendor;
}

__dpctl_give const char *
DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef)
{
char *cstr_driver = nullptr;
auto P = unwrap(PRef);
if (P) {
try {
auto driver = P->get_info<info::platform::version>();
auto cstr_len = driver.length() + 1;
cstr_driver = new char[cstr_len];
#ifdef _WIN32
strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len);
#else
std::strncpy(cstr_driver, driver.c_str(), cstr_len);
#endif
} catch (std::bad_alloc const &ba) {
// \todo log error
std::cerr << ba.what() << '\n';
return dpctl::helper::cstring_from_string(driver);
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
return nullptr;
}
}
else {
std::cerr << "Driver version cannot be looked up for a NULL platform\n";
return nullptr;
}
return cstr_driver;
}

__dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()
Expand Down

0 comments on commit a58afe9

Please sign in to comment.