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

📄 check1.cpp

📁 代码检查工具 只要简单一些switch case的配对 以及类里面memset的使用
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					this->move_next(2);
				else if (m_buf[0] == slash && m_buf[1] == slash)
					this->move_next(2);
				else
					this->move_next();
			}
		}
		else if (c == dbl_quote)
		{
			this->move_next();
			while (m_len > 0)
			{
				if (m_buf[0] == dbl_quote)
				{
					this->move_next();
					break;
				}
				if (m_buf[0] == slash && m_buf[1] == dbl_quote)
					this->move_next(2);
				else if (m_buf[0] == slash && m_buf[1] == slash)
					this->move_next(2);
				else
					this->move_next();
			}
		}
	}
}

void CCheck::skip_white_space()
{
	while (this->is_white_space())
	{
		this->move_next();
	}
}

void CCheck::skip_comment()
{
	if (m_buf[0] == '/' && m_buf[1] == '*')
	{
		this->move_next(2);
		while (!(m_buf[0] == '*' && m_buf[1] == '/'))
		{
			this->move_next();
			if (m_len <= 0)
				return;
		}
		this->move_next(2);
	}
	else if (m_buf[0] == '/' && m_buf[1] == '/')
	{
		this->move_next(2);
		while (m_buf[0] != '\r' && m_buf[0] != '\n')
		{
			this->move_next();
			if (m_len <= 0)
				return;
		}
		while (m_buf[0] == '\r' || m_buf[0] == '\n')
		{
			this->move_next();
			if (m_len <= 0)
				return;
		}
	}
}

void CCheck::skip_nop()
{
	while (m_len > 0)
	{
		char* old_buf = m_buf;
		this->skip_white_space();
		this->skip_comment();
		this->skip_quotes();
		if (old_buf == m_buf)
			return;
	}
}

void CCheck::move_next(int n)
{
	if (m_len < n)
		n = m_len;
	m_buf += n;
	m_len -= n;
}

void CCheck::clr_buf()
{
	if (m_buf)
	{
		delete[] m_buf;
		m_buf = 0;
	}
	m_len = 0;
}

bool CCheck::get_token(const char* token)
{
	if (token == 0)
		return false;
	int n = strlen(token);
	while (m_len >= n)
	{
		this->skip_nop();
		if (m_len < n)
			return false;
		
		if (memcmp(m_buf, token, n) != 0)
		{
			this->move_next();
			continue;
		}

		char* old_buf = m_buf;
		int   old_len = m_len;
		this->move_next(n);
		if (m_len == 0)
			return false;
		
		if (strcmp(token, "switch") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == '('))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "case") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == '('))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "break") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == ';'))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "return") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == ';' || 
				m_buf[0] == '(' || m_buf[0] == '+' || m_buf[0] == '-'))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "default") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == ':'))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, ";") == 0)
		{
			if (this->is_quote())
				continue;
		}
		else if (strcmp(token, "{") == 0)
		{
			if (this->is_quote())
				continue;
		}
		else if (strcmp(token, "}") == 0)
		{
			if (this->is_quote())
				continue;
		}
		m_buf = old_buf;
		m_len = old_len;
		return true;
	}
	return false;
}

char* CCheck::get_next_token()
{
	static const char* token_list[] =
	{
		"switch", "case", "break", "return", "default", ";", "{", "}",
	};
	int token_cnt = sizeof(token_list) / sizeof(token_list[0]);

	while (m_len >= 0)
	{
		this->skip_nop();
		if (m_len <= 0)
			return 0;

		//match token
		const char* token = 0;
		for (int i = 0; i < token_cnt; i++)
		{
			int n = strlen(token_list[i]);
			if (memcmp(m_buf, token_list[i], n) == 0)
			{
				token = token_list[i];
				break;
			}
		}
		if (token == 0)
		{
			this->move_next();
			continue;
		}
		
		char* old_buf = m_buf;
		int   old_len = m_len;
		this->move_next(strlen(token));
		if (m_len == 0)
		{
			if (strcmp(token, "}") == 0 || strcmp(token, ";") == 0)
			{
				if (!this->is_quote())
				{
					m_buf = old_buf;
					m_len = old_len;
					return (char*)token; //last token!
				}
			}
		}
		if (m_len <= 0) //no more token!
		{
			return 0;
		}
		
		if (strcmp(token, "switch") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == '('))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "case") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == '('))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "break") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == ';'))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "return") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == ';' || 
				m_buf[0] == '(' || m_buf[0] == '+' || m_buf[0] == '-'))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, "default") == 0)
		{
			if (!(this->is_white_space() || this->is_comment() || m_buf[0] == ':'))
				continue;
			if (this->is_var_char(*(old_buf - 1)))
				continue;
		}
		else if (strcmp(token, ";") == 0)
		{
			if (this->is_quote())
				continue;
		}
		else if (strcmp(token, "{") == 0)
		{
			if (this->is_quote())
				continue;
		}
		else if (strcmp(token, "}") == 0)
		{
			if (this->is_quote())
				continue;
		}
		m_buf = old_buf;
		m_len = old_len;
		return (char*)token;
	}
	return 0;
}

void CCheck::init_line_info()
{
	char* p = m_buf;
	int len = m_len;
	m_setline_info.clear();
	m_last_alert = 0;

	char* ptr_start = p;
	while (1)
	{
		if (*p == '\r' || *p == '\n')
		{
			LINE_INFO info = { ptr_start, p };
			m_setline_info.push_back(info);
			p++; len--;
			if (*p = '\n')
			{
				p++; len--;
			}
			ptr_start = p;
		}
		else
		{
			p++; len--;
			if (len <= 0)
				return;
		}
	}
}

bool CCheck::lookup_line(const char* p, int& line_num, std::string& content)
{
	if (p == 0)
		return false;
	int count = m_setline_info.size();
	for (int i = 0; i < count; i++)
	{
		const LINE_INFO& info = m_setline_info[i];
		if (info.line_start <= p && p <= info.line_end)
		{
			char buf[200] = "";
			int len = info.line_end - info.line_start;
			if (len > sizeof(buf) - 1)
				len = sizeof(buf) - 1;
			memcpy(buf, info.line_start, len);
			content = buf;
			line_num = i + 1;
			return true;
		}
	}
	return false;
}

void CCheck::alert(const char* ptr)
{
	int line_num = 0;
	std::string content = "";
	if (!this->lookup_line(ptr, line_num, content))
		return;
	if (m_last_alert == line_num)
		return;
	if (m_tree == 0)
		return;
	if (strstr(content.c_str(), "case") == 0)
		return;

	//insert file item
	if (m_item == 0)
	{
		if (m_file != 0)
		{
			int icon = this->get_file_icon(m_file);
			m_item = m_tree->InsertItem(m_file, icon, icon);
		}
		if (m_item == 0)
			return;
	}

	//insert error item
	char str[300] = "";
	sprintf(str, "line %d: %s", line_num, content.c_str());
	int icon = this->get_err_icon();
	m_tree->InsertItem(str, icon, icon, m_item);
	this->refresh_tree();
	m_last_alert = line_num;
}

int	CCheck::get_err_icon() { return ICON_ERR; }
int	CCheck::get_file_icon(const char* file)
{
	if (file == NULL || strlen(file) < 2)
		return 0;
	const char* p = file + strlen(file);
	if (stricmp(p - 2, ".h") == 0)	
		return ICON_H;
	
	if (stricmp(p - 2, ".c") == 0)	
		return ICON_C;
	
	if (stricmp(p - 4, ".cpp") == 0)	
		return ICON_CPP;
	return 0;
}

⌨️ 快捷键说明

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