⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directorymap.cpp

📁 JAPAN RTOS TOPPERS/JSP kernel 1.4 PreRelease simulation environment JSP kernel Windows simulation
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 *  TOPPERS/JSP Kernel
 *      Toyohashi Open Platform for Embedded Real-Time Systems/
 *      Just Standard Profile Kernel
 * 
 *  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
 *                              Toyohashi Univ. of Technology, JAPAN
 * 
 *  忋婰挊嶌尃幰偼丆埲壓偺 (1)乣(4) 偺忦審偐丆Free Software Foundation 
 *  偵傛偭偰岞昞偝傟偰偄傞 GNU General Public License 偺 Version 2 偵婰
 *  弎偝傟偰偄傞忦審傪枮偨偡応崌偵尷傝丆杮僜僼僩僂僃傾乮杮僜僼僩僂僃傾
 *  傪夵曄偟偨傕偺傪娷傓丏埲壓摨偠乯傪巊梡丒暋惢丒夵曄丒嵞攝晍乮埲壓丆
 *  棙梡偲屇傇乯偡傞偙偲傪柍彏偱嫋戻偡傞丏
 *  (1) 杮僜僼僩僂僃傾傪僜乕僗僐乕僪偺宍偱棙梡偡傞応崌偵偼丆忋婰偺挊嶌
 *      尃昞帵丆偙偺棙梡忦審偍傛傃壓婰偺柍曐徹婯掕偑丆偦偺傑傑偺宍偱僜乕
 *      僗僐乕僪拞偵娷傑傟偰偄傞偙偲丏
 *  (2) 杮僜僼僩僂僃傾傪丆儔僀僽儔儕宍幃側偳丆懠偺僜僼僩僂僃傾奐敪偵巊
 *      梡偱偒傞宍偱嵞攝晍偡傞応崌偵偼丆嵞攝晍偵敽偆僪僉儏儊儞僩乮棙梡
 *      幰儅僯儏傾儖側偳乯偵丆忋婰偺挊嶌尃昞帵丆偙偺棙梡忦審偍傛傃壓婰
 *      偺柍曐徹婯掕傪宖嵹偡傞偙偲丏
 *  (3) 杮僜僼僩僂僃傾傪丆婡婍偵慻傒崬傓側偳丆懠偺僜僼僩僂僃傾奐敪偵巊
 *      梡偱偒側偄宍偱嵞攝晍偡傞応崌偵偼丆師偺偄偢傟偐偺忦審傪枮偨偡偙
 *      偲丏
 *    (a) 嵞攝晍偵敽偆僪僉儏儊儞僩乮棙梡幰儅僯儏傾儖側偳乯偵丆忋婰偺挊
 *        嶌尃昞帵丆偙偺棙梡忦審偍傛傃壓婰偺柍曐徹婯掕傪宖嵹偡傞偙偲丏
 *    (b) 嵞攝晍偺宍懺傪丆暿偵掕傔傞曽朄偵傛偭偰丆TOPPERS僾儘僕僃僋僩偵
 *        曬崘偡傞偙偲丏
 *  (4) 杮僜僼僩僂僃傾偺棙梡偵傛傝捈愙揑傑偨偼娫愙揑偵惗偠傞偄偐側傞懝
 *      奞偐傜傕丆忋婰挊嶌尃幰偍傛傃TOPPERS僾儘僕僃僋僩傪柶愑偡傞偙偲丏
 * 
 *  杮僜僼僩僂僃傾偼丆柍曐徹偱採嫙偝傟偰偄傞傕偺偱偁傞丏忋婰挊嶌尃幰偍
 *  傛傃TOPPERS僾儘僕僃僋僩偼丆杮僜僼僩僂僃傾偵娭偟偰丆偦偺揔梡壜擻惈傕
 *  娷傔偰丆偄偐側傞曐徹傕峴傢側偄丏傑偨丆杮僜僼僩僂僃傾偺棙梡偵傛傝捈
 *  愙揑傑偨偼娫愙揑偵惗偠偨偄偐側傞懝奞偵娭偟偰傕丆偦偺愑擟傪晧傢側偄丏
 * 
 *  @(#) $Id: directorymap.cpp,v 1.8 2003/06/30 12:50:32 takayuki Exp $
 */


// $Header: /home/CVS/configurator/base/directorymap.cpp,v 1.8 2003/06/30 12:50:32 takayuki Exp $

/* MEMO:儊儌彂偒
     Q: 偙偆偄偆僋儔僗偼僥儞僾儗乕僩偵偟偨傎偆偑偄偄偲巚偆偑?
	 A: 僼傽僀儖偵揻偒弌偟偨屻丄撉傒弌偟偨偲偒偵偳偆傗偭偰僋儔僗惗惉偡傋偒偐偑傢偐傜側偄
	     (僋儔僗僼傽僋僩儕傪嶌傞偺偼傗傔偨偄)
		崱偲堘偆宆傪戙擖偝傟偨偲偒偺懳張朄偑傢偐傜側偄
*/


#include "base/directorymap.h"
#include "base/message.h"
#include <stdarg.h>
#include <typeinfo>
#include <cassert>
#include <cstdio>

#ifdef _MSC_VER
  #pragma warning(disable:4786)
#endif

using namespace std;

int Directory::defaultflag = Directory::NOTHING;

Directory::Directory(const Directory & src)
{
    parent = 0;
    flag = defaultflag;
    defaultflag &= ~DESTRUCT;

    type = src.type;
    switch(type)
    {
    case LITERAL:
        content.literal = new string(*src.content.literal);
        break;
    default:
        content = src.content;
        break;
    }
}

Directory::~Directory(void)
{
	disconnect();						//恊偲偺楢寢傪夝彍
	map<string,Directory*>::clear();	//巕僲乕僪偺嶍彍
	clearContent();							//帺暘偺拞恎傪嶍彍偡傞
}

void Directory::clearContent(void)
{
	switch(this->getType())
	{
	case LITERAL:
		delete content.literal;
		break;
	case OBJECT:
		delete content.instance;
		break;
	default:
		break;
	}

	type = UNKNOWN;
	content.pointer = 0;
}

Directory * Directory::findNode(bool automatic_creation, const string & path)
{
    string::size_type top, tail, length;
    string work;
    Directory::iterator scope;
    Directory * node = this;

	if(this == NULL)
		return NULL;

    length = path.length();
    top = 0;
    if(path[0] == '/')
    {
        while(node->getParent() != 0)
            node = node->getParent();
		if(path.size() == 1)
			return node;
        top = 1;
    }

    do {
        tail = path.find_first_of('/', top);
        if(tail == string::npos)
            work = path.substr(top);
        else
            work = path.substr(top, tail-top);

		if(work.compare(".") == 0 || work.compare("..") == 0)
		{
			if(work.size() > 1 && node->getParent() != 0)
				node = node->getParent();
		}else
		{
			scope = node->begin();
			while(scope != node->end())
			{
				if(work.compare((*scope).first) == 0)
					break;
				++ scope;
			}

			if(scope == node->end())
			{
				if(!automatic_creation)
					return 0;
				node = node->addChild(work, new Directory);
			}else
				node = (*scope).second;
		}
        top = tail + 1;
    } while( tail != string::npos && top < length );

    return node;
}

Directory * Directory::findNode(bool automatic_creation, const char * key, va_list vl)
{
    Directory::iterator scope;
    Directory * node = this;

	if(this == NULL)
		return NULL;

    if(*key == '/' && *(key+1) == '\x0')
    {
        while(node->getParent() != 0)
            node = node->getParent();
		if(vl == 0)
			return node;
        key = va_arg(vl, const char *);
    }

	do {
		if(strcmp(key,".") != 0)
		{
			if(strcmp(key,"..") == 0)
			{
				node = node->parent;
			}else
			{
				scope = node->begin();
				while(scope != node->end())
				{
					if((*scope).first.compare(key) == 0)
						break;
					++ scope;
				}

				if(scope == node->end())
				{
					if(!automatic_creation)
						return 0;
					node = node->addChild(key, new Directory);
				}else
					node = (*scope).second;
			}
		}
		if(vl != 0)
	        key = va_arg(vl, const char *);
		else
			break;
    } while( key != 0 && node != 0);

    return node;
}

Directory & Directory::operator =(void * pointer)
{
	if(this->getType() != UNKNOWN && this->getType() != POINTER)
		clearContent();

	type = POINTER;
	content.pointer = pointer;
	return *this;
}

Directory & Directory::operator =(long value)
{
	if(this->getType() != UNKNOWN && this->getType() != INTEGER)
		clearContent();

	type = INTEGER;
	content.value = value;
	return *this;
}

Directory & Directory::operator =(const string & literal)
{
	if(this->getType() != UNKNOWN && this->getType() != LITERAL)
		clearContent();

	type = LITERAL;
	content.literal = new string(literal);

	return *this;
}

Directory & Directory::operator =(const char * constliteral)
{
	if(this->getType() != UNKNOWN && this->getType() != CONSTLITERAL)
		clearContent();

	type = CONSTLITERAL;
	content.const_literal = constliteral;
	return *this;
}

Directory & Directory::operator =(Garbage * instance)
{
	if(this->getType() != UNKNOWN)
		clearContent();

	type = OBJECT;
	content.instance = instance;
	return *this;
}

void * Directory::operator new(size_t sz)
{
	defaultflag |= DESTRUCT;
	return ::operator new(sz);
}

Directory::operator const long(void) const
{
	if( type == UNKNOWN )
		ExceptionMessage("Bad cast exception","晄惓僉儍僗僩椺奜") << throwException;
	return content.value;
}

void * Directory::operator * (void) const
{
	if( type == UNKNOWN )
		ExceptionMessage("Bad cast exception","晄惓僉儍僗僩椺奜") << throwException;
	return content.pointer;
}

Directory * Directory::addChild(const std::string & key, Directory * node)
{
	iterator scope;
	std::pair<iterator, bool> work;

    if(node == 0)
        node = new Directory;
    else
    	if(node->parent != 0)
	    	node->disconnect();

	node->parent = this;
	if((scope = find(key)) != end())
	{
		Directory * old = (*scope).second;
		old->disconnect();
		old->erase();
	}

	work = insert(value_type(key, node));
	node->myself = work.first;

	return node;
}

Directory::iterator Directory::erase(iterator it)
{
    iterator result;
	Directory * scope = (*it).second;

		//栠傝抣偺嶌惉
	if((result = it) == begin())
      ++ result;
    else
      -- result;

		//奜偟偨僲乕僪偺屻巒枛
	if((scope->flag & DESTRUCT) != 0)
		delete scope;
	else
		scope->erase();

	return result;
}

void Directory::erase(void)
{
	iterator scope;

	if(this != NULL)
	{
		if(parent != 0)
		{
			parent->erase(myself);
		}else
		{
			while(!empty())
			{
				scope = begin();
				if((scope->second->flag & DESTRUCT) != 0)
					delete scope->second;
				else
					scope->second->erase();
			}
		}
	}
}

void Directory::disconnect(void)
{
	if(parent != 0)
	{
		parent->map<string,Directory*>::erase(myself);
		parent = 0;
	}
}

Directory * Directory::getNext(void) const
{
	if(parent == 0)
		return 0;

	iterator scope;
	scope = myself;
	++ scope;
	if(scope == parent->end())
		return 0;

	return (*scope).second;
}

Directory * Directory::getPrev(void) const
{
	if(parent == 0 && myself == parent->begin())
		return 0;

	reverse_iterator scope;
	
	scope = parent->rbegin();
	while(scope != parent->rend() && (*scope).second != (*myself).second)
		++ scope;

	++ scope;
	return scope != parent->rend() ? (*scope).second : 0;
}

bool Directory::changeKey(const string & key)
{
	Directory * scope;

	if( key.size() == 0)
		return false;

	scope = parent;

	disconnect();
	scope->addChild(key, this);
	return true;
}


void Directory::drawTree(ostream * out, int level, string * link)
{
	iterator scope;
	iterator scope2;

	if(level == 0)
		link = new string;
	else
		*out << (*link).substr(0, (level-1)*3) << " +-";

	*out << '[' << getKey() << ']';
    switch(type)
    {
    case POINTER:
        out->setf(ios::hex);
        *out << " : PTR [" << content.pointer << "]";
        break;
    case INTEGER:
        out->setf(ios::dec);
        *out << " : INT [" << content.value << "]";
        break;
    case LITERAL:
        *out << " : STR [" << *content.literal << "]";
        break;
    case CONSTLITERAL:
        *out << " : CSTR[" << content.const_literal << "]";
        break;
    case OBJECT:
        {
            *out << " : OBJ";
            break;
        }
	case UNKNOWN:
		break;
    default:
        *out << "UNKNOWN";
    }
    *out << '\n';
	(*link) += " | ";
	scope = begin();
	while(scope != end())
	{
		scope2 = scope;
		++ scope;

		if(scope == end())
			(*link)[level*3+1] = ' ';
		(*scope2).second->drawTree(out, level+1, link);
	}


	link->erase(level*3);
	if(level == 0)
		delete link;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -