1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsReadableUtils.h"
9 #include "nsDependentSubstring.h"
14 * Cache Service Utility Functions
17 #if defined(PR_LOGGING)
18 PRLogModuleInfo
* gCacheLog
= nullptr;
24 if (gCacheLog
) return;
25 gCacheLog
= PR_NewLogModule("cache");
26 NS_ASSERTION(gCacheLog
, "\nfailed to allocate cache log.\n");
31 CacheLogPrintPath(PRLogModuleLevel level
, const char * format
, nsIFile
* item
)
34 nsresult rv
= item
->GetNativePath(path
);
35 if (NS_SUCCEEDED(rv
)) {
36 PR_LOG(gCacheLog
, level
, (format
, path
.get()));
38 PR_LOG(gCacheLog
, level
, ("GetNativePath failed: %x", rv
));
46 SecondsFromPRTime(PRTime prTime
)
48 PRInt64 microSecondsPerSecond
, intermediateResult
;
51 LL_I2L(microSecondsPerSecond
, PR_USEC_PER_SEC
);
52 LL_DIV(intermediateResult
, prTime
, microSecondsPerSecond
);
53 LL_L2UI(seconds
, intermediateResult
);
59 PRTimeFromSeconds(PRUint32 seconds
)
61 PRInt64 microSecondsPerSecond
, intermediateResult
;
64 LL_I2L(microSecondsPerSecond
, PR_USEC_PER_SEC
);
65 LL_UI2L(intermediateResult
, seconds
);
66 LL_MUL(prTime
, intermediateResult
, microSecondsPerSecond
);
72 ClientIDFromCacheKey(const nsACString
& key
, char ** result
)
77 nsReadingIterator
<char> colon
;
78 key
.BeginReading(colon
);
80 nsReadingIterator
<char> start
;
81 key
.BeginReading(start
);
83 nsReadingIterator
<char> end
;
86 if (FindCharInReadable(':', colon
, end
)) {
87 *result
= ToNewCString( Substring(start
, colon
));
88 if (!*result
) rv
= NS_ERROR_OUT_OF_MEMORY
;
90 NS_ASSERTION(false, "FindCharInRead failed to find ':'");
91 rv
= NS_ERROR_UNEXPECTED
;
98 ClientKeyFromCacheKey(const nsCString
& key
, nsACString
&result
)
102 nsReadingIterator
<char> start
;
103 key
.BeginReading(start
);
105 nsReadingIterator
<char> end
;
108 if (FindCharInReadable(':', start
, end
)) {
109 ++start
; // advance past clientID ':' delimiter
110 result
.Assign(Substring(start
, end
));
112 NS_ASSERTION(false, "FindCharInRead failed to find ':'");
113 rv
= NS_ERROR_UNEXPECTED
;