Changeset 85840 in vbox for trunk/tools/win/vbscript
- Timestamp:
- Aug 19, 2020 5:32:39 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/win/vbscript/helpers.vbs
r85824 r85840 27 27 dim g_objFileSys 28 28 Set g_objFileSys = WScript.CreateObject("Scripting.FileSystemObject") 29 30 '' Whether to ignore (continue) on errors. 31 dim g_blnContinueOnError 32 g_blnContinueOnError = False 33 34 '' The script's exit code (for ignored errors). 35 dim g_rcScript 36 g_rcScript = 0 29 37 30 38 … … 140 148 141 149 150 '' 151 ' Compare two paths w/o abspathing them. 152 ' 153 ' Ignores case, slash direction, multiple slashes and single dot components. 154 ' 155 function PathMatch(strPath1, strPath2) 156 PathMatch = true 157 if StrComp(strPath1, strPath2, vbTextCompare) <> 0 then 158 strPath1 = DosSlashes(strPath1) 159 strPath2 = DosSlashes(strPath2) 160 if StrComp(strPath1, strPath2, vbTextCompare) <> 0 then 161 ' Compare character by character 162 dim off1 : off1 = 1 163 dim off2 : off2 = 1 164 165 ' Compare UNC prefix if any, because the code below cannot handle it. UNC has exactly two slashes. 166 if Mid(strPath1, 1, 2) = "\\" and Mid(strPath2, 1, 2) = "\\" then 167 if (Mid(strPath1, 3, 1) = "\") <> (Mid(strPath2, 3, 1) = "\") then 168 PathMatch = false 169 exit function 170 end if 171 off1 = off1 + 2 172 off2 = off2 + 2 173 if Mid(strPath1, 3, 1) = "\" then 174 off1 = PathMatchSkipSlashesAndSlashDotHelper(strPath1, off1) 175 off2 = PathMatchSkipSlashesAndSlashDotHelper(strPath2, off2) 176 end if 177 end if 178 179 ' Compare the rest. 180 dim ch1, ch2 181 do while off1 <= Len(strPath1) and off2 <= Len(strPath2) 182 ch1 = Mid(strPath1, off1, 1) 183 ch2 = Mid(strPath2, off2, 1) 184 if StrComp(ch1, ch2, vbTextCompare) = 0 then 185 off1 = off1 + 1 186 off2 = off2 + 1 187 if ch1 = "\" then 188 off1 = PathMatchSkipSlashesAndSlashDotHelper(strPath1, off1) 189 off2 = PathMatchSkipSlashesAndSlashDotHelper(strPath2, off2) 190 end if 191 else 192 PathMatch = False 193 exit function 194 end if 195 loop 196 197 ' One or both of the strings ran out. That's fine if we've only got slashes 198 ' and "." components left in the other. 199 if off1 <= Len(strPath1) and Mid(strPath1, off1, 1) = "\" then 200 off1 = PathMatchSkipSlashesAndSlashDotHelper(strPath1, off1 + 1) 201 end if 202 if off2 <= Len(strPath2) and Mid(strPath2, off2, 1) = "\" then 203 off2 = PathMatchSkipSlashesAndSlashDotHelper(strPath2, off2 + 1) 204 end if 205 PathMatch = off1 > Len(strPath1) and off2 > Len(strPath2) 206 end if 207 end if 208 end function 209 210 '' PathMatch helper 211 function PathMatchSkipSlashesAndSlashDotHelper(strPath, off) 212 dim ch 213 do while off <= Len(strPath) 214 ch = Mid(strPath, off, 1) 215 if ch = "\" then 216 off = off + 1 217 elseif ch = "." and off = Len(strPath) then 218 off = off + 1 219 elseif ch = "." and Mid(strPath, off, 2) = ".\" then 220 off = off + 2 221 else 222 exit do 223 end if 224 loop 225 PathMatchSkipSlashesAndSlashDotHelper = off 226 end function 227 228 142 229 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 143 230 ' Helpers: Files and Dirs ' … … 393 480 394 481 '' 482 ' Gets an environment variable with default value if not found. 483 function EnvGetDef(strName, strDefault) 484 dim strValue 485 strValue = g_objShell.Environment("PROCESS")(strName) 486 if strValue = "" or IsNull(strValue) or IsEmpty(strValue) then 487 EnvGetDef = strDefault 488 else 489 EnvGetDef = strValue 490 end if 491 end function 492 493 494 '' 495 ' Gets an environment variable with default value if not found or not 496 ' in the array of valid values. Issue warning about invalid values. 497 function EnvGetDefValid(strName, strDefault, arrValidValues) 498 dim strValue 499 strValue = g_objShell.Environment("PROCESS")(strName) 500 if strValue = "" or IsNull(strValue) or IsEmpty(strValue) then 501 EnvGetDefValid = strDefault 502 elseif not ArrayContainsString(arrValidValues, strValue) then 503 MsgWarning "Invalid value " & strName & " value '" & EnvGetDefValid & "', using '" & strDefault & "' instead." 504 EnvGetDefValid = strDefault 505 else 506 EnvGetDefValid = strValue 507 end if 508 end function 509 510 511 '' 395 512 ' Sets an environment variable. 396 513 sub EnvSet(strName, strValue) … … 401 518 402 519 '' 403 ' Appends a string to an environment variable 404 sub EnvAppend(strName, strValue) 405 dim str 406 str = g_objShell.Environment("PROCESS")(strName) 407 g_objShell.Environment("PROCESS")(strName) = str & strValue 408 LogPrint "EnvAppend: " & strName & "=" & str & strValue 520 ' Prepends a string to an Path-like environment variable. 521 function EnvPrependItemEx(strName, strItem, strSep, blnKeepEmpty, ByRef fnItemMatcher) 522 dim strValue 523 strValue = EnvRemoveItemEx(strName, strItem, strSep, blnKeepEmpty, fnItemMatcher, "EnvPrependItemEx") 524 if strValue <> "" then 525 strValue = strItem & strSep & strValue 526 else 527 strValue = strItem 528 end if 529 g_objShell.Environment("PROCESS")(strName) = strValue 530 EnvPrependItemEx = strValue 531 end function 532 533 534 '' 535 ' Appends a string to an Path-like environment variable, 536 function EnvAppendItemEx(strName, strItem, strSep, blnKeepEmpty, ByRef fnItemMatcher) 537 dim strValue 538 strValue = EnvRemoveItemEx(strName, strItem, strSep, blnKeepEmpty, fnItemMatcher, "EnvAppendItemEx") 539 if strValue <> "" then 540 strValue = strValue & strSep & strItem 541 else 542 strValue = strItem 543 end if 544 g_objShell.Environment("PROCESS")(strName) = strValue 545 EnvAppendItemEx = strValue 546 end function 547 548 549 '' 550 ' Generic item remover. 551 ' 552 ' fnItemMatcher(strItem1, strItem2) 553 ' 554 function EnvRemoveItemEx(strName, strItem, strSep, blnKeepEmpty, ByRef fnItemMatcher, strCaller) 555 dim strValue, off 556 strValue = g_objShell.Environment("PROCESS")(strName) 557 EnvRemoveItemEx = strValue 558 if strValue <> "" then 559 ' Split it up into an array of items 560 dim arrItems : arrItems = Split(strValue, strSep, -1, vbTextCompare) 561 562 ' Create an array of matching indexes that we should remove. 563 dim cntToRemove : cntToRemove = 0 564 redim arrIdxToRemove(ArraySize(arrItems) - 1) 565 dim i, strCur 566 for i = LBound(arrItems) to UBound(arrItems) 567 strCur = arrItems(i) 568 if fnItemMatcher(strCur, strItem) or (not blnKeepEmpty and strCur = "") then 569 arrIdxToRemove(cntToRemove) = i 570 cntToRemove = cntToRemove + 1 571 end if 572 next 573 574 ' Did we find anthing to remove? 575 if cntToRemove > 0 then 576 ' Update the array and join it up again. 577 for i = cntToRemove - 1 to 0 step -1 578 arrItems = ArrayRemove(arrItems, arrIdxToRemove(i)) 579 next 580 dim strNewValue : strNewValue = ArrayJoinString(arrItems, strSep) 581 EnvRemoveItemEx = strNewValue 582 583 ' Update the environment variable. 584 LogPrint strCaller &": " & strName & ": '" & strValue & "' --> '" & strNewValue & "'" 585 g_objShell.Environment("PROCESS")(strName) = strNewValue 586 end if 587 end if 588 end function 589 590 591 '' 592 ' Generic case-insensitive item matcher. 593 ' See also PathMatch(). 594 function EnvItemMatch(strItem1, strItem2) 595 EnvItemMatch = (StrComp(strItem1, strItem2) = 0) 596 end function 597 598 599 '' 600 ' Prepends an item to an environment variable, after first removing any 601 ' existing ones (case-insensitive, preserves empty elements). 602 function EnvPrependItem(strName, strItem, strSep) 603 EnvPrependItem = EnvPrependItemEx(strName, strItem, strSep, true, GetRef("EnvItemMatch")) 604 LogPrint "EnvPrependItem: " & strName & "=" & EnvPrependPathItem 605 end function 606 607 608 '' 609 ' Appends an item to an environment variable, after first removing any 610 ' existing ones (case-insensitive, preserves empty elements). 611 function EnvAppendItem(strName, strItem, strSep) 612 EnvAppendItem = EnvAppendItemEx(strName, strItem, strSep, true, GetRef("EnvItemMatch")) 613 LogPrint "EnvAppendItem: " & strName & "=" & EnvPrependPathItem 614 end function 615 616 617 '' 618 ' Removes a string element from an environment variable, case 619 ' insensitive but preserving empty elements. 620 function EnvRemoveItem(strName, strItem, strSep) 621 EnvRemoveItem = EnvRemoveItemEx(strName, strIten, strSep, true, GetRef("EnvItemMatch"), "EnvRemoveItem") 622 end function 623 624 625 '' 626 ' Appends a string to an Path-like environment variable, 627 function EnvPrependPathItem(strName, strItem, strSep) 628 EnvPrependPathItem = EnvPrependItemEx(strName, strItem, strSep, false, GetRef("PathMatch")) 629 LogPrint "EnvPrependPathItem: " & strName & "=" & EnvPrependPathItem 630 end function 631 632 633 '' 634 ' Appends a string to an Path-like environment variable, 635 function EnvAppendPathItem(strName, strItem, strSep) 636 EnvAppendPathItem = EnvAppendItemEx(strName, strItem, strSep, false, GetRef("PathMatch")) 637 LogPrint "EnvAppendPathItem: " & strName & "=" & EnvAppendPathItem 638 end function 639 640 641 '' 642 ' Removes a string element from an Path-like environment variable, case 643 ' insensitive and treating forward and backward slashes the same way. 644 function EnvRemovePathItem(strName, strItem, strSep) 645 EnvRemovePathItem = EnvRemoveItemEx(strName, strIten, strSep, false, GetRef("PathMatch"), "EnvRemovePathItem") 646 end function 647 648 649 '' 650 ' Prepends a string to an environment variable 651 sub EnvUnset(strName) 652 g_objShell.Environment("PROCESS").Remove(strName) 653 LogPrint "EnvUnset: " & strName 409 654 end sub 410 655 411 412 ''413 ' Prepends a string to an environment variable414 sub EnvPrepend(strName, strValue)415 dim str416 str = g_objShell.Environment("PROCESS")(strName)417 g_objShell.Environment("PROCESS")(strName) = strValue & str418 LogPrint "EnvPrepend: " & strName & "=" & strValue & str419 end sub420 656 421 657 '' … … 426 662 EnvGetFirst = g_objShell.Environment("PROCESS")(strName2) 427 663 end if 664 end function 665 666 '' 667 ' Checks if the given enviornment variable exists. 668 function EnvExists(strName) 669 EnvExists = g_objShell.Environment("PROCESS")(strName) <> "" 428 670 end function 429 671 … … 718 960 ' Returns an Array() statement string 719 961 function ArrayToString(arrStrings) 720 dim strRet 962 dim strRet, i 721 963 strRet = "Array(" 722 964 for i = LBound(arrStrings) to UBound(arrStrings) … … 725 967 next 726 968 ArrayToString = strRet & ")" 969 end function 970 971 972 '' 973 ' Joins the elements of an array into a string using the given item separator. 974 ' @remark this is the same as Join() really. 975 function ArrayJoinString(arrStrings, strSep) 976 if ArraySize(arrStrings) = 0 then 977 ArrayJoinString = "" 978 else 979 dim i 980 ArrayJoinString = "" & arrStrings(LBound(arrStrings)) 981 for i = LBound(arrStrings) + 1 to UBound(arrStrings) 982 ArrayJoinString = ArrayJoinString & strSep & arrStrings(i) 983 next 984 end if 727 985 end function 728 986 … … 1144 1402 1145 1403 '' 1404 ' Info message. 1405 sub MsgInfo(strMsg) 1406 Print "info: " & strMsg 1407 end sub 1408 1409 1410 '' 1146 1411 ' Warning message. 1147 1412 sub MsgWarning(strMsg) … … 1167 1432 g_rcScript = 1 1168 1433 end sub 1434 1435 '' 1436 ' Error message, fatal unless flag to ignore errors is given. 1437 ' @note does not return 1438 sub MsgSyntaxError(strMsg) 1439 Print "syntax error: " & strMsg 1440 Wscript.Quit(2) 1441 end sub 1442 1443 1444 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 1445 ' Helpers: Misc ' 1446 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 1447 1448 '' 1449 ' Translate a kBuild / VBox architecture name to a windows one. 1450 function XlateArchitectureToWin(strArch) 1451 strArch = LCase(strArch) 1452 XlateArchitectureToWin = strArch 1453 if strArch = "amd64" then XlateArchitectureToWin = "x64" 1454 end function 1169 1455 1170 1456 … … 1249 1535 if ArrayToString(arr) <> "Array(""v10"", ""v1"", ""v0"")" then MsgFatal "SelfTest: Array #10: " & ArrayToString(arr) 1250 1536 1537 if ArrayJoinString(arr, ":") <> "v10:v1:v0" then MsgFatal "SelfTest: Array #11: " & ArrayJoinString(arr, ":") 1538 1539 if PathMatch("c:\", "C:\") <> true then MsgFatal "SelfTest: PathMatch #1" 1540 if PathMatch("c:\\\winDows/sysTem32", "C:\WindowS\.\\.\System32\.") <> true then MsgFatal "SelfTest: PathMatch #2" 1541 if PathMatch("c:\\\winDows/sysTem32", "C:\WindowS\.\\..\System32\.") <> false then MsgFatal "SelfTest: PathMatch #3" 1542 if PathMatch("\\x\", "\\\x\") <> false then MsgFatal "SelfTest: PathMatch #4" 1543 if PathMatch("\\x\", "\\x\") <> true then MsgFatal "SelfTest: PathMatch #5" 1544 if PathMatch("\\", "\\") <> true then MsgFatal "SelfTest: PathMatch #6" 1545 if PathMatch("\\x", "\\x") <> true then MsgFatal "SelfTest: PathMatch #7" 1546 1251 1547 end sub 1252 1548
Note:
See TracChangeset
for help on using the changeset viewer.