#include<inttypes.h>

struct section
{
	const uint8_t* begin,index,end;
	ra_section_t& operator>>(ra_object_t&
};

/*static int64_t ntohll(int64_t vs)
{
	uint64_t v=*(uint64_t*)&vs;
#ifdef __LITTLE_ENDIAN__
	v = ((v >> 8 ) & 0x00FF00FF00FF00FFULL) | ((v & 0xFF00FF00FF00FF00ULL) << 8 );
	v = ((v >> 16) & 0x0000FFFF0000FFFFULL) | ((v & 0xFFFF0000FFFF0000ULL) << 16);
	v = ((v >> 32)                        ) | ((v                        ) << 32);
#endif 
	return *(int64_t*)&v;
}*/
//not super fast,but portable
static int64_t ntohll(int64_t vs)
{
	uint64_t vn=*(uint64_t*)&vs;
	uint64_t vh=0;
	for(int i=0;i<8;i++)
	{
		vh |= vn & 0xFF;
		vn >>= 8;
		vh <<= 8;
	}
	return *(int64_t*)&vh;
}
//maybe C api returns pointer to next byte, fills an array, doesn't increment the pointer if parsing fails....thats an interesting api.  
//I'll have to play with it...

int64_t read_integer(const uint8_t** stdex)//do something bad like throw an exception in C++...do nothing in C...this api sucks kinda
{
	int64_t result;
	char dt=*((*stdex)++);
	switch(dt)
	{
	case 'L':
		result=*(const int64_t*)(*stdex);
		*stdex+=8;
		break;
	case 'l':
		result=*(const int32_t*)(*stdex);
		*stdex+=4;
		break;
	case 'I':
		result=*(const int16_t*)(*stdex);
		*stdex+=2;
		break;
	case 'U':
		result=*((*stdex)++);
		break;
	case 'i':
		result=*(const int8_t*)(*(*stdex)++);
		break;
	default:
		(*stdex)--;
		return 0;
	};
	return ntohull(result);
}

int64_t read_integer(const uint8_t** 

double read_float_ra(const uint8_t** stdex)
{
	char dt=*((*stdex)++);
	switch(dt)
	{
	case 'D':
		result=*(const int64_t*)(*stdex);
		*stdex+=8;
		break;
	case 'd':
		result=*(const int32_t*)(*stdex);
		*stdex+=4;
		break;
	case 'I':
		result=*(const int16_t*)(*stdex);
		*stdex+=2;
		break;
	case 'U':
		result=*((*stdex)++);
		break;
	case 'i':
		result=*(const int8_t*)(*(*stdex)++);
		break;
	default:
		(*stdex)--;
		return 0;
	};
}





//*this code is so clever.  Too bad its slow :(
/*
static void inline _to_bigendian(uint8_t* outbuffer, uint64_t input, uint8_t num_bytes)
{
#if 0 //implementation 1
	uint8_t* op = outbuffer + num_bytes - 1;//initialize it to the last element
	for (uint8_t shiftamount = 0; shiftamount < 64; shiftamount += 8)
	{
		*op-- = (input >> shiftamount) & 0xFF;
	}
#else
	switch (num_bytes)
	{
	case 8:
		*outbuffer++ = (input >> 56ULL) & 0xFF;
		*outbuffer++ = (input >> 48ULL) & 0xFF;
		*outbuffer++ = (input >> 40ULL) & 0xFF;
		*outbuffer++ = (input >> 32ULL) & 0xFF;
	case 4:
		*outbuffer++ = (input >> 24ULL) & 0xFF;
		*outbuffer++ = (input >> 16ULL) & 0xFF;
	case 2:
		*outbuffer++ = (input >> 8ULL) & 0xFF;
	case 1:
		*outbuffer++ = (input >> 0ULL) & 0xFF;
	default:
		break;
	};
#endif

}*/


//cases

//a+size+mixed
//a+size+type+dynamic
//a+size+type+static   //this is not an API special case...its an implementation special case
//a+mixed

//o+size+dynamic
//o+size+type+dynamic
//o+size+type+static
//o+dynamic


write_buffer(TYPE,SIZE,data*); //a+size+type+static special form for writing a buffer
	
//size_tags


begin_array(size+mixed)
	//append anything, tags are necessary
end_array(endtag=no,error if size tags not written)

begin_array(size+type+dynamic)
	//append only dynamic type, no tags show up
end_array(endtag=no,error if size tags not written

begin_array(size+type+static)
	//append only static type, no tags show up...
	//no end tag, error if size tags not written
end_array()
write_buffer()//special

begin_array(dynamic)
	//append anything, tags are necessary
end_array(endtag=yes)


	