gecko-dev/ef/Runtime/FileReader/DiskFileReader.cpp
1999-11-02 06:38:29 +00:00

112 lines
2.3 KiB
C++

/* -*- 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/
*
* 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.
*
* The Original Code is mozilla.org code.
*
* 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.
*
* Contributor(s):
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DiskFileReader.h"
#include "ErrorHandling.h"
DiskFileReader::DiskFileReader(Pool &p, const char *fileName,
Int32 _bufferSize) : FileReader(p)
{
fp = 0;
buffer = 0;
#ifdef NO_NSPR
if (!(fp = fopen(fileName, "rb"))) {
#else
if (!(fp = PR_Open(fileName, PR_RDONLY, 0))) {
#endif
verifyError(VerifyError::noClassDefFound);
return;
}
bufferSize = _bufferSize;
eofReached = false;
buffer = new (pool) char[bufferSize];
if (!fillBuf())
verifyError(VerifyError::noClassDefFound);
}
/* Fill the buffer, returning number of items read */
Int32 DiskFileReader::fillBuf()
{
if (eofReached)
return 0;
#ifdef NO_NSPR
Int32 nread = fread(buffer, 1, bufferSize, fp);
#else
Int32 nread = PR_Read(fp, buffer, bufferSize);
#endif
if (nread < bufferSize) {
eofReached = true;
if (fp)
#ifdef NO_NSPR
fclose(fp);
#else
PR_Close(fp);
#endif
fp = 0;
}
curp = buffer;
limit = buffer+nread;
return nread;
}
bool DiskFileReader::read1(char *destp, int size)
{
while (curp+size > limit) {
int nread = (limit-curp);
memcpy(destp, curp, nread);
size -= nread;
destp += nread;
if (fillBuf() <= 0)
return false;
}
memcpy(destp, curp, size);
curp += size;
return true;
}
DiskFileReader::~DiskFileReader()
{
if (fp)
#ifdef NO_NSPR
fclose(fp);
#else
PR_Close(fp);
#endif
}