/* 
 * $Id: URLTool_Fetch.cpp,v 1.3 2006/03/05 05:00:09 ozawa Exp $
 *
 * Copyright 2005- ONGS Inc. All rights reserved.
 * 
 * author: Masanori OZAWA (ozawa@ongs.co.jp)
 * version: $Revision: 1.3 $
 *
 * 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.
 * 
 * THIS SOFTWARE IS PROVIDED BY ONGS INC ``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 ONGS INC OR 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.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the ONGS Inc.
 * 
 */

#include "xdtp.h"
#include "URLTool_Fetch.h"

#ifdef WITH_FETCH
# include "sys/param.h"
# include "fetch.h"
#endif

using namespace XDTP;

URLTool_Fetch::URLTool_Fetch()
{
    m_fp = NULL;
}

URLTool_Fetch::~URLTool_Fetch()
{
    closeResource(true);
}

bool URLTool_Fetch::openResource(const Glib::ustring& url, URLTYPE type)
{
#ifndef WITH_FETCH
    return false;
#else
    closeResource();
    
    m_fp = fetchGetURL(url.c_str(), "");
    
    return (m_fp ? true : false);
#endif
}

void URLTool_Fetch::closeResource(bool force)
{
    if (m_fp) {
        if (!fclose(m_fp) || force) {
            m_fp = NULL;
        }
        if (m_fp) {
            char buf[256] = {0};
            snprintf(buf, sizeof(buf) -1,
                "%s: URLTool_Fetch: Unable to close the resource. (errno = %d)",
                APP_NAME, errno);
            THROW(buf);
        }
    }
}

int URLTool_Fetch::readResource(char* buf, int len)
{
    int result = -1;
    
    if (m_fp) result = fread(buf, 1, len, m_fp);
    
    return result;
}

bool URLTool_Fetch::statResourceCore(const Glib::ustring& url, FILE_INFO& info)
{
    struct url_stat us;
    
#if 0
    struct url * pURL = fetchParseURL(url.c_str());
    if (!pURL) {
        fprintf(stderr, "%s: Unable to parse the URL. (%s)" LINE_SEP, 
            APP_NAME, url.c_str());
    }
    else {
        fprintf(stdout, "%s: debug: fetchParseURL: scheme = %s" LINE_SEP,
            APP_NAME, pURL->scheme);
        fprintf(stdout, "%s: debug: fetchParseURL: doc = %s" LINE_SEP,
            APP_NAME, pURL->doc);
        fetchFreeURL(pURL);
    }
#endif

    if (!fetchStatURL(url.c_str(), &us, "")) {
        info.size = us.size;
        info.atime = us.atime;
        info.mtime = us.mtime;

        return true;
    }
    
    return false;
}


syntax highlighted by Code2HTML, v. 0.9.1