PageRenderTime 48ms CodeModel.GetById 18ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/install/xbt/linux/misc/xif_value.cpp

http://torrentpier2.googlecode.com/
C++ | 151 lines | 140 code | 11 blank | 0 comment | 22 complexity | 6fe3c9454985d6bcafc66ea8da41da8d MD5 | raw file
  1#include "stdafx.h"
  2#include "xif_value.h"
  3
  4#include <zlib.h>
  5#include "stream_int.h"
  6
  7static float read_float(const byte*& r)
  8{
  9	assert(sizeof(float) == 4);
 10	float v;
 11	memcpy(&v, r, 4);
 12	r += 4;
 13	return v;
 14}
 15
 16static int read_int(const byte*& r)
 17{
 18	r += 4;
 19	return read_int_le(4, r - 4);
 20}
 21
 22t_vt Cxif_value::get_type() const
 23{
 24	if (m_type != vt_unknown)
 25		return m_type;
 26	const byte* data = m_data.data();
 27	if (!data)
 28		return vt_binary;
 29	int size = m_data.size();
 30	if (!data[size - 1])
 31	{
 32		const byte* r = data;
 33		int c = size - 1;
 34		while (c--)
 35		{
 36			if (*r != 9 && *r < 0x20)
 37				break;
 38			r++;
 39		}
 40		if (c == -1)
 41			return vt_string;
 42	}
 43	if (size == 4)
 44		return vt_int32;
 45	return vt_binary;
 46}
 47
 48void Cxif_value::load_old(const byte*& data)
 49{
 50	m_data.clear();
 51	int size = read_int(data);
 52	if (size == 4)
 53		memcpy(m_value, data, size);
 54	memcpy(m_data.write_start(size), data, size);
 55	data += size;
 56	m_type = vt_unknown;
 57	m_type = get_type();
 58}
 59
 60void Cxif_value::load_new(const byte*& data)
 61{
 62	m_data.clear();
 63	m_type = static_cast<t_vt>(*data++);
 64	switch (m_type)
 65	{
 66	case vt_bin32:
 67	case vt_int32:
 68		m_value_int = read_int(data);
 69		break;
 70	case vt_float:
 71		m_value_float = read_float(data);
 72		break;
 73	case vt_external_binary:
 74		m_data.write_start(read_int(data));
 75		break;
 76	default:
 77		{
 78			int size = read_int(data);
 79			memcpy(m_data.write_start(size), data, size);
 80			data += size;
 81		}
 82	}
 83}
 84
 85void Cxif_value::load_external(const byte*& data)
 86{
 87	if (!external_data())
 88		return;
 89	memcpy(m_data.data_edit(), data, get_size());
 90	data += get_size();
 91}
 92
 93int Cxif_value::skip(const byte* s)
 94{
 95	const byte* r = s;
 96	t_vt type = static_cast<t_vt>(*r++);
 97	switch (type)
 98	{
 99	case vt_bin32:
100	case vt_int32:
101		read_int(r);
102		break;
103	case vt_float:
104		read_float(r);
105		break;
106	case vt_external_binary:
107		read_int(r);
108		break;
109	default:
110		r += read_int(r);
111	}
112	return r - s;
113}
114
115void Cxif_value::save(byte*& data) const
116{
117	*data++ = external_data() ? vt_external_binary : m_type;
118	switch (m_type)
119	{
120	case vt_bin32:
121	case vt_int32:
122		data = write_int_le(4, data, get_int());
123		break;
124	case vt_float:
125		data = write_float(data, get_float());
126		break;
127	default:
128		{
129			int size = get_size();
130			data = write_int_le(4, data, size);
131			if (!external_data())
132			{
133				memcpy(data, get_data(), size);
134				data += size;
135			}
136		}
137	}
138}
139
140bool Cxif_value::external_data() const
141{
142	return m_type == vt_external_binary;
143}
144
145void Cxif_value::external_save(byte*& data) const
146{
147	if (!external_data())
148		return;
149	memcpy(data, get_data(), get_size());
150	data += get_size();
151}