Back to index

lightning-sunbird  0.9+nobinonly
xptiMisc.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Mike McCabe <mccabe@netscape.com>
00024  *   John Bandhauer <jband@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* Implementation of misc. xpti stuff. */
00041 
00042 #include "xptiprivate.h"
00043 
00044 struct xptiFileTypeEntry
00045 {
00046     const char*         name;
00047     int                 len;
00048     xptiFileType::Type  type;
00049 };
00050 
00051 static const xptiFileTypeEntry g_Entries[] = 
00052     {
00053         {".xpt", 4, xptiFileType::XPT},            
00054         {".zip", 4, xptiFileType::ZIP},            
00055         {".jar", 4, xptiFileType::ZIP},            
00056         {nsnull, 0, xptiFileType::UNKNOWN}            
00057     };
00058 
00059 // static 
00060 xptiFileType::Type xptiFileType::GetType(const char* name)
00061 {
00062     NS_ASSERTION(name, "loser!");
00063     int len = PL_strlen(name);
00064     for(const xptiFileTypeEntry* p = g_Entries; p->name; p++)
00065     {
00066         if(len > p->len && 0 == PL_strcasecmp(p->name, &(name[len - p->len])))
00067             return p->type;
00068     }
00069     return UNKNOWN;        
00070 }        
00071 
00072 /***************************************************************************/
00073 
00074 MOZ_DECL_CTOR_COUNTER(xptiAutoLog)
00075 
00076 xptiAutoLog::xptiAutoLog(xptiInterfaceInfoManager* mgr, 
00077                          nsILocalFile* logfile, PRBool append)
00078     : mMgr(nsnull), mOldFileDesc(nsnull)
00079 {
00080     MOZ_COUNT_CTOR(xptiAutoLog);
00081 
00082     if(mgr && logfile)
00083     {
00084         PRFileDesc* fd;
00085         if(NS_SUCCEEDED(logfile->
00086                     OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE | PR_APPEND |
00087                                              (append ? 0 : PR_TRUNCATE),
00088                                              0666, &fd)) && fd)
00089         {
00090 #ifdef DEBUG
00091             m_DEBUG_FileDesc = fd;
00092 #endif
00093             mMgr = mgr;
00094             mOldFileDesc = mMgr->SetOpenLogFile(fd);
00095             if(append)
00096                 PR_Seek(fd, 0, PR_SEEK_END);
00097             WriteTimestamp(fd, "++++ start logging ");
00098 
00099         }
00100         else
00101         {
00102 #ifdef DEBUG
00103         printf("xpti failed to open log file for writing\n");
00104 #endif
00105         }
00106     }
00107 }
00108 
00109 xptiAutoLog::~xptiAutoLog()
00110 {
00111     MOZ_COUNT_DTOR(xptiAutoLog);
00112 
00113     if(mMgr)
00114     {
00115         PRFileDesc* fd = mMgr->SetOpenLogFile(mOldFileDesc);
00116         NS_ASSERTION(fd == m_DEBUG_FileDesc, "bad unravel");
00117         if(fd)
00118         {
00119             WriteTimestamp(fd, "---- end logging   ");
00120             PR_Close(fd);
00121         }
00122     }
00123 }
00124 
00125 void xptiAutoLog::WriteTimestamp(PRFileDesc* fd, const char* msg)
00126 {
00127     PRExplodedTime expTime;
00128     PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &expTime);
00129     char time[128];
00130     PR_FormatTimeUSEnglish(time, 128, "%Y-%m-%d-%H:%M:%S", &expTime);
00131     PR_fprintf(fd, "\n%s %s\n\n", msg, time);
00132 }
00133 
00134 /***************************************************************************/
00135 
00136 nsresult 
00137 xptiCloneLocalFile(nsILocalFile*  aLocalFile,
00138                    nsILocalFile** aCloneLocalFile)
00139 {
00140     nsresult rv;
00141     nsCOMPtr<nsIFile> cloneRaw;
00142  
00143     rv = aLocalFile->Clone(getter_AddRefs(cloneRaw));
00144     if(NS_FAILED(rv))
00145         return rv;
00146 
00147     return CallQueryInterface(cloneRaw, aCloneLocalFile);
00148 }                        
00149 
00150 
00151 nsresult 
00152 xptiCloneElementAsLocalFile(nsISupportsArray* aArray, PRUint32 aIndex,
00153                             nsILocalFile** aLocalFile)
00154 {
00155     nsresult rv;
00156     nsCOMPtr<nsILocalFile> original;
00157 
00158     rv = aArray->QueryElementAt(aIndex, NS_GET_IID(nsILocalFile), 
00159                                 getter_AddRefs(original));
00160     if(NS_FAILED(rv))
00161         return rv;
00162 
00163     return xptiCloneLocalFile(original, aLocalFile);
00164 }