Back to index

lightning-sunbird  0.9+nobinonly
nsCache.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsCache.cpp, released
00017  * March 18, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Gordon Sheridan  <gordon@netscape.com>
00026  *   Patrick C. Beard <beard@netscape.com>
00027  *   Darin Fisher     <darin@netscape.com>
00028  *
00029  * Alternatively, the contents of this file may be used under the terms of
00030  * either the GNU General Public License Version 2 or later (the "GPL"), or
00031  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00032  * in which case the provisions of the GPL or the LGPL are applicable instead
00033  * of those above. If you wish to allow use of your version of this file only
00034  * under the terms of either the GPL or the LGPL, and not to allow others to
00035  * use your version of this file under the terms of the MPL, indicate your
00036  * decision by deleting the provisions above and replace them with the notice
00037  * and other provisions required by the GPL or the LGPL. If you do not delete
00038  * the provisions above, a recipient may use your version of this file under
00039  * the terms of any one of the MPL, the GPL or the LGPL.
00040  *
00041  * ***** END LICENSE BLOCK ***** */
00042 
00043 #include "nsCache.h"
00044 #include "nsReadableUtils.h"
00045 #include "nsDependentSubstring.h"
00046 #include "nsString.h"
00047 
00048 
00053 #if defined(PR_LOGGING)
00054 PRLogModuleInfo * gCacheLog = nsnull;
00055 
00056 
00057 void
00058 CacheLogInit()
00059 {
00060     if (gCacheLog) return;
00061     gCacheLog = PR_NewLogModule("cache");
00062     NS_ASSERTION(gCacheLog, "\nfailed to allocate cache log.\n");
00063 }
00064 
00065 
00066 void
00067 CacheLogPrintPath(PRLogModuleLevel level, char * format, nsIFile * item)
00068 {
00069     nsCAutoString path;
00070     nsresult rv = item->GetNativePath(path);
00071     if (NS_SUCCEEDED(rv)) {
00072         PR_LOG(gCacheLog, level, (format, path.get()));
00073     } else {
00074         PR_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv));
00075     }
00076 }
00077 
00078 #endif
00079 
00080 
00081 PRUint32
00082 SecondsFromPRTime(PRTime prTime)
00083 {
00084   PRInt64  microSecondsPerSecond, intermediateResult;
00085   PRUint32 seconds;
00086 
00087   LL_I2L(microSecondsPerSecond, PR_USEC_PER_SEC);
00088   LL_DIV(intermediateResult, prTime, microSecondsPerSecond);
00089   LL_L2UI(seconds, intermediateResult);
00090   return seconds;
00091 }
00092 
00093 
00094 PRTime
00095 PRTimeFromSeconds(PRUint32 seconds)
00096 {
00097   PRInt64 microSecondsPerSecond, intermediateResult;
00098   PRTime  prTime;
00099 
00100   LL_I2L(microSecondsPerSecond, PR_USEC_PER_SEC);
00101   LL_UI2L(intermediateResult, seconds);
00102   LL_MUL(prTime, intermediateResult, microSecondsPerSecond);
00103   return prTime;
00104 }
00105 
00106 
00107 nsresult
00108 ClientIDFromCacheKey(const nsACString&  key, char ** result)
00109 {
00110     nsresult  rv = NS_OK;
00111     *result = nsnull;
00112 
00113     nsReadingIterator<char> colon;
00114     key.BeginReading(colon);
00115         
00116     nsReadingIterator<char> start;
00117     key.BeginReading(start);
00118         
00119     nsReadingIterator<char> end;
00120     key.EndReading(end);
00121         
00122     if (FindCharInReadable(':', colon, end)) {
00123         *result = ToNewCString( Substring(start, colon));
00124         if (!*result) rv = NS_ERROR_OUT_OF_MEMORY;
00125     } else {
00126         NS_ASSERTION(PR_FALSE, "FindCharInRead failed to find ':'");
00127         rv = NS_ERROR_UNEXPECTED;
00128     }
00129     return rv;
00130 }
00131 
00132 
00133 nsresult
00134 ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
00135 {
00136     nsresult  rv = NS_OK;
00137 
00138     nsReadingIterator<char> start;
00139     key.BeginReading(start);
00140         
00141     nsReadingIterator<char> end;
00142     key.EndReading(end);
00143         
00144     if (FindCharInReadable(':', start, end)) {
00145         ++start;  // advance past clientID ':' delimiter
00146         result.Assign(Substring(start, end));
00147     } else {
00148         NS_ASSERTION(PR_FALSE, "FindCharInRead failed to find ':'");
00149         rv = NS_ERROR_UNEXPECTED;
00150         result.Truncate(0);
00151     }
00152     return rv;
00153 }