/*
 * Copyright (C) 2007 Pleyo.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Pleyo nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PLEYO AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL PLEYO OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "kjs/wk_config.h"
#include "BIFile.h"

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "DeprecatedString.h"

using WebCore::String;

#define _DISABLE_BI_FILE 1

namespace BAL {

int BIFile::open(char openType)
{
#ifdef _DISABLE_BI_FILE
	return -1;
#else
    if (openType == 'w')
        m_file = ::open(m_path, O_WRONLY|O_CREAT|O_TRUNC, 0666);
    else if (openType == 'r')
        m_file = ::open(m_path, O_RDONLY);
    
    return m_file;
#endif	
}

void BIFile::close()
{
#ifdef _DISABLE_BI_FILE

#else
    ::close(m_file);
#endif
}

char* BIFile::read(size_t size)
{
#ifdef _DISABLE_BI_FILE
	return NULL;
#else
    char* readData = new char[size+1];
    ::read(m_file,readData,size);
    return readData;
#endif	
}

void BIFile::write(String dataToWrite)
{
#ifdef _DISABLE_BI_FILE
	
#else
    ::write(m_file,dataToWrite.deprecatedString().ascii(),dataToWrite.length());
#endif
}

int BIFile::getSize()
{
#ifdef _DISABLE_BI_FILE
	return -1;
#else

    int fileSize, current;
    
    //save the current offset
    current = lseek(m_file,0,SEEK_CUR);
    
    //save the size
    fileSize = lseek(m_file,0,SEEK_END);
    
    //go back to the previous offset
    lseek(m_file,current,SEEK_SET);
    
    return fileSize;
#endif

}

}
