How to parse a string?

I'm trying to parse a message in C++. Here's what was happening when I did it in C#:

Say I want the DLL to return an answer based on a multiple of 2.5. I would pass 2.5 to the DLL like this:

dll$multiplefactor_25

It would get processed in the dll with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
            if (message.Length > 18)
            {
                if (Left(message, 18) == "dll$multiplefactor")
                {
                    string[] parse = message.Split('_');

                    if (parse.Length == 2)
                    {
                        double amount = Convert.ToDouble(parse[1]) / 10;
                        return multiplefactor(amount);
                    }
                    else
                    {
                        return multiplefactor(0);
                    }

                }
            }


I cannot get this to work in C++ (and really know very little about C# or C++. Any advice is appreciated.
There is no left() in C++. There's (at least) three things you can do, from most to least expensive:
1. string.substr(0,18)=="dll$multiplefactor"
2. string.find("dll$multiplefactor")==0
3. partialCompare(string,0,"dll$multiplefactor")==1
Here's the definition of partialCompare():

1
2
3
4
5
6
7
8
9
template <typename T>
bool partialCompare(const std::basic_string<T> &A,size_t offset,const std::basic_string<T> &B){
	if (A.size()-offset<B.size())
		return 0;
	for (size_t a=offset,b=0;b<B.size();a++,b++)
		if (A[a]!=B[b])
			return 0;
	return 1;
}
Last edited on
Topic archived. No new replies allowed.