/*
Copyright 2006, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2006
*/

#include "ScatterWeb.System.h"

static uint8_t* bufwriter_dest;

static const char num_to_string_table[16] = {
	'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};

bool String_writeString(fp_putchar_t writer, const uint8_t* str) {
	register const uint8_t* c;
	for( c = str; *c != '\0'; c++ )
		writer(*c);
	return True;
}

void String_writeHex(fp_putchar_t writer, uint8_t* data, uint16_t length) {
	uint8_t tmpstr[2];
	uint8_t* c = data;
	uint16_t i;

	for( i = 0; i < length; i++, c++ ) {
		String_fromByteToHex(*c, tmpstr);
		writer(tmpstr[0]);
		writer(tmpstr[1]); 
	}
}

void String_writeChar(fp_putchar_t writer, char c, int num)
{
	register int i;
	for( i = 0; i < num; i++ ) {
		if( writer(c) < 0 )
			return;
	}
}

__inline uint16_t String_parseUint16(const uint8_t* c, uint8_t** ptr) {
	unsigned long ul = strtoul(c, (char**)ptr, 0);
	return (uint16_t)(ul & 0x0000FFFF);
}

void String_fromByteToHex(uint8_t b, uint8_t* cs) {
	cs[1] = num_to_string_table[b & 0x0F];
	b >>= 4;     
	cs[0] = num_to_string_table[b];
}

uint8_t String_fromHexToByte(const uint8_t* cs) {
	register unsigned int a;
	register unsigned int b;

	a = *cs - '0'; cs++;
	if( a > 22 )
		a -= 32;
	if( a > 9 )
		a -= 7;
	a <<= 4;
	
	b = *cs - '0';
	if( b > 22 )
		b -= 32;
	if( b > 9 )
		b -= 7;
	b |= a;
	return( b );
}

const char* String_stringtableGet(const char* table, register const uint16_t index) {
	register uint16_t i = 0;
	register const char* c = table;
	
	while( i < index ) {
		switch( *c ) {
			case CHAR_NUL:
				i++;
				break;
			case CHAR_ETX:
				c--;
				return c;
		}
		c++;
	}
	return c;
}

bool String_writerInit(char* dest) {
	bufwriter_dest = dest;
	return true;
}
int String_writer(int c) {
	*bufwriter_dest = c;
	bufwriter_dest++;
	return c;
}

