1 /*
2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
387 count = WideCharToMultiByte(CP_UTF8, 0, value, -1, NULL, 0, NULL, NULL);
388
389 if (count > 0) {
390 result.data = new char[count + 1];
391 result.length = WideCharToMultiByte(CP_UTF8, 0, value, -1,
392 result.data, (int)count, NULL, NULL);
393 }
394
395 return result;
396 }
397
398 // Owner must free the return value.
399 WideString Platform::MultibyteStringToWideString(const char* value) {
400 WideString result;
401 size_t count = 0;
402
403 if (value == NULL) {
404 return result;
405 }
406
407 mbstowcs_s(&count, NULL, 0, value, _TRUNCATE);
408
409 if (count > 0) {
410 result.data = new wchar_t[count + 1];
411 mbstowcs_s(&result.length, result.data, count, value, count);
412 }
413
414 return result;
415 }
416
417 FileHandle::FileHandle(std::wstring FileName) {
418 FHandle = ::CreateFile(FileName.data(), GENERIC_READ, FILE_SHARE_READ,
419 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
420 }
421
422 FileHandle::~FileHandle() {
423 if (IsValid() == true) {
424 ::CloseHandle(FHandle);
425 }
426 }
427
428 bool FileHandle::IsValid() {
429 return FHandle != INVALID_HANDLE_VALUE;
430 }
431
|
1 /*
2 * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
387 count = WideCharToMultiByte(CP_UTF8, 0, value, -1, NULL, 0, NULL, NULL);
388
389 if (count > 0) {
390 result.data = new char[count + 1];
391 result.length = WideCharToMultiByte(CP_UTF8, 0, value, -1,
392 result.data, (int)count, NULL, NULL);
393 }
394
395 return result;
396 }
397
398 // Owner must free the return value.
399 WideString Platform::MultibyteStringToWideString(const char* value) {
400 WideString result;
401 size_t count = 0;
402
403 if (value == NULL) {
404 return result;
405 }
406
407 count = MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
408 value, -1, NULL, 0);
409
410 if (count > 0) {
411 result.data = new wchar_t[count];
412 result.length = MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
413 value, -1, result.data, (int)count);
414 if (result.length == 0) {
415 delete[] result.data;
416 result.data = NULL;
417 }
418 }
419
420 return result;
421 }
422
423 FileHandle::FileHandle(std::wstring FileName) {
424 FHandle = ::CreateFile(FileName.data(), GENERIC_READ, FILE_SHARE_READ,
425 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
426 }
427
428 FileHandle::~FileHandle() {
429 if (IsValid() == true) {
430 ::CloseHandle(FHandle);
431 }
432 }
433
434 bool FileHandle::IsValid() {
435 return FHandle != INVALID_HANDLE_VALUE;
436 }
437
|