Exemple #1
0
int ParseSearchReplace(EBuffer *B, const char *str, int replace, SearchReplaceOptions &opt) {
    int where = 0;
    int len = 0;
    const char *p = str;

    memset((void *)&opt, 0, sizeof(opt));
    if (p == 0) return 0;
    if (replace) {
        if (ParseSearchOptions(replace, BFS(B, BFS_DefFindReplaceOpt), opt.Options) == 0) return 0;
        opt.Options |= SEARCH_REPLACE;
    } else {
        if (ParseSearchOptions(replace, BFS(B, BFS_DefFindOpt), opt.Options) == 0) return 0;
    }

    while (*p) {
        switch (*p) {
        case '\\':
            if (where == 0) {
                opt.strSearch[len++] = *p++;
                if (*p == 0) return 0;
                opt.strSearch[len++] = *p++;
            } else if (where == 1) {
                opt.strReplace[len++] = *p++;
                if (*p == 0) return 0;
                opt.strReplace[len++] = *p++;
            } else
                return 0;
            break;
        case '/':
            where++;
            if (!replace && where == 1) where++;
            if (where == 2)
                opt.Options = SEARCH_NEXT;
            if (where > 2) return 0;
            len = 0;
            p++;
            break;
        default:
            if (where == 0)
                opt.strSearch[len++] = *p++;
            else if (where == 1)
                opt.strReplace[len++] = *p++;
            else {
                char c = *p;

                p++;
                if (ParseSearchOption(replace, c, opt.Options) == 0) return 0;
            }
        }
    }
    if (opt.Options & SEARCH_RE);
    else {
        if (UnquoteString(opt.strSearch) == 0) return 0;
        if (opt.Options & SEARCH_REPLACE)
            if (UnquoteString(opt.strReplace) == 0) return 0;
    }

    opt.ok = 1;
    return 1;
}
Exemple #2
0
int var::compare(const var& rhs) const
{
	// less than zero this < rhs
	// zero this == rhs 
	// greater than zero this > rhs 
	if(vt != rhs.vt || vt == EMP || rhs.vt == EMP)
		return -2;
	
	if(vt == DW)
	{
		if(dw < rhs.dw) return -1;
		if(dw == rhs.dw) return 0;
		if(dw > rhs.dw) return 1;
	} 	
	else if(vt == FLT)
	{
		if(flt < rhs.flt) return -1;
		if(flt == rhs.flt) return 0;
		if(flt > rhs.flt) return 1;
	}
	else if(vt == STR) {
		if (isbuf == rhs.isbuf)
			return str.compare(rhs.str);
		else {
			wstring Hex;
			if (isbuf) {
				//Buffer / String
				wstring s=str;
				UnquoteString(s,'#','#');
				Str2Hex((wstring)rhs.str,Hex,rhs.size);
				return s.compare(Hex);
			} else { 
				//String / Buffer
				wstring s=rhs.str;
				UnquoteString(s,'#','#');
				Str2Hex((wstring)str,Hex,size);
				return Hex.compare(s);
			}
		}
	}
	return 0;
}
	void BaseRunTimeConfig::setVariables( RunTimeVariableMap& vars )
	{
		for( RunTimeVariableMap::iterator itr = vars.begin(); itr != vars.end(); ++itr )
		{
			string value = UnquoteString( itr->second );
			if( value == "true" )
				itr->second = "\"1\"";
			else if( value == "false" )
				itr->second = "\"0\"";
			//cout << itr->first << " " << itr->second << "\n";
		}
	}
Exemple #4
0
var& var::operator+=(const wstring& rhs)
{
	wstring s;
	if(vt == STR) {
		wstring s=rhs;
		if (UnquoteString(s,'#','#')) {
			if (!isbuf) {
				//String + buf Hex Buffer to String ok
				size_t len=s.length()/2;
				wchar_t* buf = (wchar_t*)malloc(len*2+1);
				Str2Rgch(s,buf,len+1);
				s.assign(buf,len);
				str  += s;
				size += len;
				free(buf);
			} else { 
				// Buffer + Buffer
				str = L"#"+str.substr(1,str.length()-2)+s+L"#";
				size += s.length()/2;
			}
		} else {
			if (!isbuf) {
				//str + str
				str += rhs;
				size += rhs.length();
			} else {
				//buf + str
				wstring Hex;
				Str2Hex(s,Hex,s.length());
				str = L"#"+str.substr(1,str.length()-2)+Hex+L"#";
				size += s.length();
			}
		}

	} else if(vt == DW) {
		var v=(wstring)rhs;

		wchar_t dwbuf[12];
		if (v.isbuf) {
			//ulong + BUFFER >> CONCATE HEX
			s = strbuffhex();
			wsprintf(dwbuf, L"%08X",dw);
			*this = L"#"+((wstring)dwbuf)+s+L"#";
		} else {
			//ulong + STRING >> CONCATE _ultow+str
			s = _wcsupr(_ultow(dw, dwbuf, 16));
			*this = s+v.str;
		}
	}

	return *this;
}
Exemple #5
0
var::var(wstring& rhs)
{
	vt = STR;
	dw = 0;
	flt = 0;
	str  = rhs;
	wstring s=rhs;
	if (UnquoteString(s,'#','#')) {
		size = s.length()/2;
		int (*pf)(int) = toupper; 
		transform(str.begin(), str.end(), str.begin(), pf);
		isbuf = true;
	} else {
		size = rhs.length();
		isbuf = false;
	}
}
Exemple #6
0
/**
 * @brief Parse a line in control file.
 */
static bool
ParseControlFileLine(char buf[], char **outKeyword, char **outValue)
{
	char	   *keyword = NULL;
	char	   *value = NULL;
	char	   *p;
	char	   *q;

	*outKeyword = NULL;
	*outValue = NULL;

	if (buf[strlen(buf) - 1] != '\n')
		ereport(ERROR,
			(errcode(EXIT_FAILURE),
			 errmsg("too long line \"%s\"", buf)));

	p = buf;				/* pointer to keyword */

	/*
	 * replace '\n' to '\0'
	 */
	q = strchr(buf, '\n');
	if (q != NULL)
		*q = '\0';

	/*
	 * delete strings after a comment letter outside quotations
	 */
	q = FindUnquotedChar(buf, '#', '"', '\\');
	if (q != NULL)
		*q = '\0';

	/*
	 * if result of trimming is a null string, it is treated as an empty line
	 */
	p = TrimSpaces(buf);
	if (*p == '\0')
		return false;

	/*
	 * devide after '='
	 */
	q = FindUnquotedChar(buf, '=', '"', '\\');
	if (q != NULL)
		*q = '\0';
	else
		ereport(ERROR,
			(errcode(EXIT_FAILURE),
			 errmsg("invalid input \"%s\"", buf)));

	q++;					/* pointer to input value */

	/*
	 * return a value trimmed space
	 */
	keyword = TrimSpaces(p);
	value = TrimSpaces(q);

	if (!keyword[0] || !value[0])
		ereport(ERROR,
			(errcode(EXIT_FAILURE),
			 errmsg("invalid input \"%s\"", buf)));

	value = UnquoteString(value, '"', '\\');
	if (!value)
		ereport(ERROR,
			(errcode(EXIT_FAILURE),
			 errmsg("unterminated quoted field")));

	*outKeyword = keyword;
	*outValue = value;
	return true;
}
	void BaseRunTimeConfig::initializeFromBuffer( const string& cfgStr )
	{ TRACER_OP_START("BaseRunTimeConfig::initializeFromBuffer"); TRACER(cfgStr, READ, HEAP, "Configuration string");
        if (&cfgStr != &this->cfgStr)
            this->cfgStr = cfgStr;

        m_warnings.str("");
        RunTimeVariableMap newVars;
        int lineNum = 0;

        try
        {
            getVariables();

            istringstream cfgStream(cfgStr);

            string line;
		    while (getline(cfgStream, line))
            {
                ++lineNum;
                bal::trim(line); // trim whitespace

                // skip blank or comment lines
                if (line.empty() || line.find_first_of("#[") == 0)
                    continue;

                // otherwise, the line must be in the form "Key=Value" and Key must be in the variables map
                if (!bal::contains(line, "="))
                {
                    m_warnings << "Line " << lineNum << ": line does not define a parameter in the \"Parameter = Value\" format.\n";
                    continue;
                }

                size_t predIdx = line.find_first_of('=') + 1;
				TRACER_OP_START("get value for key"); TRACER_BI;
                string key = line.substr(0, predIdx-1); TRACER(key, WRITE, STACK, "Parameter name");
                bal::trim(key);

                if (m_variables.count(key) == 0)
                {
                    m_warnings << "Line " << lineNum << ": \"" << key << "\" is not a supported parameter.\n";
                    continue;
                }

                RunTimeVariableMap::iterator itr = newVars.find(key);
                if (itr != newVars.end())
                {
                    m_warnings << "Line " << lineNum << ": \"" << key << "\" has already been defined.\n";
                    continue;
                }
				
                size_t valBegin = line.find_first_not_of("\t ", predIdx);
                size_t valEnd = valBegin;
                bool inQuote = false;
                for (valEnd = valBegin; valEnd < line.size(); ++valEnd)
                {
                    if (line[valEnd] == '"' && line[valEnd-1] != '\\')
                        inQuote = !inQuote;
                    else if ((line[valEnd] == '#') && !inQuote)
                        break; // stop at unquoted comment token
                }
                
                if (valEnd == valBegin || valBegin == string::npos)
                {
                    m_warnings << "Line " << lineNum << ": no value set for \"" << key << "\"; did you mean to use an empty string (\"\")?\n";
                    continue;
                }

                string& value = newVars[key];
                value = TrimWhitespace(line.substr(valBegin, valEnd-valBegin));
                if (value.empty())
                {
                    m_warnings << "Line " << lineNum << ": no value set for \"" << key << "\"; did you mean to use an empty string (\"\")?\n";
                    continue;
                }

                value = UnquoteString(value);
                bal::replace_all(value, "\\\"", "\"");
                bal::replace_all(value, "true", "1");
                bal::replace_all(value, "false", "0"); TRACER(value, WRITE, HEAP, std::string("Value for ")+std::string(key)); TRACER_BO; TRACER_OP_END("get value for key");
            }
        }
        catch (exception& e)
        {
            m_warnings << "Line " << lineNum << ": " << e.what() << "\n";
        }

        // apply the new variable values
        setVariables(newVars); TRACER_OP_END("BaseRunTimeConfig::initializeFromBuffer");
	}