Changeset 40179 in vbox for trunk/src/VBox/Main/src-server/VirtualBoxImpl.cpp
- Timestamp:
- Feb 20, 2012 3:03:06 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/src-server/VirtualBoxImpl.cpp
r40156 r40179 1300 1300 ///////////////////////////////////////////////////////////////////////////// 1301 1301 1302 /* Helper for VirtualBox::ComposeMachineFilename */ 1303 static void sanitiseMachineFilename(Utf8Str &aName); 1304 1302 1305 STDMETHODIMP VirtualBox::ComposeMachineFilename(IN_BSTR aName, 1303 1306 IN_BSTR aBaseFolder, … … 1324 1327 Utf8Str strBase = aBaseFolder; 1325 1328 Utf8Str strName = aName; 1329 sanitiseMachineFilename(strName); 1330 1331 if (strBase.isEmpty()) 1332 /* we use the non-full folder value below to keep the path relative */ 1333 getDefaultMachineFolder(strBase); 1334 1335 calculateFullPath(strBase, strBase); 1336 1337 Bstr bstrSettingsFile = BstrFmt("%s%c%s%c%s.vbox", 1338 strBase.c_str(), 1339 RTPATH_DELIMITER, 1340 strName.c_str(), 1341 RTPATH_DELIMITER, 1342 strName.c_str()); 1343 1344 #if 0 /* Try to get a unique name. */ 1345 for (unsigned i = 1; RTFileExists(bstrSettingsFile.c_str() && i < 100; ++i) 1346 bstrSettingsFile = BstrFmt("%s%c%s%u%c%s%u.vbox", 1347 strBase.c_str(), 1348 RTPATH_DELIMITER, 1349 strName.c_str(), i, 1350 RTPATH_DELIMITER, 1351 strName.c_str()); 1352 #endif 1353 1354 bstrSettingsFile.detachTo(aFilename); 1355 1356 return S_OK; 1357 } 1358 1359 /** 1360 * Remove characters from a machine file name which can be problematic on 1361 * particular systems. 1362 * @param strName The file name to sanitise. 1363 */ 1364 void sanitiseMachineFilename(Utf8Str &strName) 1365 { 1326 1366 /** Set of characters which should be safe for use in filenames: some basic 1327 1367 * ASCII, Unicode from Latin-1 alphabetic to the end of Hangul. We try to … … 1346 1386 for (size_t i = strName.length() - 1; i && pszName[i] == ' '; --i) 1347 1387 pszName[i] = '_'; 1348 1349 if (strBase.isEmpty()) 1350 /* we use the non-full folder value below to keep the path relative */ 1351 getDefaultMachineFolder(strBase); 1352 1353 calculateFullPath(strBase, strBase); 1354 1355 Bstr bstrSettingsFile = BstrFmt("%s%c%s%c%s.vbox", 1356 strBase.c_str(), 1357 RTPATH_DELIMITER, 1358 strName.c_str(), 1359 RTPATH_DELIMITER, 1360 strName.c_str()); 1361 1362 #if 0 /* Try to get a unique name. */ 1363 for (unsigned i = 1; RTFileExists(bstrSettingsFile.c_str() && i < 100; ++i) 1364 bstrSettingsFile = BstrFmt("%s%c%s%u%c%s%u.vbox", 1365 strBase.c_str(), 1366 RTPATH_DELIMITER, 1367 strName.c_str(), i, 1368 RTPATH_DELIMITER, 1369 strName.c_str()); 1388 } 1389 1390 /** Simple unit test/operation examples for sanitiseMachineFilename(). */ 1391 static unsigned testSanitiseMachineFilename(void (*pfnPrintf)(const char *, ...)) 1392 { 1393 unsigned cErrors = 0; 1394 1395 /** Expected results of sanitising given file names. */ 1396 static struct 1397 { 1398 /** The test file name to be sanitised (Utf-8). */ 1399 const char *pcszIn; 1400 /** The expected sanitised output (Utf-8). */ 1401 const char *pcszOutExpected; 1402 } aTest[] = 1403 { 1404 { "OS/2 2.1", "OS_2 2.1" }, 1405 { "-!My VM!-", "__My VM_-" }, 1406 { "\xF0\x90\x8C\xB0", "____" }, 1407 { " My VM ", "__My VM__" }, 1408 { ".My VM.", "_My VM_" } 1409 }; 1410 for (unsigned i = 0; i < RT_ELEMENTS(aTest); ++i) 1411 { 1412 Utf8Str str(aTest[i].pcszIn); 1413 sanitiseMachineFilename(str); 1414 if (str.compare(aTest[i].pcszOutExpected)) 1415 { 1416 ++cErrors; 1417 pfnPrintf("%s: line %d, expected %s, actual %s\n", 1418 __PRETTY_FUNCTION__, i, aTest[i].pcszOutExpected, 1419 str.c_str()); 1420 } 1421 } 1422 return cErrors; 1423 } 1424 1425 #ifdef DEBUG 1426 /** @todo Proper testcase. */ 1427 /** @todo Do we have a better method of doing init functions? */ 1428 namespace 1429 { 1430 class TestSanitiseMachineFilename 1431 { 1432 public: 1433 TestSanitiseMachineFilename(void) 1434 { 1435 Assert(!testSanitiseMachineFilename(RTAssertMsg2)); 1436 } 1437 }; 1438 TestSanitiseMachineFilename s_TestSanitiseMachineFilename; 1439 } 1370 1440 #endif 1371 1372 bstrSettingsFile.detachTo(aFilename);1373 1374 return S_OK;1375 }1376 1441 1377 1442 /** @note Locks mSystemProperties object for reading. */
Note:
See TracChangeset
for help on using the changeset viewer.