1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rdf/base/src/rdfutil.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + 1.11 + Implementations for a bunch of useful RDF utility routines. Many of 1.12 + these will eventually be exported outside of RDF.DLL via the 1.13 + nsIRDFService interface. 1.14 + 1.15 + TO DO 1.16 + 1.17 + 1) Make this so that it doesn't permanently leak the RDF service 1.18 + object. 1.19 + 1.20 + 2) Make container functions thread-safe. They currently don't ensure 1.21 + that the RDF:nextVal property is maintained safely. 1.22 + 1.23 + */ 1.24 + 1.25 +#include "nsCOMPtr.h" 1.26 +#include "nsIRDFDataSource.h" 1.27 +#include "nsIRDFNode.h" 1.28 +#include "nsIRDFService.h" 1.29 +#include "nsIServiceManager.h" 1.30 +#include "nsIURL.h" 1.31 +#include "nsIIOService.h" 1.32 +#include "nsIURL.h" 1.33 +#include "nsNetUtil.h" 1.34 +#include "nsRDFCID.h" 1.35 +#include "nsString.h" 1.36 +#include "nsXPIDLString.h" 1.37 +#include "nsUnicharUtils.h" 1.38 +#include "rdfutil.h" 1.39 + 1.40 +//////////////////////////////////////////////////////////////////////// 1.41 + 1.42 +nsresult 1.43 +rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI) 1.44 +{ 1.45 + // This implementation is extremely simple: e.g., it can't compute 1.46 + // relative paths, or anything fancy like that. If the context URI 1.47 + // is not a prefix of the URI in question, we'll just bail. 1.48 + uint32_t prefixLen = aBaseURI.Length(); 1.49 + if (prefixLen != 0 && StringBeginsWith(aURI, aBaseURI)) { 1.50 + if (prefixLen < aURI.Length() && aURI.CharAt(prefixLen) == '/') 1.51 + ++prefixLen; // chop the leading slash so it's not `absolute' 1.52 + 1.53 + aURI.Cut(0, prefixLen); 1.54 + } 1.55 + 1.56 + return NS_OK; 1.57 +} 1.58 + 1.59 +void 1.60 +rdf_FormatDate(PRTime aTime, nsACString &aResult) 1.61 +{ 1.62 + // Outputs Unixish date in GMT plus usecs; e.g., 1.63 + // Wed Jan 9 19:15:13 2002 +002441 1.64 + // 1.65 + PRExplodedTime t; 1.66 + PR_ExplodeTime(aTime, PR_GMTParameters, &t); 1.67 + 1.68 + char buf[256]; 1.69 + PR_FormatTimeUSEnglish(buf, sizeof buf, "%a %b %d %H:%M:%S %Y", &t); 1.70 + aResult.Append(buf); 1.71 + 1.72 + // usecs 1.73 + aResult.Append(" +"); 1.74 + int32_t usec = t.tm_usec; 1.75 + for (int32_t digit = 100000; digit > 1; digit /= 10) { 1.76 + aResult.Append(char('0' + (usec / digit))); 1.77 + usec %= digit; 1.78 + } 1.79 + aResult.Append(char('0' + usec)); 1.80 +} 1.81 + 1.82 +PRTime 1.83 +rdf_ParseDate(const nsACString &aTime) 1.84 +{ 1.85 + PRTime t; 1.86 + PR_ParseTimeString(PromiseFlatCString(aTime).get(), true, &t); 1.87 + 1.88 + int32_t usec = 0; 1.89 + 1.90 + nsACString::const_iterator begin, digit, end; 1.91 + aTime.BeginReading(begin); 1.92 + aTime.EndReading(end); 1.93 + 1.94 + // Walk backwards until we find a `+', run out of string, or a 1.95 + // non-numeric character. 1.96 + digit = end; 1.97 + while (--digit != begin && *digit != '+') { 1.98 + if (*digit < '0' || *digit > '9') 1.99 + break; 1.100 + } 1.101 + 1.102 + if (digit != begin && *digit == '+') { 1.103 + // There's a usec field specified (or, at least, something 1.104 + // that looks close enough. Parse it, and add it to the time. 1.105 + while (++digit != end) { 1.106 + usec *= 10; 1.107 + usec += *digit - '0'; 1.108 + } 1.109 + 1.110 + t += usec; 1.111 + } 1.112 + 1.113 + return t; 1.114 +}