/*-------------------------------------------------------------------------
   Line function copy right information

   Copyright (C) 2006-2008. Tang, Li      <tangli@palmmicro.com>
                            Lin, Rongrong <woody@palmmicro.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-------------------------------------------------------------------------*/

#include "type.h"
#include "core.h"
#include "apps.h"

USHORT _sLineLen;
USHORT _sLineLenBackup;
PCHAR _pLine;
PCHAR _pLineBackup;

void line_backup()
{
	_sLineLenBackup = _sLineLen;
	_pLineBackup = _pLine;
}

void line_restore()
{
	_sLineLen = _sLineLenBackup;
	_pLine = _pLineBackup;
}

void line_start(PCHAR pDst)
{
	_pLine = pDst;
	_sLineLen = 0;
}

USHORT line_get_len()
{
	return _sLineLen;
}

void line_update_len()
{
	_sLineLen += strlen((PCHAR)(_pLine + _sLineLen));
}

PCHAR line_get_buf()
{
	return (PCHAR)(_pLine + _sLineLen);
}

void line_add_data(PCHAR pData, USHORT sLen)
{
	memcpy((PCHAR)(_pLine + _sLineLen), pData, sLen);
	_sLineLen += sLen;
	_pLine[_sLineLen] = 0;
}

void line_add_str(PCHAR pStr)	_naked
{
/*	if (pStr)
	{
		strcpy((PCHAR)(_pLine + _sLineLen), pStr);
		_sLineLen += strlen(pStr);
	}
*/
	pStr;
	_asm
	push	ix
	ld	ix,#0
	add	ix,sp
	; if (pStr)
	ld	a,4(ix)
	or	a,5(ix)
	jr	Z,line_add_str_fork

	ld	hl,#__pLine
	ld	iy,#__sLineLen
	ld	a,0(iy)
	add	a,(hl)
	ld	e,a
	ld	a,1(iy)
	inc	hl
	adc	a,(hl)
	ld	d,a
	ld	l,4(ix)
	ld	h,5(ix)
	xor	a,a
	ld	b,a
	ld	c,a

line_add_str_loop:
	ld	a,(hl)
	ldi
	or	a,a
	jr	nz,line_add_str_loop

	inc	bc
	ld	a,0(iy)
	sub	a,c
	ld	0(iy),a
	ld	a,1(iy)
	sbc	a,b
	ld	1(iy),a
	
line_add_str_fork:
	pop	ix
	ret
	_endasm;
}

void line_add_short(USHORT sVal)
{
	itoa(sVal, (PCHAR)(_pLine + _sLineLen), 10);
	line_update_len();
}

void line_add_long(ULONG lVal)
{
	ltoa(lVal, (PCHAR)(_pLine + _sLineLen), 10);
	line_update_len();
}

void line_add_ip(PCHAR pIPAddr)
{
	ip2str(pIPAddr, (PCHAR)(_pLine + _sLineLen));
	line_update_len();
}

void line_add_char(UCHAR iChar)
{
	_pLine[_sLineLen] = iChar;
	_sLineLen ++;
	_pLine[_sLineLen] = 0;
}

void line_add_crlf()
{
//	line_add_str("\r\n");
	_pLine[_sLineLen] = '\r';
	_sLineLen ++;
	_pLine[_sLineLen] = '\n';
	_sLineLen ++;
	_pLine[_sLineLen] = 0;
}

void line_add_str_with_char(PCHAR pStr, UCHAR iChar)
{
	line_add_str(pStr);
	line_add_char(iChar);
}

UCHAR is_endof_line(PCHAR pData)
{
	if (pData[0] == 0x0d)
	{
		if (pData[1] == 0x0a)	return 2;
		else					return 1;
	}
	else if (pData[0] == 0x0a)
	{
		if (pData[1] == 0x0d)	return 2;
		else					return 1;
	}
	return 0;
}

UCHAR count_space(PCHAR pStr)
{
	UCHAR iLen;

	iLen = 0;
	while (*pStr== '\t' || *pStr == ' ')
	{
		iLen ++;
		pStr ++;
	}
	return iLen;
}
