试图模拟一个linux shell.Problems模拟mkdir

时间:2018-02-05 01:26:30

标签: c++ linux shell pointers

我正在尝试制作一个c ++程序来模拟Linux shell。没什么好看的,只是一些命令。

我想这样做:

我创建了一个类,尝试使用节点和子节点模拟文件系统。 每个节点都可以有n个子节点,每个子节点都有一个指向其父节点的指针。

Node类看起来像这样:

class Node
{
public:
    Node();
    Node(bool isFile);
    Node(string name);
    friend class Tree;
    bool isFile;
    vector<Node*> childs;
    string name;
    string text;
    Node *parrent;
    string names = "-";

protected:

private:



};

我创建了一个addNode方法,我想用它来将节点添加到我的“Tree”中。而我试图做的就是这样:

void Tree::addNode(string name,bool isFile,string pwd)
{
size_t pos=0;
string delimiter = "\\";
string token;
Node *inserter = new Node();
Node *aux;
inserter->name=name;
inserter->isFile=isFile;
aux=&this->root;
pos = pwd.find(delimiter);
token = pwd.substr(0,pos);
pwd.erase(0,pos + delimiter.length());
if((pos = pwd.find(delimiter)) == string::npos)
{
    inserter->parrent=aux;
    if(aux->names.find(name) == string::npos)
    {
        aux->childs.push_back(inserter);
        aux->names.append(name);
        aux->names.append(" ");
    }
    else
    {

        cout << "The directory already exists" << endl;
    }
}
else
{
    while((pos = pwd.find(delimiter)) != string::npos)
    {
        token = pwd.substr(0,pos);
        pwd.erase(0,pos + delimiter.length());
        for(int i = 0; i<aux->childs.size(); i++)
        {
            if(aux->childs[i]->name.compare(token))
            {
                aux=aux->childs[i];
                cout << token << endl;
            }
        }

    }
    inserter->parrent=aux;
    if(aux->names.find(name) == string::npos)
    {
        aux->childs.push_back(inserter);
        aux->names.append(name);
        aux->names.append(" ");
    }
    else
    {

        cout << "The directory already exists" << endl;
    }
}

 }

所以,基本上,每次调用这个方法时,我都要确保每个目录都存在,并且我将指针aux更改为指向子地址,依此类推。

第一个用于在根目录中创建目录,以及用于每个其他目录的内容。

但是,由于我对指针的理解不足,我看不出哪里出错了。 当我在“根”节点中添加节点时,一切正常,但是当我在任何一个孩子中添加节点时,事情变得糟糕,我无法完全按照它进行操作。

我也有ls和cd实现 ls工作正常,但cd没有。我在检查目录的存在时尝试做同样的事情,但有些,即使目录不存在,如果你输入cd“name of directroy你是在“它只是添加到pdw,即使该目录不存在。

这是cd方法

bool Tree::exitsChild(string name,string pwd)
{
size_t pos=0;
string delimiter = "\\";
string token;
Node *aux;
aux=&root;
pos = pwd.find(delimiter);
token = pwd.substr(0,pos);
pwd.erase(0,pos + delimiter.length());
if((pos = pwd.find(delimiter)) == string::npos)
{
    if(aux->names.find(name)!= string::npos)
    {
        return true;
    }
    else
        return false;
}
else
{
    while((pos = pwd.find(delimiter)) != string::npos)
    {
        token = pwd.substr(0,pos);
        pwd.erase(0,pos + delimiter.length());
        for(int i = 0; i<aux->childs.size(); i++)
        {
            if(aux->childs[i]->name.compare(token))
            {
                aux=aux->childs[i];
            }
        }
    }
    if(aux->names.find(name) != string::npos)
        {
            return true;
        }
        else
        {
            return false;
        }
}

}

1 个答案:

答案 0 :(得分:2)

我有点愚蠢,我想通了,如果有比较,没有== 0,抱歉打扰