/*
Copyright 2007, 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, 2007
*/

/** 
 * @file ScatterWeb.String.h
 * @ingroup kernel
 *
 * \brief The ScatterWeb.String represents text; that is, a series of Unicode
 * characters.
 *
 *
 * @version       $Rev: 232 $
 * @since         1.0     
 */

#ifndef ScatterWeb_String
#define ScatterWeb_String

#define CHAR_NUL	'\x00'
#define CHAR_ETX	'\x03'
#define CHAR_TAB	'\x09'
#define CHAR_SPC	'\x20'

typedef int(*fp_putchar_t)(int);

/**
 @brief		Parses int.

 @param[in]	c       c
 @param[in]	ptr     ptr
 @return			parsed int

 @since		1.0
 */
uint16_t String_parseUint16(const uint8_t* c, uint8_t** ptr);

/**
 @brief		Converts the byte *b into the string cs[0] & cs[1].

 @param[in]	b	byte
 @param[in]	cs	pointer to character buffer of at least two bytes size

 @since		1.0
 */
void String_fromByteToHex(uint8_t b, uint8_t* cs);

/**
 @brief Converts the string at cs[0] & cs[1] into a byte at *b.
 
 @param[in]	cs	string
 @return			value to convert into.

 @since		1.0

 Valid format for cs: [0-9a-fA-F]{2}
 */
uint8_t String_fromHexToByte(const uint8_t* cs);

/**
 @brief		Writes a string into a writer.

 @param[in]	writer	function to write
 @param[in]	str		string to write
 @return			<code>True</code> always.

 @since		1.0
 */
bool String_writeString(fp_putchar_t writer, const uint8_t* str);

/**
 * @brief	Writes a byte array as hex-string into a writer
 *
 * @param	writer		function to write
 * @param	data		byte array
 * @param	length		number of bytes to write
 * @since				1.1
 */
void String_writeHex(fp_putchar_t writer, uint8_t* data, uint16_t length);

/**
 * @brief		Writes a single character n times
 * @param[in]	writer	function to write
 * @param[in]	c		character to writer
 * @param[in]	num		number of times to repeat c
 * @since		1.1
 */
void String_writeChar(fp_putchar_t writer, char c, int num);

/**
 * @brief	Get a string from a stringtable
 * 
 * @param[in]	table	Stringtable
 * @param[in]	index	String to get from table
 * @return		Pointer to start of string index from table. If the end of the table is reached, returns pointer to NUL;
 * 
 * @since		1.1
 * 
 * A stringtable shall be a character array with one or more strings, with each string
 * ending with a null-character. The table is to be terminated by an end-of-text character.
 * The first string shall have the index 0.
 * Example: "string1\0string2\0\3"
 */
const char* String_stringtableGet(const char* table, register const uint16_t index);

bool String_writerInit(char* dest);
int String_writer(int c);


#endif
