Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / rdf / base / src / rdfutil.cpp
blob2f598765a2779aa9569206553f714e9409edf6df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 Implementations for a bunch of useful RDF utility routines. Many of
41 these will eventually be exported outside of RDF.DLL via the
42 nsIRDFService interface.
44 TO DO
46 1) Make this so that it doesn't permanently leak the RDF service
47 object.
49 2) Make container functions thread-safe. They currently don't ensure
50 that the RDF:nextVal property is maintained safely.
54 #include "nsCOMPtr.h"
55 #include "nsIRDFDataSource.h"
56 #include "nsIRDFNode.h"
57 #include "nsIRDFService.h"
58 #include "nsIServiceManager.h"
59 #include "nsIURL.h"
60 #include "nsIIOService.h"
61 #include "nsIURL.h"
62 #include "nsNetUtil.h"
63 #include "nsRDFCID.h"
64 #include "nsString.h"
65 #include "nsXPIDLString.h"
66 #include "nsUnicharUtils.h"
67 #include "prtime.h"
68 #include "rdfutil.h"
70 ////////////////////////////////////////////////////////////////////////
72 nsresult
73 rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI)
75 // This implementation is extremely simple: e.g., it can't compute
76 // relative paths, or anything fancy like that. If the context URI
77 // is not a prefix of the URI in question, we'll just bail.
78 PRUint32 prefixLen = aBaseURI.Length();
79 if (prefixLen != 0 && StringBeginsWith(aURI, aBaseURI)) {
80 if (prefixLen < aURI.Length() && aURI.CharAt(prefixLen) == '/')
81 ++prefixLen; // chop the leading slash so it's not `absolute'
83 aURI.Cut(0, prefixLen);
86 return NS_OK;
89 void
90 rdf_FormatDate(PRTime aTime, nsACString &aResult)
92 // Outputs Unixish date in GMT plus usecs; e.g.,
93 // Wed Jan 9 19:15:13 2002 +002441
95 PRExplodedTime t;
96 PR_ExplodeTime(aTime, PR_GMTParameters, &t);
98 char buf[256];
99 PR_FormatTimeUSEnglish(buf, sizeof buf, "%a %b %d %H:%M:%S %Y", &t);
100 aResult.Append(buf);
102 // usecs
103 aResult.Append(" +");
104 PRInt32 usec = t.tm_usec;
105 for (PRInt32 digit = 100000; digit > 1; digit /= 10) {
106 aResult.Append(char('0' + (usec / digit)));
107 usec %= digit;
109 aResult.Append(char('0' + usec));
112 PRTime
113 rdf_ParseDate(const nsACString &aTime)
115 PRTime t;
116 PR_ParseTimeString(PromiseFlatCString(aTime).get(), PR_TRUE, &t);
118 PRInt32 usec = 0;
120 nsACString::const_iterator begin, digit, end;
121 aTime.BeginReading(begin);
122 aTime.EndReading(end);
124 // Walk backwards until we find a `+', run out of string, or a
125 // non-numeric character.
126 digit = end;
127 while (--digit != begin && *digit != '+') {
128 if (*digit < '0' || *digit > '9')
129 break;
132 if (digit != begin && *digit == '+') {
133 // There's a usec field specified (or, at least, something
134 // that looks close enough. Parse it, and add it to the time.
135 while (++digit != end) {
136 usec *= 10;
137 usec += *digit - '0';
140 PRTime temp;
141 LL_I2L(temp, usec);
142 LL_ADD(t, t, temp);
145 return t;