- 论坛徽章:
- 0
|
读代码要耐心读。如果想训练自己的读代码能力,欢迎阅读GCC。
PS:这段代码中的名字以及一些pattern还是很好辨认的,平时看代码的时候留心些。
/* There is a string in the *s, parse it, put result into *t.
* type is the TYPE of the data in *s, maybe(see the below Macros)
* len is the length of string *s
* if the data is a float or money, dec means the decision of the float number
*/
int ntc(char *s, char *t, int type,int len,int dec)
{
register i;
char *q;
int zero=1,l,p,neg=0;
char r[2000+1];
int mdy[3];
int y,m,d;
t [0] = 0;
if (type==FVARL) // If type is FVARL, copy src to target and return
{
strcpy( t, s );
return 0;
}
if (len<=0) // len must >0, or return failed
return 1;
else if (len>2000) // if len > 2000, just copy src to target
{
memcpy( t, s, len );
t[len] = 0;
return 0;
}
memcpy( r, s, len ); //if 0<len<=2000, analyze it
r[len] = 0;
if (type == FINT || type == FFLOAT || type == FMONEY )
{
if (r[len-1]>'9')
{
if (r[len-1]==0x7d) // if last char is '}', change it to '0'
r[len-1] = '0';
else
r[len-1] -= 0x19;//minux 0x19 ? I,J,K...
neg=1; // if last char > '9', set this flag, negative amount
}
}
switch(type)
{
case FCHAR:
case FDGTCH:
case FPASS:
memcpy( t, r, len ); //this three type just copy to target
t[len] = 0;
break;
case FINT:
l=0;
if (neg)
t[l++]='-';
for (i=0; i<len; i++)
{
// There may be problem, should be r\[i\] ... r\[i\]
if ((r=='0'||r==' ') && zero) continue; //skip leading ' ' and '0'
zero=0;
// There may be problem, should be r
t[l++]=r;
}
if (l==neg) //if no other digit put '0' at the first place
{
t[0] = '0';
l = 1;
}
t[l] = '\0'; //put a tail 0
break;
case FFLOAT:
case FMONEY:
l=0;
if (neg) t[l++]='-';
for (i=0; i<len-dec; i++) //dec means decision of floating point number
// There may be problem
// should be r\[i\] ... r\[i\]...
if ((r=='0'||r==' ') && zero) continue;// like in FINT
zero=0;
// There may be problem, should be r
t[l++]=r;
}
if (l==neg)
{
t[neg] = '0';
l++;
}
t[l++]='.'; //floating point
if (dec!=0) memcpy( t+l, r+i, dec );
*(t+l+dec) = 0;
if (t[l]=='\0') //if no number after floating point, put 2 '0'
{
t[l++]='0';
t[l++]='0';
t[l]='\0';
}
break;
case FDATE:
get_date(&y,&m,&d); //get_date get the current year,month,day
r[len] = 0;
if (r[0]=='\0' || r[0]==' ') // if no char or first char is ' ', get out
{ // donot change target
t[0]=0;
break;
}
switch (len)
{
case 4:
sscanf(r,"%02d%02d",&mdy[0],&mdy[1]); //r has MMDD,put them in mdy
mdy[2] = y; //current year
break;
case 6:
sscanf(r,"%02d%02d%02d",&mdy[2],&mdy[0],&mdy[1]);//r has YYMMDD
mdy[2] += (mdy[2]>=80) ? 1900 : 2000 ; // if year >=80, it is 19xx
break; // otherwise, it is 20xx
case 8:
default: //r has YYYYDDMM
sscanf(r,"%04d%02d%02d",&mdy[2],&mdy[0],&mdy[1]);
mdy[2] += (mdy[2]<100) ? 1900 : 0 ; // if year < 100, add 1900
break;
}
sprintf(t,"%04d%02d%02d",mdy[2],mdy[0],mdy[1]);// put date in target
break;
}
return 0;
}
|
[ 本帖最后由 freearth 于 2008-9-3 20:44 编辑 ] |
|