@@ -25,7 +25,7 @@ import dmd.root.file;
25
25
import dmd.root.filename;
26
26
import dmd.root.outbuffer;
27
27
import dmd.root.rmem;
28
- import dmd.root.string ;
28
+ import dmd.root.string : toDString ;
29
29
30
30
version (IN_LLVM) enum supportedPre2017Versions = [" 14.0" .ptr];
31
31
else enum supportedPre2017Versions = [" 14.0" .ptr, " 12.0" , " 11.0" , " 10.0" , " 9.0" ];
142
142
{
143
143
// debug info needs DLLs from $(VSInstallDir)\Common7\IDE for most linker versions
144
144
// so prepend it too the PATH environment variable
145
- const path = getenv(" PATH" w);
145
+ char * path = getenv(" PATH" w);
146
146
const pathlen = strlen(path);
147
147
const addpathlen = strlen(addpath);
148
148
@@ -267,7 +267,7 @@ version (IN_LLVM)
267
267
// VS Build Tools 2017 (default installation path)
268
268
const numWritten = ExpandEnvironmentStringsW(r " %ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools" w.ptr, buffer.ptr, buffer.length);
269
269
if (numWritten <= buffer.length && exists(buffer.ptr))
270
- VSInstallDir = toUTF8 (buffer.ptr) ;
270
+ VSInstallDir = toNarrowStringz (buffer[ 0 .. numWritten - 1 ]).ptr ;
271
271
}
272
272
273
273
if (VSInstallDir is null )
@@ -507,7 +507,7 @@ extern(D):
507
507
// one with the largest version that also contains the test file
508
508
static const (char )* findLatestSDKDir (const (char )* baseDir, const (char )* testfile)
509
509
{
510
- wchar [] wbase = toUTF16 (baseDir);
510
+ wchar [] wbase = toWStringz (baseDir.toDString );
511
511
wchar * allfiles = cast (wchar * ) mem.xmalloc_noscan((wbase.length + 3 ) * wchar .sizeof);
512
512
scope (exit) mem.xfree(allfiles);
513
513
allfiles[0 .. wbase.length] = wbase;
@@ -524,16 +524,16 @@ extern(D):
524
524
{
525
525
if (fileinfo.cFileName[0 ] >= ' 1' && fileinfo.cFileName[0 ] <= ' 9' )
526
526
{
527
- auto name = toUTF8 (fileinfo.cFileName.ptr);
528
- if ((! res || strcmp(res, name) < 0 ) &&
529
- FileName.exists(buildPath(baseDir, name, testfile)))
527
+ char [] name = toNarrowStringz (fileinfo.cFileName.ptr.toDString );
528
+ if ((! res || strcmp(res, name.ptr ) < 0 ) &&
529
+ FileName.exists(buildPath(baseDir, name.ptr , testfile)))
530
530
{
531
531
if (res)
532
532
mem.xfree(res);
533
- res = name;
533
+ res = name.ptr ;
534
534
}
535
535
else
536
- mem.xfree(name);
536
+ mem.xfree(name.ptr );
537
537
}
538
538
}
539
539
while (FindNextFileW(h, &fileinfo));
@@ -551,7 +551,7 @@ extern(D):
551
551
* softwareKeyPath = path below HKLM\SOFTWARE
552
552
* valueName = name of the value to read
553
553
* Returns:
554
- * the registry value (in UTF8) if it exists and has string type
554
+ * the registry value if it exists and has string type
555
555
*/
556
556
const (char )* GetRegistryString (const (char )* softwareKeyPath, wstring valueName) const
557
557
{
@@ -581,20 +581,23 @@ extern(D):
581
581
DWORD size = buf.sizeof;
582
582
DWORD type;
583
583
int hr = RegQueryValueExW(key, valueName.ptr, null , &type, cast (ubyte * ) buf.ptr, &size);
584
- if (type != REG_SZ )
584
+ if (type != REG_SZ || size == 0 )
585
585
return null ;
586
586
587
587
wchar * wideValue = buf.ptr;
588
588
scope (exit) wideValue != buf.ptr && mem.xfree(wideValue);
589
589
if (hr == ERROR_MORE_DATA )
590
590
{
591
- wideValue = cast (wchar * ) mem.xmalloc_noscan(( size + 1 ) * wchar .sizeof );
591
+ wideValue = cast (wchar * ) mem.xmalloc_noscan(size);
592
592
hr = RegQueryValueExW(key, valueName.ptr, null , &type, cast (ubyte * ) wideValue, &size);
593
593
}
594
- if (hr != 0 || size <= 0 )
594
+ if (hr != 0 )
595
595
return null ;
596
596
597
- return toUTF8 (wideValue);
597
+ auto wideLength = size / wchar .sizeof;
598
+ if (wideValue[wideLength - 1 ] == 0 ) // may or may not be null-terminated
599
+ -- wideLength;
600
+ return toNarrowStringz (wideValue[0 .. wideLength]).ptr;
598
601
}
599
602
600
603
/* **
@@ -630,36 +633,17 @@ extern(D):
630
633
631
634
private :
632
635
633
- char * toUTF8 ( const (wchar )* wide )
636
+ inout ( wchar )[] toDString ( inout (wchar )* s )
634
637
{
635
- if (! wide)
636
- return null ;
637
-
638
- const requiredSize = WideCharToMultiByte(CP_UTF8 , 0 , wide, - 1 , null , 0 , null , null );
639
- char * value = cast (char * ) mem.xmalloc_noscan(requiredSize);
640
- const size = WideCharToMultiByte(CP_UTF8 , 0 , wide, - 1 , value, requiredSize, null , null );
641
- assert (size == requiredSize);
642
- return value;
643
- }
644
-
645
- wchar [] toUTF16 (const (char )* utf8)
646
- {
647
- if (! utf8)
648
- return null ;
649
-
650
- const requiredCount = MultiByteToWideChar(CP_UTF8 , 0 , utf8, - 1 , null , 0 );
651
- wchar * wide = cast (wchar * ) mem.xmalloc_noscan(requiredCount * wchar .sizeof);
652
- const count = MultiByteToWideChar(CP_UTF8 , 0 , utf8, - 1 , wide, requiredCount);
653
- assert (count == requiredCount);
654
- return wide[0 .. count- 1 ];
638
+ return s ? s[0 .. wcslen(s)] : null ;
655
639
}
656
640
657
641
extern (C ) wchar * _wgetenv(const (wchar )* name);
658
642
659
643
char * getenv (wstring name)
660
644
{
661
645
if (auto wide = _wgetenv(name.ptr))
662
- return toUTF8 (wide) ;
646
+ return toNarrowStringz (wide.toDString).ptr ;
663
647
return null ;
664
648
}
665
649
@@ -720,7 +704,6 @@ bool exists(const(wchar)* path)
720
704
// COM interfaces to find VS2017+ installations
721
705
import core.sys.windows.com ;
722
706
import core.sys.windows.wtypes : BSTR ;
723
- import core.sys.windows.winnls : MultiByteToWideChar, WideCharToMultiByte, CP_UTF8 ;
724
707
import core.sys.windows.oleauto : SysFreeString, SysStringLen;
725
708
726
709
pragma (lib, " ole32.lib" );
@@ -787,9 +770,14 @@ const(char)* detectVSInstallDirViaCOM()
787
770
BSTR ptr;
788
771
this (this ) @disable ;
789
772
~this () { SysFreeString(ptr); }
790
- bool opCast (T : bool )() { return ptr ! is null ; }
773
+ bool opCast (T : bool )() const { return ptr ! is null ; }
791
774
size_t length () { return SysStringLen(ptr); }
792
- void moveTo (ref WrappedBString other) { SysFreeString(other.ptr); other.ptr = ptr; ptr = null ; }
775
+ void moveTo (ref WrappedBString other)
776
+ {
777
+ SysFreeString(other.ptr);
778
+ other.ptr = ptr;
779
+ ptr = null ;
780
+ }
793
781
}
794
782
795
783
WrappedBString versionString, installDir;
@@ -818,5 +806,5 @@ const(char)* detectVSInstallDirViaCOM()
818
806
thisInstallDir.moveTo(installDir);
819
807
}
820
808
821
- return installDir ? toUTF8 (installDir.ptr) : null ;
809
+ return ! installDir ? null : toNarrowStringz (installDir.ptr[ 0 .. installDir.length]).ptr ;
822
810
}
0 commit comments