www.pudn.com > firev0.01.rar > filelist.cpp


/*
This file is part of the FIRE -- Flexible Image Retrieval System

FIRE is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FIRE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FIRE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include 
#include 
#include 

#include "filelist.hpp"
#include "diag.hpp"
#ifdef __HAVE_PORTER_STEMMER__
#include "porterstemmer.hpp"
#endif

using namespace std;


const int FileList::searchIndexForFilename(const string & filename) const {
  int idx=-1;
  for(unsigned int i=0;isize();++i) {
    for(unsigned int j=0;jnOfSuffices();++j) {
      if(filename==this->operator()(i,j)) {
        idx=i;
      }
    }
  }
  return idx;
}

const int FileList::searchClassForFilename(const string & filename) const {
  int cls=-1;
  for(unsigned int i=0;isize();++i) {
    for(unsigned int j=0;jnOfSuffices();++j) {
      if(filename==this->operator()(i,j)) {
        cls=this->cls(i);
      }
    }
  }
  return cls;
}

FileList::FileList() {  
  filename_="";
  path_="";
  suffices_=StringVector(0);
  filenames_=StringVector(0);
  classes_=IntVector(0);
  withDescription_=false;
  withCls_=false;
}



FileList::FileList(std::string filename) {
  filename_="";
  path_="";
  suffices_=StringVector(0);
  filenames_=StringVector(0);
  classes_=IntVector(0);
  load(filename);
  withDescription_=false;
  withCls_=false;

}


const ::std::string& FileList::description(const unsigned int i) const {
  return descriptions_[i];
}

const ::std::set FileList::decomposedDescription(const unsigned int i) const {
  set result;
#ifdef __HAVE_PORTER_STEMMER__
  string desc=this->description(i);
  string tmp;


  PorterStemmer stemmer;

  unsigned int k=0;
  while(k='a' && desc[k]<='z') || desc[k]>='A' && desc[k]<='Z')) ++k;;
    while(k='a' && desc[k]<='z') || desc[k]>='A' && desc[k]<='Z') {
      tmp.append(1,desc[k]);
      ++k;
    }
    if(tmp!="") result.insert(stemmer.stem(tmp));
  }
  stemmer.~PorterStemmer();
#else
  ERR << "Porter Stemmer not available... returning empty set" << endl;
#endif
  return result;  
}

const std::string FileList::basename(const unsigned int i) const {
  return filenames_[i];
}


int FileList::load(string filename){
  int cls;
  withDescription_=false;
  withCls_=false;
  this->clear();
  filename_=filename;
  ifstream ifp(filename_.c_str());
  if(!ifp) {
    ERR << "[FileList::load] Can not open file '" << filename_ << "'." << endl;
    return -1;
  }

  while(ifp.peek() == '#') {
    char devnull[1024];
    ifp.getline(devnull,1024);
  }
  
  string keyword, tmp;
  while(!ifp.eof()) {
    ifp >> keyword;
    if(!ifp.eof()) {
      if(keyword=="path") {
        ifp >> tmp;
        if (tmp=="this") {
          tmp=filename_;
          int end=tmp.rfind("/");
          path_.assign(tmp,0,end+1);
          //        cout << path_ << endl;
        } else {
          path_=tmp;
        }
      } else if(keyword=="#") {
        char devnull[1024];
        ifp.getline(devnull,1024);
      } else if (keyword=="suffix") {
        ifp >> tmp;
        suffices_.push_back(tmp);
      } else if (keyword=="withclasses") {
        withCls_=true;
      } else if (keyword=="withdescription") {
        withDescription_=true;
      } else if (keyword=="file") {
        ifp >> tmp;
        filenames_.push_back(tmp);
        if(withCls_) {
          ifp >> cls;
          classes_.push_back(cls);
        } else {
          classes_.push_back(-1);
        }
        if(withDescription_) {
          //read line to the end
          string desc="";
          while(ifp.peek()!='\n') {
            desc+=ifp.get();
          }
          descriptions_.push_back(desc);
        } else {
          descriptions_.push_back("");
        }
      } else { 
        DBG(DBG_STRANGE) << "[FileList::load] Unknown keyword '"<size();
}
int FileList::save(string){
  ERR << "[FileList::save] not yet implemented" << endl;
  return 0;
}

const string FileList::operator[](const unsigned int i) const{
  return(path_+filenames_[i]+suffices_[0]);
}

const string FileList::operator()(const unsigned int i, const unsigned int j) const{
  return(path_+filenames_[i]+suffices_[j]);
}
 
const int FileList::cls(const unsigned int i) const {
  return classes_[i];
}

int & FileList::cls(const unsigned int i) {
  return classes_[i];
}

string& FileList::filename(){
  return filename_;
}
const string& FileList::filename() const{
  return filename_;
}

string& FileList::path(){
  return path_;
}
const string& FileList::path() const{
  return path_;
}

const string& FileList::suffix(const unsigned int i) const{
  return suffices_[i];
}

const unsigned int FileList::nOfSuffices() const {
  return suffices_.size();
}

const unsigned int FileList::size() const{
  return filenames_.size();
}


void FileList::clear() {
  suffices_.clear();
  descriptions_.clear();
  path_="";
  filename_="";
  filenames_.clear();
  classes_.clear();
}