Bug 783551 - Get tooltool running on the b2g on OS X builds. r=respindola
[gecko.git] / netwerk / cache / nsCache.cpp
blob441cd9e527af76babd6afee38b4bc55ef70157f3
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/. */
7 #include "nsCache.h"
8 #include "nsReadableUtils.h"
9 #include "nsDependentSubstring.h"
10 #include "nsString.h"
13 /**
14 * Cache Service Utility Functions
17 #if defined(PR_LOGGING)
18 PRLogModuleInfo * gCacheLog = nullptr;
21 void
22 CacheLogInit()
24 if (gCacheLog) return;
25 gCacheLog = PR_NewLogModule("cache");
26 NS_ASSERTION(gCacheLog, "\nfailed to allocate cache log.\n");
30 void
31 CacheLogPrintPath(PRLogModuleLevel level, const char * format, nsIFile * item)
33 nsCAutoString path;
34 nsresult rv = item->GetNativePath(path);
35 if (NS_SUCCEEDED(rv)) {
36 PR_LOG(gCacheLog, level, (format, path.get()));
37 } else {
38 PR_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv));
42 #endif
45 PRUint32
46 SecondsFromPRTime(PRTime prTime)
48 PRInt64 microSecondsPerSecond, intermediateResult;
49 PRUint32 seconds;
51 LL_I2L(microSecondsPerSecond, PR_USEC_PER_SEC);
52 LL_DIV(intermediateResult, prTime, microSecondsPerSecond);
53 LL_L2UI(seconds, intermediateResult);
54 return seconds;
58 PRTime
59 PRTimeFromSeconds(PRUint32 seconds)
61 PRInt64 microSecondsPerSecond, intermediateResult;
62 PRTime prTime;
64 LL_I2L(microSecondsPerSecond, PR_USEC_PER_SEC);
65 LL_UI2L(intermediateResult, seconds);
66 LL_MUL(prTime, intermediateResult, microSecondsPerSecond);
67 return prTime;
71 nsresult
72 ClientIDFromCacheKey(const nsACString& key, char ** result)
74 nsresult rv = NS_OK;
75 *result = nullptr;
77 nsReadingIterator<char> colon;
78 key.BeginReading(colon);
80 nsReadingIterator<char> start;
81 key.BeginReading(start);
83 nsReadingIterator<char> end;
84 key.EndReading(end);
86 if (FindCharInReadable(':', colon, end)) {
87 *result = ToNewCString( Substring(start, colon));
88 if (!*result) rv = NS_ERROR_OUT_OF_MEMORY;
89 } else {
90 NS_ASSERTION(false, "FindCharInRead failed to find ':'");
91 rv = NS_ERROR_UNEXPECTED;
93 return rv;
97 nsresult
98 ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
100 nsresult rv = NS_OK;
102 nsReadingIterator<char> start;
103 key.BeginReading(start);
105 nsReadingIterator<char> end;
106 key.EndReading(end);
108 if (FindCharInReadable(':', start, end)) {
109 ++start; // advance past clientID ':' delimiter
110 result.Assign(Substring(start, end));
111 } else {
112 NS_ASSERTION(false, "FindCharInRead failed to find ':'");
113 rv = NS_ERROR_UNEXPECTED;
114 result.Truncate(0);
116 return rv;