simple C/C++ Serialization or deflating or Marshalling user defined data types or objects

But is it to use boost or not it is your decision 🙂  It is a good lib. But I find it very handy for small in another way as below;-

// Just a simple serialization in C++
#ifndef CVARIABLELENDATA_HPP
#define CVARIABLELENDATA_HPP

#include <string>

typedef char*	BuffPtr;
typedef size_t	BuffLen;

class CVariableLenData
{
public:
	//Serilizes data members to a char buffer
	void Serialize(BuffPtr &ptr, BuffLen len, BuffLen &writelen) const;

	//DeSerilizes from a char buffer & constructs an object
	static CVariableLenData* DeSerialize(const BuffPtr ptr, BuffLen len);

	void SetData(std::string& str);
	void SetData(int& val);

	CVariableLenData();
private:
	int m_some_int;			// fixed length
	std::string m_some_str; // variable length
	CVariableLenData(const CVariableLenData&);
};

#endif //CVARIABLELENDATA_HPP

// Source file
#include <cstdio>
#include "CVariableLenData.h"

using namespace std;

CVariableLenData::CVariableLenData():m_some_int(0),m_some_str(){}

void CVariableLenData::Serialize(BuffPtr &ptr, BuffLen bufflen, BuffLen& writelen) const
{
	if(!ptr)
		return;
	writelen = _snprintf (ptr, bufflen,"%d", m_some_int);
	*(ptr+writelen) = ' ';		// after each numeric value add an space
	size_t len = writelen + 1;
	// now write the string
	// copy length
	writelen = _snprintf ( (ptr+len), bufflen,"%u", m_some_str.length());
	*(ptr + len ) = ' ';
	len = len + writelen + 1;
	// Copy the data
	memcpy( (ptr+len), m_some_str.c_str(), m_some_str.length());
	writelen = len;
	return;
}

Some links.

Advertisements

Usefull command for Linux Developers

Locating a variable, function or finding a variable, or finding the occurrence / usage of function / variable in an efficient way. For example, you want to find the header file which defines a constant “PATH_MAX”.

    find / -iname *.h | xargs grep -Hn “PATH_MAX”

    or, if you want to redirect the errors, then

    find / -iname *.h 2> /dev/null | xargs grep -Hn “PATH_MAX”

    JDBC : Ldap – sql bridge

    I am not able to find a suitable use case for this one. I do not know what requirement it will serve other than maintaining some legacy one. Maybe i am wrong.

    Some links:
    http://www.vogella.de/articles/Eclipse/article.html
    http://myvd.sourceforge.net/jdbcldap.html

    Some usefull GCC options for preprocessor

    http://sources.redhat.com/gdb/current/onlinedocs/gdb_11.html

    We pass the `-gdwarf-2′ and `-g3′

    flags to ensure the compiler includes information about preprocessor macro. For example;-

    gcc -gdwarf-2 -g3 sample.c -o sample
    
    To print the preprocessor output and stop there :
    
    gcc -E sample.c