Skip to content

Commit a58afe9

Browse files
author
Diptorup Deb
committed
Add a helper to convert a C++ string to and a C string.
1 parent 38d5a1f commit a58afe9

File tree

6 files changed

+85
-102
lines changed

6 files changed

+85
-102
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- dpctl_string_utils.hpp - C++ to C string converted -*-C++-*- ===//
2+
//
3+
// Data Parallel Control (dpctl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// Helper function to convert a C++ string to a C string.
23+
//===----------------------------------------------------------------------===//
24+
#include <cstring>
25+
#include <iostream>
26+
#include <string>
27+
28+
#pragma once
29+
30+
namespace dpctl
31+
{
32+
namespace helper
33+
{
34+
/*!
35+
* @brief Convert a C++ std::string to a const char* and return the string to
36+
* caller.
37+
*
38+
* @param str A C++ string that has to be converted to a C string.
39+
* @return A const char* string representation of the C++ string.
40+
*/
41+
static inline __dpctl_give const char *
42+
cstring_from_string(const std::string &str)
43+
{
44+
char *cstr = nullptr;
45+
try {
46+
auto cstr_len = str.length() + 1;
47+
cstr = new char[cstr_len];
48+
#ifdef _WIN32
49+
strncpy_s(cstr, cstr_len, str.c_str(), cstr_len);
50+
#else
51+
std::strncpy(cstr, str.c_str(), cstr_len);
52+
#endif
53+
} catch (std::bad_alloc const &ba) {
54+
// \todo log error
55+
std::cerr << ba.what() << '\n';
56+
}
57+
58+
return cstr;
59+
}
60+
} // namespace helper
61+
} // namespace dpctl

dpctl-capi/source/dpctl_service.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,13 @@
2626
#include "dpctl_service.h"
2727
#include "Config/dpctl_config.h"
2828

29+
#include "../helper/include/dpctl_string_utils.hpp"
2930
#include <algorithm>
3031
#include <cstring>
3132
#include <iostream>
3233

3334
__dpctl_give const char *DPCTLService_GetDPCPPVersion(void)
3435
{
3536
std::string version = DPCTL_DPCPP_VERSION;
36-
char *version_cstr = nullptr;
37-
try {
38-
auto cstr_len = version.length() + 1;
39-
version_cstr = new char[cstr_len];
40-
#ifdef _WIN32
41-
strncpy_s(version_cstr, cstr_len, version.c_str(), cstr_len);
42-
#else
43-
std::strncpy(version_cstr, version.c_str(), cstr_len);
44-
#endif
45-
} catch (std::bad_alloc const &ba) {
46-
// \todo log error
47-
std::cerr << ba.what() << '\n';
48-
}
49-
return version_cstr;
37+
return dpctl::helper::cstring_from_string(version);
5038
}

dpctl-capi/source/dpctl_sycl_device_interface.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_device_interface.h"
28+
#include "../helper/include/dpctl_string_utils.hpp"
2829
#include "../helper/include/dpctl_utils_helper.h"
2930
#include "Support/CBindingWrapping.h"
3031
#include "dpctl_sycl_device_manager.h"
@@ -307,21 +308,12 @@ DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef)
307308
__dpctl_give const char *
308309
DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef)
309310
{
310-
char *cstr_name = nullptr;
311+
const char *cstr_name = nullptr;
311312
auto D = unwrap(DRef);
312313
if (D) {
313314
try {
314315
auto name = D->get_info<info::device::name>();
315-
auto cstr_len = name.length() + 1;
316-
cstr_name = new char[cstr_len];
317-
#ifdef _WIN32
318-
strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len);
319-
#else
320-
std::strncpy(cstr_name, name.c_str(), cstr_len);
321-
#endif
322-
} catch (std::bad_alloc const &ba) {
323-
// \todo log error
324-
std::cerr << ba.what() << '\n';
316+
cstr_name = dpctl::helper::cstring_from_string(name);
325317
} catch (runtime_error const &re) {
326318
// \todo log error
327319
std::cerr << re.what() << '\n';
@@ -333,21 +325,12 @@ DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef)
333325
__dpctl_give const char *
334326
DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef)
335327
{
336-
char *cstr_vendor = nullptr;
328+
const char *cstr_vendor = nullptr;
337329
auto D = unwrap(DRef);
338330
if (D) {
339331
try {
340332
auto vendor = D->get_info<info::device::vendor>();
341-
auto cstr_len = vendor.length() + 1;
342-
cstr_vendor = new char[cstr_len];
343-
#ifdef _WIN32
344-
strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len);
345-
#else
346-
std::strncpy(cstr_vendor, vendor.c_str(), cstr_len);
347-
#endif
348-
} catch (std::bad_alloc const &ba) {
349-
// \todo log error
350-
std::cerr << ba.what() << '\n';
333+
cstr_vendor = dpctl::helper::cstring_from_string(vendor);
351334
} catch (runtime_error const &re) {
352335
// \todo log error
353336
std::cerr << re.what() << '\n';
@@ -359,21 +342,12 @@ DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef)
359342
__dpctl_give const char *
360343
DPCTLDevice_GetDriverVersion(__dpctl_keep const DPCTLSyclDeviceRef DRef)
361344
{
362-
char *cstr_driver = nullptr;
345+
const char *cstr_driver = nullptr;
363346
auto D = unwrap(DRef);
364347
if (D) {
365348
try {
366349
auto driver = D->get_info<info::device::driver_version>();
367-
auto cstr_len = driver.length() + 1;
368-
cstr_driver = new char[cstr_len];
369-
#ifdef _WIN32
370-
strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len);
371-
#else
372-
std::strncpy(cstr_driver, driver.c_str(), cstr_len);
373-
#endif
374-
} catch (std::bad_alloc const &ba) {
375-
// \todo log error
376-
std::cerr << ba.what() << '\n';
350+
cstr_driver = dpctl::helper::cstring_from_string(driver);
377351
} catch (runtime_error const &re) {
378352
// \todo log error
379353
std::cerr << re.what() << '\n';

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//===----------------------------------------------------------------------===//
2525

2626
#include "dpctl_sycl_device_manager.h"
27+
#include "../helper/include/dpctl_string_utils.hpp"
2728
#include "../helper/include/dpctl_utils_helper.h"
2829
#include "Support/CBindingWrapping.h"
2930
#include "dpctl_sycl_enum_types.h"
@@ -171,21 +172,12 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
171172
__dpctl_give const char *
172173
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef)
173174
{
174-
char *cstr_info = nullptr;
175+
const char *cstr_info = nullptr;
175176
auto D = unwrap(DRef);
176177
if (D) {
177178
try {
178179
auto infostr = get_device_info_str(*D);
179-
auto cstr_len = infostr.length() + 1;
180-
cstr_info = new char[cstr_len];
181-
#ifdef _WIN32
182-
strncpy_s(cstr_info, cstr_len, infostr.c_str(), cstr_len);
183-
#else
184-
std::strncpy(cstr_info, infostr.c_str(), cstr_len);
185-
#endif
186-
} catch (std::bad_alloc const &ba) {
187-
// \todo log error
188-
std::cerr << ba.what() << '\n';
180+
cstr_info = dpctl::helper::cstring_from_string(infostr);
189181
} catch (runtime_error const &re) {
190182
// \todo log error
191183
std::cerr << re.what() << '\n';

dpctl-capi/source/dpctl_sycl_kernel_interface.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_kernel_interface.h"
28+
#include "../helper/include/dpctl_string_utils.hpp"
2829
#include "Support/CBindingWrapping.h"
2930
#include <CL/sycl.hpp> /* Sycl headers */
3031

@@ -49,14 +50,7 @@ DPCTLKernel_GetFunctionName(__dpctl_keep const DPCTLSyclKernelRef Kernel)
4950
auto kernel_name = SyclKernel->get_info<info::kernel::function_name>();
5051
if (kernel_name.empty())
5152
return nullptr;
52-
auto cstr_len = kernel_name.length() + 1;
53-
auto cstr_name = new char[cstr_len];
54-
#ifdef _WIN32
55-
strncpy_s(cstr_name, cstr_len, kernel_name.c_str(), cstr_len);
56-
#else
57-
std::strncpy(cstr_name, kernel_name.c_str(), cstr_len);
58-
#endif
59-
return cstr_name;
53+
return dpctl::helper::cstring_from_string(kernel_name);
6054
}
6155

6256
size_t DPCTLKernel_GetNumArgs(__dpctl_keep const DPCTLSyclKernelRef Kernel)

dpctl-capi/source/dpctl_sycl_platform_interface.cpp

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_platform_interface.h"
28+
#include "../helper/include/dpctl_string_utils.hpp"
2829
#include "../helper/include/dpctl_utils_helper.h"
2930
#include "Support/CBindingWrapping.h"
3031
#include <CL/sycl.hpp>
@@ -121,88 +122,61 @@ DPCTLPlatform_GetBackend(__dpctl_keep const DPCTLSyclPlatformRef PRef)
121122
__dpctl_give const char *
122123
DPCTLPlatform_GetName(__dpctl_keep const DPCTLSyclPlatformRef PRef)
123124
{
124-
char *cstr_name = nullptr;
125125
auto P = unwrap(PRef);
126126
if (P) {
127127
try {
128128
auto name = P->get_info<info::platform::name>();
129-
auto cstr_len = name.length() + 1;
130-
cstr_name = new char[cstr_len];
131-
#ifdef _WIN32
132-
strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len);
133-
#else
134-
std::strncpy(cstr_name, name.c_str(), cstr_len);
135-
#endif
136-
} catch (std::bad_alloc const &ba) {
137-
// \todo log error
138-
std::cerr << ba.what() << '\n';
129+
return dpctl::helper::cstring_from_string(name);
139130
} catch (runtime_error const &re) {
140131
// \todo log error
141132
std::cerr << re.what() << '\n';
133+
return nullptr;
142134
}
143135
}
144136
else {
145137
std::cerr << "Name cannot be looked up for a NULL platform\n";
138+
return nullptr;
146139
}
147-
return cstr_name;
148140
}
149141

150142
__dpctl_give const char *
151143
DPCTLPlatform_GetVendor(__dpctl_keep const DPCTLSyclPlatformRef PRef)
152144
{
153-
char *cstr_vendor = nullptr;
154145
auto P = unwrap(PRef);
155146
if (P) {
156147
try {
157148
auto vendor = P->get_info<info::platform::vendor>();
158-
auto cstr_len = vendor.length() + 1;
159-
cstr_vendor = new char[cstr_len];
160-
#ifdef _WIN32
161-
strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len);
162-
#else
163-
std::strncpy(cstr_vendor, vendor.c_str(), cstr_len);
164-
#endif
165-
} catch (std::bad_alloc const &ba) {
166-
// \todo log error
167-
std::cerr << ba.what() << '\n';
149+
return dpctl::helper::cstring_from_string(vendor);
168150
} catch (runtime_error const &re) {
169151
// \todo log error
170152
std::cerr << re.what() << '\n';
153+
return nullptr;
171154
}
172155
}
173156
else {
174157
std::cerr << "Vendor cannot be looked up for a NULL platform\n";
158+
return nullptr;
175159
}
176-
return cstr_vendor;
177160
}
178161

179162
__dpctl_give const char *
180163
DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef)
181164
{
182-
char *cstr_driver = nullptr;
183165
auto P = unwrap(PRef);
184166
if (P) {
185167
try {
186168
auto driver = P->get_info<info::platform::version>();
187-
auto cstr_len = driver.length() + 1;
188-
cstr_driver = new char[cstr_len];
189-
#ifdef _WIN32
190-
strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len);
191-
#else
192-
std::strncpy(cstr_driver, driver.c_str(), cstr_len);
193-
#endif
194-
} catch (std::bad_alloc const &ba) {
195-
// \todo log error
196-
std::cerr << ba.what() << '\n';
169+
return dpctl::helper::cstring_from_string(driver);
197170
} catch (runtime_error const &re) {
198171
// \todo log error
199172
std::cerr << re.what() << '\n';
173+
return nullptr;
200174
}
201175
}
202176
else {
203177
std::cerr << "Driver version cannot be looked up for a NULL platform\n";
178+
return nullptr;
204179
}
205-
return cstr_driver;
206180
}
207181

208182
__dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()

0 commit comments

Comments
 (0)