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

📄 stringtoprop.cpp

📁 .net 方面的开发说明资料。
💻 CPP
字号:
// ========================================================
// String to PropGroup converter
//
// Design and Implementation by Floris van den Berg
// ========================================================

#include <string>
#include <vector>

#include "OpenNet.h"

// ========================================================

struct Prop {
	std::string key;
	std::string value;
};

struct PropGroup {
	std::string type;
	std::vector<Prop> properties;
};

// ========================================================

static void
SkipWhite(int &pos, std::string &s) {
	while ((pos < s.length()) && (isspace(s[pos])))
		++pos;
}

static void
ReadWord(int &pos, std::string &s, std::string &res) {
	res.erase();

	while ((pos < s.length()) && (isalpha(s[pos])))
		res = res + s[pos++];
}

static void
ReadKey(int &pos, std::string &s, std::string &res) {
	res.erase();

	while ((pos < s.length()) && (isalpha(s[pos]) && (s[pos] != '=')))
		res = res + s[pos++];
}

static bool
ReadValue(int &pos, std::string &s, std::string &res) {
	res.erase();

	if (s[pos] == '\"') {
		++pos;

		while ((pos < s.length()) && (s[pos] != '\"'))
			res = res + s[pos++];

		if (pos >= s.length())
			return false;
		
		if (s[pos] != '\"')
			return false;

		++pos;
	} else {
		while ((pos < s.length()) && ((isalnum(s[pos]) || (s[pos] == '$'))))
			res = res + s[pos++];
	}

	return true;
}

static bool
PropGetType(PropGroup *group, std::string &type) {
	if (group) {
		type = group->type;
		return true;
	}

	return false;
}

static bool
PropGetValue(PropGroup *group, std::string key, std::string &value) {
	if (group) {
		for (int i = 0; i < group->properties.size(); ++i) {
			if (stricmp(group->properties[i].key.c_str(), key.c_str()) == 0) {
				value = group->properties[i].value;
				return true;
			}
		}
	}

	return false;
}

static bool
PropGetValueInt(PropGroup *group, std::string key, int *value) {
	if (group) {
		for (int i = 0; i < group->properties.size(); ++i) {
			if (stricmp(group->properties[i].key.c_str(), key.c_str()) == 0) {
				if (group->properties[i].value.length() > 0) {
					std::string v = group->properties[i].value;
					char *n = &v[v.length() - 1];

					if (v[0] == '$') {
						*value = strtol(&v[1], &n, 16);
					} else {
						*value = strtol(&v[0], &n, 10);
					}
				}

				return true;
			}
		}
	}

	return false;
}

// ========================================================

DLL_API PROPERTYGROUP_HANDLE DLL_CALLCONV
EpCreatePropertyGroup() {
	return new PropGroup;
}

DLL_API bool DLL_CALLCONV
EpLoadProperties(PROPERTYGROUP_HANDLE g, const char *t) {
	int pos = 0;
	PropGroup *group = (PropGroup *)g;
	std::string s(t);
	std::string word;

	// first word in the string is the transport type

	if (!s.empty()) {
		SkipWhite(pos, s);
		ReadWord(pos, s, group->type);

		if (!group->type.empty()) {
			// next words are key/value pairs

			while (pos < s.length()) {
				Prop prop;

				// read in the key

				SkipWhite(pos, s);
				ReadKey(pos, s, prop.key);

				if (prop.key.empty())
					return false;

				SkipWhite(pos, s);
				
				// a key is seperated from a value using '='

				if (s[pos] != '=')
					return false;

				SkipWhite(++pos, s);

				// read in the value

				if (!ReadValue(pos, s, prop.value))
					return false;

				// put in the properties group

				group->properties.push_back(prop);
			}

			return true;
		}
	}

	return false;
}

DLL_API bool DLL_CALLCONV
EpPropGetType(PROPERTYGROUP_HANDLE group, char *type) {
	if ((group) && (type)) {
		std::string t;

		if (PropGetType((PropGroup *)group, t)) {
			strcpy(type, t.c_str());
			return true;
		}
	}

	return false;
}

DLL_API bool DLL_CALLCONV 
EpPropKeyExists(PROPERTYGROUP_HANDLE g, const char *key) {
	if (g) {
		PropGroup *group = (PropGroup *)g;

		for (int i = 0; i < group->properties.size(); ++i) {
			if (stricmp(group->properties[i].key.c_str(), key) == 0) {
				return true;
			}
		}
	}

	return false;
}

DLL_API bool DLL_CALLCONV 
EpPropGetValue(PROPERTYGROUP_HANDLE group, const char *key, char *value) {
	std::string v;

	if (PropGetValue((PropGroup *)group, std::string(key), v)) {
		strcpy(value, v.c_str());
		return true;
	}

	return false;
}

DLL_API bool DLL_CALLCONV 
EpPropGetValueAsBool(PROPERTYGROUP_HANDLE group, const char *key, bool *value) {
	std::string tmp1;
	int tmp2 = 0;
	
	if (PropGetValue((PropGroup *)group, key, tmp1)) {
		*value = (stricmp(tmp1.c_str(), "true") == 0);
		return true;
	} else if (PropGetValueInt((PropGroup *)group, key, &tmp2)) {
		*value = (tmp2 != 0);
		return true;
	}

	return false;
}

DLL_API bool DLL_CALLCONV 
EpPropGetValueAsInt(PROPERTYGROUP_HANDLE group, const char *key, int *value) {
	return PropGetValueInt((PropGroup *)group, key, value);
}

DLL_API void DLL_CALLCONV
EpDestroyPropertyGroup(PROPERTYGROUP_HANDLE group) {
	delete ((PropGroup *)group);
}

⌨️ 快捷键说明

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