|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 |
|
8 Implementations for a bunch of useful RDF utility routines. Many of |
|
9 these will eventually be exported outside of RDF.DLL via the |
|
10 nsIRDFService interface. |
|
11 |
|
12 TO DO |
|
13 |
|
14 1) Make this so that it doesn't permanently leak the RDF service |
|
15 object. |
|
16 |
|
17 2) Make container functions thread-safe. They currently don't ensure |
|
18 that the RDF:nextVal property is maintained safely. |
|
19 |
|
20 */ |
|
21 |
|
22 #include "nsCOMPtr.h" |
|
23 #include "nsIRDFDataSource.h" |
|
24 #include "nsIRDFNode.h" |
|
25 #include "nsIRDFService.h" |
|
26 #include "nsIServiceManager.h" |
|
27 #include "nsIURL.h" |
|
28 #include "nsIIOService.h" |
|
29 #include "nsIURL.h" |
|
30 #include "nsNetUtil.h" |
|
31 #include "nsRDFCID.h" |
|
32 #include "nsString.h" |
|
33 #include "nsXPIDLString.h" |
|
34 #include "nsUnicharUtils.h" |
|
35 #include "rdfutil.h" |
|
36 |
|
37 //////////////////////////////////////////////////////////////////////// |
|
38 |
|
39 nsresult |
|
40 rdf_MakeRelativeRef(const nsCSubstring& aBaseURI, nsCString& aURI) |
|
41 { |
|
42 // This implementation is extremely simple: e.g., it can't compute |
|
43 // relative paths, or anything fancy like that. If the context URI |
|
44 // is not a prefix of the URI in question, we'll just bail. |
|
45 uint32_t prefixLen = aBaseURI.Length(); |
|
46 if (prefixLen != 0 && StringBeginsWith(aURI, aBaseURI)) { |
|
47 if (prefixLen < aURI.Length() && aURI.CharAt(prefixLen) == '/') |
|
48 ++prefixLen; // chop the leading slash so it's not `absolute' |
|
49 |
|
50 aURI.Cut(0, prefixLen); |
|
51 } |
|
52 |
|
53 return NS_OK; |
|
54 } |
|
55 |
|
56 void |
|
57 rdf_FormatDate(PRTime aTime, nsACString &aResult) |
|
58 { |
|
59 // Outputs Unixish date in GMT plus usecs; e.g., |
|
60 // Wed Jan 9 19:15:13 2002 +002441 |
|
61 // |
|
62 PRExplodedTime t; |
|
63 PR_ExplodeTime(aTime, PR_GMTParameters, &t); |
|
64 |
|
65 char buf[256]; |
|
66 PR_FormatTimeUSEnglish(buf, sizeof buf, "%a %b %d %H:%M:%S %Y", &t); |
|
67 aResult.Append(buf); |
|
68 |
|
69 // usecs |
|
70 aResult.Append(" +"); |
|
71 int32_t usec = t.tm_usec; |
|
72 for (int32_t digit = 100000; digit > 1; digit /= 10) { |
|
73 aResult.Append(char('0' + (usec / digit))); |
|
74 usec %= digit; |
|
75 } |
|
76 aResult.Append(char('0' + usec)); |
|
77 } |
|
78 |
|
79 PRTime |
|
80 rdf_ParseDate(const nsACString &aTime) |
|
81 { |
|
82 PRTime t; |
|
83 PR_ParseTimeString(PromiseFlatCString(aTime).get(), true, &t); |
|
84 |
|
85 int32_t usec = 0; |
|
86 |
|
87 nsACString::const_iterator begin, digit, end; |
|
88 aTime.BeginReading(begin); |
|
89 aTime.EndReading(end); |
|
90 |
|
91 // Walk backwards until we find a `+', run out of string, or a |
|
92 // non-numeric character. |
|
93 digit = end; |
|
94 while (--digit != begin && *digit != '+') { |
|
95 if (*digit < '0' || *digit > '9') |
|
96 break; |
|
97 } |
|
98 |
|
99 if (digit != begin && *digit == '+') { |
|
100 // There's a usec field specified (or, at least, something |
|
101 // that looks close enough. Parse it, and add it to the time. |
|
102 while (++digit != end) { |
|
103 usec *= 10; |
|
104 usec += *digit - '0'; |
|
105 } |
|
106 |
|
107 t += usec; |
|
108 } |
|
109 |
|
110 return t; |
|
111 } |