/** 
* @file FCWriteCache.cpp
* @author Erik Rodner
* @date 11/15/2007

*/
#include <iostream>

#include <sys/types.h>
#include <sys/stat.h>

#include "vislearning/features/simplefeatures/FCWriteCache.h"
#include "vislearning/baselib/Globals.h"

using namespace OBJREC;

using namespace std;
using namespace NICE;



FCWriteCache::FCWriteCache( const Config * conf,
			    FeatureFactory *_ff ) : 
    FeatureFactory ( conf ), ff(_ff)
{
    cachedir = conf->gS("cache", "root");
    cachemode = Globals::getCacheMode ( conf->gS("cache", "mode", "cat") );
}

FCWriteCache::~FCWriteCache()
{
}

int FCWriteCache::convert ( const NICE::Image & img, NICE::Vector & vec )
{
    int ret = ff->convert ( img, vec );
    std::string filename = Globals::getCacheFilename( cachedir, cachemode );
    std::string filename_feat = filename + ".feature";

    struct stat dummy;
    if ( stat ( filename_feat.c_str(), &dummy ) < 0 )
    {
	fprintf (stderr, "FCWriteCache: writing to %s\n", filename_feat.c_str());
	ofstream ofs ( filename_feat.c_str(), ios::out);
	if ( !ofs.good() )
	{
	    fprintf (stderr, "FCWriteCache: unable to write to file %s\n",
		filename_feat.c_str() );
	    exit(-1);
	}
	ofs << vec;
	ofs.close();
    } else {
	fprintf (stderr, "FCWriteCache: feature file %s exists !\n", filename_feat.c_str());
    }

    return ret;
}