##// END OF EJS Templates
Merge
Merge

File last commit:

r18:bd9ab647f70a default
r46:7d306b65e7c9 merge dev_alexis
Show More
libucstrings.c
434 lines | 13.4 KiB | text/x-c | CLexer
/*------------------------------------------------------------------------------
#-- This file is a part of the libuc, microcontroler library
#-- Copyright (C) 2011, Alexis Jeandet
#--
#-- This program is free software; you can redistribute it and/or modify
#-- it under the terms of the GNU General Public License as published by
#-- the Free Software Foundation; either version 3 of the License, or
#-- (at your option) any later version.
#--
#-- This program 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 General Public License for more details.
#--
#-- You should have received a copy of the GNU General Public License
#-- along with this program; if not, write to the Free Software
#-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#-------------------------------------------------------------------------------
#-- Author : Alexis Jeandet
#-- Mail : alexis.jeandet@gmail.com
#-------------------------------------------------------------------------------*/
#include <stdarg.h>
#include "libucstrings.h"
extern void consoleputc(char);
extern char consolegetc();
#define _x_prtconv_(a) printhexfromint((a),39)
#define _X_prtconv_(a) printhexfromint((a),7)
#define _d_prtconv_(a) printdecfromint((a))
#define _x_scnconv_() scanintfromhex()
#define _d_scnconv_() scanintfromdec()
void clearch(char* a)
{
while((*a))
*a++=' ';
a--;
*a='\n';
}
int scanintfromdec()
{
char c[8]=" ";
int e=0;
int result=0;
do
{
c[e] = consolegetc();
consoleputc(c[e]);
if(((c[e] & 0xF0) == 0x30))
e++;
}while( (c[e]!='\n') && (e<8));
libucprintf("\n%d number(s) read\n",e);
int i=0;
while((i<e))
{
c[i]&=0x0F;
result = (result*10) + c[i];
libucprintf("iterration %d c[i]=%d result=%d\n",i,c[i],result);
i++;
}
return result;
}
int scanintfromhex()
{
char c[8]=" ";
int e=0;
int result=0;
do
{
c[e] = consolegetc();
consoleputc(c[e]);
if(((c[e] & 0xF0) == 0x30)||((c[e]&0xF8)==0x40)||((c[e]&0xF8)==0x60))
e++;
}while( (c[e]!='\n') && (e<8));
int i=0;
libucprintf("\n%d number(s) read\n",e);
while(i<e)
{
libucprintf("c[i] = %x\n",c[i]);
if((c[i]&0xF8)==0x60)
{
c[i]-=39;
}
else
{
if((c[i]&0xF8)==0x40)
{
c[i]-=7;
}
else
{
c[i]&=0x0F;
}
}
result = result*16 + c[i];
i++;
}
return result;
}
void printhexfromint(int a, int caseoffset)
{
char c[8]=" ";
int e=convhexfromint(a,c,caseoffset);
while(e-->0)
consoleputc(c[e]);
}
int convhexfromint(int a,char* c, int caseoffset)
{
//char c[8]=" ";
int e=0;
while(e<8)
{
c[e] = a & 0xF;
if(c[e]>9)
{
c[e] = c[e]+caseoffset;
}
c[e] = c[e] + 0x30;
a=a>>4;
e++;
}
while(c[--e]=='0');
e++;
return e;
/*while(e-->0)
consoleputc(c[e]);*/
}
void printdecfromint(int a)
{
char c[10]=" ";
int e=convdecfromint(a,c);
while(e-->0)
consoleputc(c[e]);
}
int convdecfromint(int a,char* c)
{
//char c[10]=" ";
int e=0;
int d=a;
if((d & 0x80000000) == 0x80000000)
{
d ^= -1;
d++;
consoleputc('-');
}
while(d>0 && e<10)
{
c[e++] = 0x30 + (d % 10);
d = d / 10;
}
return e;
}
void int2hex(unsigned long a,char*b)
{
char*d = b;
char c[16]=" ";
int e=0;
while(e<8)
{
c[e] = a & 0xF;
if(c[e]>9)
{
c[e] = c[e]+7;
}
c[e] = c[e] + 0x30;
a=a>>4;
e++;
}
for(e=(e-1);e>=0;e--)
{
*d=c[e];
d = d +1;
}
}
/// Partialy implemented printf function capable to compute %d,x,X conversions
int libucprintf(const char* format,...)
{
//return libucfprintf(stdo,format);
/* int nout=0;
va_list ap;
va_start(ap,format);
while(*format)
{
if(*format!='%')
{
consoleputc(*format++);
nout++;
}
else
{
format++;
if(*format!='%')
{
switch(*format)
{
case 'c':
consoleputc((char)(0x7f & va_arg(ap,int)));
format++;
break;
case 'd':
_d_prtconv_(va_arg(ap,int));
format++;
break;
case 'e':
va_arg(ap,int);
format++;
break;
case 'E':
va_arg(ap,int);
format++;
break;
case 'f':
va_arg(ap,int);
format++;
break;
case 's':
va_arg(ap,int);
format++;
break;
case 'x':
_x_prtconv_(va_arg(ap,int)) ;
format++;
break;
case 'X':
_X_prtconv_(va_arg(ap,int)) ;
format++;
break;
case '%':
consoleputc(*format++);
break;
default:
va_arg(ap,int);
format++;
break;
}
}
else
{
consoleputc(*format++);
nout++;
}
}
}
va_end(ap);
return nout;*/
}
/*int libucprintf(const char* format,...)
{
return libucfprintf(stdo,format,...);
}*/
int libucfprintf(streamdevice* device,const char* format,...)
{
int nout=0;
va_list ap;
va_start(ap,format);
char c[10]=" ";
char val;
int e;
while(*format)
{
if(*format!='%')
{
device->write(device,format++,1,1);
nout++;
}
else
{
format++;
if(*format!='%')
{
switch(*format)
{
case 'c':
//consoleputc((char)(0x7f & va_arg(ap,int)));
val = ((char)(0x7f & va_arg(ap,int)));
device->write(device,&val,1,1);
format++;
break;
case 'd':
e=convdecfromint(va_arg(ap,int),c);
while(e-->0)
device->write(device,c+e,1,1);
format++;
break;
case 'e':
va_arg(ap,int);
format++;
break;
case 'E':
va_arg(ap,int);
format++;
break;
case 'f':
va_arg(ap,int);
format++;
break;
case 's':
va_arg(ap,int);
format++;
break;
case 'x':
e=convhexfromint(va_arg(ap,int),c,39);
while(e-->0)
device->write(device,c+e,1,1);
format++;
break;
case 'X':
e=convhexfromint(va_arg(ap,int),c,7);
while(e-->0)
device->write(device,c+e,1,1);
format++;
break;
case '%':
device->write(device,format++,1,1);
break;
default:
va_arg(ap,int);
format++;
break;
}
}
else
{
device->write(device,format++,1,1);
nout++;
}
}
}
va_end(ap);
return nout;
}
int libucscanf(const char* format,...)
{
int nin=0;
int* value;
va_list ap;
va_start(ap,format);
while(*format)
{
while(*format!='%')format++;
format++;
libucprintf("find %%%c\n",*format);
switch(*format)
{
case 'c':
va_arg(ap,int*);
format++;
break;
case 'd':
value = (int*)va_arg(ap,int*);
*value=_d_scnconv_() ;
format++;
break;
case 'e':
va_arg(ap,int*);
format++;
break;
case 'E':
va_arg(ap,int*);
format++;
break;
case 'f':
va_arg(ap,int*);
format++;
break;
case 's':
va_arg(ap,int*);
format++;
break;
case 'u':
va_arg(ap,int*);
format++;
break;
case 'x':
value = (int*)va_arg(ap,int*);
*value = _x_scnconv_();
format++;
break;
case 'X':
value = (int*)va_arg(ap,int*);
*value = _x_scnconv_();
format++;
break;
default:
va_arg(ap,int*);
format++;
break;
}
}
va_end(ap);
return nin;
}
void libucprintchartable(char* table,int size,const char* format,const char* separator)
{
int i =0;
for(i=0;i<size;i++)
{
libucprintf(format,table[i]);
libucprintf(separator);
}
}