void RemoveBadFileNameCharacters(nsAString& aFileName, PRBool aAllPlatforms) { // Get the list of bad characters. const char* badCharacters; if (aAllPlatforms) badCharacters = SB_FILE_BAD_CHARACTERS_ALL_PLATFORMS; else badCharacters = SB_FILE_BAD_CHARACTERS; // Remove all bad characters from the file name. aFileName.StripChars(badCharacters); // Windows does not like spaces at the begining or end of a file/folder name. // Windows also does not like dots at the begining or end of the file/folder // name and dots are bad as well on some other operating systems as they // represent a hidden file. aFileName.Trim(" .", PR_TRUE, PR_TRUE); }
void CompressWhitespace(nsAString& aString) { aString.Trim(" \n\t\r"); PRUnichar *start; PRUint32 len = NS_StringGetMutableData(aString, PR_UINT32_MAX, &start); PRUnichar *end = start + len; for (PRUnichar *cur = start; cur < end; ++cur) { if (!NS_IsAsciiWhitespace(*cur)) continue; *cur = ' '; PRUnichar *wend; for (wend = cur + 1; wend < end && NS_IsAsciiWhitespace(*wend); ++wend) { // nothing to do but loop } if (wend == cur + 1) continue; PRUint32 wlen = wend - cur - 1; // fix "end" end -= wlen; // move everything forwards a bit for (PRUnichar *m = cur + 1; m < end; ++m) { *m = *(m + wlen); } } // re-terminate *end = '\0'; // Set the new length. aString.SetLength(end - start); }
nsresult sbWinGetSCSIProductInfo(DEVINST aDevInst, nsAString& aVendorID, nsAString& aProductID) { DWORD byteCount; BOOL success; errno_t errno; nsresult rv; // Create a disk interface device file. sbAutoHANDLE diskHandle; GUID guid = GUID_DEVINTERFACE_DISK; rv = sbWinCreateDeviceFile(diskHandle.StartAssignment(), aDevInst, &guid, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); NS_ENSURE_SUCCESS(rv, rv); // Set up a storage property query to get the storage device descriptor. STORAGE_PROPERTY_QUERY storagePropertyQuery; memset(&storagePropertyQuery, 0, sizeof(storagePropertyQuery)); storagePropertyQuery.PropertyId = StorageDeviceProperty; storagePropertyQuery.QueryType = PropertyStandardQuery; // Determine how many bytes are required to hold the full storage device // descriptor. STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader; success = DeviceIoControl(diskHandle, IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(storagePropertyQuery), &storageDescriptorHeader, sizeof(storageDescriptorHeader), &byteCount, NULL); NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); NS_ENSURE_TRUE(byteCount == sizeof(storageDescriptorHeader), NS_ERROR_FAILURE); // Allocate the storage device descriptor. sbAutoMemPtr<STORAGE_DEVICE_DESCRIPTOR> storageDeviceDescriptor = static_cast<PSTORAGE_DEVICE_DESCRIPTOR> (malloc(storageDescriptorHeader.Size)); NS_ENSURE_TRUE(storageDeviceDescriptor, NS_ERROR_OUT_OF_MEMORY); // Get the storage device descriptor. success = DeviceIoControl(diskHandle, IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(storagePropertyQuery), storageDeviceDescriptor, storageDescriptorHeader.Size, &byteCount, NULL); NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); NS_ENSURE_TRUE(byteCount == storageDescriptorHeader.Size, NS_ERROR_FAILURE); // Return results with trailing spaces trimmed. SCSI inquiry vendor and // product IDs have trailing spaces as filler. aVendorID.AssignLiteral (reinterpret_cast<char*>(storageDeviceDescriptor.get()) + storageDeviceDescriptor->VendorIdOffset); aVendorID.Trim(" ", PR_FALSE, PR_TRUE); aProductID.AssignLiteral (reinterpret_cast<char*>(storageDeviceDescriptor.get()) + storageDeviceDescriptor->ProductIdOffset); aProductID.Trim(" ", PR_FALSE, PR_TRUE); return NS_OK; }