How to get multiple parameters from boost::split - c++

How can split a std::string so that both the values of structure object get filled.
q.Qcmd = "Command1"
q.timeValue = 1.0
This is the sample code.
struct QueueCommand
{
std::vector<std::string>QCmd;
std::vector<float>timeValue;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::string str = "command1|1.0"
std::string str1 = "command2|2.0"
QueueCommand q;
boost::split( q,str,boost::is_any_of("|")); // need to fill Qcmd and timevalue
boost::split( q,str1,boost::is_any_of("|"));
return 0;
}

This is not a correct usage of boost::split because the first parameter has to be a container for string, and split does not know how to fill the specific structure. I give you hints on how to solve it. I have not tested the code, but you can try yourself:
First, you have to declare a vector to store the parts:
std::vector<std::string> parts;
Then, boost::split can split the command string:
boost::split( parts, str, boost::is_any_of("|"));
Reserve enough space in the corresponding QueueCommand variable:
q.QCmd.resize(parts.size() - 1);
(the last one contains the float number). Copy the strings to the structure. You have to make sure the parts array contains at least two elements:
std::copy(parts.begin(), parts.begin() + parts.size() - 1, q.QCmd.begin());
Set the float part of the struct:
q.timeValue = boost::lexical_cast<float>(parts[parts.size() - 1]);
5gon12eder suggestion in comments:
Which seems more elegant and efficient, but requires C++11 for std::move:
q.timeValue = boost::lexical_cast<float>(parts.back());
parts.pop_back();
q.QCmd = std::move(parts);

Related

Locating doubles in a char array (C++/ROOT)

I have parsed some XML data, and I want latitude and longitude information. The line I need to parse is this:
trkptlat="60.397015"lon="5.32299"
This is an element in a char array, how can I extract/parse the numbers as doubles? Note that the number precision varies as the data goes on, so I can't solely rely on picking out the column indexes.
You are using C-Style char arrays as strings. So, my assumptions that you still are on C. Otherwise, you would std::string. Ìn C++ there is no jsutification to use a char array instead of a std::string.
Please see the C-Style solution:
#include <cstring>
#include <cstdlib>
#include <cstdio>
int main() {
char data[] = "SomeTrashMoreTrashtrkptlat=\"60.397015\"lon=\"5.32299\"MoreTrashMoreTrash";
char firstDoubleIndicator[] = "trkptlat=\"";
char secondDoubleIndicator[] = "\"lon=\"";
double latitude = 0;
double longitude = 0;
char* startPosition = strstr(data, firstDoubleIndicator);
if (startPosition) {
latitude = std::atof(startPosition + std::strlen(firstDoubleIndicator));
}
startPosition = strstr(data, secondDoubleIndicator);
if (startPosition) {
longitude = std::atof(startPosition + std::strlen(secondDoubleIndicator));
}
std::printf("\nlatitude:\t%f\nlongitude:\t%f\n\n", latitude, longitude);
return 0;
}
This can be done in numerous ways, warning totally untested code ahead
Using a safe version of sscan with a parameter that looks something like this
"\"trkptlat=\"%d\"lon=\"%d\""
Be sure to check the return value for length and errors.
Using std::find_first_of with digits and with dots
auto start = find_first_of (haystack.begin(), haystack.end(), digits.begin(), digit.end());
auto end = find_first_of (it, haystack.end(), digitsdot.begin(), digitdot.end(),
[](char a, char b){ return a != b; });
double lat = atof(start); // somewhere there might be a version that returns how many chars read also.
// check for errors
etc.
see further http://www.cplusplus.com/reference/algorithm/find_first_of/
if your the trusting kind you can take some shortcuts that you know that
"trkptlat="
will be prepended so you can start at
auto start = haystack+preLen;

How to break up a CString that does not have any delimiters within it?

I have a CString that I want to break up into small strings. It is a string consisting of a constant 2 byte header and 2 byte footer, but the rest of the string has no discernible pattern. I need to break them up based on sizes: so the first two bytes become the header, then I need to extract the next 2, then 3 and so on. (these numbers are in no pattern either)
Example:
CString test = "1010eefabbccde1f1f"
I need
CString header = "1010";
CString test1 = "eefa";
CString test2 = "bbccde";
CString footer = "1f1f";
I read about sscanf being used for this purpose, but I have only managed to use it split strings into int.
Example:
CString test = '101022223333331010';
int a,b,c,d;
sscanf(test,"%02d%02d%03d%02d",&a,&b,&c,&d);
This works for strings containing only numbers. But when I do the same for strings by changing %d to %s, exceptions get raised.
Is there a better way to do this?
My understanding is that given an input string test and vector<size_t> sizes of sizes, you wish to break the string apart into those sizes, and then you wish to take those parts, treat them as hex numbers, and return them in vector<int> result.
I'm going to presume that you have already tested test to ensure the correct number of characters exist. And I'm going to assume that the sizes include the header and footer sizes.
After running something like this:
const auto test = "1010eefabbccde1f1f"s;
const vector<size_t> sizes { 4U, 4U, 6U, 4U };
const auto result = accumulate(cbegin(sizes), cend(sizes), vector<int>(), [&, pos = 0U](auto& a, const auto& b) mutable {
a.push_back(stoi(test.substr(pos, b), nullptr, 16));
pos += b;
return a;
});
result will contain:
4112
61178
12307678
7967
Live Example

Parse (replace) in C++ std::string

I have a "custom" string that has the following format. Example:
std::string MyString = "RndOrder%5d - RndCustomer%8s - RndHex%8x";
I would like to replace/parse the string:
the %5d (%NUM_d) would be replaced with a random 5-digit decimal
the %8s (%NUM_s) would be replaced with a random 8-chars
the %8x (%NUM_x) would be replaced with a random 8-digit hexadecimal
Is there any function that helps me parse those "special marks"? Not sure if I would have to parse the string char by char and check for every possible combination.
If the format can be variant (not always the fixed 3 arguments: %5d, %8s and %8x) and you want to be flexible in that manner, you should write your own implementation for that.
Assuming that count defined after % is a general digit (not only 5 or 8) you could try using the std::regex_search or std::regex_match to find the actual mnemonics you are looking for. For example your expression could look like %\d+[dsx]
Then you should parse it to find the COUNT and type and substitute with a random number acquired with the desired generator.
To parse you could try updating the above expression to %(\d+)([dsx]) and capturing groups.
A sample parse implementation for your case could look like this:
std::string text = "RndOrder%5d - RndCustomer%8s - RndHex%8x";
auto reg = std::regex("%(\\d+)([sdx])");
std::smatch match;
while (std::regex_search(text, match, reg))
{
const auto& full = match.str(); // in 1st iter contains "%5d"
const auto& count = match.str(1); // in 1st iter contains "5"
const auto& type = match.str(2); // in 1st iter contains "d"
// further processing: type conversion, number generation, string replacement
text = match.suffix().str();
}
For implementation example with search and group capturing you can also check out another question: Retrieving a regex search in C++
Ok, assuming that you're actually asking about string parsing here (and not random number/data generation)... have a look at this:
int iRandom1 = 12345; // 5-digit decimal
int iRandom3 = 0x12345678; // 8-digit hexadecimal
char cRandom2[9] = "RandomXY\0"; // Don't forget to NULL-terminate!
std::string sFormat = "RndOrder%5d - RndCustomer%8s - RndHex%8x";
char cResultBuffer[500]; // Make sure this buffer is big enough!
std::sprintf( cResultBuffer, sFormat.c_str(), iRandom1, cRandom2, iRandom3 );
std::string MyString = cResultBuffer; // MyString = "RndOrder12345 - RndCustomerRandomXY - RndHex12345678";
It's a candidate for std::snprintf (c++14), but take care to request the correct buffer size in one call, allocate a buffer and then format the string into the buffer:
#include <iostream>
#include <cstring>
#include <string>
template<class...Args>
std::string replace(const char* format, Args const&... args)
{
// determine number of characters in output
auto len = std::snprintf(nullptr, 0, format, args...);
// allocate buffer space
auto result = std::string(std::size_t(len), ' ');
// write string into buffer. Note the +1 is allowing for the implicit trailing
// zero in a std::string
std::snprintf(&result[0], len + 1, format, args...);
return result;
};
int main() {
auto s = replace("RndOrder%5d - RndCustomer%8s - RndHex%8x", 5, "foo", 257);
std::cout << s << std::endl;
}
expected output:
RndOrder 5 - RndCustomer foo - RndHex 101

Parse char vector to std::map<string, string>

I have a vector of unsigned char values. The data inside is keys and values in the format "key=value". Each pair is terminated by a '\0' character and the last pair is terminated by a double '/0'. A value can contain a "=" as well, so this delimiter shouldnt be a criteria for the pair, only the terminating '\0'
From this vector of single unsigned char I want to get a std::map<std::string, std::string> object.
What would be an effective way to go through the vector and fill the map?
Thanks in advance!
This code finds all pairs, but when printing them, my console seems to mess things up, so I suspect invalid characters... maybe it has to do with the fact that I'm using unsigned char?
unsigned char *env, *nxt;
std::string delimiter = "=";
std::string line;
std::map<string, string> myMap;
for (env = &myVector[0]; *env != '\0'; env = nxt + 1)
{
for (nxt = env; *nxt != '\0'; ++nxt)
{
if (nxt >= &myVector[myVector.size()])
{
printf("string not terminated\n");
return -1;
}
}
line = std::string(env, env + myVector.size());
myMap.insert(std::pair<string, string>(
line.substr(0, line.find(delimiter)),
line.substr(line.find(delimiter) + 1, line.size())));
}
Since the key cannot contain the = sign, it's fairly easy:
Split on (generate all positions of) zero characters.
Split created entries on first =.
Use the generated regions as key/value.
To make things clearer (than your double nested loops, ew), I'd use some reasonable data structure to mark the faux-strings in the buffer (if you want to avoid copying), such as pair<unsigned, unsigned>.
So, the signatures of the functions (which in this case tell more than their implementations, I suppose), could look like this:
using BufferRange = pair<unsigned, unsigned>;
using BufferEntry = pair<BufferRange, BufferRange>;
list<BufferRange> splitOnZeroes(Buffer const& b);
BufferEntry splitOnEquality(BufferRange const& br, Buffer const& b);
void addToMap(map<string, string>& m, BufferEntry const& p)
Those are fairly simplified; my code design OCD tells me that BufferRange could be a type carrying not only the numerical indices, but also the reference to the buffer itself. Changing that (if required) left as an exercise.

How to work with null pointers in a std::vector

Say I have a vector of null terminates strings some of which may be null pointers. I don't know even if this is legal. It is a learning exercise. Example code
std::vector<char*> c_strings1;
char* p1 = "Stack Over Flow";
c_strings1.push_back(p1);
p1 = NULL; // I am puzzled you can do this and what exactly is stored at this memory location
c_strings1.push_back(p1);
p1 = "Answer";
c_strings1.push_back(p1);
for(std::vector<char*>::size_type i = 0; i < c_strings1.size(); ++i)
{
if( c_strings1[i] != 0 )
{
cout << c_strings1[i] << endl;
}
}
Note that the size of vector is 3 even though I have a NULL at location c_strings1[1]
Question. How can you re-write this code using std::vector<char>
What exactly is stored in the vector when you push a null value?
EDIT
The first part of my question has been thoroughly answered but not the second. Not to my statisfaction at least. I do want to see usage of vector<char>; not some nested variant or std::vector<std::string> Those are familiar. So here is what I tried ( hint: it does not work)
std::vector<char> c_strings2;
string s = "Stack Over Flow";
c_strings2.insert(c_strings2.end(), s.begin(), s.end() );
// char* p = NULL;
s = ""; // this is not really NULL, But would want a NULL here
c_strings2.insert(c_strings2.end(), s.begin(), s.end() );
s = "Answer";
c_strings2.insert(c_strings2.end(), s.begin(), s.end() );
const char *cs = &c_strings2[0];
while (cs <= &c_strings2[2])
{
std::cout << cs << "\n";
cs += std::strlen(cs) + 1;
}
You don't have a vector of strings -- you have a vector of pointer-to-char. NULL is a perfectly valid pointer-to-char which happens to not point to anything, so it is stored in the vector.
Note that the pointers you are actually storing are pointers to char literals. The strings are not copied.
It doesn't make a lot of sense to mix the C++ style vector with the C-style char pointers. Its not illegal to do so, but mixing paradigms like this often results in confused & busted code.
Instead of using a vector<char*> or a vector<char>, why not use a vector<string> ?
EDIT
Based on your edit, it seems like what your'e trying to do is flatten several strings in to a single vector<char>, with a NULL-terminator between each of the flattened strings.
Here's a simple way to accomplish this:
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
int main()
{
// create a vector of strings...
typedef vector<string> Strings;
Strings c_strings;
c_strings.push_back("Stack Over Flow");
c_strings.push_back("");
c_strings.push_back("Answer");
/* Flatten the strings in to a vector of char, with
a NULL terminator between each string
So the vector will end up looking like this:
S t a c k _ O v e r _ F l o w \0 \0 A n s w e r \0
***********************************************************/
vector<char> chars;
for( Strings::const_iterator s = c_strings.begin(); s != c_strings.end(); ++s )
{
// append this string to the vector<char>
copy( s->begin(), s->end(), back_inserter(chars) );
// append a null-terminator
chars.push_back('\0');
}
}
So,
char *p1 = "Stack Over Flow";
char *p2 = NULL;
char *p3 = "Answer";
If you notice, the type of all three of those is exactly the same. They are all char *. Because of this, we would expect them all to have the same size in memory as well.
You may think that it doesn't make sense for them to have the same size in memory, because p3 is shorter than p1. What actually happens, is that the compiler, at compile-time, will find all of the strings in the program. In this case, it would find "Stack Over Flow" and "Answer". It will throw those to some constant place in memory, that it knows about. Then, when you attempt to say that p3 = "Answer", the compiler actually transforms that to something like p3 = 0x123456A0.
Therefore, with either version of the push_back call, you are only pushing into the vector a pointer, not the actual string itself.
The vector itself, doesn't know, or care that a NULL char * is an empty string. So in it's counting, it sees that you have pushed three pointers into it, so it reports a size of 3.
I have a funny feeling that what you would really want is to have the vector contain something like "Stack Over Flow Answer" (possibly without space before "Answer").
In this case, you can use a std::vector<char>, you just have to push the whole arrays, not just pointers to them.
This cannot be accomplished with push_back, however vector have an insert method that accept ranges.
/// Maintain the invariant that the vector shall be null terminated
/// p shall be either null or point to a null terminated string
void push_back(std::vector<char>& v, char const* p) {
if (p) {
v.insert(v.end(), p, p + strlen(p));
}
v.push_back('\0');
} // push_back
int main() {
std::vector<char> v;
push_back(v, "Stack Over Flow");
push_back(v, 0);
push_back(v, "Answer");
for (size_t i = 0, max = v.size(); i < max; i += strlen(&v[i]) + 1) {
std::cout << &v[i] << "\n";
}
}
This uses a single contiguous buffer to store multiple null-terminated strings. Passing a null string to push_back results in an empty string being displayed.
What exactly is stored in the vector when you push a null value?
A NULL. You're storing pointers, and NULL is a possible value for a pointer. Why is this unexpected in any way?
Also, use std::string as the value type (i.e. std::vector<std::string>), char* shouldn't be used unless it's needed for C interop. To replicate your code using std::vector<char>, you'd need std::vector<std::vector<char>>.
You have to be careful when storing pointers in STL containers - copying the containers results in shallow copy and things like that.
With regard to your specific question, the vector will store a pointer of type char* regardless of whether or not that pointer points to something. It's entirely possible you would want to store a null-pointer of type char* within that vector for some reason - for example, what if you decide to delete that character string at a later point from the vector? Vectors only support amortized constant time for push_back and pop_back, so there's a good chance if you were deleting a string inside that vector (but not at the end) that you would prefer to just set it null quickly and save some time.
Moving on - I would suggest making a std::vector > if you want a dynamic array of strings which looks like what you're going for.
A std::vector as you mentioned would be useless compared to your original code because your original code stores a dynamic array of strings and a std::vector would only hold one dynamically changable string (as a string is an array of characters essentially).
NULL is just 0. A pointer with value 0 has a meaning. But a char with value 0 has a different meaning. It is used as a delimiter to show the end of a string. Therefore, if you use std::vector<char> and push_back 0, the vector will contain a character with value 0. vector<char> is a vector of characters, while std::vector<char*> is a vector of C-style strings -- very different things.
Update. As the OP wants, I am giving an idea of how to store (in a vector) null terminated strings some of which are nulls.
Option 1: Suppose we have vector<char> c_strings;. Then, we define a function to store a string pi. A lot of complexity is introduced since we need to distinguish between an empty string and a null char*. We select a delimiting character that does not occur in our usage. Suppose this is the '~' character.
char delimiter = '~';
// push each character in pi into c_strings
void push_into_vec(vector<char>& c_strings, char* pi) {
if(pi != 0) {
for(char* p=pi; *p!='\0'; p++)
c_strings.push_back(*p);
// also add a NUL character to denote end-of-string
c_strings.push_back('\0');
}
c_strings.push_back(deimiter);
// Note that a NULL pointer would be stored as a single '~' character
// while an empty string would be stored as '\0~'.
}
// now a method to retrieve each of the stored strings.
vector<char*> get_stored_strings(const vector<char>& c_strings) {
vector<char*> r;
char* end = &c_strings[0] + c_strings.size();
char* current = 0;
bool nullstring = true;
for(char* c = current = &c_strings[0]; c != end+1; c++) {
if(*c == '\0') {
int size = c - current - 1;
char* nc = new char[size+1];
strncpy(nc, current, size);
r.push_back(nc);
nullstring = false;
}
if(*c == delimiter) {
if(nullstring) r.push_back(0);
nullstring = true; // reset nullstring for the next string
current = c+1; // set the next string
}
}
return r;
}
You still need to call delete[] on the memory allocated by new[] above. All this complexity is taken care of by using the string class. I very rarely use char* in C++.
Option 2: You could use vector<boost::optional<char> > . Then the '~' can be replaced by an empty boost::optional, but other other parts are the same as option 1. But the memory usage in this case would be higher.