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

📄 util.cpp

📁 当前支持 16-bit, 32-bit and 64-bit 的二进制文件
💻 CPP
字号:
// util.cpp
// Copyright (C) 2009 Willow Schlanger

#include "util.h"

#include <dirent.h>
#include <errno.h>
#include <stdio.h>

#include <cctype>
#include <set>
#include <map>
#include <string>

#include <iostream>
#include <string>
#include <set>
#include <cctype>

#include <cstdio>
#include <cctype>
#include <iostream>

namespace ceres
{

std::string fixslash(std::string s)
{
	for(std::string::iterator i = s.begin(); i != s.end(); ++i)
	{
		if(*i == '\\')
			*i = '/';
	}
	if(!s.empty())
	{
		if(s[s.size()-1] != '/')
			s += "/";
	}
	return s;
}

int string_to_unsigned(std::string s)
{
	if(s.empty())
		return -1;
	std::string::iterator i = s.end();
	int value = 0;
	for(;;)
	{
		--i;
		if(*i < '0' || *i > '9')
		{
			return -1;
		}
		value = (value * 10) + (*i - '0');
		if(i == s.begin())
			break;
	}
	return value;
}

std::string int_to_string(int num)
{
	char s[1024];
	sprintf(s, "%d", num);
	return std::string(s);
}

// status: 0 on success, 1 if file has 0 size, 2 if error.
U1 *load_file(const char *filename, U8 &size, int &status)
{
	using namespace std;
	FILE *f = fopen(filename, "rb");
	if(f == NULL)
	{
		status = 2;
		return NULL;
	}
	if(feof(f))
	{
		fclose(f);
		status = 1;
		return NULL;
	}
	fseek(f, -1, SEEK_END);
	size = (U8)(U4)(1 + ftell(f));
	U1 *data = new U1 [size];
	rewind(f);
	if(fread(data, size, 1, f) != 1)
	{
		delete [] data;
		fclose(f);
		status = 2;
		return NULL;
	}
	fclose(f);
	status = 0;
	return data;
}

std::string make_uppercase(const std::string s)
{
	std::string t;
	for(std::string::const_iterator i = s.begin(); i != s.end(); ++i)
		t += std::toupper(*i);
	return t;
}

std::string make_lowercase(const std::string s)
{
	std::string t;
	for(std::string::const_iterator i = s.begin(); i != s.end(); ++i)
		t += std::tolower(*i);
	return t;
}

std::string remove_extension(const std::string s)
{
	if(s.empty())
		return "";
	std::string::const_iterator i = s.end();
	for(;;)
	{
		--i;
		if(*i == '.')
			break;
		if(i == s.begin())
			return s;
	}
	return std::string(s.begin(), i);
}

dll_finder_t::dll_finder_t(std::string pathlist)
{
	pathlist += ";.;";
	std::string::iterator i = pathlist.begin();
	std::string tmp;
	while(i != pathlist.end())
	{
		if(*i == ';')
		{
			if(!tmp.empty())
			{
				if(tmp[tmp.size() - 1] != '/')
					tmp += '/';
				files[tmp] = case_insensitive_set_t();
			}
			tmp.clear();
		}
		else
			tmp += *i;
		++i;
	}
	for(std::map<std::string, case_insensitive_set_t >::iterator j = files.begin();
		j != files.end();
		++j
	)
	{
		take_directory(j->first, j->second);
	}
}

void dll_finder_t::take_directory(std::string dir, case_insensitive_set_t &dest)
{
	dest.clear();
	DIR *pdir;
	struct dirent *pfile;
	pdir = opendir(dir.c_str());
	if(pdir == NULL)
		return;
	errno = 0;
	while((pfile = readdir(pdir)) != NULL)
	{
		dest.insert(pfile->d_name);
	}
	if(errno == 0)
		closedir(pdir);
}

void split_filename(std::string filename, std::string &directory, std::string &s)
{
	std::string::iterator i;
	
	for(i = filename.begin(); i != filename.end(); ++i)
		if(*i == '\\')
			*i = '/';
	
	i = filename.end();
	s = filename;
	while(i != filename.begin())
	{
		--i;
		if(*i == '/')
		{
			// found a slash.
			++i;
			directory = std::string(filename.begin(), i);
			s = std::string(i, filename.end());
			break;
		}
	}
}

std::string dll_finder_t::find(std::string filename)
{
	std::string directory;
	std::string s;
	split_filename(filename, directory, s);
	filename = s;	
	if(filename.empty())
		return "";
	
	if(!directory.empty())
	{
		// directory was specified. do not search current directory.
		case_insensitive_set_t files;
		take_directory(directory, files);
		if(files.find(filename) == files.end())
			return "";	// not found
		return directory + *files.find(filename);
	}
	
	// no directory specified. first search current directory, then search
	// the path list.
	if(files["."].find(filename) != files["."].end())
		return *files["."].find(filename);
	
	std::map<std::string, case_insensitive_set_t >::iterator u = files.begin();
	while(u != files.end())
	{
		if(u->second.find(filename) != u->second.end())
		{
			if(u->first == "./")
				return *u->second.find(filename);
			return u->first + *u->second.find(filename);
		}
		++u;
	}
	return "";	// can't find it!
}

}	// namespace ceres

⌨️ 快捷键说明

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