<commit_before>#include "SymbolMgr.h"
#include "SymbolFactory.h"
#include "Symbol.h"
#include "Exception.h"
#include "FileHelper.h"
#include "StringHelper.h"
#include "Visitor.h"
#include "SymbolType.h"

#include <gum/FilepathHelper.h>

namespace ee
{

CU_SINGLETON_DEFINITION(SymbolMgr);

SymbolMgr::SymbolMgr()
{
}

SymbolMgr::~SymbolMgr()
{
	Clear();
}

SymPtr SymbolMgr::FetchSymbol(const std::string& filepath, int type)
{
	std::string fixed_path = gum::FilepathHelper::Format(filepath.c_str()).c_str();

	std::string filename = FileHelper::GetFilename(filepath);
	auto itr = m_syms.find(fixed_path);
	if (filename != SYM_GROUP_TAG && itr != m_syms.end())
	{
		SymPtr ret = itr->second.lock();
		if (ret) {
			return ret;
		} else {
			m_syms.erase(itr);
		}
	}
		
	auto sym = SymbolFactory::Create(fixed_path, type);
	if (!sym) 
	{
		const char* path = filepath.c_str();
		throw Exception("Create symbol fail: %s", path);
//			return NULL;
	}

	bool succ = sym->LoadFromFile(fixed_path);
	if (succ)
	{
		m_syms.insert(std::make_pair(fixed_path, sym));
		return sym;
	}
	else
	{
		const char* path = filepath.c_str();
		throw Exception("Load symbol %s fail!", path);

//			return NULL;
	}
}

void SymbolMgr::Insert(SymPtr& sym)
{
	std::string fixed_path = gum::FilepathHelper::Format(sym->GetFilepath().c_str()).c_str();
	m_syms.insert(std::make_pair(fixed_path, sym));
}

void SymbolMgr::Remove(const SymPtr& sym)
{
	std::string lowerpath = sym->GetFilepath();
	StringHelper::ToLower(lowerpath);
	auto itr = m_syms.find(lowerpath);
	// todo: new DummySymbol()
//	assert(itr != m_syms.end());
	if (itr != m_syms.end()) {
		m_syms.erase(itr);
	}
}

void SymbolMgr::Clear()
{
	m_syms.clear();<commit_msg>add log<commit_after>#include "SymbolMgr.h"
#include "SymbolFactory.h"
#include "Symbol.h"
#include "Exception.h"
#include "FileHelper.h"
#include "StringHelper.h"
#include "Visitor.h"
#include "SymbolType.h"

#include <gum/FilepathHelper.h>

namespace ee
{

CU_SINGLETON_DEFINITION(SymbolMgr);

SymbolMgr::SymbolMgr()
{
}

SymbolMgr::~SymbolMgr()
{
	Clear();
}

SymPtr SymbolMgr::FetchSymbol(const std::string& filepath, int type)
{
	std::string fixed_path = gum::FilepathHelper::Format(filepath.c_str()).c_str();

	std::string filename = FileHelper::GetFilename(filepath);
	auto itr = m_syms.find(fixed_path);
	if (filename != SYM_GROUP_TAG && itr != m_syms.end())
	{
		SymPtr ret = itr->second.lock();
		if (ret) {
			return ret;
		} else {
			m_syms.erase(itr);
		}
	}
		
	auto sym = SymbolFactory::Create(fixed_path, type);
	if (!sym) 
	{
		const char* path = filepath.c_str();
		throw Exception("Create symbol fail: %s", path);
//			return NULL;
	}

	bool succ = sym->LoadFromFile(fixed_path);
	if (succ)
	{
		m_syms.insert(std::make_pair(fixed_path, sym));
		return sym;
	}
	else
	{
		const char* path = filepath.c_str();
		printf("Load symbol %s fail!", path);
		throw Exception("Load symbol %s fail!", path);

//			return NULL;
	}
}

void SymbolMgr::Insert(SymPtr& sym)
{
	std::string fixed_path = gum::FilepathHelper::Format(sym->GetFilepath().c_str()).c_str();
	m_syms.insert(std::make_pair(fixed_path, sym));
}

void SymbolMgr::Remove(const SymPtr& sym)
{
	std::string lowerpath = sym->GetFilepath();
	StringHelper::ToLower(lowerpath);
	auto itr = m_syms.find(lowerpath);
	// todo: new DummySymbol()
//	assert(itr != m_syms.end());
	if (itr != m_syms.end()) {
		m_syms.erase(itr);
	}
}

void SymbolMgr::Clear()
{
	m_syms.clear();
{'fraction_non_alphanumeric': 0.1740139211136891, 'fraction_numerical': 0.0, 'mean_word_length': 4.998260869565217, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2224207', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1366, 'n_words': 253}