NS_METHOD nsTextHelper::GetText(nsString& aTextBuffer, PRUint32 aBufferSize, PRUint32& aActualSize) { aTextBuffer.SetLength(0); aTextBuffer.Append(mText); aActualSize = mText.Length(); return NS_OK; }
void nsEudoraCompose::ExtractCharset(nsString& str) { int32_t idx = MsgFind(str, "charset=", true, 0); if (idx != -1) { str.Cut(0, idx + 8); idx = str.FindChar(';'); if (idx != -1) str.SetLength(idx); str.Trim(kWhitespace); if ((str.CharAt(0) == '"') && (str.Length() > 2)) { str.SetLength(str.Length() - 1); str.Cut(0, 1); str.Trim(kWhitespace); } } else str.Truncate(); }
void nsEudoraCompose::ExtractType(nsString& str) { nsString tStr; int32_t idx = str.FindChar(';'); if (idx != -1) str.SetLength(idx); str.Trim(kWhitespace); if ((str.CharAt(0) == '"') && (str.Length() > 2)) { str.SetLength(str.Length() - 1); str.Cut(0, 1); str.Trim(kWhitespace); } // if multipart then ignore it since no outlook message body is ever // valid multipart! if (StringBeginsWith(str, NS_LITERAL_STRING("multipart/"), nsCaseInsensitiveStringComparator())) str.Truncate(); }
void AndroidGeckoEvent::ReadStringFromJString(nsString &aString, JNIEnv *jenv, jstring s) { if (!s) { aString.SetIsVoid(true); return; } int len = jenv->GetStringLength(s); aString.SetLength(len); jenv->GetStringRegion(s, 0, len, reinterpret_cast<jchar*>(aString.BeginWriting())); }
static void splitString(nsString& aSource, nsString& aTarget) { aTarget.Truncate() ; PRInt32 offset = aSource.FindChar('\n') ; if (offset >= 0) { const PRUnichar *source = aSource.get() + offset + 1 ; while (*source) { if (*source == '\n' || *source == '\r') { aTarget.Append(PRUnichar(' ')) ; } else { aTarget.Append(*source) ; } ++ source ; } aSource.SetLength(offset); } }
void nsOutlookMail::SplitString(nsString& val1, nsString& val2) { // Find the last line if there is more than one! int32_t idx = val1.RFind("\x0D\x0A"); int32_t cnt = 2; if (idx == -1) { cnt = 1; idx = val1.RFindChar(13); } if (idx == -1) idx= val1.RFindChar(10); if (idx != -1) { val2 = Substring(val1, idx + cnt); val1.SetLength(idx); SanitizeValue(val1); } }
/** Returns the specified attribute value associated with a DOM node, * or a null string if the attribute is not defined, or if the DOM node * is not an element. * @param aDOMNode node whose attribute is to be determined * @param aAttName attribute to be determined. Must be ASCII. * @param aAttValue output attribute value * @return NS_OK if no errors occurred */ NS_EXPORT nsresult mozXMLTermUtils::GetNodeAttribute(nsIDOMNode* aDOMNode, const char* aAttName, nsString& aAttValue) { XMLT_LOG(mozXMLTermUtils::GetNodeAttribute,20,("aAttName=%s\n", aAttName)); nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(aDOMNode); if (!domElement) { aAttValue.SetLength(0); return NS_OK; } nsAutoString attName; attName.AssignASCII(aAttName); return domElement->GetAttribute(attName, aAttValue); }
// Returns NULL if there is no personal namespace on the given host NS_IMETHODIMP nsIMAPHostSessionList::GetOnlineInboxPathForHost(const char *serverKey, nsString &result) { PR_EnterMonitor(gCachedHostInfoMonitor); nsIMAPHostInfo *host = FindHost(serverKey); if (host) { nsIMAPNamespace *ns = NULL; ns = host->fNamespaceList->GetDefaultNamespaceOfType(kPersonalNamespace); if (ns) { result.AssignWithConversion(ns->GetPrefix()); result.AppendLiteral("INBOX"); } } else { result.SetLength(0); } PR_ExitMonitor(gCachedHostInfoMonitor); return (host == NULL) ? NS_ERROR_ILLEGAL_VALUE : NS_OK; }
// Helper functions for structured cloning inline bool ReadString(JSStructuredCloneReader* aReader, nsString& aString) { MOZ_ASSERT(aReader); bool read; uint32_t nameLength, zero; read = JS_ReadUint32Pair(aReader, &nameLength, &zero); if (!read) { return false; } MOZ_ASSERT(zero == 0); aString.SetLength(nameLength); size_t charSize = sizeof(nsString::char_type); read = JS_ReadBytes(aReader, (void*) aString.BeginWriting(), nameLength * charSize); if (!read) { return false; } return true; }
/** Returns a timestamp string containing the local time, if at least * deltaSec seconds have elapsed since the last timestamp. Otherwise, * a null string is returned. * @param deltaSec minimum elapsed seconds since last timestamp (>=0) * @param lastTime in/out parameter containing time of last timestamp * @param aTimeStamp returned timestamp string * @return NS_OK on success */ NS_IMETHODIMP mozXMLTermUtils::TimeStamp(PRInt32 deltaSec, PRTime& lastTime, nsString& aTimeStamp) { static const PRInt32 DATE_LEN = 19; PRTime deltaTime ; char dateStr[DATE_LEN+1]; PRTime curTime, difTime; curTime = PR_Now(); LL_SUB(difTime, curTime, lastTime); LL_I2L(deltaTime, deltaSec*1000000); if (LL_CMP(difTime, <, deltaTime)) { // Not enough time has elapsed for a new time stamp aTimeStamp.SetLength(0); return NS_OK; } lastTime = curTime; // Current local time PRExplodedTime localTime; PR_ExplodeTime(curTime, PR_LocalTimeParameters, &localTime); PRInt32 nWritten = PR_snprintf(dateStr, DATE_LEN+1, "%02d:%02d:%02d %02d/%02d/%04d", // XXX i18n! localTime.tm_hour, localTime.tm_min, localTime.tm_sec, localTime.tm_mday, localTime.tm_month+1, localTime.tm_year); if (nWritten != DATE_LEN) return NS_ERROR_FAILURE; XMLT_LOG(mozXMLTermUtils::LocalTime,99,("localTime=%s\n", dateStr)); aTimeStamp.AssignASCII(dateStr); return NS_OK; }
/* static */ nsresult nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData, PRUint32 aLength, const nsString& aHintCharset, nsIDocument* aDocument, nsString& aString) { if (!aLength) { aString.Truncate(); return NS_OK; } nsCAutoString characterSet; nsresult rv = NS_OK; if (aChannel) { rv = aChannel->GetContentCharset(characterSet); } if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) { // charset name is always ASCII. LossyCopyUTF16toASCII(aHintCharset, characterSet); } if (NS_FAILED(rv) || characterSet.IsEmpty()) { DetectByteOrderMark(aData, aLength, characterSet); } if (characterSet.IsEmpty()) { // charset from document default characterSet = aDocument->GetDocumentCharacterSet(); } if (characterSet.IsEmpty()) { // fall back to ISO-8859-1, see bug 118404 characterSet.AssignLiteral("ISO-8859-1"); } nsCOMPtr<nsICharsetConverterManager> charsetConv = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder; if (NS_SUCCEEDED(rv) && charsetConv) { rv = charsetConv->GetUnicodeDecoder(characterSet.get(), getter_AddRefs(unicodeDecoder)); if (NS_FAILED(rv)) { // fall back to ISO-8859-1 if charset is not supported. (bug 230104) rv = charsetConv->GetUnicodeDecoderRaw("ISO-8859-1", getter_AddRefs(unicodeDecoder)); } } // converts from the charset to unicode if (NS_SUCCEEDED(rv)) { PRInt32 unicodeLength = 0; rv = unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData), aLength, &unicodeLength); if (NS_SUCCEEDED(rv)) { if (!EnsureStringLength(aString, unicodeLength)) return NS_ERROR_OUT_OF_MEMORY; PRUnichar *ustr = aString.BeginWriting(); PRInt32 consumedLength = 0; PRInt32 originalLength = aLength; PRInt32 convertedLength = 0; PRInt32 bufferLength = unicodeLength; do { rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData), (PRInt32 *) &aLength, ustr, &unicodeLength); if (NS_FAILED(rv)) { // if we failed, we consume one byte, replace it with U+FFFD // and try the conversion again. ustr[unicodeLength++] = (PRUnichar)0xFFFD; ustr += unicodeLength; unicodeDecoder->Reset(); } aData += ++aLength; consumedLength += aLength; aLength = originalLength - consumedLength; convertedLength += unicodeLength; unicodeLength = bufferLength - convertedLength; } while (NS_FAILED(rv) && (originalLength > consumedLength) && (bufferLength > convertedLength)); aString.SetLength(convertedLength); } } return rv; }
nsresult nsUICommandCollector::GetEventTargets(nsIDOMEvent *event, nsString &targetId, nsString &targetAnonId) const { // This code deals with both anonymous and explicit (non-anonymous) content. // // For explicit content, we just return the id of the event target in // targetId, and leave targetAnonId empty. If there is no id, then // we return failure. // // For anonymous content, we return the id of the event target (after // retargeting), in targetId. We return the anonid of the event's // originalTarget in targetAnonId, so that XBL child elements can be // distinguished. If there is no anonid, then we return failure. // Note that the originalTarget is set after text node retargting, but // before any XBL retargeting. // // We assume that if the originalTarget has no id, we're dealing with // anonymous content (this isn't always true, but it's good enough for what // this code does). nsCOMPtr<nsIDOMNSEvent> nsEvent = do_QueryInterface(event); NS_ENSURE_STATE(nsEvent); nsCOMPtr<nsIDOMEventTarget> originalTarget; nsEvent->GetOriginalTarget(getter_AddRefs(originalTarget)); NS_ENSURE_STATE(originalTarget); nsString origElementId; nsCOMPtr<nsIDOMElement> origElement(do_QueryInterface(originalTarget)); if (origElement) { origElement->GetAttribute(NS_LITERAL_STRING("id"), origElementId); origElement->GetAttribute(NS_LITERAL_STRING("anonid"), targetAnonId); } nsCOMPtr<nsIDOMEventTarget> target; event->GetTarget(getter_AddRefs(target)); NS_ENSURE_STATE(target); nsCOMPtr<nsIDOMElement> targetElement(do_QueryInterface(target)); if (targetElement) { targetElement->GetAttribute(NS_LITERAL_STRING("id"), targetId); } MS_LOG(("Original Target Id: %s, Original Target Anonid: %s, Target Id: %s", NS_ConvertUTF16toUTF8(origElementId).get(), NS_ConvertUTF16toUTF8(targetAnonId).get(), NS_ConvertUTF16toUTF8(targetId).get())); if (targetId.IsEmpty()) { // There's nothing useful to log in this case -- even if we have an anonid, // it's not possible to determine its position in the document. MS_LOG(("Warning: skipping logging because of empty target ID")); return NS_ERROR_FAILURE; } if (origElementId.IsEmpty()) { // We're dealing with anonymous content, so don't continue if we didn't // find an anonid. if (targetAnonId.IsEmpty()) { MS_LOG(("Warning: skipping logging because of empty anonid")); return NS_ERROR_FAILURE; } } else { // We're dealing with normal explicit content, so don't return an anonid. targetAnonId.SetLength(0); } return NS_OK; }