/*************************************************************************** fs_mod.cpp - description ------------------- begin : Tue Sep 16 09:38:51 2008 copyright : (C) 1999-2008 by Cavalli Andrea email : cavalli@bioc.unizh.ch **************************************************************************/ /*************************************************************************** * * * This program 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. * * * ***************************************************************************/ #include #include #include #include #include #include using namespace Almost; using namespace boost::filesystem; template<> inline string to_string(const path & p ){ return p.string(); } template<> inline string to_string(const boost::regex & exp){ return exp.str(); } vector fs_list(const path & p){ vector res; if(!is_directory(p)) throw ">> filesystem error: "+p.string()+" is not a directory"; directory_iterator iter(p),end; while(iter!=end){ res.push_back(iter->string()); ++iter; } return res; } vector file_glob(const path & p, string r){ vector res; boost::regex reg(r.c_str(),boost::regex::basic); if(!is_directory(p)) throw ">> filesystem error: "+p.string()+" is not a directory"; directory_iterator iter(p),end; while(iter!=end){ if(boost::regex_match(iter->path().filename().c_str(),reg)) { res.push_back(iter->string()); } ++iter; } return res; } extern "C" { void init_fs(){ //declarations here Module mod = Module("fs","Provides classes and functions to acces files and directories"); Class >(mod.self(),"path") .def_method("string",&path::string) .def_method("file_string",&path::file_string) .def_method("directory_string",&path::directory_string) .def_method("root_name",&path::root_name) .def_method("root_directory",&path::root_directory) .def_method("root_path",&path::root_path) .def_method("relative_path",&path::relative_path) .def_method("parent_path",&path::parent_path) .def_method("filename",&path::filename) .def_method("stem",&path::stem) .def_method("extension",&path::extension) .def_method("empty",&path::empty) .def_method("is_complete",&path::is_complete) .def_method("has_root_name",&path::has_root_name) .def_method("has_root_directory",&path::has_root_directory) .def_method("has_root_path",&path::has_root_path) .def_method("has_relative_path",&path::has_relative_path) .def_method("has_filename",&path::has_filename) .def_method("has_parent_path",&path::has_parent_path) ; //Add + and so bool (*f1)(const path &) = exists; void (*f1v)(const path &); void (*f2)(const path &, const path &);; //Predicates mod.def_function("exists","",f1); f1 = is_directory; mod.def_function("is_directory","",f1); f1= is_regular_file; mod.def_function("is_regular_file","",f1); f1 = is_other; mod.def_function("is_other","",f1); f1 = is_symlink; mod.def_function("is_symlink","",f1); f1 = is_empty; mod.def_function("is_empty","",f1); //Dir mod.def_function("ls","",fs_list); mod.def_function("file_glob","",file_glob); f1 = create_directory; mod.def_function("create_directory","",f1); f1v = boost::filesystem::remove; mod.def_function("remove","",f1v); f2 =boost::filesystem::rename; mod.def_function("rename","",f2); f2 = boost::filesystem::copy_file; mod.def_function("copy_file","",f2); } }