gecko-dev/modules/libjar/nsJAR.cpp

303 строки
6.8 KiB
C++
Исходник Обычный вид История

1999-06-02 01:08:32 +04:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
1999-06-02 01:08:32 +04:00
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
1999-06-02 01:08:32 +04:00
*
* The Original Code is Mozilla Communicator client code,
* released March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
1999-06-02 01:08:32 +04:00
*
* Contributor(s):
1999-06-02 01:08:32 +04:00
* Daniel Veditz <dveditz@netscape.com>
* Samir Gehani <sgehani@netscape.com>
* Mitch Stoltz <mstoltz@netsape.com>
1999-06-02 01:08:32 +04:00
*/
#include <string.h>
#include "nscore.h"
#include "pratom.h"
#include "prmem.h"
#include "prio.h"
#include "plstr.h"
#include "prlog.h"
#include "xp_regexp.h"
#include "nsRepository.h"
#include "nsIComponentManager.h"
#include "nsIZip.h"
1999-06-02 01:08:32 +04:00
#include "nsJAR.h"
#include "nsJARInputStream.h"
1999-06-02 01:08:32 +04:00
nsJAR::nsJAR()
{
NS_INIT_REFCNT();
1999-06-02 01:08:32 +04:00
}
nsJAR::~nsJAR()
{
}
1999-11-01 23:50:04 +03:00
NS_IMPL_ISUPPORTS2(nsJAR, nsIZip, nsIJAR);
1999-06-02 01:08:32 +04:00
NS_IMETHODIMP
nsJAR::Open(const char *aZipFileName, PRInt32 *_retval)
1999-06-02 01:08:32 +04:00
{
1999-11-01 23:50:04 +03:00
*_retval = mZip.OpenArchive(aZipFileName);
1999-06-02 01:08:32 +04:00
return NS_OK;
}
NS_IMETHODIMP
nsJAR::Extract(const char *aFilename, const char *aOutname, PRInt32 *_retval)
1999-06-02 01:08:32 +04:00
{
1999-11-01 23:50:04 +03:00
*_retval = mZip.ExtractFile(aFilename, aOutname);
1999-06-02 01:08:32 +04:00
return NS_OK;
}
NS_IMETHODIMP
nsJAR::Find(const char *aPattern, nsISimpleEnumerator **_retval)
{
if (!_retval)
return NS_ERROR_INVALID_POINTER;
1999-11-01 23:50:04 +03:00
nsZipFind *find = mZip.FindInit(aPattern);
if (!find)
return NS_ERROR_OUT_OF_MEMORY;
nsISimpleEnumerator *zipEnum = new nsJAREnumerator(find);
if (!zipEnum)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF( zipEnum );
*_retval = zipEnum;
return NS_OK;
}
NS_IMETHODIMP
nsJAR::GetInputStream(const char *aFilename, nsIInputStream **_retval)
{
nsresult rv;
nsJARInputStream* is = nsnull;
1999-11-01 23:50:04 +03:00
rv = nsJARInputStream::Create(nsnull, NS_GET_IID(nsIInputStream), (void**)&is);
if (!is) return NS_ERROR_FAILURE;
1999-11-01 23:50:04 +03:00
rv = is->Init(&mZip, aFilename);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
*_retval = (nsIInputStream*)is;
NS_ADDREF(*_retval);
return NS_OK;
}
//----------------------------------------------
// nsJAREnumerator constructor and destructor
//----------------------------------------------
nsJAREnumerator::nsJAREnumerator(nsZipFind *aFind)
: mFind(aFind),
mCurr(nsnull),
mIsCurrStale(PR_TRUE)
{
mArchive = mFind->GetArchive();
NS_INIT_REFCNT();
}
nsJAREnumerator::~nsJAREnumerator()
{
mArchive->FindFree(mFind);
}
NS_IMPL_ISUPPORTS(nsJAREnumerator, nsISimpleEnumerator::GetIID());
//----------------------------------------------
// nsJAREnumerator::HasMoreElements
//----------------------------------------------
1999-06-02 01:08:32 +04:00
NS_IMETHODIMP
nsJAREnumerator::HasMoreElements(PRBool* aResult)
1999-06-02 01:08:32 +04:00
{
PRInt32 err;
if (!mFind)
return NS_ERROR_NOT_INITIALIZED;
// try to get the next element
if (mIsCurrStale)
{
err = mArchive->FindNext( mFind, &mCurr );
if (err == ZIP_ERR_FNF)
{
*aResult = PR_FALSE;
return NS_OK;
}
if (err != ZIP_OK)
return NS_ERROR_FAILURE; // no error translation
mIsCurrStale = PR_FALSE;
}
*aResult = PR_TRUE;
return NS_OK;
1999-06-02 01:08:32 +04:00
}
//----------------------------------------------
// nsJAREnumerator::GetNext
//----------------------------------------------
NS_IMETHODIMP
nsJAREnumerator::GetNext(nsISupports** aResult)
{
nsresult rv;
PRBool bMore;
// check if the current item is "stale"
if (mIsCurrStale)
{
rv = HasMoreElements( &bMore );
if (NS_FAILED(rv))
return rv;
if (bMore == PR_FALSE)
{
*aResult = nsnull; // null return value indicates no more elements
return NS_OK;
}
}
// pack into an nsIJARItem
nsIJARItem* jarItem = new nsJARItem(mCurr);
if(jarItem)
{
jarItem->AddRef();
*aResult = jarItem;
mIsCurrStale = PR_TRUE; // we just gave this one away
return NS_OK;
}
else
return NS_ERROR_OUT_OF_MEMORY;
}
//-------------------------------------------------
// nsJARItem constructors and destructor
//-------------------------------------------------
nsJARItem::nsJARItem()
{
}
nsJARItem::nsJARItem(nsZipItem* aOther)
{
NS_INIT_ISUPPORTS();
name = PL_strndup( aOther->name, aOther->namelen );
namelen = aOther->namelen;
offset = aOther->offset;
headerloc = aOther->headerloc;
compression = aOther->compression;
size = aOther->size;
realsize = aOther->realsize;
crc32 = aOther->crc32;
next = nsnull; // unused by a JARItem
}
nsJARItem::~nsJARItem()
{
}
NS_IMPL_ISUPPORTS(nsJARItem, nsIJARItem::GetIID());
//------------------------------------------
// nsJARItem::GetName
//------------------------------------------
NS_IMETHODIMP
nsJARItem::GetName(char * *aName)
{
char *namedup;
if ( !aName )
return NS_ERROR_NULL_POINTER;
if ( !name )
return NS_ERROR_FAILURE;
namedup = PL_strndup( name, namelen );
if ( !namedup )
return NS_ERROR_OUT_OF_MEMORY;
*aName = namedup;
return NS_OK;
}
//------------------------------------------
// nsJARItem::GetCompression
//------------------------------------------
NS_IMETHODIMP
nsJARItem::GetCompression(PRUint16 *aCompression)
{
if (!aCompression)
return NS_ERROR_NULL_POINTER;
if (!compression)
return NS_ERROR_FAILURE;
*aCompression = compression;
return NS_OK;
}
//------------------------------------------
// nsJARItem::GetSize
//------------------------------------------
NS_IMETHODIMP
nsJARItem::GetSize(PRUint32 *aSize)
{
if (!aSize)
return NS_ERROR_NULL_POINTER;
if (!size)
return NS_ERROR_FAILURE;
*aSize = size;
return NS_OK;
}
//------------------------------------------
// nsJARItem::GetRealSize
//------------------------------------------
NS_IMETHODIMP
nsJARItem::GetRealsize(PRUint32 *aRealsize)
{
if (!aRealsize)
return NS_ERROR_NULL_POINTER;
if (!realsize)
return NS_ERROR_FAILURE;
*aRealsize = realsize;
return NS_OK;
}
//------------------------------------------
// nsJARItem::GetCrc32
//------------------------------------------
NS_IMETHODIMP
nsJARItem::GetCrc32(PRUint32 *aCrc32)
{
if (!aCrc32)
return NS_ERROR_NULL_POINTER;
if (!crc32)
return NS_ERROR_FAILURE;
*aCrc32 = crc32;
return NS_OK;
}