src/expander.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2003 - 2004 Marcel Manthe <schneegloeckchen at gmx.li>
00002 // See ../LICENSE.txt for license details.
00003 //
00004 // $Id: expander.cpp 7 2007-05-24 11:03:53Z eg1981 $
00005 
00006 extern "C" {
00007 #include <unistd.h>
00008 #include <stdlib.h>
00009 
00010 #include <sys/types.h>
00011 #include <dirent.h>
00012 #include <sys/stat.h>
00013 }
00014 
00015 #include "expander.h"
00016 #include "util.h"
00017 
00018 
00019 Expander::Expander(){
00020     this->key_ = "";
00021     this->path_ = "";
00022     this->rehash();
00023 }
00024 
00025 bool Expander::rehash(){
00026     string path_env;
00027     char *p_c = getenv("PATH");
00028     if (p_c != NULL) {
00029         path_env = (string)p_c;
00030     }
00031     else{
00032         return(false);
00033     }
00034     paths_ = Util::stringSplit(path_env, ":");
00035     pathExpands_ = this->read();
00036     return true;
00037 }
00038 
00039 bool Expander::queryString (string s){
00040     paths_.clear();
00041     if ((s.find('/') == string::npos) || (Util::trimL(s).substr(0,1) == ".")) {
00042         //so performing on PATH
00043         key_ = s;
00044         pathSearch_=true;
00045     }
00046     else{
00047         //we're using the given string as search-base
00048 
00049         path_ = s.substr(0, s.find_last_of('/')+1);
00050         key_= s.substr(s.find_last_of('/')+1);
00051 
00052         paths_.insert(path_);
00053         pathSearch_=false;
00054     }
00055     return(true);
00056 }
00057 
00058 set<string> Expander::expands (){
00059 
00060     set<string> *target;
00061     if (pathSearch_) {
00062         target = &pathExpands_;
00063     }
00064     else
00065     {
00066         target = &expands_;
00067         *target = this->read();
00068     }
00069 
00070     return *target;
00071 }
00072 
00073 set<string> Expander::read(){
00074     set<string> expands;
00075     DIR *dir;
00076     dirent *entry;
00077 
00078     for (set<string>::iterator iter = paths_.begin(); iter != paths_.end(); iter++){
00079         dir=opendir(iter->c_str());
00080         if (dir != NULL) {
00081             do {
00082                 entry=readdir(dir);
00083                 if (entry != NULL) {
00084                     bool expand = false;
00085                     string filename = (string)(*iter) + "/"
00086                         + (string)(entry->d_name);
00087 
00088                     /* if we're dealing with a directory, its
00089                        sufficient to have read-permission (bash
00090                        acts like this) */
00091 
00092                     struct stat Buf;
00093                     if (stat(filename.c_str(), &Buf) == 0)
00094                     {
00095                         if (S_ISDIR(Buf.st_mode)) {
00096                             expand = true;
00097                         }
00098                     }
00099                     /* this test for executability uses the real
00100                        ids (see man 2 access) and thats why may
00101                        be incorrect if ncwm is s(u/g)id'd */
00102 
00103                     if (0 == access(filename.c_str(), X_OK)) {
00104                         expand = true;
00105                     }
00106 
00107                     if (expand == true) {
00108                         expands.insert((string)(entry->d_name));
00109                     }
00110                 }
00111             } while (entry != NULL);
00112             closedir(dir);
00113         }
00114     }
00115     expands.erase(".");
00116     expands.erase("..");
00117     return(expands);
00118 }

Generated on Thu May 24 15:19:32 2007 for ncwm by  doxygen 1.5.1