Skip to content

Commit 299fbb4

Browse files
author
Michael Meskes
committed
Added rfmtlong compatibility function.
1 parent 3ad406b commit 299fbb4

File tree

2 files changed

+211
-1
lines changed

2 files changed

+211
-1
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,14 @@ Thu May 1 14:54:41 CEST 2003
13961396

13971397
- Enable more Informix shortcuts.
13981398
- Added option '-i' to parse files included via cpp diretive as well.
1399+
1400+
Fri May 2 16:37:06 CEST 2003
1401+
1402+
- Fixed double definition of compat_mode.
1403+
1404+
Tue May 6 11:51:33 CEST 2003
1405+
1406+
- Added rfmtlong compatibility function.
13991407
- Set ecpg version to 2.12.0.
14001408
- Set ecpg library to 3.4.2.
14011409
- Set pgtypes library to 1.0.0

src/interfaces/ecpg/compatlib/informix.c

Lines changed: 203 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include <errno.h>
4+
#include <math.h>
45
#include <ctype.h>
56

67
#include <ecpg_informix.h>
@@ -400,10 +401,211 @@ intoasc(Interval *i, char *str)
400401
return 0;
401402
}
402403

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+
static struct {
413+
long val;
414+
int maxdigits;
415+
int digits;
416+
int remaining;
417+
char sign;
418+
char *val_string;
419+
} value;
420+
421+
/**
422+
* initialize the struct, wich holds the different forms
423+
* of the long value
424+
*/
425+
static void initValue(long lng_val) {
426+
int i, div, dig;
427+
char tmp[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+
static int getRightMostDot(char* str) {
458+
size_t len = strlen(str);
459+
int i,j;
460+
j=0;
461+
for(i=len-1; i >= 0; i--) {
462+
if (str[i] == '.') {
463+
return len-j-1;
464+
}
465+
j++;
466+
}
467+
return -1;
468+
}
469+
403470
/* And finally some misc functions */
404471
int
405-
rfmtlong(long lvalue, char *format, char *outbuf)
472+
rfmtlong(long lng_val, char *fmt, char *outbuf)
406473
{
474+
size_t fmt_len = strlen(fmt);
475+
size_t temp_len;
476+
int i, j, k, dotpos;
477+
int leftalign = 0, blank = 0, sign = 0, entity = 0,
478+
entitydone = 0, signdone = 0, brackets_ok = 0;
479+
char temp[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+
else if (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+
else tmp[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+
else fmtchar = 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+
else tmp[0] = value.val_string[k];
542+
break;
543+
case '&':
544+
if (blank) tmp[0] = '0';
545+
else tmp[0] = value.val_string[k];
546+
break;
547+
case '#':
548+
if (blank) tmp[0] = ' ';
549+
else tmp[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+
else if (blank) tmp[0] = ' ';
560+
else tmp[0] = value.val_string[k];
561+
break;
562+
case '+':
563+
if (sign && !signdone) {
564+
tmp[0] = value.sign;
565+
signdone = 1;
566+
}
567+
else if (blank) tmp[0] = ' ';
568+
else tmp[0] = value.val_string[k];
569+
break;
570+
case '(':
571+
if (sign && brackets_ok && value.sign == '-') tmp[0] = '(';
572+
else if (blank) tmp[0] = ' ';
573+
else tmp[0] = value.val_string[k];
574+
break;
575+
case ')':
576+
if (brackets_ok && value.sign == '-') tmp[0] = ')';
577+
else tmp[0] = ' ';
578+
break;
579+
case '$':
580+
if (blank && !entitydone) {
581+
tmp[0] = '$';
582+
entitydone = 1;
583+
}
584+
else if (blank) tmp[0] = ' ';
585+
else tmp[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+
407609
return 0;
408610
}
409611

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy