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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : /*
39 :
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.
43 :
44 : TO DO
45 :
46 : 1) Make this so that it doesn't permanently leak the RDF service
47 : object.
48 :
49 : 2) Make container functions thread-safe. They currently don't ensure
50 : that the RDF:nextVal property is maintained safely.
51 :
52 : */
53 :
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"
69 :
70 : ////////////////////////////////////////////////////////////////////////
71 :
72 : nsresult
73 664 : rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI)
74 : {
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 664 : PRUint32 prefixLen = aBaseURI.Length();
79 664 : if (prefixLen != 0 && StringBeginsWith(aURI, aBaseURI)) {
80 0 : if (prefixLen < aURI.Length() && aURI.CharAt(prefixLen) == '/')
81 0 : ++prefixLen; // chop the leading slash so it's not `absolute'
82 :
83 0 : aURI.Cut(0, prefixLen);
84 : }
85 :
86 664 : return NS_OK;
87 : }
88 :
89 : void
90 0 : rdf_FormatDate(PRTime aTime, nsACString &aResult)
91 : {
92 : // Outputs Unixish date in GMT plus usecs; e.g.,
93 : // Wed Jan 9 19:15:13 2002 +002441
94 : //
95 : PRExplodedTime t;
96 0 : PR_ExplodeTime(aTime, PR_GMTParameters, &t);
97 :
98 : char buf[256];
99 0 : PR_FormatTimeUSEnglish(buf, sizeof buf, "%a %b %d %H:%M:%S %Y", &t);
100 0 : aResult.Append(buf);
101 :
102 : // usecs
103 0 : aResult.Append(" +");
104 0 : PRInt32 usec = t.tm_usec;
105 0 : for (PRInt32 digit = 100000; digit > 1; digit /= 10) {
106 0 : aResult.Append(char('0' + (usec / digit)));
107 0 : usec %= digit;
108 : }
109 0 : aResult.Append(char('0' + usec));
110 0 : }
111 :
112 : PRTime
113 0 : rdf_ParseDate(const nsACString &aTime)
114 : {
115 : PRTime t;
116 0 : PR_ParseTimeString(PromiseFlatCString(aTime).get(), true, &t);
117 :
118 0 : PRInt32 usec = 0;
119 :
120 0 : nsACString::const_iterator begin, digit, end;
121 0 : aTime.BeginReading(begin);
122 0 : aTime.EndReading(end);
123 :
124 : // Walk backwards until we find a `+', run out of string, or a
125 : // non-numeric character.
126 0 : digit = end;
127 0 : while (--digit != begin && *digit != '+') {
128 0 : if (*digit < '0' || *digit > '9')
129 0 : break;
130 : }
131 :
132 0 : 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 0 : while (++digit != end) {
136 0 : usec *= 10;
137 0 : usec += *digit - '0';
138 : }
139 :
140 : PRTime temp;
141 0 : LL_I2L(temp, usec);
142 0 : LL_ADD(t, t, temp);
143 : }
144 :
145 0 : return t;
146 : }
|