Line data Source code
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 "nsRDFCID.h"
31 : #include "nsString.h"
32 : #include "nsXPIDLString.h"
33 : #include "nsUnicharUtils.h"
34 : #include "rdfutil.h"
35 : #include "prtime.h"
36 :
37 : ////////////////////////////////////////////////////////////////////////
38 :
39 : nsresult
40 0 : rdf_MakeRelativeRef(const nsACString& 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 0 : uint32_t prefixLen = aBaseURI.Length();
46 0 : if (prefixLen != 0 && StringBeginsWith(aURI, aBaseURI)) {
47 0 : if (prefixLen < aURI.Length() && aURI.CharAt(prefixLen) == '/')
48 0 : ++prefixLen; // chop the leading slash so it's not `absolute'
49 :
50 0 : aURI.Cut(0, prefixLen);
51 : }
52 :
53 0 : return NS_OK;
54 : }
55 :
56 : void
57 0 : 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 0 : PR_ExplodeTime(aTime, PR_GMTParameters, &t);
64 :
65 : char buf[256];
66 0 : PR_FormatTimeUSEnglish(buf, sizeof buf, "%a %b %d %H:%M:%S %Y", &t);
67 0 : aResult.Append(buf);
68 :
69 : // usecs
70 0 : aResult.AppendLiteral(" +");
71 0 : int32_t usec = t.tm_usec;
72 0 : for (int32_t digit = 100000; digit > 1; digit /= 10) {
73 0 : aResult.Append(char('0' + (usec / digit)));
74 0 : usec %= digit;
75 : }
76 0 : aResult.Append(char('0' + usec));
77 0 : }
78 :
79 : PRTime
80 0 : rdf_ParseDate(const nsACString &aTime)
81 : {
82 : PRTime t;
83 0 : PR_ParseTimeString(PromiseFlatCString(aTime).get(), true, &t);
84 :
85 0 : int32_t usec = 0;
86 :
87 0 : nsACString::const_iterator begin, digit, end;
88 0 : aTime.BeginReading(begin);
89 0 : aTime.EndReading(end);
90 :
91 : // Walk backwards until we find a `+', run out of string, or a
92 : // non-numeric character.
93 0 : digit = end;
94 0 : while (--digit != begin && *digit != '+') {
95 0 : if (*digit < '0' || *digit > '9')
96 0 : break;
97 : }
98 :
99 0 : 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 0 : while (++digit != end) {
103 0 : usec *= 10;
104 0 : usec += *digit - '0';
105 : }
106 :
107 0 : t += usec;
108 : }
109 :
110 0 : return t;
111 : }
|