michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: michael@0: Implementations for a bunch of useful RDF utility routines. Many of michael@0: these will eventually be exported outside of RDF.DLL via the michael@0: nsIRDFService interface. michael@0: michael@0: TO DO michael@0: michael@0: 1) Make this so that it doesn't permanently leak the RDF service michael@0: object. michael@0: michael@0: 2) Make container functions thread-safe. They currently don't ensure michael@0: that the RDF:nextVal property is maintained safely. michael@0: michael@0: */ michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIRDFDataSource.h" michael@0: #include "nsIRDFNode.h" michael@0: #include "nsIRDFService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIURL.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsIURL.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsRDFCID.h" michael@0: #include "nsString.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "rdfutil.h" michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: nsresult michael@0: rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI) michael@0: { michael@0: // This implementation is extremely simple: e.g., it can't compute michael@0: // relative paths, or anything fancy like that. If the context URI michael@0: // is not a prefix of the URI in question, we'll just bail. michael@0: uint32_t prefixLen = aBaseURI.Length(); michael@0: if (prefixLen != 0 && StringBeginsWith(aURI, aBaseURI)) { michael@0: if (prefixLen < aURI.Length() && aURI.CharAt(prefixLen) == '/') michael@0: ++prefixLen; // chop the leading slash so it's not `absolute' michael@0: michael@0: aURI.Cut(0, prefixLen); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: rdf_FormatDate(PRTime aTime, nsACString &aResult) michael@0: { michael@0: // Outputs Unixish date in GMT plus usecs; e.g., michael@0: // Wed Jan 9 19:15:13 2002 +002441 michael@0: // michael@0: PRExplodedTime t; michael@0: PR_ExplodeTime(aTime, PR_GMTParameters, &t); michael@0: michael@0: char buf[256]; michael@0: PR_FormatTimeUSEnglish(buf, sizeof buf, "%a %b %d %H:%M:%S %Y", &t); michael@0: aResult.Append(buf); michael@0: michael@0: // usecs michael@0: aResult.Append(" +"); michael@0: int32_t usec = t.tm_usec; michael@0: for (int32_t digit = 100000; digit > 1; digit /= 10) { michael@0: aResult.Append(char('0' + (usec / digit))); michael@0: usec %= digit; michael@0: } michael@0: aResult.Append(char('0' + usec)); michael@0: } michael@0: michael@0: PRTime michael@0: rdf_ParseDate(const nsACString &aTime) michael@0: { michael@0: PRTime t; michael@0: PR_ParseTimeString(PromiseFlatCString(aTime).get(), true, &t); michael@0: michael@0: int32_t usec = 0; michael@0: michael@0: nsACString::const_iterator begin, digit, end; michael@0: aTime.BeginReading(begin); michael@0: aTime.EndReading(end); michael@0: michael@0: // Walk backwards until we find a `+', run out of string, or a michael@0: // non-numeric character. michael@0: digit = end; michael@0: while (--digit != begin && *digit != '+') { michael@0: if (*digit < '0' || *digit > '9') michael@0: break; michael@0: } michael@0: michael@0: if (digit != begin && *digit == '+') { michael@0: // There's a usec field specified (or, at least, something michael@0: // that looks close enough. Parse it, and add it to the time. michael@0: while (++digit != end) { michael@0: usec *= 10; michael@0: usec += *digit - '0'; michael@0: } michael@0: michael@0: t += usec; michael@0: } michael@0: michael@0: return t; michael@0: }