|
1 | 1 | #include<stdlib.h>
|
2 | 2 | #include<string.h>
|
3 | 3 | #include<errno.h>
|
| 4 | +#include<math.h> |
4 | 5 | #include<ctype.h>
|
5 | 6 |
|
6 | 7 | #include<ecpg_informix.h>
|
@@ -400,10 +401,211 @@ intoasc(Interval *i, char *str)
|
400 | 401 | return0;
|
401 | 402 | }
|
402 | 403 |
|
| 404 | +/*************************************************************************** |
| 405 | + rfmt.c - description |
| 406 | + ------------------- |
| 407 | + begin : Wed Apr 2 2003 |
| 408 | + copyright : (C) 2003 by Carsten Wolff |
| 409 | + email : carsten.wolff@credativ.de |
| 410 | + ***************************************************************************/ |
| 411 | + |
| 412 | +staticstruct { |
| 413 | +longval; |
| 414 | +intmaxdigits; |
| 415 | +intdigits; |
| 416 | +intremaining; |
| 417 | +charsign; |
| 418 | +char*val_string; |
| 419 | +}value; |
| 420 | + |
| 421 | +/** |
| 422 | + * initialize the struct, wich holds the different forms |
| 423 | + * of the long value |
| 424 | + */ |
| 425 | +staticvoidinitValue(longlng_val) { |
| 426 | +inti,div,dig; |
| 427 | +chartmp[2]=" "; |
| 428 | + |
| 429 | +// set some obvious things |
| 430 | +value.val=lng_val >=0 ?lng_val :lng_val* (-1); |
| 431 | +value.sign=lng_val >=0 ?'+' :'-'; |
| 432 | +value.maxdigits=log10(2)*(8*sizeof(long)-1); |
| 433 | + |
| 434 | +// determine the number of digits |
| 435 | +for(i=1;i <=value.maxdigits;i++) { |
| 436 | +if ((int)(value.val /pow(10,i))!=0) { |
| 437 | +value.digits=i+1; |
| 438 | +} |
| 439 | +} |
| 440 | +value.remaining=value.digits; |
| 441 | + |
| 442 | +// convert the long to string |
| 443 | +value.val_string= (char*)malloc(value.digits+1); |
| 444 | +for(i=value.digits;i>0;i--) { |
| 445 | +div=pow(10,i); |
| 446 | +dig= (value.val %div) / (div /10); |
| 447 | +tmp[0]= (char)(dig+48); |
| 448 | +strcat(value.val_string,tmp); |
| 449 | + } |
| 450 | +// safety-net |
| 451 | +value.val_string[value.digits]='\0'; |
| 452 | +// clean up |
| 453 | +free(tmp); |
| 454 | +} |
| 455 | + |
| 456 | +/* return the position oft the right-most dot in some string */ |
| 457 | +staticintgetRightMostDot(char*str) { |
| 458 | +size_tlen=strlen(str); |
| 459 | +inti,j; |
| 460 | +j=0; |
| 461 | +for(i=len-1;i >=0;i--) { |
| 462 | +if (str[i]=='.') { |
| 463 | +returnlen-j-1; |
| 464 | +} |
| 465 | +j++; |
| 466 | +} |
| 467 | +return-1; |
| 468 | +} |
| 469 | + |
403 | 470 | /* And finally some misc functions */
|
404 | 471 | int
|
405 |
| -rfmtlong(longlvalue,char*format,char*outbuf) |
| 472 | +rfmtlong(longlng_val,char*fmt,char*outbuf) |
406 | 473 | {
|
| 474 | +size_tfmt_len=strlen(fmt); |
| 475 | +size_ttemp_len; |
| 476 | +inti,j,k,dotpos; |
| 477 | +intleftalign=0,blank=0,sign=0,entity=0, |
| 478 | +entitydone=0,signdone=0,brackets_ok=0; |
| 479 | +chartemp[fmt_len+1],tmp[2]=" ",lastfmt=' ',fmtchar=' '; |
| 480 | + |
| 481 | +// put all info about the long in a struct |
| 482 | +initValue(lng_val); |
| 483 | + |
| 484 | +// '<' is the only format, where we have to align left |
| 485 | +if (strchr(fmt, (int)'<')) { |
| 486 | +leftalign=1; |
| 487 | +} |
| 488 | + |
| 489 | +// '(' requires ')' |
| 490 | +if (strchr(fmt, (int)'(')&&strchr(fmt, (int)')')) { |
| 491 | +brackets_ok=1; |
| 492 | +} |
| 493 | + |
| 494 | +// get position of the right-most dot in the format-string |
| 495 | +// and fill the temp-string wit '0's up to there. |
| 496 | +dotpos=getRightMostDot(fmt); |
| 497 | + |
| 498 | +// start to parse the formatstring |
| 499 | +temp[0]='\0'; |
| 500 | +j=0;// position in temp |
| 501 | +k=value.digits-1;// position in the value_string |
| 502 | +for(i=fmt_len-1,j=0;i>=0;i--,j++) { |
| 503 | +// qualify, where we are in the value_string |
| 504 | +if (k<0) { |
| 505 | +if (leftalign) { |
| 506 | +// can't use strncat(,,0) here, Solaris would freek out |
| 507 | +temp[j]='\0'; |
| 508 | +break; |
| 509 | +} |
| 510 | +blank=1; |
| 511 | +if (k==-2) { |
| 512 | +entity=1; |
| 513 | +} |
| 514 | +elseif (k==-1) { |
| 515 | +sign=1; |
| 516 | +} |
| 517 | +} |
| 518 | +// if we're right side of the right-most dot, print '0' |
| 519 | +if (dotpos >=0&&dotpos <=i) { |
| 520 | +if (dotpos<i) { |
| 521 | +if (fmt[i]==')')tmp[0]=value.sign=='-' ?')' :' '; |
| 522 | +elsetmp[0]='0'; |
| 523 | +} |
| 524 | +else { |
| 525 | +tmp[0]='.'; |
| 526 | +} |
| 527 | +strcat(temp,tmp); |
| 528 | +continue; |
| 529 | +} |
| 530 | +// the ',' needs special attention, if it is in the blank area |
| 531 | +if (blank&&fmt[i]==',')fmtchar=lastfmt; |
| 532 | +elsefmtchar=fmt[i]; |
| 533 | +// analyse this format-char |
| 534 | +switch(fmtchar) { |
| 535 | +case',': |
| 536 | +tmp[0]=','; |
| 537 | +k++; |
| 538 | +break; |
| 539 | +case'*': |
| 540 | +if (blank)tmp[0]='*'; |
| 541 | +elsetmp[0]=value.val_string[k]; |
| 542 | +break; |
| 543 | +case'&': |
| 544 | +if (blank)tmp[0]='0'; |
| 545 | +elsetmp[0]=value.val_string[k]; |
| 546 | +break; |
| 547 | +case'#': |
| 548 | +if (blank)tmp[0]=' '; |
| 549 | +elsetmp[0]=value.val_string[k]; |
| 550 | +break; |
| 551 | +case'<': |
| 552 | +tmp[0]=value.val_string[k]; |
| 553 | +break; |
| 554 | +case'-': |
| 555 | +if (sign&&value.sign=='-'&& !signdone) { |
| 556 | +tmp[0]='-'; |
| 557 | +signdone=1; |
| 558 | +} |
| 559 | +elseif (blank)tmp[0]=' '; |
| 560 | +elsetmp[0]=value.val_string[k]; |
| 561 | +break; |
| 562 | +case'+': |
| 563 | +if (sign&& !signdone) { |
| 564 | +tmp[0]=value.sign; |
| 565 | +signdone=1; |
| 566 | +} |
| 567 | +elseif (blank)tmp[0]=' '; |
| 568 | +elsetmp[0]=value.val_string[k]; |
| 569 | +break; |
| 570 | +case'(': |
| 571 | +if (sign&&brackets_ok&&value.sign=='-')tmp[0]='('; |
| 572 | +elseif (blank)tmp[0]=' '; |
| 573 | +elsetmp[0]=value.val_string[k]; |
| 574 | +break; |
| 575 | +case')': |
| 576 | +if (brackets_ok&&value.sign=='-')tmp[0]=')'; |
| 577 | +elsetmp[0]=' '; |
| 578 | +break; |
| 579 | +case'$': |
| 580 | +if (blank&& !entitydone) { |
| 581 | +tmp[0]='$'; |
| 582 | +entitydone=1; |
| 583 | +} |
| 584 | +elseif (blank)tmp[0]=' '; |
| 585 | +elsetmp[0]=value.val_string[k]; |
| 586 | +break; |
| 587 | +default:tmp[0]=fmt[i]; |
| 588 | +} |
| 589 | +strcat(temp,tmp); |
| 590 | +lastfmt=fmt[i]; |
| 591 | +k--; |
| 592 | +} |
| 593 | +// safety-net |
| 594 | +temp[fmt_len]='\0'; |
| 595 | + |
| 596 | +// reverse the temp-string and put it into the outbuf |
| 597 | +temp_len=strlen(temp); |
| 598 | +outbuf[0]='\0'; |
| 599 | +for(i=temp_len-1;i>=0;i--) { |
| 600 | +tmp[0]=temp[i]; |
| 601 | +strcat(outbuf,tmp); |
| 602 | +} |
| 603 | +outbuf[temp_len]='\0'; |
| 604 | + |
| 605 | +// cleaning up |
| 606 | +free(tmp); |
| 607 | +free(value.val_string); |
| 608 | + |
407 | 609 | return0;
|
408 | 610 | }
|
409 | 611 |
|
|