You would probably have your class which contains all the data have a static function which creates the object given your serialized string. How it works depending on your data. You say you have defined offsets for fields, but also say they are delimited. This poses a problem as both need to be handled differently.
Simple case for your example:
class DataContainer
{
using namespace std; // FIXME Is this legal?
public:
DataContainer(string part1, string part2, string part3, string part4, string part5) :
m_part1(part1), m_part2(part2), m_part3(part3), m_part4(part4), m_part5(part5)
{
}
static DataContainer readFromStream(istream &stream)
{
DataContainer ret;
char tmp[16];
stream.get(tmp, 4);
ret.m_part1 = string(tmp, 4);
stream.get(tmp, 4);
ret.m_part2 = string(tmp, 4);
stream.get(tmp, 3);
ret.m_part3 = string(tmp, 3);
stream.get(tmp, 4);
ret.m_part4 = string(tmp, 4);
stream.get(tmp, 3);
ret.m_part5 = string(tmp, 3);
return ret;
}
private:
DataContainer()
{
}
string m_part1, m_part2, m_part3, m_part4, m_part5; // Put these in an array if they belong in an array.
};
Note: I have inlined all my functions. You should probably put your functions in their own .cpp file.
Use it like so:
DataContainer dc = DataContainer::readFromStream(std::cin);
This reads your data from cin
(stdin
) and puts the data structure in dc
.
If you already have the data read, you can use STL iterators (which are basically pointers to elements of a container) quite easily. You can also use classic indexing, which is simpler for your given case. For example:
static DataContainer readFromBuffer(string buffer)
{
DataContainer ret;
ret.m_part1 = string(buffer, 0, 4);
ret.m_part2 = string(buffer, 4, 4);
ret.m_part3 = string(buffer, 8, 3);
ret.m_part4 = string(buffer, 11, 4);
ret.m_part5 = string(buffer, 15, 3);
return ret;
}