Пример #1
0
void Exception::formatError(HRESULT result, IUnknown* object, const IID& iid, const tchar* operation)
{
	tstring wmiText(formatWmiError(result));

	if (!wmiText.empty())
	{
		m_details = Core::fmt(TXT("%s [0x%08X - %s]"), operation, result, wmiText.c_str());
	}
	else
	{
		tstring source;
		tstring description;

		// Query for IErrorInfo details.
		extractErrorInfo(object, iid, source, description);

		tstring resultCode(CStrCvt::FormatError(result));

		// Format the error string.
		if (!source.empty() || !description.empty())
			m_details = Core::fmt(TXT("%s [0x%08X - %s] {%s : %s}"), operation, result, resultCode.c_str(), source.c_str(), description.c_str());
		else
			m_details = Core::fmt(TXT("%s [0x%08X - %s]"), operation, result, resultCode.c_str());
	}
}
string Preprocessor::applyDefines(string code) {

    const string definePointer = "#define";
    string resultCode(code);
    string::size_type definePositionBegin = code.find(definePointer, 0);
    string::size_type definePosition;
    while (definePositionBegin != code.npos) {
        definePosition = definePositionBegin + definePointer.length();
        while (code[definePosition] == ' ') {
            definePosition++;
        }
        if (!this->inBrackets(code, definePositionBegin)) {
            string::size_type macroEndPosition = code.find(' ', definePosition);
            string macroValue = code.substr(definePosition, macroEndPosition - definePosition);
            string::size_type constantEndPosition = code.find(' ', macroEndPosition + 1);
            if (code.find('\n', macroEndPosition + 1) < constantEndPosition) {
                constantEndPosition = code.find('\n', macroEndPosition + 1);
            }
            string constantValue = code.substr(macroEndPosition + 1, constantEndPosition - macroEndPosition - 1);
            string::size_type macroPosition = resultCode.find(macroValue);
            while (macroPosition != resultCode.npos) {
                resultCode.replace(macroPosition, macroValue.length(), constantValue);
                macroPosition = resultCode.find(macroValue, macroPosition);
            }
        }
        definePositionBegin = code.find(definePointer, definePosition);
    }
    definePositionBegin = resultCode.find(definePointer, 0);
    while (definePositionBegin != resultCode.npos) {
        definePosition = definePositionBegin + definePointer.length();
        while (code[definePosition] == ' ') {
            definePosition++;
        }
        if (!this->inBrackets(code, definePositionBegin)) {
            string::size_type definePositionEnd = resultCode.find('\n', definePositionBegin);
            resultCode.replace(definePositionBegin, definePositionEnd - definePositionBegin, "");
        }
        definePositionBegin = resultCode.find(definePointer, definePositionBegin);
    }
    return resultCode;
}
string Preprocessor::deleteComments(string code) {
    const string lineCommentPointer = "//";
    const string commentBeginPointer = "/*";
    const string commentEndPointer = "*/";
    string resultCode (code);
    string::size_type commentPosition = resultCode.find(lineCommentPointer, 0);
    string::size_type commentEndPosition;
    while (commentPosition != string::npos) {
        commentEndPosition = resultCode.find('\n', commentPosition);
        if (commentEndPosition == resultCode.npos) {
            commentEndPosition = resultCode.length();
        }
        if (!inBrackets(resultCode, commentPosition)) {
            resultCode.replace(commentPosition, commentEndPosition - commentPosition, "");
        } else {
            commentPosition += lineCommentPointer.length();
        }
        commentPosition = resultCode.find(lineCommentPointer, commentPosition);
    }
    commentPosition = resultCode.find(commentBeginPointer, 0);

    while (commentPosition != string::npos) {
        commentEndPosition = resultCode.find(commentEndPointer, commentPosition);
        if (commentEndPosition == resultCode.npos) {
            commentEndPosition = resultCode.length();
        } else {
            commentEndPosition += commentEndPointer.length();
        }
        if (!inBrackets(resultCode, commentPosition)) {
            resultCode.replace(commentPosition, commentEndPosition - commentPosition, "");
        } else {
            commentPosition += commentBeginPointer.length();
        }
        commentPosition = resultCode.find(commentBeginPointer, commentPosition);
    }

    return resultCode;
}
string Preprocessor::applyIncludes(string code, string filePath, string specialDirectory) {
    const string includePointer = "#include";
    const string format = ".cmm";
    string resultCode(code);
    string::size_type includePositionBegin = code.find(includePointer, 0);
    string::size_type includePosition;
    while (includePositionBegin != resultCode.npos) {
        includePosition = resultCode.find(' ', includePositionBegin) + 1;
        if (!this->inBrackets(resultCode, includePositionBegin)) {
            if (resultCode[includePosition] == '<') {
                includePosition++;
                string::size_type fileNameEndPosition = resultCode.find('>', includePosition);
                string fileName = resultCode.substr(includePosition, fileNameEndPosition - includePosition);
                string fileSource = this->preprocess(specialDirectory + fileName + format);
                if (fileSource == "") {
                    println("No file in directory:" + fileName + " in " + specialDirectory);
                }
                string::size_type replaceSizeCount = fileNameEndPosition - includePositionBegin + 1;
                resultCode.replace(includePositionBegin, replaceSizeCount, fileSource);
            } else if (resultCode[includePosition] == '"'){
                includePosition++;
                string::size_type fileNameEndPosition = resultCode.find('"', includePosition);
                string fileName = resultCode.substr(includePosition, fileNameEndPosition - includePosition);
                string fileSource = this->preprocess(filePath + fileName, filePath + fileName.substr(0, fileName.find_last_of('/')), specialDirectory);
                if (fileSource == "") {
                    println("No file in directory:" + fileName + " in " + specialDirectory);
                }
                string::size_type replaceSizeCount = fileNameEndPosition - includePositionBegin + 1;
                resultCode.replace(includePositionBegin, replaceSizeCount, fileSource);
            }
        }
        includePositionBegin = resultCode.find(includePointer, includePosition);

    }
    return resultCode;
}