diff options
Diffstat (limited to 'src/interfaces/ecpg')
37 files changed, 2109 insertions, 1622 deletions
diff --git a/src/interfaces/ecpg/compatlib/informix.c b/src/interfaces/ecpg/compatlib/informix.c index a1b1f9c5667..15101094f58 100644 --- a/src/interfaces/ecpg/compatlib/informix.c +++ b/src/interfaces/ecpg/compatlib/informix.c @@ -10,17 +10,18 @@ #include <pgtypes_date.h> #include <sqltypes.h> -char * ECPGalloc(long, int); +char *ECPGalloc(long, int); static int -deccall2(Decimal *arg1, Decimal *arg2, int (*ptr)(Numeric *, Numeric *)) +deccall2(Decimal * arg1, Decimal * arg2, int (*ptr) (Numeric *, Numeric *)) { - Numeric *a1, *a2; - int i; + Numeric *a1, + *a2; + int i; if ((a1 = PGTYPESnumeric_new()) == NULL) return -1211; - + if ((a2 = PGTYPESnumeric_new()) == NULL) { PGTYPESnumeric_free(a1); @@ -33,37 +34,39 @@ deccall2(Decimal *arg1, Decimal *arg2, int (*ptr)(Numeric *, Numeric *)) PGTYPESnumeric_free(a2); return -1211; } - + if (PGTYPESnumeric_from_decimal(arg2, a2) != 0) { PGTYPESnumeric_free(a1); PGTYPESnumeric_free(a2); return -1211; } - - i = (*ptr)(a1, a2); - + + i = (*ptr) (a1, a2); + PGTYPESnumeric_free(a1); PGTYPESnumeric_free(a2); - + return (i); } static int -deccall3(Decimal *arg1, Decimal *arg2, Decimal *result, int (*ptr)(Numeric *, Numeric *, Numeric *)) +deccall3(Decimal * arg1, Decimal * arg2, Decimal * result, int (*ptr) (Numeric *, Numeric *, Numeric *)) { - Numeric *a1, *a2, *nres; - int i; + Numeric *a1, + *a2, + *nres; + int i; - if (risnull(CDECIMALTYPE, (char *)arg1) || risnull(CDECIMALTYPE, (char *)arg2)) + if (risnull(CDECIMALTYPE, (char *) arg1) || risnull(CDECIMALTYPE, (char *) arg2)) { - rsetnull(CDECIMALTYPE, (char *)result); + rsetnull(CDECIMALTYPE, (char *) result); return 0; } - + if ((a1 = PGTYPESnumeric_new()) == NULL) return -1211; - + if ((a2 = PGTYPESnumeric_new()) == NULL) { PGTYPESnumeric_free(a1); @@ -84,7 +87,7 @@ deccall3(Decimal *arg1, Decimal *arg2, Decimal *result, int (*ptr)(Numeric *, Nu PGTYPESnumeric_free(nres); return -1211; } - + if (PGTYPESnumeric_from_decimal(arg2, a2) != 0) { PGTYPESnumeric_free(a1); @@ -92,39 +95,41 @@ deccall3(Decimal *arg1, Decimal *arg2, Decimal *result, int (*ptr)(Numeric *, Nu PGTYPESnumeric_free(nres); return -1211; } - - i = (*ptr)(a1, a2, nres); - - if (i == 0) /* No error */ + + i = (*ptr) (a1, a2, nres); + + if (i == 0) /* No error */ PGTYPESnumeric_to_decimal(nres, result); PGTYPESnumeric_free(nres); PGTYPESnumeric_free(a1); PGTYPESnumeric_free(a2); - + return (i); } + /* we start with the numeric functions */ int -decadd(Decimal *arg1, Decimal *arg2, Decimal *sum) +decadd(Decimal * arg1, Decimal * arg2, Decimal * sum) { deccall3(arg1, arg2, sum, PGTYPESnumeric_add); if (errno == PGTYPES_NUM_OVERFLOW) return -1200; else if (errno != 0) - return -1201; - else return 0; + return -1201; + else + return 0; } int -deccmp(Decimal *arg1, Decimal *arg2) +deccmp(Decimal * arg1, Decimal * arg2) { - return(deccall2(arg1, arg2, PGTYPESnumeric_cmp)); + return (deccall2(arg1, arg2, PGTYPESnumeric_cmp)); } void -deccopy(Decimal *src, Decimal *target) +deccopy(Decimal * src, Decimal * target) { memcpy(target, src, sizeof(Decimal)); } @@ -132,10 +137,10 @@ deccopy(Decimal *src, Decimal *target) static char * strndup(const char *str, size_t len) { - int real_len = strlen(str); - int use_len = (real_len > len) ? len : real_len; - - char *new = malloc(use_len + 1); + int real_len = strlen(str); + int use_len = (real_len > len) ? len : real_len; + + char *new = malloc(use_len + 1); if (new) { @@ -149,15 +154,16 @@ strndup(const char *str, size_t len) } int -deccvasc(char *cp, int len, Decimal *np) +deccvasc(char *cp, int len, Decimal * np) { - char *str = strndup(cp, len); /* Decimal_in always converts the complete string */ - int ret = 0; - Numeric *result; + char *str = strndup(cp, len); /* Decimal_in always converts the + * complete string */ + int ret = 0; + Numeric *result; if (risnull(CSTRINGTYPE, cp)) { - rsetnull(CDECIMALTYPE, (char *)np); + rsetnull(CDECIMALTYPE, (char *) np); return 0; } @@ -170,36 +176,39 @@ deccvasc(char *cp, int len, Decimal *np) { switch (errno) { - case PGTYPES_NUM_OVERFLOW: ret = -1200; - break; - case PGTYPES_NUM_BAD_NUMERIC: ret = -1213; - break; - default: ret = -1216; - break; + case PGTYPES_NUM_OVERFLOW: + ret = -1200; + break; + case PGTYPES_NUM_BAD_NUMERIC: + ret = -1213; + break; + default: + ret = -1216; + break; } } else { - if (PGTYPESnumeric_to_decimal(result, np) !=0) + if (PGTYPESnumeric_to_decimal(result, np) != 0) ret = -1200; free(result); } } - + free(str); return ret; } int -deccvdbl(double dbl, Decimal *np) +deccvdbl(double dbl, Decimal * np) { - Numeric *nres = PGTYPESnumeric_new(); - int result = 1; - - if (risnull(CDOUBLETYPE, (char *)&dbl)) + Numeric *nres = PGTYPESnumeric_new(); + int result = 1; + + if (risnull(CDOUBLETYPE, (char *) &dbl)) { - rsetnull(CDECIMALTYPE, (char *)np); + rsetnull(CDECIMALTYPE, (char *) np); return 0; } @@ -211,18 +220,18 @@ deccvdbl(double dbl, Decimal *np) result = PGTYPESnumeric_to_decimal(nres, np); PGTYPESnumeric_free(nres); - return(result); + return (result); } int -deccvint(int in, Decimal *np) +deccvint(int in, Decimal * np) { - Numeric *nres = PGTYPESnumeric_new(); - int result = 1; - - if (risnull(CINTTYPE, (char *)&in)) + Numeric *nres = PGTYPESnumeric_new(); + int result = 1; + + if (risnull(CINTTYPE, (char *) &in)) { - rsetnull(CDECIMALTYPE, (char *)np); + rsetnull(CDECIMALTYPE, (char *) np); return 0; } @@ -234,18 +243,18 @@ deccvint(int in, Decimal *np) result = PGTYPESnumeric_to_decimal(nres, np); PGTYPESnumeric_free(nres); - return(result); + return (result); } int -deccvlong(long lng, Decimal *np) +deccvlong(long lng, Decimal * np) { - Numeric *nres = PGTYPESnumeric_new(); - int result = 1; - - if (risnull(CLONGTYPE, (char *)&lng)) + Numeric *nres = PGTYPESnumeric_new(); + int result = 1; + + if (risnull(CLONGTYPE, (char *) &lng)) { - rsetnull(CDECIMALTYPE, (char *)np); + rsetnull(CDECIMALTYPE, (char *) np); return 0; } @@ -257,80 +266,87 @@ deccvlong(long lng, Decimal *np) result = PGTYPESnumeric_to_decimal(nres, np); PGTYPESnumeric_free(nres); - return(result); + return (result); } int -decdiv(Decimal *n1, Decimal *n2, Decimal *n3) +decdiv(Decimal * n1, Decimal * n2, Decimal * n3) { - int i = deccall3(n1, n2, n3, PGTYPESnumeric_div); + int i = deccall3(n1, n2, n3, PGTYPESnumeric_div); if (i != 0) switch (errno) { - case PGTYPES_NUM_DIVIDE_ZERO: return -1202; - break; - case PGTYPES_NUM_OVERFLOW: return -1200; - break; - default: return -1201; - break; + case PGTYPES_NUM_DIVIDE_ZERO: + return -1202; + break; + case PGTYPES_NUM_OVERFLOW: + return -1200; + break; + default: + return -1201; + break; } return 0; } -int -decmul(Decimal *n1, Decimal *n2, Decimal *n3) +int +decmul(Decimal * n1, Decimal * n2, Decimal * n3) { - int i = deccall3(n1, n2, n3, PGTYPESnumeric_mul); - + int i = deccall3(n1, n2, n3, PGTYPESnumeric_mul); + if (i != 0) switch (errno) { - case PGTYPES_NUM_OVERFLOW: return -1200; - break; - default: return -1201; - break; + case PGTYPES_NUM_OVERFLOW: + return -1200; + break; + default: + return -1201; + break; } return 0; } int -decsub(Decimal *n1, Decimal *n2, Decimal *n3) +decsub(Decimal * n1, Decimal * n2, Decimal * n3) { - int i = deccall3(n1, n2, n3, PGTYPESnumeric_sub); + int i = deccall3(n1, n2, n3, PGTYPESnumeric_sub); if (i != 0) switch (errno) { - case PGTYPES_NUM_OVERFLOW: return -1200; - break; - default: return -1201; - break; + case PGTYPES_NUM_OVERFLOW: + return -1200; + break; + default: + return -1201; + break; } return 0; } int -dectoasc(Decimal *np, char *cp, int len, int right) +dectoasc(Decimal * np, char *cp, int len, int right) { - char *str; - Numeric *nres = PGTYPESnumeric_new(); + char *str; + Numeric *nres = PGTYPESnumeric_new(); if (nres == NULL) return -1211; - if (risnull(CDECIMALTYPE, (char *)np)) + if (risnull(CDECIMALTYPE, (char *) np)) { - rsetnull(CSTRINGTYPE, (char *)cp); + rsetnull(CSTRINGTYPE, (char *) cp); return 0; } if (PGTYPESnumeric_from_decimal(np, nres) != 0) return -1211; - + if (right >= 0) str = PGTYPESnumeric_to_asc(nres, right); else @@ -339,26 +355,29 @@ dectoasc(Decimal *np, char *cp, int len, int right) PGTYPESnumeric_free(nres); if (!str) return -1; - - /* TODO: have to take care of len here and create exponatial notion if necessary */ + + /* + * TODO: have to take care of len here and create exponatial notion if + * necessary + */ strncpy(cp, str, len); - free (str); + free(str); return 0; } int -dectodbl(Decimal *np, double *dblp) +dectodbl(Decimal * np, double *dblp) { - Numeric *nres = PGTYPESnumeric_new(); - int i; + Numeric *nres = PGTYPESnumeric_new(); + int i; if (nres == NULL) return -1211; - + if (PGTYPESnumeric_from_decimal(np, nres) != 0) return -1211; - + i = PGTYPESnumeric_to_double(nres, dblp); PGTYPESnumeric_free(nres); @@ -366,118 +385,123 @@ dectodbl(Decimal *np, double *dblp) } int -dectoint(Decimal *np, int *ip) +dectoint(Decimal * np, int *ip) { - int ret; - Numeric *nres = PGTYPESnumeric_new(); + int ret; + Numeric *nres = PGTYPESnumeric_new(); if (nres == NULL) return -1211; - + if (PGTYPESnumeric_from_decimal(np, nres) != 0) return -1211; - + ret = PGTYPESnumeric_to_int(nres, ip); if (ret == PGTYPES_NUM_OVERFLOW) ret = -1200; - + return ret; } int -dectolong(Decimal *np, long *lngp) +dectolong(Decimal * np, long *lngp) { - int ret; - Numeric *nres = PGTYPESnumeric_new();; + int ret; + Numeric *nres = PGTYPESnumeric_new();; if (nres == NULL) return -1211; - + if (PGTYPESnumeric_from_decimal(np, nres) != 0) return -1211; - + ret = PGTYPESnumeric_to_long(nres, lngp); if (ret == PGTYPES_NUM_OVERFLOW) ret = -1200; - + return ret; } /* Now the date functions */ int -rdatestr (Date d, char *str) +rdatestr(Date d, char *str) { - char *tmp = PGTYPESdate_to_asc(d); + char *tmp = PGTYPESdate_to_asc(d); if (!tmp) return -1210; - + /* move to user allocated buffer */ strcpy(str, tmp); free(tmp); - + return 0; } int -rstrdate (char *str, Date *d) +rstrdate(char *str, Date * d) { - Date dat = PGTYPESdate_from_asc(str, NULL); + Date dat = PGTYPESdate_from_asc(str, NULL); if (errno != PGTYPES_DATE_BAD_DATE && dat == 0) return -1218; - *d=dat; + *d = dat; return 0; } void -rtoday (Date *d) +rtoday(Date * d) { PGTYPESdate_today(d); return; } int -rjulmdy (Date d, short mdy[3]) +rjulmdy(Date d, short mdy[3]) { - int mdy_int[3]; - + int mdy_int[3]; + PGTYPESdate_julmdy(d, mdy_int); - mdy[0] = (short)mdy_int[0]; - mdy[1] = (short)mdy_int[1]; - mdy[2] = (short)mdy_int[2]; + mdy[0] = (short) mdy_int[0]; + mdy[1] = (short) mdy_int[1]; + mdy[2] = (short) mdy_int[2]; return 0; } int -rdefmtdate (Date *d, char *fmt, char *str) +rdefmtdate(Date * d, char *fmt, char *str) { /* TODO: take care of DBCENTURY environment variable */ /* PGSQL functions allow all centuries */ if (PGTYPESdate_defmt_asc(d, fmt, str) == 0) return 0; - + switch (errno) { - case PGTYPES_DATE_ERR_ENOSHORTDATE: return -1209; + case PGTYPES_DATE_ERR_ENOSHORTDATE: + return -1209; case PGTYPES_DATE_ERR_EARGS: - case PGTYPES_DATE_ERR_ENOTDMY: return -1212; - case PGTYPES_DATE_BAD_DAY: return -1204; - case PGTYPES_DATE_BAD_MONTH: return -1205; - default: return -1206; + case PGTYPES_DATE_ERR_ENOTDMY: + return -1212; + case PGTYPES_DATE_BAD_DAY: + return -1204; + case PGTYPES_DATE_BAD_MONTH: + return -1205; + default: + return -1206; } } int -rfmtdate (Date d, char *fmt, char *str) +rfmtdate(Date d, char *fmt, char *str) { if (PGTYPESdate_fmt_asc(d, fmt, str) == 0) return 0; - + if (errno == ENOMEM) return -1211; @@ -485,9 +509,9 @@ rfmtdate (Date d, char *fmt, char *str) } int -rmdyjul (short mdy[3], Date *d) +rmdyjul(short mdy[3], Date * d) { - int mdy_int[3]; + int mdy_int[3]; mdy_int[0] = mdy[0]; mdy_int[1] = mdy[1]; @@ -499,58 +523,58 @@ rmdyjul (short mdy[3], Date *d) int rdayofweek(Date d) { - return(PGTYPESdate_dayofweek(d)); + return (PGTYPESdate_dayofweek(d)); } - + /* And the datetime stuff */ void -dtcurrent (Timestamp *ts) +dtcurrent(Timestamp *ts) { - PGTYPEStimestamp_current (ts); + PGTYPEStimestamp_current(ts); } int -dtcvasc (char *str, Timestamp *ts) +dtcvasc(char *str, Timestamp *ts) { - Timestamp ts_tmp; - int i; - char **endptr = &str; + Timestamp ts_tmp; + int i; + char **endptr = &str; - ts_tmp = PGTYPEStimestamp_from_asc(str, endptr); - i = errno; - if (i) { - return i; - } - if (**endptr) { - /* extra characters exist at the end */ - return -1264; - } + ts_tmp = PGTYPEStimestamp_from_asc(str, endptr); + i = errno; + if (i) + return i; + if (**endptr) + { + /* extra characters exist at the end */ + return -1264; + } + + /* everything went fine */ + *ts = ts_tmp; - /* everything went fine */ - *ts = ts_tmp; - return 0; } int -dtsub (Timestamp *ts1, Timestamp *ts2, Interval *iv) +dtsub(Timestamp *ts1, Timestamp *ts2, Interval *iv) { return PGTYPEStimestamp_sub(ts1, ts2, iv); } int -dttoasc (Timestamp *ts, char *output) +dttoasc(Timestamp *ts, char *output) { - char *asctime = PGTYPEStimestamp_to_asc( *ts ); - - strcpy (output, asctime); + char *asctime = PGTYPEStimestamp_to_asc(*ts); + + strcpy(output, asctime); free(asctime); return 0; } int -dttofmtasc (Timestamp *ts, char *output, int str_len, char *fmtstr) +dttofmtasc(Timestamp *ts, char *output, int str_len, char *fmtstr) { return PGTYPEStimestamp_fmt_asc(ts, output, str_len, fmtstr); } @@ -559,59 +583,65 @@ int intoasc(Interval *i, char *str) { str = PGTYPESinterval_to_asc(i); - + if (!str) return -errno; - + return 0; } /*************************************************************************** - rfmt.c - description - ------------------- - begin : Wed Apr 2 2003 - copyright : (C) 2003 by Carsten Wolff - email : carsten.wolff@credativ.de + rfmt.c - description + ------------------- + begin : Wed Apr 2 2003 + copyright : (C) 2003 by Carsten Wolff + email : carsten.wolff@credativ.de ***************************************************************************/ -static struct { - long val; - int maxdigits; - int digits; - int remaining; - char sign; - char *val_string; -} value; +static struct +{ + long val; + int maxdigits; + int digits; + int remaining; + char sign; + char *val_string; +} value; /** * initialize the struct, wich holds the different forms * of the long value */ -static void initValue(long lng_val) { - int i, div, dig; - char tmp[2] = " "; +static void +initValue(long lng_val) +{ + int i, + div, + dig; + char tmp[2] = " "; /* set some obvious things */ value.val = lng_val >= 0 ? lng_val : lng_val * (-1); value.sign = lng_val >= 0 ? '+' : '-'; - value.maxdigits = log10(2)*(8*sizeof(long)-1); + value.maxdigits = log10(2) * (8 * sizeof(long) - 1); /* determine the number of digits */ - for(i=1; i <= value.maxdigits; i++) { - if ((int)(value.val / pow(10, i)) != 0) { - value.digits = i+1; - } + for (i = 1; i <= value.maxdigits; i++) + { + if ((int) (value.val / pow(10, i)) != 0) + value.digits = i + 1; } value.remaining = value.digits; /* convert the long to string */ - value.val_string = (char *)malloc(value.digits + 1); - for(i=value.digits; i > 0; i--) { - div = pow(10,i); + value.val_string = (char *) malloc(value.digits + 1); + for (i = value.digits; i > 0; i--) + { + div = pow(10, i); dig = (value.val % div) / (div / 10); - tmp[0] = (char)(dig + 48); + tmp[0] = (char) (dig + 48); strcat(value.val_string, tmp); - } + } /* safety-net */ value.val_string[value.digits] = '\0'; /* clean up */ @@ -619,14 +649,18 @@ static void initValue(long lng_val) { } /* return the position oft the right-most dot in some string */ -static int getRightMostDot(char* str) { - size_t len = strlen(str); - int i,j; - j=0; - for(i=len-1; i >= 0; i--) { - if (str[i] == '.') { - return len-j-1; - } +static int +getRightMostDot(char *str) +{ + size_t len = strlen(str); + int i, + j; + + j = 0; + for (i = len - 1; i >= 0; i--) + { + if (str[i] == '.') + return len - j - 1; j++; } return -1; @@ -636,29 +670,36 @@ static int getRightMostDot(char* str) { int rfmtlong(long lng_val, char *fmt, char *outbuf) { - size_t fmt_len = strlen(fmt); - size_t temp_len; - int i, j, k, dotpos; - int leftalign = 0, blank = 0, sign = 0, entity = 0, - entitydone = 0, signdone = 0, brackets_ok = 0; - char *temp; - char tmp[2] = " "; - char lastfmt = ' ', fmtchar = ' '; - - temp = (char *) malloc(fmt_len+1); + size_t fmt_len = strlen(fmt); + size_t temp_len; + int i, + j, + k, + dotpos; + int leftalign = 0, + blank = 0, + sign = 0, + entity = 0, + entitydone = 0, + signdone = 0, + brackets_ok = 0; + char *temp; + char tmp[2] = " "; + char lastfmt = ' ', + fmtchar = ' '; + + temp = (char *) malloc(fmt_len + 1); /* put all info about the long in a struct */ initValue(lng_val); /* '<' is the only format, where we have to align left */ - if (strchr(fmt, (int)'<')) { + if (strchr(fmt, (int) '<')) leftalign = 1; - } /* '(' requires ')' */ - if (strchr(fmt, (int)'(') && strchr(fmt, (int)')')) { + if (strchr(fmt, (int) '(') && strchr(fmt, (int) ')')) brackets_ok = 1; - } /* get position of the right-most dot in the format-string */ /* and fill the temp-string wit '0's up to there. */ @@ -666,94 +707,122 @@ rfmtlong(long lng_val, char *fmt, char *outbuf) /* start to parse the formatstring */ temp[0] = '\0'; - j = 0; /* position in temp */ - k = value.digits - 1; /* position in the value_string */ - for(i=fmt_len-1, j=0; i>=0; i--, j++) { + j = 0; /* position in temp */ + k = value.digits - 1; /* position in the value_string */ + for (i = fmt_len - 1, j = 0; i >= 0; i--, j++) + { /* qualify, where we are in the value_string */ - if (k < 0) { - if (leftalign) { + if (k < 0) + { + if (leftalign) + { /* can't use strncat(,,0) here, Solaris would freek out */ temp[j] = '\0'; break; } blank = 1; - if (k == -2) { + if (k == -2) entity = 1; - } - else if (k == -1) { + else if (k == -1) sign = 1; - } } /* if we're right side of the right-most dot, print '0' */ - if (dotpos >= 0 && dotpos <= i) { - if (dotpos < i) { - if (fmt[i] == ')') tmp[0] = value.sign == '-' ? ')' : ' '; - else tmp[0] = '0'; + if (dotpos >= 0 && dotpos <= i) + { + if (dotpos < i) + { + if (fmt[i] == ')') + tmp[0] = value.sign == '-' ? ')' : ' '; + else + tmp[0] = '0'; } - else { + else tmp[0] = '.'; - } strcat(temp, tmp); continue; } /* the ',' needs special attention, if it is in the blank area */ - if (blank && fmt[i] == ',') fmtchar = lastfmt; - else fmtchar = fmt[i]; + if (blank && fmt[i] == ',') + fmtchar = lastfmt; + else + fmtchar = fmt[i]; /* analyse this format-char */ - switch(fmtchar) { + switch (fmtchar) + { case ',': tmp[0] = ','; k++; break; case '*': - if (blank) tmp[0] = '*'; - else tmp[0] = value.val_string[k]; + if (blank) + tmp[0] = '*'; + else + tmp[0] = value.val_string[k]; break; case '&': - if (blank) tmp[0] = '0'; - else tmp[0] = value.val_string[k]; + if (blank) + tmp[0] = '0'; + else + tmp[0] = value.val_string[k]; break; case '#': - if (blank) tmp[0] = ' '; - else tmp[0] = value.val_string[k]; + if (blank) + tmp[0] = ' '; + else + tmp[0] = value.val_string[k]; break; case '<': tmp[0] = value.val_string[k]; break; case '-': - if (sign && value.sign == '-' && !signdone) { + if (sign && value.sign == '-' && !signdone) + { tmp[0] = '-'; signdone = 1; } - else if (blank) tmp[0] = ' '; - else tmp[0] = value.val_string[k]; + else if (blank) + tmp[0] = ' '; + else + tmp[0] = value.val_string[k]; break; case '+': - if (sign && !signdone) { + if (sign && !signdone) + { tmp[0] = value.sign; signdone = 1; } - else if (blank) tmp[0] = ' '; - else tmp[0] = value.val_string[k]; + else if (blank) + tmp[0] = ' '; + else + tmp[0] = value.val_string[k]; break; case '(': - if (sign && brackets_ok && value.sign == '-') tmp[0] = '('; - else if (blank) tmp[0] = ' '; - else tmp[0] = value.val_string[k]; + if (sign && brackets_ok && value.sign == '-') + tmp[0] = '('; + else if (blank) + tmp[0] = ' '; + else + tmp[0] = value.val_string[k]; break; case ')': - if (brackets_ok && value.sign == '-') tmp[0] = ')'; - else tmp[0] = ' '; + if (brackets_ok && value.sign == '-') + tmp[0] = ')'; + else + tmp[0] = ' '; break; case '$': - if (blank && !entitydone) { + if (blank && !entitydone) + { tmp[0] = '$'; entitydone = 1; } - else if (blank) tmp[0] = ' '; - else tmp[0] = value.val_string[k]; + else if (blank) + tmp[0] = ' '; + else + tmp[0] = value.val_string[k]; break; - default: tmp[0] = fmt[i]; + default: + tmp[0] = fmt[i]; } strcat(temp, tmp); lastfmt = fmt[i]; @@ -765,9 +834,10 @@ rfmtlong(long lng_val, char *fmt, char *outbuf) /* reverse the temp-string and put it into the outbuf */ temp_len = strlen(temp); outbuf[0] = '\0'; - for(i=temp_len-1; i>=0; i--) { + for (i = temp_len - 1; i >= 0; i--) + { tmp[0] = temp[i]; - strcat(outbuf, tmp); + strcat(outbuf, tmp); } outbuf[temp_len] = '\0'; @@ -782,22 +852,23 @@ void rupshift(char *str) { for (; *str != '\0'; str++) - if (islower(*str)) *str = toupper(*str); + if (islower(*str)) + *str = toupper(*str); return; } int byleng(char *str, int len) { - for (len--; str[len] && str[len] == ' '; len--); - return (len+1); + for (len--; str[len] && str[len] == ' '; len--); + return (len + 1); } void ldchar(char *src, int len, char *dest) { - memmove(dest, src, len); - dest[len]=0; + memmove(dest, src, len); + dest[len] = 0; } int @@ -825,17 +896,17 @@ rtypwidth(int sqltype, int sqllen) } int -dtcvfmtasc (char *inbuf, char *fmtstr, dtime_t *dtvalue) +dtcvfmtasc(char *inbuf, char *fmtstr, dtime_t * dtvalue) { - return PGTYPEStimestamp_defmt_asc(inbuf, fmtstr, dtvalue); + return PGTYPEStimestamp_defmt_asc(inbuf, fmtstr, dtvalue); } static struct var_list { - int number; - void *pointer; + int number; + void *pointer; struct var_list *next; -} *ivlist = NULL; +} *ivlist = NULL; void ECPG_informix_set_var(int number, void *pointer, int lineno) @@ -853,7 +924,7 @@ ECPG_informix_set_var(int number, void *pointer, int lineno) } /* a new one has to be added */ - ptr = (struct var_list *) ECPGalloc (sizeof(struct var_list), lineno); + ptr = (struct var_list *) ECPGalloc(sizeof(struct var_list), lineno); ptr->number = number; ptr->pointer = pointer; ptr->next = ivlist; @@ -869,14 +940,15 @@ ECPG_informix_get_var(int number) return (ptr) ? ptr->pointer : NULL; } -int rsetnull(int t, char *ptr) +int +rsetnull(int t, char *ptr) { ECPGset_informix_null(t, ptr); return 0; } -int risnull(int t, char *ptr) +int +risnull(int t, char *ptr) { - return(ECPGis_informix_null(t, ptr)); + return (ECPGis_informix_null(t, ptr)); } - diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c index 1030b041ab1..679efb6466b 100644 --- a/src/interfaces/ecpg/ecpglib/connect.c +++ b/src/interfaces/ecpg/ecpglib/connect.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.14 2003/08/01 13:53:36 petere Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.15 2003/08/04 00:43:32 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" @@ -15,49 +15,48 @@ #ifdef USE_THREADS static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER; #endif -static struct connection *all_connections = NULL; +static struct connection *all_connections = NULL; static struct connection *actual_connection = NULL; static struct connection * ecpg_get_connection_nr(const char *connection_name) { - struct connection *ret = NULL; - - if( (connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0) ) - { - ret = actual_connection; - } - else - { - struct connection *con; - - for( con = all_connections; con != NULL; con = con->next) + struct connection *ret = NULL; + + if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0)) + ret = actual_connection; + else { - if( strcmp(connection_name, con->name) == 0 ) - break; + struct connection *con; + + for (con = all_connections; con != NULL; con = con->next) + { + if (strcmp(connection_name, con->name) == 0) + break; + } + ret = con; } - ret = con; - } - return( ret ); + return (ret); } struct connection * ECPGget_connection(const char *connection_name) { - struct connection *ret = NULL; + struct connection *ret = NULL; + #ifdef USE_THREADS - pthread_mutex_lock(&connections_mutex); + pthread_mutex_lock(&connections_mutex); #endif - - ret = ecpg_get_connection_nr(connection_name); + + ret = ecpg_get_connection_nr(connection_name); #ifdef USE_THREADS - pthread_mutex_unlock(&connections_mutex); + pthread_mutex_unlock(&connections_mutex); #endif - return (ret); - + return (ret); + } static void @@ -70,9 +69,10 @@ ecpg_finish(struct connection * act) PQfinish(act->connection); - /* no need to lock connections_mutex - we're always called - by ECPGdisconnect or ECPGconnect, which are holding - the lock */ + /* + * no need to lock connections_mutex - we're always called by + * ECPGdisconnect or ECPGconnect, which are holding the lock + */ /* remove act from the list */ if (act == all_connections) @@ -158,26 +158,26 @@ ECPGsetconn(int lineno, const char *connection_name) static void ECPGnoticeReceiver(void *arg, const PGresult *result) { - char *sqlstate = PQresultErrorField(result, 'C'); - char *message = PQresultErrorField(result, 'M'); + char *sqlstate = PQresultErrorField(result, 'C'); + char *message = PQresultErrorField(result, 'M'); struct sqlca_t *sqlca = ECPGget_sqlca(); - int sqlcode; + int sqlcode; /* these are not warnings */ - if (strncmp(sqlstate, "00", 2)==0) + if (strncmp(sqlstate, "00", 2) == 0) return; ECPGlog("%s", message); /* map to SQLCODE for backward compatibility */ - if (strcmp(sqlstate, ECPG_SQLSTATE_INVALID_CURSOR_NAME)==0) + if (strcmp(sqlstate, ECPG_SQLSTATE_INVALID_CURSOR_NAME) == 0) sqlcode = ECPG_WARNING_UNKNOWN_PORTAL; - else if (strcmp(sqlstate, ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION)==0) + else if (strcmp(sqlstate, ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION) == 0) sqlcode = ECPG_WARNING_IN_TRANSACTION; - else if (strcmp(sqlstate, ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION)==0) + else if (strcmp(sqlstate, ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION) == 0) sqlcode = ECPG_WARNING_NO_TRANSACTION; - else if (strcmp(sqlstate, ECPG_SQLSTATE_DUPLICATE_CURSOR)==0) + else if (strcmp(sqlstate, ECPG_SQLSTATE_DUPLICATE_CURSOR) == 0) sqlcode = ECPG_WARNING_PORTAL_EXISTS; else sqlcode = 0; @@ -210,21 +210,23 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p *options = NULL; ECPGinit_sqlca(sqlca); - + if (INFORMIX_MODE(compat)) { - char *envname; - - /* Informix uses an environment variable DBPATH that overrides - * the connection parameters given here. - * We do the same with PG_DBPATH as the syntax is different. */ + char *envname; + + /* + * Informix uses an environment variable DBPATH that overrides the + * connection parameters given here. We do the same with PG_DBPATH + * as the syntax is different. + */ envname = getenv("PG_DBPATH"); if (envname) { ECPGfree(dbname); dbname = strdup(envname); } - + } if ((this = (struct connection *) ECPGalloc(sizeof(struct connection), lineno)) == NULL) @@ -378,21 +380,21 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p if (PQstatus(this->connection) == CONNECTION_BAD) { - const char *errmsg = PQerrorMessage(this->connection); - char *db = realname ? realname : "<DEFAULT>"; + const char *errmsg = PQerrorMessage(this->connection); + char *db = realname ? realname : "<DEFAULT>"; ecpg_finish(this); #ifdef USE_THREADS pthread_mutex_unlock(&connections_mutex); #endif ECPGlog("connect: could not open database %s on %s port %s %s%s%s%s in line %d\n\t%s\n", - db, + db, host ? host : "<DEFAULT>", port ? port : "<DEFAULT>", options ? "with options " : "", options ? options : "", user ? "for user " : "", user ? user : "", lineno, errmsg); - + ECPGraise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, db); if (host) ECPGfree(host); @@ -455,14 +457,14 @@ ECPGdisconnect(int lineno, const char *connection_name) con = ecpg_get_connection_nr(connection_name); if (!ECPGinit(con, connection_name, lineno)) - { + { #ifdef USE_THREADS - pthread_mutex_unlock(&connections_mutex); + pthread_mutex_unlock(&connections_mutex); #endif - return (false); - } + return (false); + } else - ecpg_finish(con); + ecpg_finish(con); } #ifdef USE_THREADS diff --git a/src/interfaces/ecpg/ecpglib/data.c b/src/interfaces/ecpg/ecpglib/data.c index 14e459d1b60..e095ed671d3 100644 --- a/src/interfaces/ecpg/ecpglib/data.c +++ b/src/interfaces/ecpg/ecpglib/data.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/data.c,v 1.15 2003/08/01 13:53:36 petere Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/data.c,v 1.16 2003/08/04 00:43:32 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" @@ -24,7 +24,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, { struct sqlca_t *sqlca = ECPGget_sqlca(); char *pval = (char *) PQgetvalue(results, act_tuple, act_field); - int value_for_indicator = 0; + int value_for_indicator = 0; ECPGlog("ECPGget_data line %d: RESULT: %s offset: %ld\n", lineno, pval ? pval : "", offset); @@ -54,11 +54,12 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, /* We will have to decode the value */ /* - * check for null value and set indicator accordingly, i.e. -1 if NULL and 0 if not + * check for null value and set indicator accordingly, i.e. -1 if NULL + * and 0 if not */ if (PQgetisnull(results, act_tuple, act_field)) value_for_indicator = -1; - + switch (ind_type) { case ECPGt_short: @@ -81,11 +82,13 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, #endif /* HAVE_LONG_LONG_INT_64 */ case ECPGt_NO_INDICATOR: if (value_for_indicator == -1) - { + { if (force_indicator == false) { - /* Informix has an additional way to specify NULLs - * note that this uses special values to denote NULL */ + /* + * Informix has an additional way to specify NULLs + * note that this uses special values to denote NULL + */ ECPGset_informix_null(type, var + offset * act_tuple); } else @@ -109,13 +112,13 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, switch (type) { long res; - unsigned long ures; + unsigned long ures; double dres; - char *scan_length; - Numeric *nres; + char *scan_length; + Numeric *nres; Date ddres; Timestamp tres; - Interval *ires; + Interval *ires; case ECPGt_short: case ECPGt_int: @@ -294,9 +297,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, case ECPGt_unsigned_char: { if (varcharsize == 0) - { - strncpy((char *) ((long) var + offset * act_tuple), pval, strlen(pval)+1); - } + strncpy((char *) ((long) var + offset * act_tuple), pval, strlen(pval) + 1); else { strncpy((char *) ((long) var + offset * act_tuple), pval, varcharsize); @@ -340,9 +341,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, variable->len = strlen(pval); if (varcharsize == 0) - { strncpy(variable->arr, pval, variable->len); - } else { strncpy(variable->arr, pval, varcharsize); @@ -403,12 +402,12 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, else nres = PGTYPESnumeric_from_asc("0.0", &scan_length); - if (type == ECPGt_numeric) - PGTYPESnumeric_copy(nres, (Numeric *)(var + offset * act_tuple)); + if (type == ECPGt_numeric) + PGTYPESnumeric_copy(nres, (Numeric *) (var + offset * act_tuple)); else - PGTYPESnumeric_to_decimal(nres, (Decimal *)(var + offset * act_tuple)); + PGTYPESnumeric_to_decimal(nres, (Decimal *) (var + offset * act_tuple)); break; - + case ECPGt_interval: if (pval) { @@ -430,7 +429,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, else ires = PGTYPESinterval_from_asc("0 seconds", NULL); - PGTYPESinterval_copy(ires, (Interval *)(var + offset * act_tuple)); + PGTYPESinterval_copy(ires, (Interval *) (var + offset * act_tuple)); break; case ECPGt_date: if (pval) @@ -450,7 +449,7 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, return (false); } - *((Date *)(var + offset * act_tuple)) = ddres; + *((Date *) (var + offset * act_tuple)) = ddres; } break; @@ -472,10 +471,10 @@ ECPGget_data(const PGresult *results, int act_tuple, int act_field, int lineno, return (false); } - *((Timestamp *)(var + offset * act_tuple)) = tres; + *((Timestamp *) (var + offset * act_tuple)) = tres; } break; - + default: ECPGraise(lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ECPGtype_name(type)); return (false); diff --git a/src/interfaces/ecpg/ecpglib/descriptor.c b/src/interfaces/ecpg/ecpglib/descriptor.c index 0beae2d93ab..839fc780cbc 100644 --- a/src/interfaces/ecpg/ecpglib/descriptor.c +++ b/src/interfaces/ecpg/ecpglib/descriptor.c @@ -1,6 +1,6 @@ /* dynamic SQL support routines * - * $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.5 2003/08/01 13:53:36 petere Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/descriptor.c,v 1.6 2003/08/04 00:43:32 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL @@ -454,7 +454,7 @@ ECPGdescriptor_lvalue(int line, const char *descriptor) } bool -ECPGdescribe(int line, bool input, const char *statement, ...) +ECPGdescribe(int line, bool input, const char *statement,...) { ECPGlog("ECPGdescribe called on line %d for %s in %s\n", line, (input) ? "input" : "output", statement); return false; diff --git a/src/interfaces/ecpg/ecpglib/error.c b/src/interfaces/ecpg/ecpglib/error.c index cfeb4e65b0b..33bac6e892d 100644 --- a/src/interfaces/ecpg/ecpglib/error.c +++ b/src/interfaces/ecpg/ecpglib/error.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/error.c,v 1.5 2003/08/01 13:53:36 petere Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/error.c,v 1.6 2003/08/04 00:43:32 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" @@ -13,9 +13,10 @@ void -ECPGraise(int line, int code, const char * sqlstate, const char *str) +ECPGraise(int line, int code, const char *sqlstate, const char *str) { struct sqlca_t *sqlca = ECPGget_sqlca(); + sqlca->sqlcode = code; strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate)); @@ -161,8 +162,8 @@ ECPGraise_backend(int line, PGresult *result, PGconn *conn, int compat) /* copy error message */ snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), - "'%s' in line %d.", - result ? PQresultErrorField(result, 'M') : PQerrorMessage(conn), + "'%s' in line %d.", + result ? PQresultErrorField(result, 'M') : PQerrorMessage(conn), line); sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc); @@ -172,9 +173,9 @@ ECPGraise_backend(int line, PGresult *result, PGconn *conn, int compat) sizeof(sqlca->sqlstate)); /* assign SQLCODE for backward compatibility */ - if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate))==0) + if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate)) == 0) sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_DUPLICATE_KEY : ECPG_DUPLICATE_KEY; - if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate))==0) + if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate)) == 0) sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_SUBSELECT_NOT_ONE; else sqlca->sqlcode = ECPG_PGSQL; diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c index 0d4247897d5..c45441d47ca 100644 --- a/src/interfaces/ecpg/ecpglib/execute.c +++ b/src/interfaces/ecpg/ecpglib/execute.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.22 2003/08/01 13:53:36 petere Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.23 2003/08/04 00:43:32 momjian Exp $ */ /* * The aim is to get a simpler inteface to the database routines. @@ -65,7 +65,7 @@ quote_postgres(char *arg, int lineno) res[ri++] = '\''; res[ri] = '\0'; - + return res; } @@ -138,13 +138,16 @@ create_statement(int lineno, int compat, int force_indicator, struct connection else var->value = var->pointer; - /* negative values are used to indicate an array without given bounds */ + /* + * negative values are used to indicate an array without given + * bounds + */ /* reset to zero for us */ if (var->arrsize < 0) var->arrsize = 0; if (var->varcharsize < 0) var->varcharsize = 0; - + var->ind_type = va_arg(ap, enum ECPGttype); var->ind_pointer = va_arg(ap, char *); var->ind_varcharsize = va_arg(ap, long); @@ -157,8 +160,11 @@ create_statement(int lineno, int compat, int force_indicator, struct connection var->ind_value = *((char **) (var->ind_pointer)); else var->ind_value = var->ind_pointer; - - /* negative values are used to indicate an array without given bounds */ + + /* + * negative values are used to indicate an array without given + * bounds + */ /* reset to zero for us */ if (var->ind_arrsize < 0) var->ind_arrsize = 0; @@ -482,11 +488,11 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var, * we do not know if the attribute is an array here */ #if 0 - if (var->arrsize > 1 && ...) - { + if (var->arrsize > 1 &&...) + { ECPGraise(stmt->lineno, ECPG_ARRAY_INSERT, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL); return false; - } + } #endif /* @@ -739,7 +745,7 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var, break; case ECPGt_bool: - if (!(mallocedval = ECPGalloc(var->arrsize +sizeof ("array []"), stmt->lineno))) + if (!(mallocedval = ECPGalloc(var->arrsize + sizeof("array []"), stmt->lineno))) return false; if (var->arrsize > 1) @@ -838,30 +844,30 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var, case ECPGt_decimal: case ECPGt_numeric: { - char *str = NULL; - int slen; - Numeric *nval = PGTYPESnumeric_new(); - + char *str = NULL; + int slen; + Numeric *nval = PGTYPESnumeric_new(); + if (var->arrsize > 1) { for (element = 0; element < var->arrsize; element++) { if (var->type == ECPGt_numeric) - PGTYPESnumeric_copy((Numeric *)((var + var->offset * element)->value), nval); + PGTYPESnumeric_copy((Numeric *) ((var + var->offset * element)->value), nval); else - PGTYPESnumeric_from_decimal((Decimal *)((var + var->offset * element)->value), nval); - + PGTYPESnumeric_from_decimal((Decimal *) ((var + var->offset * element)->value), nval); + str = PGTYPESnumeric_to_asc(nval, 0); PGTYPESnumeric_free(nval); - slen = strlen (str); - + slen = strlen(str); + if (!(mallocedval = ECPGrealloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [] "), stmt->lineno))) return false; - + if (!element) strcpy(mallocedval, "array ["); - - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); strcpy(mallocedval + strlen(mallocedval), ","); } strcpy(mallocedval + strlen(mallocedval) - 1, "]"); @@ -869,22 +875,22 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var, else { if (var->type == ECPGt_numeric) - PGTYPESnumeric_copy((Numeric *)(var->value), nval); + PGTYPESnumeric_copy((Numeric *) (var->value), nval); else - PGTYPESnumeric_from_decimal((Decimal *)(var->value), nval); - + PGTYPESnumeric_from_decimal((Decimal *) (var->value), nval); + str = PGTYPESnumeric_to_asc(nval, 0); PGTYPESnumeric_free(nval); - slen = strlen (str); - + slen = strlen(str); + if (!(mallocedval = ECPGalloc(slen + 1, stmt->lineno))) return false; - strncpy(mallocedval, str , slen); + strncpy(mallocedval, str, slen); mallocedval[slen] = '\0'; } - + *tobeinserted_p = mallocedval; *malloced_p = true; free(str); @@ -893,41 +899,41 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var, case ECPGt_interval: { - char *str = NULL; - int slen; - + char *str = NULL; + int slen; + if (var->arrsize > 1) { for (element = 0; element < var->arrsize; element++) { - str = quote_postgres(PGTYPESinterval_to_asc((Interval *)((var + var->offset * element)->value)), stmt->lineno); - slen = strlen (str); - + str = quote_postgres(PGTYPESinterval_to_asc((Interval *) ((var + var->offset * element)->value)), stmt->lineno); + slen = strlen(str); + if (!(mallocedval = ECPGrealloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [],interval "), stmt->lineno))) return false; - + if (!element) strcpy(mallocedval, "array ["); - + strcpy(mallocedval + strlen(mallocedval), "interval "); - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); strcpy(mallocedval + strlen(mallocedval), ","); } strcpy(mallocedval + strlen(mallocedval) - 1, "]"); } else { - str = quote_postgres(PGTYPESinterval_to_asc((Interval *)(var->value)), stmt->lineno); - slen = strlen (str); - + str = quote_postgres(PGTYPESinterval_to_asc((Interval *) (var->value)), stmt->lineno); + slen = strlen(str); + if (!(mallocedval = ECPGalloc(slen + sizeof("interval ") + 1, stmt->lineno))) return false; strcpy(mallocedval, "interval "); /* also copy trailing '\0' */ - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); } - + *tobeinserted_p = mallocedval; *malloced_p = true; free(str); @@ -936,90 +942,90 @@ ECPGstore_input(const struct statement * stmt, const struct variable * var, case ECPGt_date: { - char *str = NULL; - int slen; - + char *str = NULL; + int slen; + if (var->arrsize > 1) { for (element = 0; element < var->arrsize; element++) { - str = quote_postgres(PGTYPESdate_to_asc(*(Date *)((var + var->offset * element)->value)), stmt->lineno); - slen = strlen (str); - + str = quote_postgres(PGTYPESdate_to_asc(*(Date *) ((var + var->offset * element)->value)), stmt->lineno); + slen = strlen(str); + if (!(mallocedval = ECPGrealloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [],date "), stmt->lineno))) return false; - + if (!element) strcpy(mallocedval, "array ["); - + strcpy(mallocedval + strlen(mallocedval), "date "); - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); strcpy(mallocedval + strlen(mallocedval), ","); } strcpy(mallocedval + strlen(mallocedval) - 1, "]"); } else { - str = quote_postgres(PGTYPESdate_to_asc(*(Date *)(var->value)), stmt->lineno); - slen = strlen (str); - + str = quote_postgres(PGTYPESdate_to_asc(*(Date *) (var->value)), stmt->lineno); + slen = strlen(str); + if (!(mallocedval = ECPGalloc(slen + sizeof("date ") + 1, stmt->lineno))) return false; strcpy(mallocedval, "date "); /* also copy trailing '\0' */ - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); } - + *tobeinserted_p = mallocedval; *malloced_p = true; free(str); } break; - + case ECPGt_timestamp: { - char *str = NULL; - int slen; - + char *str = NULL; + int slen; + if (var->arrsize > 1) { for (element = 0; element < var->arrsize; element++) { - str = quote_postgres(PGTYPEStimestamp_to_asc(*(Timestamp *)((var + var->offset * element)->value)), stmt->lineno); - slen = strlen (str); - + str = quote_postgres(PGTYPEStimestamp_to_asc(*(Timestamp *) ((var + var->offset * element)->value)), stmt->lineno); + slen = strlen(str); + if (!(mallocedval = ECPGrealloc(mallocedval, strlen(mallocedval) + slen + sizeof("array [], timestamp "), stmt->lineno))) return false; - + if (!element) strcpy(mallocedval, "array ["); - + strcpy(mallocedval + strlen(mallocedval), "timestamp "); - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); strcpy(mallocedval + strlen(mallocedval), ","); } strcpy(mallocedval + strlen(mallocedval) - 1, "]"); } else { - str = quote_postgres(PGTYPEStimestamp_to_asc(*(Timestamp *)(var->value)), stmt->lineno); - slen = strlen (str); - + str = quote_postgres(PGTYPEStimestamp_to_asc(*(Timestamp *) (var->value)), stmt->lineno); + slen = strlen(str); + if (!(mallocedval = ECPGalloc(slen + sizeof("timestamp") + 1, stmt->lineno))) return false; strcpy(mallocedval, "timestamp "); /* also copy trailing '\0' */ - strncpy(mallocedval + strlen(mallocedval), str , slen + 1); + strncpy(mallocedval + strlen(mallocedval), str, slen + 1); } - + *tobeinserted_p = mallocedval; *malloced_p = true; free(str); } break; - + default: /* Not implemented yet */ ECPGraise(stmt->lineno, ECPG_UNSUPPORTED, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (char *) ECPGtype_name(var->type)); @@ -1219,10 +1225,10 @@ ECPGexecute(struct statement * stmt) sqlca->sqlerrd[2] = atol(PQcmdTuples(results)); ECPGlog("ECPGexecute line %d Ok: %s\n", stmt->lineno, cmdstat); if (stmt->compat != ECPG_COMPAT_INFORMIX_SE && - !sqlca->sqlerrd[2] && - ( !strncmp(cmdstat, "UPDATE", 6) - || !strncmp(cmdstat, "INSERT", 6) - || !strncmp(cmdstat, "DELETE", 6))) + !sqlca->sqlerrd[2] && + (!strncmp(cmdstat, "UPDATE", 6) + || !strncmp(cmdstat, "INSERT", 6) + || !strncmp(cmdstat, "DELETE", 6))) ECPGraise(stmt->lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL); break; case PGRES_NONFATAL_ERROR: @@ -1326,4 +1332,3 @@ ECPGdo_descriptor(int line, const char *connection, ECPGt_descriptor, descriptor, 0L, 0L, 0L, ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT); } - diff --git a/src/interfaces/ecpg/ecpglib/extern.h b/src/interfaces/ecpg/ecpglib/extern.h index d98f158bacc..3976d5b24db 100644 --- a/src/interfaces/ecpg/ecpglib/extern.h +++ b/src/interfaces/ecpg/ecpglib/extern.h @@ -5,7 +5,11 @@ #include "libpq-fe.h" #include "sqlca.h" -enum COMPAT_MODE { ECPG_COMPAT_PGSQL = 0, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE}; +enum COMPAT_MODE +{ + ECPG_COMPAT_PGSQL = 0, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE +}; + #define INFORMIX_MODE(X) ((X) == ECPG_COMPAT_INFORMIX || (X) == ECPG_COMPAT_INFORMIX_SE) /* Here are some methods used by the lib. */ @@ -51,7 +55,7 @@ struct statement char *command; struct connection *connection; enum COMPAT_MODE compat; - bool force_indicator; + bool force_indicator; struct variable *inlist; struct variable *outlist; }; @@ -109,7 +113,7 @@ bool ECPGstore_result(const PGresult *results, int act_field, #define ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION "08001" #define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST "08003" #define ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN "08007" -#define ECPG_SQLSTATE_CARDINALITY_VIOLATION "21000" +#define ECPG_SQLSTATE_CARDINALITY_VIOLATION "21000" #define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER "22002" #define ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION "25001" #define ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION "25P01" @@ -124,4 +128,4 @@ bool ECPGstore_result(const PGresult *results, int act_field, #define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR "YE000" #define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY "YE001" -#endif /* _ECPG_LIB_EXTERN_H */ +#endif /* _ECPG_LIB_EXTERN_H */ diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c index f42b8f721e7..e87643d8df5 100644 --- a/src/interfaces/ecpg/ecpglib/misc.c +++ b/src/interfaces/ecpg/ecpglib/misc.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.12 2003/08/01 13:53:36 petere Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.13 2003/08/04 00:43:32 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" @@ -23,7 +23,7 @@ #define LONG_LONG_MIN LLONG_MIN #endif #endif - + static struct sqlca_t sqlca_init = { { @@ -52,8 +52,9 @@ static struct sqlca_t sqlca_init = }; #ifdef USE_THREADS -static pthread_key_t sqlca_key; -static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT; +static pthread_key_t sqlca_key; +static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT; + #else static struct sqlca_t sqlca = { @@ -84,22 +85,23 @@ static struct sqlca_t sqlca = #endif #ifdef USE_THREADS -static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER; #endif -static int simple_debug = 0; +static int simple_debug = 0; static FILE *debugstream = NULL; void -ECPGinit_sqlca(struct sqlca_t *sqlca) +ECPGinit_sqlca(struct sqlca_t * sqlca) { - memcpy((char *)sqlca, (char *)&sqlca_init, sizeof(struct sqlca_t)); + memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t)); } bool ECPGinit(const struct connection * con, const char *connection_name, const int lineno) { struct sqlca_t *sqlca = ECPGget_sqlca(); + ECPGinit_sqlca(sqlca); if (con == NULL) { @@ -115,7 +117,7 @@ ECPGinit(const struct connection * con, const char *connection_name, const int l static void ecpg_sqlca_key_init(void) { - pthread_key_create(&sqlca_key, NULL); + pthread_key_create(&sqlca_key, NULL); } #endif @@ -123,20 +125,20 @@ struct sqlca_t * ECPGget_sqlca(void) { #ifdef USE_THREADS - struct sqlca_t *sqlca; - - pthread_once(&sqlca_key_once, ecpg_sqlca_key_init); - - sqlca = pthread_getspecific(sqlca_key); - if( sqlca == NULL ) - { - sqlca = malloc(sizeof(struct sqlca_t)); - ECPGinit_sqlca(sqlca); - pthread_setspecific(sqlca_key, sqlca); - } - return( sqlca ); + struct sqlca_t *sqlca; + + pthread_once(&sqlca_key_once, ecpg_sqlca_key_init); + + sqlca = pthread_getspecific(sqlca_key); + if (sqlca == NULL) + { + sqlca = malloc(sizeof(struct sqlca_t)); + ECPGinit_sqlca(sqlca); + pthread_setspecific(sqlca_key, sqlca); + } + return (sqlca); #else - return( &sqlca ); + return (&sqlca); #endif } @@ -227,16 +229,17 @@ ECPGlog(const char *format,...) pthread_mutex_lock(&debug_mutex); #endif - if( simple_debug ) + if (simple_debug) { - char *f = (char *)malloc(strlen(format) + 100); - if( f == NULL ) - { + char *f = (char *) malloc(strlen(format) + 100); + + if (f == NULL) + { #ifdef USE_THREADS pthread_mutex_unlock(&debug_mutex); #endif return; - } + } sprintf(f, "[%d]: %s", (int) getpid(), format); @@ -258,7 +261,7 @@ ECPGset_informix_null(enum ECPGttype type, void *ptr) { switch (type) { - case ECPGt_char: + case ECPGt_char: case ECPGt_unsigned_char: *((char *) ptr) = 0x00; break; @@ -307,10 +310,12 @@ ECPGset_informix_null(enum ECPGttype type, void *ptr) } } -static bool _check(unsigned char *ptr, int length) +static bool +_check(unsigned char *ptr, int length) { - for (;ptr[--length] == 0xff && length >= 0; length --); - if (length < 0) return true; + for (; ptr[--length] == 0xff && length >= 0; length--); + if (length < 0) + return true; return false; } @@ -319,49 +324,57 @@ ECPGis_informix_null(enum ECPGttype type, void *ptr) { switch (type) { - case ECPGt_char: + case ECPGt_char: case ECPGt_unsigned_char: - if (*((char *)ptr) == 0x00) return true; + if (*((char *) ptr) == 0x00) + return true; break; case ECPGt_short: case ECPGt_unsigned_short: - if (*((short int *) ptr) == SHRT_MIN) return true; + if (*((short int *) ptr) == SHRT_MIN) + return true; break; case ECPGt_int: case ECPGt_unsigned_int: - if (*((int *) ptr) == INT_MIN) return true; + if (*((int *) ptr) == INT_MIN) + return true; break; case ECPGt_long: case ECPGt_unsigned_long: case ECPGt_date: - if (*((long *) ptr) == LONG_MIN) return true; + if (*((long *) ptr) == LONG_MIN) + return true; break; #ifdef HAVE_LONG_LONG_INT_64 case ECPGt_long_long: case ECPGt_unsigned_long_long: - if (*((long long *) ptr) == LONG_LONG_MIN) return true; + if (*((long long *) ptr) == LONG_LONG_MIN) + return true; break; #endif /* HAVE_LONG_LONG_INT_64 */ case ECPGt_float: - return(_check(ptr, sizeof(float))); + return (_check(ptr, sizeof(float))); break; case ECPGt_double: - return(_check(ptr, sizeof(double))); + return (_check(ptr, sizeof(double))); break; case ECPGt_varchar: - if (*(((struct ECPGgeneric_varchar *) ptr)->arr) == 0x00) return true; + if (*(((struct ECPGgeneric_varchar *) ptr)->arr) == 0x00) + return true; break; case ECPGt_decimal: - if (((Decimal *) ptr)->sign == NUMERIC_NAN) return true; + if (((Decimal *) ptr)->sign == NUMERIC_NAN) + return true; break; case ECPGt_numeric: - if (((Numeric *) ptr)->sign == NUMERIC_NAN) return true; + if (((Numeric *) ptr)->sign == NUMERIC_NAN) + return true; break; case ECPGt_interval: - return(_check(ptr, sizeof(Interval))); + return (_check(ptr, sizeof(Interval))); break; case ECPGt_timestamp: - return(_check(ptr, sizeof(Timestamp))); + return (_check(ptr, sizeof(Timestamp))); break; default: break; diff --git a/src/interfaces/ecpg/ecpglib/prepare.c b/src/interfaces/ecpg/ecpglib/prepare.c index 5569d36da07..55f497e03ab 100644 --- a/src/interfaces/ecpg/ecpglib/prepare.c +++ b/src/interfaces/ecpg/ecpglib/prepare.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/prepare.c,v 1.8 2003/08/01 13:53:36 petere Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/prepare.c,v 1.9 2003/08/04 00:43:32 momjian Exp $ */ #define POSTGRES_ECPG_INTERNAL #include "postgres_fe.h" @@ -66,7 +66,7 @@ ECPGprepare(int lineno, char *name, char *variable) for (this = prep_stmts; this != NULL && strcmp(this->name, name) != 0; this = this->next); if (this) { - bool b = ECPGdeallocate(lineno, ECPG_COMPAT_PGSQL, name); + bool b = ECPGdeallocate(lineno, ECPG_COMPAT_PGSQL, name); if (!b) return false; @@ -109,17 +109,19 @@ ECPGprepare(int lineno, char *name, char *variable) bool ECPGdeallocate(int lineno, int c, char *name) { - bool ret = ECPGdeallocate_one(lineno, name); + bool ret = ECPGdeallocate_one(lineno, name); enum COMPAT_MODE compat = c; if (INFORMIX_MODE(compat)) { - /* Just ignore all errors since we do not know the list of cursors we - * are allowed to free. We have to trust that the software. */ - return true; + /* + * Just ignore all errors since we do not know the list of cursors + * we are allowed to free. We have to trust that the software. + */ + return true; } - - if (!ret) + + if (!ret) ECPGraise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, name); return ret; @@ -156,7 +158,7 @@ ECPGdeallocate_all(int lineno) /* deallocate all prepared statements */ while (prep_stmts != NULL) { - bool b = ECPGdeallocate(lineno, ECPG_COMPAT_PGSQL, prep_stmts->name); + bool b = ECPGdeallocate(lineno, ECPG_COMPAT_PGSQL, prep_stmts->name); if (!b) return false; diff --git a/src/interfaces/ecpg/include/datetime.h b/src/interfaces/ecpg/include/datetime.h index da16faa4855..ed8f31dfc8a 100644 --- a/src/interfaces/ecpg/include/datetime.h +++ b/src/interfaces/ecpg/include/datetime.h @@ -3,16 +3,16 @@ #ifndef dtime_t #define dtime_t Timestamp -#endif /* dtime_t */ +#endif /* dtime_t */ #ifndef intrvl_t #define intrvl_t Interval -#endif /* intrvl_t */ +#endif /* intrvl_t */ -extern void dtcurrent (dtime_t *); -extern int dtcvasc (char *, dtime_t *); -extern int dtsub (dtime_t *, dtime_t *, intrvl_t *); -extern int dttoasc (dtime_t *, char *); -extern int dttofmtasc (dtime_t *, char *, int, char *); -extern int intoasc(intrvl_t *, char *); -extern int dtcvfmtasc(char *, char *, dtime_t *); +extern void dtcurrent(dtime_t *); +extern int dtcvasc(char *, dtime_t *); +extern int dtsub(dtime_t *, dtime_t *, intrvl_t *); +extern int dttoasc(dtime_t *, char *); +extern int dttofmtasc(dtime_t *, char *, int, char *); +extern int intoasc(intrvl_t *, char *); +extern int dtcvfmtasc(char *, char *, dtime_t *); diff --git a/src/interfaces/ecpg/include/decimal.h b/src/interfaces/ecpg/include/decimal.h index e0cb06bd5c8..c68e253e682 100644 --- a/src/interfaces/ecpg/include/decimal.h +++ b/src/interfaces/ecpg/include/decimal.h @@ -2,20 +2,19 @@ #ifndef dec_t #define dec_t Decimal -#endif /* dec_t */ - -int decadd(dec_t *, dec_t *, dec_t *); -int deccmp(dec_t *, dec_t *); -void deccopy(dec_t *, dec_t *); -int deccvasc(char *, int, dec_t *); -int deccvdbl(double, dec_t *); -int deccvint(int, dec_t *); -int deccvlong(long, dec_t *); -int decdiv(dec_t *, dec_t *, dec_t *); -int decmul(dec_t *, dec_t *, dec_t *); -int decsub(dec_t *, dec_t *, dec_t *); -int dectoasc(dec_t *, char *, int, int); -int dectodbl(dec_t *, double *); -int dectoint(dec_t *, int *); -int dectolong(dec_t *, long *); +#endif /* dec_t */ +int decadd(dec_t *, dec_t *, dec_t *); +int deccmp(dec_t *, dec_t *); +void deccopy(dec_t *, dec_t *); +int deccvasc(char *, int, dec_t *); +int deccvdbl(double, dec_t *); +int deccvint(int, dec_t *); +int deccvlong(long, dec_t *); +int decdiv(dec_t *, dec_t *, dec_t *); +int decmul(dec_t *, dec_t *, dec_t *); +int decsub(dec_t *, dec_t *, dec_t *); +int dectoasc(dec_t *, char *, int, int); +int dectodbl(dec_t *, double *); +int dectoint(dec_t *, int *); +int dectolong(dec_t *, long *); diff --git a/src/interfaces/ecpg/include/ecpg_informix.h b/src/interfaces/ecpg/include/ecpg_informix.h index 59991c70a91..b5c8ed4884f 100644 --- a/src/interfaces/ecpg/include/ecpg_informix.h +++ b/src/interfaces/ecpg/include/ecpg_informix.h @@ -1,4 +1,4 @@ -/* +/* * This file contains stuff needed to be as compatible to Informix as possible. */ @@ -10,27 +10,27 @@ #ifndef Date #define Date long -#endif /* ! Date */ +#endif /* ! Date */ -extern int rdatestr (Date, char *); -extern void rtoday (Date *); -extern int rjulmdy (Date, short *); -extern int rdefmtdate (Date *, char *, char *); -extern int rfmtdate (Date, char *, char *); -extern int rmdyjul (short *, Date *); -extern int rstrdate (char *, Date *); -extern int rdayofweek(Date); +extern int rdatestr(Date, char *); +extern void rtoday(Date *); +extern int rjulmdy(Date, short *); +extern int rdefmtdate(Date *, char *, char *); +extern int rfmtdate(Date, char *, char *); +extern int rmdyjul(short *, Date *); +extern int rstrdate(char *, Date *); +extern int rdayofweek(Date); -extern int rfmtlong(long, char *, char *); -extern int rgetmsg(int, char *, int); -extern int risnull(int, char *); -extern int rsetnull(int, char *); -extern int rtypalign(int, int); -extern int rtypmsize(int, int); -extern int rtypwidth(int, int); +extern int rfmtlong(long, char *, char *); +extern int rgetmsg(int, char *, int); +extern int risnull(int, char *); +extern int rsetnull(int, char *); +extern int rtypalign(int, int); +extern int rtypmsize(int, int); +extern int rtypwidth(int, int); extern void rupshift(char *); -extern int byleng(char *, int); +extern int byleng(char *, int); extern void ldchar(char *, int, char *); extern void ECPG_informix_set_var(int, void *, int); diff --git a/src/interfaces/ecpg/include/ecpgerrno.h b/src/interfaces/ecpg/include/ecpgerrno.h index eb1936beb18..7a551551541 100644 --- a/src/interfaces/ecpg/include/ecpgerrno.h +++ b/src/interfaces/ecpg/include/ecpgerrno.h @@ -52,8 +52,8 @@ /* for compatibility we define some different error codes for the same error * if adding a new one make sure to not double define it */ -#define ECPG_INFORMIX_DUPLICATE_KEY -239 -#define ECPG_INFORMIX_SUBSELECT_NOT_ONE -284 +#define ECPG_INFORMIX_DUPLICATE_KEY -239 +#define ECPG_INFORMIX_SUBSELECT_NOT_ONE -284 /* backend WARNINGs, starting at 600 */ #define ECPG_WARNING_UNRECOGNIZED -600 diff --git a/src/interfaces/ecpg/include/ecpglib.h b/src/interfaces/ecpg/include/ecpglib.h index 3f510fbfa60..fc429c96433 100644 --- a/src/interfaces/ecpg/include/ecpglib.h +++ b/src/interfaces/ecpg/include/ecpglib.h @@ -41,7 +41,7 @@ extern "C" struct sqlca_t; -void ECPGinit_sqlca(struct sqlca_t *sqlca); +void ECPGinit_sqlca(struct sqlca_t * sqlca); void ECPGdebug(int, FILE *); bool ECPGstatus(int, const char *); bool ECPGsetcommit(int, const char *, const char *); @@ -57,7 +57,7 @@ bool ECPGdeallocate_all(int); char *ECPGprepared_statement(char *); void ECPGlog(const char *format,...); -char *ECPGerrmsg(void); +char *ECPGerrmsg(void); /* print an error message */ void sqlprint(void); @@ -79,14 +79,13 @@ bool ECPGget_desc(int, char *, int,...); void ECPGset_informix_null(enum ECPGttype, void *); bool ECPGis_informix_null(enum ECPGttype, void *); -bool ECPGdescribe(int, bool, const char *, ...); +bool ECPGdescribe(int, bool, const char *,...); /* dynamic result allocation */ void ECPGfree_auto_mem(void); #ifdef __cplusplus } - #endif -#endif /* _ECPGLIB_H */ +#endif /* _ECPGLIB_H */ diff --git a/src/interfaces/ecpg/include/ecpgtype.h b/src/interfaces/ecpg/include/ecpgtype.h index 7a299d0be89..3d544029b73 100644 --- a/src/interfaces/ecpg/include/ecpgtype.h +++ b/src/interfaces/ecpg/include/ecpgtype.h @@ -44,20 +44,22 @@ enum ECPGttype ECPGt_bool, ECPGt_float, ECPGt_double, ECPGt_varchar, ECPGt_varchar2, - ECPGt_numeric, /* this is a decimal that stores its digits in a malloced array */ - ECPGt_decimal, /* this is a decimal that stores its digits in a fixed array */ + ECPGt_numeric, /* this is a decimal that stores its + * digits in a malloced array */ + ECPGt_decimal, /* this is a decimal that stores its + * digits in a fixed array */ ECPGt_date, ECPGt_timestamp, ECPGt_interval, ECPGt_array, ECPGt_struct, ECPGt_union, - ECPGt_descriptor, /* sql descriptor, no C variable */ + ECPGt_descriptor, /* sql descriptor, no C variable */ ECPGt_char_variable, - ECPGt_const, /* a constant is needed sometimes */ - ECPGt_EOIT, /* End of insert types. */ - ECPGt_EORT, /* End of result types. */ - ECPGt_NO_INDICATOR /* no indicator */ + ECPGt_const, /* a constant is needed sometimes */ + ECPGt_EOIT, /* End of insert types. */ + ECPGt_EORT, /* End of result types. */ + ECPGt_NO_INDICATOR /* no indicator */ }; /* descriptor items */ @@ -88,4 +90,4 @@ enum ECPGdtype } #endif -#endif /* _ECPGTYPE_H */ +#endif /* _ECPGTYPE_H */ diff --git a/src/interfaces/ecpg/include/pgtypes_date.h b/src/interfaces/ecpg/include/pgtypes_date.h index 190a9de703a..391eeedce8c 100644 --- a/src/interfaces/ecpg/include/pgtypes_date.h +++ b/src/interfaces/ecpg/include/pgtypes_date.h @@ -8,10 +8,11 @@ extern Date PGTYPESdate_from_asc(char *, char **); extern char *PGTYPESdate_to_asc(Date); extern Date PGTYPESdate_from_timestamp(Timestamp); -extern void PGTYPESdate_julmdy(Date, int*); -extern void PGTYPESdate_mdyjul(int*, Date *); -extern int PGTYPESdate_dayofweek(Date); -extern void PGTYPESdate_today (Date *); -extern int PGTYPESdate_defmt_asc(Date *, char *, char *); -extern int PGTYPESdate_fmt_asc(Date, char *, char *); -#endif /* PGTYPES_DATETIME */ +extern void PGTYPESdate_julmdy(Date, int *); +extern void PGTYPESdate_mdyjul(int *, Date *); +extern int PGTYPESdate_dayofweek(Date); +extern void PGTYPESdate_today(Date *); +extern int PGTYPESdate_defmt_asc(Date *, char *, char *); +extern int PGTYPESdate_fmt_asc(Date, char *, char *); + +#endif /* PGTYPES_DATETIME */ diff --git a/src/interfaces/ecpg/include/pgtypes_error.h b/src/interfaces/ecpg/include/pgtypes_error.h index e5a102ca98d..4a1008adc33 100644 --- a/src/interfaces/ecpg/include/pgtypes_error.h +++ b/src/interfaces/ecpg/include/pgtypes_error.h @@ -1,4 +1,4 @@ -#define PGTYPES_NUM_OVERFLOW 301 +#define PGTYPES_NUM_OVERFLOW 301 #define PGTYPES_NUM_BAD_NUMERIC 302 #define PGTYPES_NUM_DIVIDE_ZERO 303 @@ -10,7 +10,6 @@ #define PGTYPES_DATE_BAD_MONTH 315 #define PGTYPES_TS_BAD_TIMESTAMP 320 -#define PGTYPES_TS_ERR_EINFTIME 321 +#define PGTYPES_TS_ERR_EINFTIME 321 #define PGTYPES_INTVL_BAD_INTERVAL 330 - diff --git a/src/interfaces/ecpg/include/pgtypes_interval.h b/src/interfaces/ecpg/include/pgtypes_interval.h index 884df67216a..4e1fa6e10b2 100644 --- a/src/interfaces/ecpg/include/pgtypes_interval.h +++ b/src/interfaces/ecpg/include/pgtypes_interval.h @@ -4,15 +4,18 @@ typedef struct { #ifdef HAVE_INT64_TIMESTAMP - int64 time; /* all time units other than months and years */ + int64 time; /* all time units other than months and + * years */ #else - double time; /* all time units other than months and years */ + double time; /* all time units other than months and + * years */ #endif - long month; /* months and years, after time for alignment */ + long month; /* months and years, after time for + * alignment */ } Interval; extern Interval *PGTYPESinterval_from_asc(char *, char **); extern char *PGTYPESinterval_to_asc(Interval *); -extern int PGTYPESinterval_copy(Interval *, Interval *); - -#endif /* PGTYPES_INTERVAL */ +extern int PGTYPESinterval_copy(Interval *, Interval *); + +#endif /* PGTYPES_INTERVAL */ diff --git a/src/interfaces/ecpg/include/pgtypes_numeric.h b/src/interfaces/ecpg/include/pgtypes_numeric.h index 28b902f290d..55d2924e593 100644 --- a/src/interfaces/ecpg/include/pgtypes_numeric.h +++ b/src/interfaces/ecpg/include/pgtypes_numeric.h @@ -1,55 +1,59 @@ #ifndef PGTYPES_NUMERIC #define PGTYPES_NUMERIC -#define NUMERIC_POS 0x0000 -#define NUMERIC_NEG 0x4000 -#define NUMERIC_NAN 0xC000 -#define NUMERIC_MAX_PRECISION 1000 -#define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION -#define NUMERIC_MIN_DISPLAY_SCALE 0 -#define NUMERIC_MIN_SIG_DIGITS 16 +#define NUMERIC_POS 0x0000 +#define NUMERIC_NEG 0x4000 +#define NUMERIC_NAN 0xC000 +#define NUMERIC_MAX_PRECISION 1000 +#define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION +#define NUMERIC_MIN_DISPLAY_SCALE 0 +#define NUMERIC_MIN_SIG_DIGITS 16 #define DECSIZE 30 typedef unsigned char NumericDigit; -typedef struct +typedef struct { - int ndigits; /* number of digits in digits[] - can be 0! */ - int weight; /* weight of first digit */ - int rscale; /* result scale */ - int dscale; /* display scale */ - int sign; /* NUMERIC_POS, NUMERIC_NEG, or NUMERIC_NAN */ - NumericDigit *buf; /* start of alloc'd space for digits[] */ - NumericDigit *digits; /* decimal digits */ + int ndigits; /* number of digits in digits[] - can be + * 0! */ + int weight; /* weight of first digit */ + int rscale; /* result scale */ + int dscale; /* display scale */ + int sign; /* NUMERIC_POS, NUMERIC_NEG, or + * NUMERIC_NAN */ + NumericDigit *buf; /* start of alloc'd space for digits[] */ + NumericDigit *digits; /* decimal digits */ } Numeric; -typedef struct +typedef struct { - int ndigits; /* number of digits in digits[] - can be 0! */ - int weight; /* weight of first digit */ - int rscale; /* result scale */ - int dscale; /* display scale */ - int sign; /* NUMERIC_POS, NUMERIC_NEG, or NUMERIC_NAN */ - NumericDigit digits[DECSIZE]; /* decimal digits */ -} Decimal; + int ndigits; /* number of digits in digits[] - can be + * 0! */ + int weight; /* weight of first digit */ + int rscale; /* result scale */ + int dscale; /* display scale */ + int sign; /* NUMERIC_POS, NUMERIC_NEG, or + * NUMERIC_NAN */ + NumericDigit digits[DECSIZE]; /* decimal digits */ +} Decimal; -Numeric *PGTYPESnumeric_new(void); -void PGTYPESnumeric_free(Numeric *); -Numeric *PGTYPESnumeric_from_asc(char *, char **); -char *PGTYPESnumeric_to_asc(Numeric *, int); -int PGTYPESnumeric_add(Numeric *, Numeric *, Numeric *); -int PGTYPESnumeric_sub(Numeric *, Numeric *, Numeric *); -int PGTYPESnumeric_mul(Numeric *, Numeric *, Numeric *); -int PGTYPESnumeric_div(Numeric *, Numeric *, Numeric *); -int PGTYPESnumeric_cmp(Numeric *, Numeric *); -int PGTYPESnumeric_from_int(signed int, Numeric *); -int PGTYPESnumeric_from_long(signed long int, Numeric *); -int PGTYPESnumeric_copy(Numeric *, Numeric *); -int PGTYPESnumeric_from_double(double, Numeric *); -int PGTYPESnumeric_to_double(Numeric *, double *); -int PGTYPESnumeric_to_int(Numeric *, int *); -int PGTYPESnumeric_to_long(Numeric *, long *); -int PGTYPESnumeric_to_decimal(Numeric *, Decimal *); -int PGTYPESnumeric_from_decimal(Decimal *, Numeric *); +Numeric *PGTYPESnumeric_new(void); +void PGTYPESnumeric_free(Numeric *); +Numeric *PGTYPESnumeric_from_asc(char *, char **); +char *PGTYPESnumeric_to_asc(Numeric *, int); +int PGTYPESnumeric_add(Numeric *, Numeric *, Numeric *); +int PGTYPESnumeric_sub(Numeric *, Numeric *, Numeric *); +int PGTYPESnumeric_mul(Numeric *, Numeric *, Numeric *); +int PGTYPESnumeric_div(Numeric *, Numeric *, Numeric *); +int PGTYPESnumeric_cmp(Numeric *, Numeric *); +int PGTYPESnumeric_from_int(signed int, Numeric *); +int PGTYPESnumeric_from_long(signed long int, Numeric *); +int PGTYPESnumeric_copy(Numeric *, Numeric *); +int PGTYPESnumeric_from_double(double, Numeric *); +int PGTYPESnumeric_to_double(Numeric *, double *); +int PGTYPESnumeric_to_int(Numeric *, int *); +int PGTYPESnumeric_to_long(Numeric *, long *); +int PGTYPESnumeric_to_decimal(Numeric *, Decimal *); +int PGTYPESnumeric_from_decimal(Decimal *, Numeric *); -#endif /* PGTYPES_NUMERIC */ +#endif /* PGTYPES_NUMERIC */ diff --git a/src/interfaces/ecpg/include/pgtypes_timestamp.h b/src/interfaces/ecpg/include/pgtypes_timestamp.h index 9294e1d77a0..f1aabcbea2b 100644 --- a/src/interfaces/ecpg/include/pgtypes_timestamp.h +++ b/src/interfaces/ecpg/include/pgtypes_timestamp.h @@ -14,9 +14,9 @@ typedef double TimestampTz; extern Timestamp PGTYPEStimestamp_from_asc(char *, char **); extern char *PGTYPEStimestamp_to_asc(Timestamp); -extern int PGTYPEStimestamp_sub (Timestamp *, Timestamp *, Interval *); -extern int PGTYPEStimestamp_fmt_asc (Timestamp *, char *, int, char *); -extern void PGTYPEStimestamp_current (Timestamp *); -extern int PGTYPEStimestamp_defmt_asc(char *, char *, Timestamp *); +extern int PGTYPEStimestamp_sub(Timestamp *, Timestamp *, Interval *); +extern int PGTYPEStimestamp_fmt_asc(Timestamp *, char *, int, char *); +extern void PGTYPEStimestamp_current(Timestamp *); +extern int PGTYPEStimestamp_defmt_asc(char *, char *, Timestamp *); -#endif /* PGTYPES_TIMESTAMP */ +#endif /* PGTYPES_TIMESTAMP */ diff --git a/src/interfaces/ecpg/include/sqlca.h b/src/interfaces/ecpg/include/sqlca.h index 37370b6f605..8d58d3dfd36 100644 --- a/src/interfaces/ecpg/include/sqlca.h +++ b/src/interfaces/ecpg/include/sqlca.h @@ -56,7 +56,7 @@ struct sqlca_t struct sqlca_t *ECPGget_sqlca(void); #ifndef POSTGRES_ECPG_INTERNAL -# define sqlca (*ECPGget_sqlca()) +#define sqlca (*ECPGget_sqlca()) #endif #ifdef __cplusplus diff --git a/src/interfaces/ecpg/include/sqlda.h b/src/interfaces/ecpg/include/sqlda.h index e69de29bb2d..8b137891791 100644 --- a/src/interfaces/ecpg/include/sqlda.h +++ b/src/interfaces/ecpg/include/sqlda.h @@ -0,0 +1 @@ + diff --git a/src/interfaces/ecpg/include/sqltypes.h b/src/interfaces/ecpg/include/sqltypes.h index c50bf54314b..1ebde04714e 100644 --- a/src/interfaces/ecpg/include/sqltypes.h +++ b/src/interfaces/ecpg/include/sqltypes.h @@ -3,23 +3,23 @@ #define CINTTYPE ECPGt_int #define CLONGTYPE ECPGt_long #define CFLOATTYPE ECPGt_float -#define CDOUBLETYPE ECPGt_double +#define CDOUBLETYPE ECPGt_double #define CDECIMALTYPE ECPGt_decimal #define CFIXCHARTYPE 108 -#define CSTRINGTYPE ECPGt_char +#define CSTRINGTYPE ECPGt_char #define CDATETYPE ECPGt_date #define CMONEYTYPE 111 #define CDTIMETYPE 112 -#define CLOCATORTYPE 113 -#define CVCHARTYPE ECPGt_varchar +#define CLOCATORTYPE 113 +#define CVCHARTYPE ECPGt_varchar #define CINVTYPE 115 #define CFILETYPE 116 #define CINT8TYPE ECPGt_long_long -#define CCOLLTYPE 118 -#define CLVCHARTYPE 119 -#define CFIXBINTYPE 120 -#define CVARBINTYPE 121 -#define CBOOLTYPE ECPGt_bool -#define CROWTYPE 123 -#define CLVCHARPTRTYPE 124 +#define CCOLLTYPE 118 +#define CLVCHARTYPE 119 +#define CFIXBINTYPE 120 +#define CVARBINTYPE 121 +#define CBOOLTYPE ECPGt_bool +#define CROWTYPE 123 +#define CLVCHARPTRTYPE 124 #define CTYPEMAX 25 diff --git a/src/interfaces/ecpg/pgtypeslib/common.c b/src/interfaces/ecpg/pgtypeslib/common.c index f9762c8ae34..986d8fc3f00 100644 --- a/src/interfaces/ecpg/pgtypeslib/common.c +++ b/src/interfaces/ecpg/pgtypeslib/common.c @@ -5,7 +5,7 @@ char * pgtypes_alloc(long size) { - char *new = (char *) calloc(1L, size); + char *new = (char *) calloc(1L, size); if (!new) { @@ -20,7 +20,7 @@ pgtypes_alloc(long size) char * pgtypes_strdup(char *str) { - char *new = (char *) strdup(str); + char *new = (char *) strdup(str); if (!new) errno = ENOMEM; @@ -28,40 +28,47 @@ pgtypes_strdup(char *str) } int -pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char** output, int *pstr_len) { - /* general purpose variable, set to 0 in order to fix compiler - * warning */ - int i = 0; - switch(replace_type) { +pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char **output, int *pstr_len) +{ + /* + * general purpose variable, set to 0 in order to fix compiler warning + */ + int i = 0; + + switch (replace_type) + { case PGTYPES_TYPE_NOTHING: break; case PGTYPES_TYPE_STRING_CONSTANT: case PGTYPES_TYPE_STRING_MALLOCED: i = strlen(replace_val.str_val); - if (i + 1 <= *pstr_len) { - /* copy over i + 1 bytes, that includes the - * tailing terminator */ + if (i + 1 <= *pstr_len) + { + /* + * copy over i + 1 bytes, that includes the tailing + * terminator + */ strncpy(*output, replace_val.str_val, i + 1); *pstr_len -= i; *output += i; - if (replace_type == PGTYPES_TYPE_STRING_MALLOCED) { + if (replace_type == PGTYPES_TYPE_STRING_MALLOCED) free(replace_val.str_val); - } return 0; - } else { - return -1; } + else + return -1; break; case PGTYPES_TYPE_CHAR: - if (*pstr_len >= 2) { + if (*pstr_len >= 2) + { (*output)[0] = replace_val.char_val; (*output)[1] = '\0'; (*pstr_len)--; (*output)++; return 0; - } else { - return -1; } + else + return -1; break; case PGTYPES_TYPE_DOUBLE_NF: case PGTYPES_TYPE_INT64: @@ -71,53 +78,58 @@ pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char** outp case PGTYPES_TYPE_UINT_3_LZ: case PGTYPES_TYPE_UINT_4_LZ: { - char* t = pgtypes_alloc(PGTYPES_FMT_NUM_MAX_DIGITS); - if (!t) { + char *t = pgtypes_alloc(PGTYPES_FMT_NUM_MAX_DIGITS); + + if (!t) return ENOMEM; - } - switch (replace_type) { + switch (replace_type) + { case PGTYPES_TYPE_DOUBLE_NF: i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - "%0.0g", replace_val.double_val); + "%0.0g", replace_val.double_val); break; #ifdef HAVE_INT64 case PGTYPES_TYPE_INT64: i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - INT64_FORMAT, replace_val.int64_val); + INT64_FORMAT, replace_val.int64_val); break; #endif case PGTYPES_TYPE_UINT: - i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - "%u", replace_val.uint_val); + i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, + "%u", replace_val.uint_val); break; case PGTYPES_TYPE_UINT_2_LZ: - i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - "%02u", replace_val.uint_val); + i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, + "%02u", replace_val.uint_val); break; case PGTYPES_TYPE_UINT_2_LS: - i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - "%2u", replace_val.uint_val); + i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, + "%2u", replace_val.uint_val); break; case PGTYPES_TYPE_UINT_3_LZ: - i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - "%03u", replace_val.uint_val); + i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, + "%03u", replace_val.uint_val); break; case PGTYPES_TYPE_UINT_4_LZ: - i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, - "%04u", replace_val.uint_val); + i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS, + "%04u", replace_val.uint_val); break; } - if (i < 0) { + if (i < 0) + { free(t); return -1; } i = strlen(t); *pstr_len -= i; - /* if *pstr_len == 0, we don't have enough - * space for the terminator and the - * conversion fails */ - if (*pstr_len <= 0) { + + /* + * if *pstr_len == 0, we don't have enough space for the + * terminator and the conversion fails + */ + if (*pstr_len <= 0) + { free(t); return -1; } @@ -131,5 +143,3 @@ pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char** outp } return 0; } - - diff --git a/src/interfaces/ecpg/pgtypeslib/datetime.c b/src/interfaces/ecpg/pgtypeslib/datetime.c index 789f8400fa7..34ed888b66a 100644 --- a/src/interfaces/ecpg/pgtypeslib/datetime.c +++ b/src/interfaces/ecpg/pgtypeslib/datetime.c @@ -14,17 +14,17 @@ PGTYPESdate_from_timestamp(Timestamp dt) { Date dDate; - dDate = 0; /* suppress compiler warning */ + dDate = 0; /* suppress compiler warning */ if (TIMESTAMP_NOT_FINITE(dt)) - return + return #ifdef HAVE_INT64_TIMESTAMP - /* Microseconds to days */ - dDate = (dt / INT64CONST(86400000000)); + /* Microseconds to days */ + dDate = (dt / INT64CONST(86400000000)); #else - /* Seconds to days */ - dDate = (dt / 86400.0); + /* Seconds to days */ + dDate = (dt / 86400.0); #endif return dDate; @@ -33,7 +33,7 @@ PGTYPESdate_from_timestamp(Timestamp dt) Date PGTYPESdate_from_asc(char *str, char **endptr) { - + Date dDate; fsec_t fsec; struct tm tt, @@ -44,9 +44,9 @@ PGTYPESdate_from_asc(char *str, char **endptr) char *field[MAXDATEFIELDS]; int ftype[MAXDATEFIELDS]; char lowstr[MAXDATELEN + 1]; - char *realptr; - char **ptr = (endptr != NULL) ? endptr : &realptr; - + char *realptr; + char **ptr = (endptr != NULL) ? endptr : &realptr; + bool EuroDates = FALSE; errno = 0; @@ -57,7 +57,7 @@ PGTYPESdate_from_asc(char *str, char **endptr) } if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf, ptr) != 0) - || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp, EuroDates) != 0)) + || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tzp, EuroDates) != 0)) { errno = PGTYPES_DATE_BAD_DATE; return 0; @@ -69,7 +69,7 @@ PGTYPESdate_from_asc(char *str, char **endptr) break; case DTK_EPOCH: - GetEpochTime(tm); + GetEpochTime(tm); break; default: @@ -85,21 +85,24 @@ PGTYPESdate_from_asc(char *str, char **endptr) char * PGTYPESdate_to_asc(Date dDate) { - struct tm tt, *tm = &tt; - char buf[MAXDATELEN + 1]; - int DateStyle=1; + struct tm tt, + *tm = &tt; + char buf[MAXDATELEN + 1]; + int DateStyle = 1; bool EuroDates = FALSE; - + j2date((dDate + date2j(2000, 1, 1)), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday)); EncodeDateOnly(tm, DateStyle, buf, EuroDates); return pgtypes_strdup(buf); } void -PGTYPESdate_julmdy(Date jd, int* mdy) +PGTYPESdate_julmdy(Date jd, int *mdy) { - int y, m, d; - + int y, + m, + d; + j2date((int) (jd + date2j(2000, 1, 1)), &y, &m, &d); mdy[0] = m; mdy[1] = d; @@ -107,10 +110,10 @@ PGTYPESdate_julmdy(Date jd, int* mdy) } void -PGTYPESdate_mdyjul(int* mdy, Date *jdate) +PGTYPESdate_mdyjul(int *mdy, Date * jdate) { /* month is mdy[0] */ - /* day is mdy[1] */ + /* day is mdy[1] */ /* year is mdy[2] */ *jdate = (Date) (date2j(mdy[2], mdy[0], mdy[1]) - date2j(2000, 1, 1)); @@ -120,19 +123,14 @@ int PGTYPESdate_dayofweek(Date dDate) { /* - Sunday: 0 - Monday: 1 - Tuesday: 2 - Wednesday: 3 - Thursday: 4 - Friday: 5 - Saturday: 6 - */ - return 6-j2day(dDate+3); + * Sunday: 0 Monday: 1 Tuesday: 2 Wednesday: 3 + * Thursday: 4 Friday: 5 Saturday: 6 + */ + return 6 - j2day(dDate + 3); } void -PGTYPESdate_today (Date *d) +PGTYPESdate_today(Date * d) { struct tm ts; @@ -141,40 +139,60 @@ PGTYPESdate_today (Date *d) return; } -#define PGTYPES_DATE_NUM_MAX_DIGITS 20 /* should suffice for most years... */ +#define PGTYPES_DATE_NUM_MAX_DIGITS 20 /* should suffice for most + * years... */ -#define PGTYPES_FMTDATE_DAY_DIGITS_LZ 1 /* LZ means "leading zeroes" */ +#define PGTYPES_FMTDATE_DAY_DIGITS_LZ 1 /* LZ means "leading + * zeroes" */ #define PGTYPES_FMTDATE_DOW_LITERAL_SHORT 2 #define PGTYPES_FMTDATE_MONTH_DIGITS_LZ 3 -#define PGTYPES_FMTDATE_MONTH_LITERAL_SHORT 4 +#define PGTYPES_FMTDATE_MONTH_LITERAL_SHORT 4 #define PGTYPES_FMTDATE_YEAR_DIGITS_SHORT 5 #define PGTYPES_FMTDATE_YEAR_DIGITS_LONG 6 int -PGTYPESdate_fmt_asc(Date dDate, char* fmtstring, char* outbuf) { - static struct { - char* format; - int component; - } mapping[] = { - /* format items have to be sorted according to their length, - * since the first pattern that matches gets replaced by its - * value */ - {"ddd", PGTYPES_FMTDATE_DOW_LITERAL_SHORT }, - {"dd", PGTYPES_FMTDATE_DAY_DIGITS_LZ }, - {"mmm", PGTYPES_FMTDATE_MONTH_LITERAL_SHORT }, - {"mm", PGTYPES_FMTDATE_MONTH_DIGITS_LZ }, - {"yyyy", PGTYPES_FMTDATE_YEAR_DIGITS_LONG }, - {"yy", PGTYPES_FMTDATE_YEAR_DIGITS_SHORT }, - { NULL, 0 } +PGTYPESdate_fmt_asc(Date dDate, char *fmtstring, char *outbuf) +{ + static struct + { + char *format; + int component; + } mapping[] = + { + /* + * format items have to be sorted according to their length, since + * the first pattern that matches gets replaced by its value + */ + { + "ddd", PGTYPES_FMTDATE_DOW_LITERAL_SHORT + }, + { + "dd", PGTYPES_FMTDATE_DAY_DIGITS_LZ + }, + { + "mmm", PGTYPES_FMTDATE_MONTH_LITERAL_SHORT + }, + { + "mm", PGTYPES_FMTDATE_MONTH_DIGITS_LZ + }, + { + "yyyy", PGTYPES_FMTDATE_YEAR_DIGITS_LONG + }, + { + "yy", PGTYPES_FMTDATE_YEAR_DIGITS_SHORT + }, + { + NULL, 0 + } }; union un_fmt_comb replace_val; - int replace_type; + int replace_type; - int i; - int dow; - char* start_pattern; - struct tm tm; + int i; + int dow; + char *start_pattern; + struct tm tm; /* XXX error handling ? */ /* copy the string over */ @@ -184,9 +202,12 @@ PGTYPESdate_fmt_asc(Date dDate, char* fmtstring, char* outbuf) { j2date((dDate + date2j(2000, 1, 1)), &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday)); dow = PGTYPESdate_dayofweek(dDate); - for (i = 0; mapping[i].format != NULL; i++) { - while ((start_pattern = strstr(outbuf, mapping[i].format)) != NULL) { - switch(mapping[i].component) { + for (i = 0; mapping[i].format != NULL; i++) + { + while ((start_pattern = strstr(outbuf, mapping[i].format)) != NULL) + { + switch (mapping[i].component) + { case PGTYPES_FMTDATE_DOW_LITERAL_SHORT: replace_val.str_val = pgtypes_date_weekdays_short[dow]; replace_type = PGTYPES_TYPE_STRING_CONSTANT; @@ -196,7 +217,7 @@ PGTYPESdate_fmt_asc(Date dDate, char* fmtstring, char* outbuf) { replace_type = PGTYPES_TYPE_UINT_2_LZ; break; case PGTYPES_FMTDATE_MONTH_LITERAL_SHORT: - replace_val.str_val = months[tm.tm_mon-1]; + replace_val.str_val = months[tm.tm_mon - 1]; replace_type = PGTYPES_TYPE_STRING_CONSTANT; break; case PGTYPES_FMTDATE_MONTH_DIGITS_LZ: @@ -212,61 +233,65 @@ PGTYPESdate_fmt_asc(Date dDate, char* fmtstring, char* outbuf) { replace_type = PGTYPES_TYPE_UINT_2_LZ; break; default: - /* should not happen, set something - * anyway */ + + /* + * should not happen, set something anyway + */ replace_val.str_val = " "; replace_type = PGTYPES_TYPE_STRING_CONSTANT; } - switch(replace_type) { + switch (replace_type) + { case PGTYPES_TYPE_STRING_MALLOCED: case PGTYPES_TYPE_STRING_CONSTANT: strncpy(start_pattern, replace_val.str_val, strlen(replace_val.str_val)); - if (replace_type == PGTYPES_TYPE_STRING_MALLOCED) { + if (replace_type == PGTYPES_TYPE_STRING_MALLOCED) free(replace_val.str_val); - } break; case PGTYPES_TYPE_UINT: { - char* t = pgtypes_alloc(PGTYPES_DATE_NUM_MAX_DIGITS); - if (!t) { + char *t = pgtypes_alloc(PGTYPES_DATE_NUM_MAX_DIGITS); + + if (!t) return -1; - } snprintf(t, PGTYPES_DATE_NUM_MAX_DIGITS, - "%u", replace_val.uint_val); + "%u", replace_val.uint_val); strncpy(start_pattern, t, strlen(t)); free(t); } break; case PGTYPES_TYPE_UINT_2_LZ: { - char* t = pgtypes_alloc(PGTYPES_DATE_NUM_MAX_DIGITS); - if (!t) { + char *t = pgtypes_alloc(PGTYPES_DATE_NUM_MAX_DIGITS); + + if (!t) return -1; - } snprintf(t, PGTYPES_DATE_NUM_MAX_DIGITS, - "%02u", replace_val.uint_val); + "%02u", replace_val.uint_val); strncpy(start_pattern, t, strlen(t)); free(t); } break; case PGTYPES_TYPE_UINT_4_LZ: { - char* t = pgtypes_alloc(PGTYPES_DATE_NUM_MAX_DIGITS); - if (!t) { + char *t = pgtypes_alloc(PGTYPES_DATE_NUM_MAX_DIGITS); + + if (!t) return -1; - } snprintf(t, PGTYPES_DATE_NUM_MAX_DIGITS, - "%04u", replace_val.uint_val); + "%04u", replace_val.uint_val); strncpy(start_pattern, t, strlen(t)); free(t); } break; default: - /* doesn't happen (we set - * replace_type to - * PGTYPES_TYPE_STRING_CONSTANT - * in case of an error above) */ + + /* + * doesn't happen (we set replace_type to + * PGTYPES_TYPE_STRING_CONSTANT in case of an error + * above) + */ break; } } @@ -279,32 +304,37 @@ PGTYPESdate_fmt_asc(Date dDate, char* fmtstring, char* outbuf) { * PGTYPESdate_defmt_asc * * function works as follows: - * - first we analyze the paramters - * - if this is a special case with no delimiters, add delimters - * - find the tokens. First we look for numerical values. If we have found - * less than 3 tokens, we check for the months' names and thereafter for - * the abbreviations of the months' names. - * - then we see which parameter should be the date, the month and the - * year and from these values we calculate the date + * - first we analyze the paramters + * - if this is a special case with no delimiters, add delimters + * - find the tokens. First we look for numerical values. If we have found + * less than 3 tokens, we check for the months' names and thereafter for + * the abbreviations of the months' names. + * - then we see which parameter should be the date, the month and the + * year and from these values we calculate the date */ -#define PGTYPES_DATE_MONTH_MAXLENGTH 20 /* probably even less :-) */ +#define PGTYPES_DATE_MONTH_MAXLENGTH 20 /* probably even less :-) */ int -PGTYPESdate_defmt_asc(Date *d, char *fmt, char *str) +PGTYPESdate_defmt_asc(Date * d, char *fmt, char *str) { - /* token[2] = { 4,6 } means that token 2 starts at - * position 4 and ends at (including) position 6 */ - int token[3][2]; - int token_values[3] = { -1, -1, -1 }; - char* fmt_token_order; - char* fmt_ystart, *fmt_mstart, *fmt_dstart; - int i; - int reading_digit; - int token_count; - char* str_copy; - struct tm tm; - - if (!d || !str || !fmt) { + /* + * token[2] = { 4,6 } means that token 2 starts at position 4 and ends + * at (including) position 6 + */ + int token[3][2]; + int token_values[3] = {-1, -1, -1}; + char *fmt_token_order; + char *fmt_ystart, + *fmt_mstart, + *fmt_dstart; + int i; + int reading_digit; + int token_count; + char *str_copy; + struct tm tm; + + if (!d || !str || !fmt) + { errno = PGTYPES_DATE_ERR_EARGS; return -1; } @@ -314,229 +344,305 @@ PGTYPESdate_defmt_asc(Date *d, char *fmt, char *str) fmt_mstart = strstr(fmt, "mm"); fmt_dstart = strstr(fmt, "dd"); - if (!fmt_ystart || !fmt_mstart || !fmt_dstart) { + if (!fmt_ystart || !fmt_mstart || !fmt_dstart) + { errno = PGTYPES_DATE_ERR_EARGS; return -1; } - if (fmt_ystart < fmt_mstart) { + if (fmt_ystart < fmt_mstart) + { /* y m */ - if (fmt_dstart < fmt_ystart) { + if (fmt_dstart < fmt_ystart) + { /* d y m */ fmt_token_order = "dym"; - } else if (fmt_dstart > fmt_mstart) { + } + else if (fmt_dstart > fmt_mstart) + { /* y m d */ fmt_token_order = "ymd"; - } else { + } + else + { /* y d m */ fmt_token_order = "ydm"; } - } else { + } + else + { /* fmt_ystart > fmt_mstart */ /* m y */ - if (fmt_dstart < fmt_mstart) { + if (fmt_dstart < fmt_mstart) + { /* d m y */ fmt_token_order = "dmy"; - } else if (fmt_dstart > fmt_ystart) { + } + else if (fmt_dstart > fmt_ystart) + { /* m y d */ fmt_token_order = "myd"; - } else { + } + else + { /* m d y */ fmt_token_order = "mdy"; } } - /* handle the special cases where there is no delimiter between the + /* + * handle the special cases where there is no delimiter between the * digits. If we see this: * - * only digits, 6 or 8 bytes then it might be ddmmyy and ddmmyyyy - * (or similar) + * only digits, 6 or 8 bytes then it might be ddmmyy and ddmmyyyy (or + * similar) * * we reduce it to a string with delimiters and continue processing - * */ + */ /* check if we have only digits */ reading_digit = 1; - for (i = 0; str[i]; i++) { - if (!isdigit(str[i])) { + for (i = 0; str[i]; i++) + { + if (!isdigit(str[i])) + { reading_digit = 0; break; } } - if (reading_digit) { - int frag_length[3]; - int target_pos; + if (reading_digit) + { + int frag_length[3]; + int target_pos; i = strlen(str); - if (i != 8 && i != 6) { + if (i != 8 && i != 6) + { errno = PGTYPES_DATE_ERR_ENOSHORTDATE; return -1; } /* okay, this really is the special case */ - /* as long as the string, one additional byte for the - * terminator and 2 for the delimiters between the 3 fiedls - * */ + /* + * as long as the string, one additional byte for the terminator + * and 2 for the delimiters between the 3 fiedls + */ str_copy = pgtypes_alloc(strlen(str) + 1 + 2); - if (!str_copy) { + if (!str_copy) return -1; - } /* determine length of the fragments */ - if (i == 6) { - frag_length[0] = 2; frag_length[1] = 2; frag_length[2] = 2; - } else { - if (fmt_token_order[0] == 'y') { - frag_length[0] = 4; frag_length[1] = 2; frag_length[2] = 2; - } else if (fmt_token_order[1] == 'y') { - frag_length[0] = 2; frag_length[1] = 4; frag_length[2] = 2; - } else { - frag_length[0] = 2; frag_length[1] = 2; frag_length[2] = 4; + if (i == 6) + { + frag_length[0] = 2; + frag_length[1] = 2; + frag_length[2] = 2; + } + else + { + if (fmt_token_order[0] == 'y') + { + frag_length[0] = 4; + frag_length[1] = 2; + frag_length[2] = 2; + } + else if (fmt_token_order[1] == 'y') + { + frag_length[0] = 2; + frag_length[1] = 4; + frag_length[2] = 2; + } + else + { + frag_length[0] = 2; + frag_length[1] = 2; + frag_length[2] = 4; } } target_pos = 0; - /* XXX: Here we could calculate the positions of the tokens - * and save the for loop down there where we again check - * with isdigit() for digits. */ - for (i = 0; i < 3; i++) { - int start_pos = 0; - if (i >= 1) { start_pos += frag_length[0]; } - if (i == 2) { start_pos += frag_length[1]; } + + /* + * XXX: Here we could calculate the positions of the tokens and + * save the for loop down there where we again check with + * isdigit() for digits. + */ + for (i = 0; i < 3; i++) + { + int start_pos = 0; + + if (i >= 1) + start_pos += frag_length[0]; + if (i == 2) + start_pos += frag_length[1]; strncpy(str_copy + target_pos, str + start_pos, - frag_length[i]); + frag_length[i]); target_pos += frag_length[i]; - if (i != 2) { + if (i != 2) + { str_copy[target_pos] = ' '; target_pos++; } } str_copy[target_pos] = '\0'; - } else { + } + else + { str_copy = pgtypes_strdup(str); - if (!str_copy) { + if (!str_copy) return -1; - } /* convert the whole string to lower case */ - for (i = 0; str_copy[i]; i++) { + for (i = 0; str_copy[i]; i++) str_copy[i] = (char) tolower(str_copy[i]); - } } /* look for numerical tokens */ reading_digit = 0; - token_count = 0; - for (i = 0; i < strlen(str_copy); i++) { - if (!isdigit(str_copy[i]) && reading_digit) { + token_count = 0; + for (i = 0; i < strlen(str_copy); i++) + { + if (!isdigit(str_copy[i]) && reading_digit) + { /* the token is finished */ - token[token_count][1] = i-1; + token[token_count][1] = i - 1; reading_digit = 0; token_count++; - } else if (isdigit(str_copy[i]) && !reading_digit) { + } + else if (isdigit(str_copy[i]) && !reading_digit) + { /* we have found a token */ token[token_count][0] = i; reading_digit = 1; } } - /* we're at the end of the input string, but maybe we are still reading a - * number... */ - if (reading_digit) { - token[token_count][1] = i-1; + + /* + * we're at the end of the input string, but maybe we are still + * reading a number... + */ + if (reading_digit) + { + token[token_count][1] = i - 1; token_count++; } - if (token_count < 2) { - /* not all tokens found, no way to find 2 missing tokens - * with string matches */ + if (token_count < 2) + { + /* + * not all tokens found, no way to find 2 missing tokens with + * string matches + */ free(str_copy); errno = PGTYPES_DATE_ERR_ENOTDMY; return -1; } - if (token_count != 3) { - /* not all tokens found but we may find another one with - * string matches by testing for the months names and months - * abbreviations */ - char *month_lower_tmp = pgtypes_alloc(PGTYPES_DATE_MONTH_MAXLENGTH); - char *start_pos; - int j; - int offset; - int found = 0; - char** list; - - if (!month_lower_tmp) { + if (token_count != 3) + { + /* + * not all tokens found but we may find another one with string + * matches by testing for the months names and months + * abbreviations + */ + char *month_lower_tmp = pgtypes_alloc(PGTYPES_DATE_MONTH_MAXLENGTH); + char *start_pos; + int j; + int offset; + int found = 0; + char **list; + + if (!month_lower_tmp) + { /* free variables we alloc'ed before */ free(str_copy); return -1; } list = pgtypes_date_months; - for (i = 0; list[i]; i++) { - for (j = 0; j < PGTYPES_DATE_MONTH_MAXLENGTH; j++) { + for (i = 0; list[i]; i++) + { + for (j = 0; j < PGTYPES_DATE_MONTH_MAXLENGTH; j++) + { month_lower_tmp[j] = (char) tolower(list[i][j]); - if (!month_lower_tmp[j]) { + if (!month_lower_tmp[j]) + { /* properly terminated */ break; } } - if ((start_pos = strstr(str_copy, month_lower_tmp))) { + if ((start_pos = strstr(str_copy, month_lower_tmp))) + { offset = start_pos - str_copy; - /* sort the new token into the numeric - * tokens, shift them if necessary */ - if (offset < token[0][0]) { + + /* + * sort the new token into the numeric tokens, shift them + * if necessary + */ + if (offset < token[0][0]) + { token[2][0] = token[1][0]; token[2][1] = token[1][1]; token[1][0] = token[0][0]; token[1][1] = token[0][1]; token_count = 0; - } else if (offset < token[1][0]) { + } + else if (offset < token[1][0]) + { token[2][0] = token[1][0]; token[2][1] = token[1][1]; token_count = 1; - } else { - token_count = 2; } + else + token_count = 2; token[token_count][0] = offset; token[token_count][1] = offset + strlen(month_lower_tmp) - 1; - /* the value is the index of the month in - * the array of months + 1 (January is month - * 0) */ - token_values[token_count] = i+1; + + /* + * the value is the index of the month in the array of + * months + 1 (January is month 0) + */ + token_values[token_count] = i + 1; found = 1; break; } - /* evil[tm] hack: - * if we read the pgtypes_date_months and haven't - * found a match, reset list to point to - * pgtypes_date_months_short and reset the counter - * variable i */ - if (list == pgtypes_date_months) { - if (list[i+1] == NULL) { + + /* + * evil[tm] hack: if we read the pgtypes_date_months and + * haven't found a match, reset list to point to + * pgtypes_date_months_short and reset the counter variable i + */ + if (list == pgtypes_date_months) + { + if (list[i + 1] == NULL) + { list = months; i = -1; } } } - if (!found) { + if (!found) + { free(month_lower_tmp); free(str_copy); errno = PGTYPES_DATE_ERR_ENOTDMY; return -1; } - /* here we found a month. token[token_count] and + /* + * here we found a month. token[token_count] and * token_values[token_count] reflect the month's details. * * only the month can be specified with a literal. Here we can do a * quick check if the month is at the right position according to - * the format string because we can check if the token that - * we expect to be the month is at the position of the only - * token that already has a value. If we wouldn't check here - * we could say "December 4 1990" with a fmt string of - * "dd mm yy" for 12 April 1990. + * the format string because we can check if the token that we + * expect to be the month is at the position of the only token + * that already has a value. If we wouldn't check here we could + * say "December 4 1990" with a fmt string of "dd mm yy" for 12 + * April 1990. */ - if (fmt_token_order[token_count] != 'm') { + if (fmt_token_order[token_count] != 'm') + { /* deal with the error later on */ token_values[token_count] = -1; } @@ -544,24 +650,24 @@ PGTYPESdate_defmt_asc(Date *d, char *fmt, char *str) } /* terminate the tokens with ASCII-0 and get their values */ - for (i = 0; i < 3; i++) { + for (i = 0; i < 3; i++) + { *(str_copy + token[i][1] + 1) = '\0'; /* A month already has a value set, check for token_value == -1 */ - if (token_values[i] == -1) { + if (token_values[i] == -1) + { errno = 0; token_values[i] = strtol(str_copy + token[i][0], (char **) NULL, 10); /* strtol sets errno in case of an error */ - if (errno) { + if (errno) token_values[i] = -1; - } } - if (fmt_token_order[i] == 'd') { + if (fmt_token_order[i] == 'd') tm.tm_mday = token_values[i]; - } else if (fmt_token_order[i] == 'm') { + else if (fmt_token_order[i] == 'm') tm.tm_mon = token_values[i]; - } else if (fmt_token_order[i] == 'y') { + else if (fmt_token_order[i] == 'y') tm.tm_year = token_values[i]; - } } free(str_copy); @@ -577,7 +683,7 @@ PGTYPESdate_defmt_asc(Date *d, char *fmt, char *str) return -1; } - if (tm.tm_mday == 31 && (tm.tm_mon == 4 || tm.tm_mon == 6 || tm.tm_mon == 9 || tm.tm_mon ==11)) + if (tm.tm_mday == 31 && (tm.tm_mon == 4 || tm.tm_mon == 6 || tm.tm_mon == 9 || tm.tm_mon == 11)) { errno = PGTYPES_DATE_BAD_DAY; return -1; diff --git a/src/interfaces/ecpg/pgtypeslib/dt.h b/src/interfaces/ecpg/pgtypeslib/dt.h index e756c9c72db..ba6a38f2bd8 100644 --- a/src/interfaces/ecpg/pgtypeslib/dt.h +++ b/src/interfaces/ecpg/pgtypeslib/dt.h @@ -3,7 +3,7 @@ #include <pgtypes_timestamp.h> -#define MAXTZLEN 10 +#define MAXTZLEN 10 #ifdef HAVE_INT64_TIMESTAMP @@ -17,10 +17,10 @@ typedef double fsec_t; #define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV) #endif -#define USE_POSTGRES_DATES 0 -#define USE_ISO_DATES 1 -#define USE_SQL_DATES 2 -#define USE_GERMAN_DATES 3 +#define USE_POSTGRES_DATES 0 +#define USE_ISO_DATES 1 +#define USE_SQL_DATES 2 +#define USE_GERMAN_DATES 3 #define DAGO "ago" #define EPOCH "epoch" @@ -49,7 +49,7 @@ typedef double fsec_t; #define DA_D "ad" #define DB_C "bc" #define DTIMEZONE "timezone" -#define DCURRENT "current" +#define DCURRENT "current" /* * Fundamental time field definitions for parsing. @@ -275,13 +275,12 @@ do { \ #define DT_NOBEGIN (-DBL_MAX) #define DT_NOEND (DBL_MAX) #endif - #endif /* HAVE_INT64_TIMESTAMP */ -#define TIMESTAMP_NOBEGIN(j) do {j = DT_NOBEGIN;} while (0) -#define TIMESTAMP_NOEND(j) do {j = DT_NOEND;} while (0) +#define TIMESTAMP_NOBEGIN(j) do {j = DT_NOBEGIN;} while (0) +#define TIMESTAMP_NOEND(j) do {j = DT_NOEND;} while (0) #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN) -#define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND) +#define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND) #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j)) int DecodeTimeOnly(char **field, int *ftype, @@ -292,31 +291,30 @@ int DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct tm * tm, fsec_t *fsec); -int EncodeTimeOnly(struct tm * tm, fsec_t fsec, int *tzp, int style, char *str); -int EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, char *str, bool); -int EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str); +int EncodeTimeOnly(struct tm * tm, fsec_t fsec, int *tzp, int style, char *str); +int EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, char *str, bool); +int EncodeInterval(struct tm * tm, fsec_t fsec, int style, char *str); -int tm2timestamp(struct tm *, fsec_t, int *, Timestamp *); +int tm2timestamp(struct tm *, fsec_t, int *, Timestamp *); -int DecodeUnits(int field, char *lowtoken, int *val); -bool ClearDateCache(bool, bool, bool); +int DecodeUnits(int field, char *lowtoken, int *val); +bool ClearDateCache(bool, bool, bool); -int j2day(int jd); +int j2day(int jd); -bool CheckDateTokenTables(void); +bool CheckDateTokenTables(void); -int EncodeDateOnly(struct tm *, int, char *, bool); -void GetEpochTime(struct tm *); -int ParseDateTime(char *, char *, char **, int *, int, int *, char **); -int DecodeDateTime(char **, int *, int, int *, struct tm *, fsec_t *, int *, bool); -void j2date(int, int *, int *, int *); -void GetCurrentDateTime(struct tm*); -int date2j(int, int, int); +int EncodeDateOnly(struct tm *, int, char *, bool); +void GetEpochTime(struct tm *); +int ParseDateTime(char *, char *, char **, int *, int, int *, char **); +int DecodeDateTime(char **, int *, int, int *, struct tm *, fsec_t *, int *, bool); +void j2date(int, int *, int *, int *); +void GetCurrentDateTime(struct tm *); +int date2j(int, int, int); -extern char* pgtypes_date_weekdays_short[]; -extern char* pgtypes_date_months[]; -extern char* months[]; -extern char* days[]; +extern char *pgtypes_date_weekdays_short[]; +extern char *pgtypes_date_months[]; +extern char *months[]; +extern char *days[]; #endif /* DT_H */ - diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.c index 89f50fe9a9a..c4d28b03496 100644 --- a/src/interfaces/ecpg/pgtypeslib/dt_common.c +++ b/src/interfaces/ecpg/pgtypeslib/dt_common.c @@ -8,18 +8,18 @@ #include "dt.h" #include "pgtypes_timestamp.h" -static int day_tab[2][13] = { - {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}, - {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}}; +static int day_tab[2][13] = { + {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}, +{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}}; typedef long AbsoluteTime; - -#define ABS_SIGNBIT ((char) 0200) -#define POS(n) (n) -#define NEG(n) ((n)|ABS_SIGNBIT) -#define FROMVAL(tp) (-SIGNEDCHAR((tp)->value) * 15) /* uncompress */ -#define VALMASK ((char) 0177) -#define SIGNEDCHAR(c) ((c)&ABS_SIGNBIT? -((c)&VALMASK): (c)) + +#define ABS_SIGNBIT ((char) 0200) +#define POS(n) (n) +#define NEG(n) ((n)|ABS_SIGNBIT) +#define FROMVAL(tp) (-SIGNEDCHAR((tp)->value) * 15) /* uncompress */ +#define VALMASK ((char) 0177) +#define SIGNEDCHAR(c) ((c)&ABS_SIGNBIT? -((c)&VALMASK): (c)) static datetkn datetktbl[] = { /* text, token, lexval */ @@ -497,41 +497,41 @@ static datetkn deltatktbl[] = { static unsigned int szdatetktbl = sizeof datetktbl / sizeof datetktbl[0]; static unsigned int szdeltatktbl = sizeof deltatktbl / sizeof deltatktbl[0]; -static datetkn *datecache[MAXDATEFIELDS] = {NULL}; +static datetkn *datecache[MAXDATEFIELDS] = {NULL}; -static datetkn *deltacache[MAXDATEFIELDS] = {NULL}; +static datetkn *deltacache[MAXDATEFIELDS] = {NULL}; -char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL}; +char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL}; -char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL}; +char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL}; -char* pgtypes_date_weekdays_short[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL}; +char *pgtypes_date_weekdays_short[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL}; -char* pgtypes_date_months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL}; +char *pgtypes_date_months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL}; static datetkn * datebsearch(char *key, datetkn *base, unsigned int nel) { - datetkn *last = base + nel - 1, - *position; - int result; + datetkn *last = base + nel - 1, + *position; + int result; while (last >= base) - { - position = base + ((last - base) >> 1); - result = key[0] - position->token[0]; - if (result == 0) - { - result = strncmp(key, position->token, TOKMAXLEN); - if (result == 0) - return position; - } - if (result < 0) - last = position - 1; - else - base = position + 1; - } - return NULL; + { + position = base + ((last - base) >> 1); + result = key[0] - position->token[0]; + if (result == 0) + { + result = strncmp(key, position->token, TOKMAXLEN); + if (result == 0) + return position; + } + if (result < 0) + last = position - 1; + else + base = position + 1; + } + return NULL; } /* DecodeUnits() @@ -580,7 +580,7 @@ DecodeUnits(int field, char *lowtoken, int *val) * * Rewritten to eliminate overflow problems. This now allows the * routines to work correctly for all Julian day counts from - * 0 to 2147483647 (Nov 24, -4713 to Jun 3, 5874898) assuming + * 0 to 2147483647 (Nov 24, -4713 to Jun 3, 5874898) assuming * a 32-bit integer. Longer types should also work to the limits * of their precision. */ @@ -591,18 +591,21 @@ date2j(int y, int m, int d) int julian; int century; - if (m > 2) { + if (m > 2) + { m += 1; y += 4800; - } else { + } + else + { m += 13; y += 4799; } - century = y/100; - julian = y*365 - 32167; - julian += y/4 - century + century/4; - julian += 7834*m/256 + d; + century = y / 100; + julian = y * 365 - 32167; + julian += y / 4 - century + century / 4; + julian += 7834 * m / 256 + d; return julian; } /* date2j() */ @@ -610,25 +613,25 @@ date2j(int y, int m, int d) void j2date(int jd, int *year, int *month, int *day) { - unsigned int julian; - unsigned int quad; - unsigned int extra; + unsigned int julian; + unsigned int quad; + unsigned int extra; int y; julian = jd; julian += 32044; - quad = julian/146097; - extra = (julian - quad*146097)*4 + 3; - julian += 60 + quad*3 + extra/146097; - quad = julian/1461; - julian -= quad*1461; + quad = julian / 146097; + extra = (julian - quad * 146097) * 4 + 3; + julian += 60 + quad * 3 + extra / 146097; + quad = julian / 1461; + julian -= quad * 1461; y = julian * 4 / 1461; julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366)) + 123; - y += quad*4; + y += quad * 4; *year = y - 4800; quad = julian * 2141 / 65536; - *day = julian - 7834*quad/256; + *day = julian - 7834 * quad / 256; *month = (quad + 10) % 12 + 1; return; @@ -637,13 +640,13 @@ j2date(int jd, int *year, int *month, int *day) int j2day(int date) { - unsigned int day; + unsigned int day; - day = date; - day += 1; - day %= 7; - return (int) day; -} /*j2day() */ + day = date; + day += 1; + day %= 7; + return (int) day; +} /* j2day() */ /* DecodeSpecial() * Decode text string using lookup table. @@ -753,14 +756,14 @@ EncodeDateOnly(struct tm * tm, int style, char *str, bool EuroDates) static void TrimTrailingZeros(char *str) { - int len = strlen(str); - - /* chop off trailing zeros... but leave at least 2 fractional digits */ - while ((*(str + len - 1) == '0') && (*(str + len - 3) != '.')) - { - len--; - *(str + len) = '\0'; - } + int len = strlen(str); + + /* chop off trailing zeros... but leave at least 2 fractional digits */ + while ((*(str + len - 1) == '0') && (*(str + len - 3) != '.')) + { + len--; + *(str + len) = '\0'; + } } /* EncodeDateTime() @@ -791,8 +794,8 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min); /* - * Print fractional seconds if any. The field widths here should - * be at least equal to MAX_TIMESTAMP_PRECISION. + * Print fractional seconds if any. The field widths here + * should be at least equal to MAX_TIMESTAMP_PRECISION. * * In float mode, don't print fractional seconds before 1 AD, * since it's unlikely there's any precision left ... @@ -841,8 +844,8 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha tm->tm_hour, tm->tm_min); /* - * Print fractional seconds if any. The field widths here should - * be at least equal to MAX_TIMESTAMP_PRECISION. + * Print fractional seconds if any. The field widths here + * should be at least equal to MAX_TIMESTAMP_PRECISION. * * In float mode, don't print fractional seconds before 1 AD, * since it's unlikely there's any precision left ... @@ -887,8 +890,8 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha tm->tm_hour, tm->tm_min); /* - * Print fractional seconds if any. The field widths here should - * be at least equal to MAX_TIMESTAMP_PRECISION. + * Print fractional seconds if any. The field widths here + * should be at least equal to MAX_TIMESTAMP_PRECISION. * * In float mode, don't print fractional seconds before 1 AD, * since it's unlikely there's any precision left ... @@ -941,8 +944,8 @@ EncodeDateTime(struct tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, cha sprintf((str + 10), " %02d:%02d", tm->tm_hour, tm->tm_min); /* - * Print fractional seconds if any. The field widths here should - * be at least equal to MAX_TIMESTAMP_PRECISION. + * Print fractional seconds if any. The field widths here + * should be at least equal to MAX_TIMESTAMP_PRECISION. * * In float mode, don't print fractional seconds before 1 AD, * since it's unlikely there's any precision left ... @@ -1041,21 +1044,22 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn) * We have a brute force time zone per SQL99? Then use it without * change since we have already rotated to the time zone. */ - *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */ + *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */ + /* * XXX FreeBSD man pages indicate that this should work - tgl * 97/04/23 */ if (tzn != NULL) { - /* - * Copy no more than MAXTZLEN bytes of timezone to tzn, in - * case it contains an error message, which doesn't fit in - * the buffer - */ - StrNCpy(*tzn, tm->tm_zone, MAXTZLEN + 1); - if (strlen(tm->tm_zone) > MAXTZLEN) - tm->tm_isdst = -1; + /* + * Copy no more than MAXTZLEN bytes of timezone to tzn, in + * case it contains an error message, which doesn't fit in the + * buffer + */ + StrNCpy(*tzn, tm->tm_zone, MAXTZLEN + 1); + if (strlen(tm->tm_zone) > MAXTZLEN) + tm->tm_isdst = -1; } } else @@ -1063,19 +1067,19 @@ abstime2tm(AbsoluteTime _time, int *tzp, struct tm * tm, char **tzn) #elif defined(HAVE_INT_TIMEZONE) if (tzp != NULL) { - *tzp = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL); + *tzp = ((tm->tm_isdst > 0) ? (TIMEZONE_GLOBAL - 3600) : TIMEZONE_GLOBAL); - if (tzn != NULL) - { - /* - * Copy no more than MAXTZLEN bytes of timezone to tzn, in - * case it contains an error message, which doesn't fit in - * the buffer - */ - StrNCpy(*tzn, tzname[tm->tm_isdst], MAXTZLEN + 1); - if (strlen(tzname[tm->tm_isdst]) > MAXTZLEN) - tm->tm_isdst = -1; - } + if (tzn != NULL) + { + /* + * Copy no more than MAXTZLEN bytes of timezone to tzn, in + * case it contains an error message, which doesn't fit in the + * buffer + */ + StrNCpy(*tzn, tzname[tm->tm_isdst], MAXTZLEN + 1); + if (strlen(tzname[tm->tm_isdst]) > MAXTZLEN) + tm->tm_isdst = -1; + } } else tm->tm_isdst = -1; @@ -1245,33 +1249,33 @@ static void dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec) { #ifdef HAVE_INT64_TIMESTAMP - int64 time; + int64 time; #else - double time; + double time; #endif time = jd; #ifdef HAVE_INT64_TIMESTAMP - *hour = (time / INT64CONST(3600000000)); - time -= ((*hour) * INT64CONST(3600000000)); - *min = (time / INT64CONST(60000000)); - time -= ((*min) * INT64CONST(60000000)); - *sec = (time / INT64CONST(1000000)); - *fsec = (time - (*sec * INT64CONST(1000000))); + *hour = (time / INT64CONST(3600000000)); + time -= ((*hour) * INT64CONST(3600000000)); + *min = (time / INT64CONST(60000000)); + time -= ((*min) * INT64CONST(60000000)); + *sec = (time / INT64CONST(1000000)); + *fsec = (time - (*sec * INT64CONST(1000000))); #else - *hour = (time / 3600); - time -= ((*hour) * 3600); + *hour = (time / 3600); + time -= ((*hour) * 3600); *min = (time / 60); - time -= ((*min) * 60); - *sec = time; - *fsec = JROUND(time - *sec); + time -= ((*min) * 60); + *sec = time; + *fsec = JROUND(time - *sec); #endif - return; -} /* dt2time() */ + return; +} /* dt2time() */ + + - - /* DecodeNumberField() * Interpret numeric string as a concatenated date or time field. * Use the context of previously decoded fields to help with @@ -1279,7 +1283,7 @@ dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec) */ static int DecodeNumberField(int len, char *str, int fmask, - int *tmask, struct tm * tm, fsec_t *fsec, int *is2digits, bool EuroDates) +int *tmask, struct tm * tm, fsec_t *fsec, int *is2digits, bool EuroDates) { char *cp; @@ -1386,7 +1390,7 @@ DecodeNumberField(int len, char *str, int fmask, */ static int DecodeNumber(int flen, char *str, int fmask, - int *tmask, struct tm * tm, fsec_t *fsec, int *is2digits, bool EuroDates) +int *tmask, struct tm * tm, fsec_t *fsec, int *is2digits, bool EuroDates) { int val; char *cp; @@ -1405,7 +1409,7 @@ DecodeNumber(int flen, char *str, int fmask, */ if ((cp - str) > 2) return DecodeNumberField(flen, str, (fmask | DTK_DATE_M), - tmask, tm, fsec, is2digits, EuroDates); + tmask, tm, fsec, is2digits, EuroDates); *fsec = strtod(cp, &cp); if (*cp != '\0') @@ -1797,7 +1801,7 @@ DecodePosixTimezone(char *str, int *tzp) */ int ParseDateTime(char *timestr, char *lowstr, - char **field, int *ftype, int maxfields, int *numfields, char **endstr) + char **field, int *ftype, int maxfields, int *numfields, char **endstr) { int nf = 0; char *lp = lowstr; @@ -1980,7 +1984,7 @@ ParseDateTime(char *timestr, char *lowstr, */ int DecodeDateTime(char **field, int *ftype, int nf, - int *dtype, struct tm * tm, fsec_t *fsec, int *tzp, bool EuroDates) + int *dtype, struct tm * tm, fsec_t *fsec, int *tzp, bool EuroDates) { int fmask = 0, tmask, @@ -2085,7 +2089,7 @@ DecodeDateTime(char **field, int *ftype, int nf, * concatenated time */ if ((ftype[i] = DecodeNumberField(strlen(field[i]), field[i], fmask, - &tmask, tm, fsec, &is2digits, EuroDates)) < 0) + &tmask, tm, fsec, &is2digits, EuroDates)) < 0) return -1; /* @@ -2272,7 +2276,7 @@ DecodeDateTime(char **field, int *ftype, int nf, case DTK_TIME: /* previous field was "t" for ISO time */ if ((ftype[i] = DecodeNumberField(strlen(field[i]), field[i], (fmask | DTK_DATE_M), - &tmask, tm, fsec, &is2digits, EuroDates)) < 0) + &tmask, tm, fsec, &is2digits, EuroDates)) < 0) return -1; if (tmask != DTK_TIME_M) @@ -2310,18 +2314,18 @@ DecodeDateTime(char **field, int *ftype, int nf, * later. Example: 20011223 or 040506 */ if ((ftype[i] = DecodeNumberField(flen, field[i], fmask, - &tmask, tm, fsec, &is2digits, EuroDates)) < 0) + &tmask, tm, fsec, &is2digits, EuroDates)) < 0) return -1; } else if (flen > 4) { if ((ftype[i] = DecodeNumberField(flen, field[i], fmask, - &tmask, tm, fsec, &is2digits, EuroDates)) < 0) + &tmask, tm, fsec, &is2digits, EuroDates)) < 0) return -1; } /* otherwise it is a single date/time field... */ else if (DecodeNumber(flen, field[i], fmask, - &tmask, tm, fsec, &is2digits, EuroDates) != 0) + &tmask, tm, fsec, &is2digits, EuroDates) != 0) return -1; } break; @@ -2565,110 +2569,130 @@ DecodeDateTime(char **field, int *ftype, int nf, * * */ -static char* find_end_token(char* str, char* fmt) { - /* str: here is28the day12the hour - * fmt: here is%dthe day%hthe hour +static char * +find_end_token(char *str, char *fmt) +{ + /* + * str: here is28the day12the hour fmt: here is%dthe day%hthe hour * - * we extract the 28, we read the percent sign and the type "d" - * then this functions gets called as - * find_end_token("28the day12the hour", "the day%hthehour") + * we extract the 28, we read the percent sign and the type "d" then this + * functions gets called as find_end_token("28the day12the hour", "the + * day%hthehour") * - * fmt points to "the day%hthehour", next_percent points to - * %hthehour and we have to find a match for everything between - * these positions ("the day"). We look for "the day" in str and - * know that the pattern we are about to scan ends where this string - * starts (right after the "28") + * fmt points to "the day%hthehour", next_percent points to %hthehour and + * we have to find a match for everything between these positions + * ("the day"). We look for "the day" in str and know that the pattern + * we are about to scan ends where this string starts (right after the + * "28") * * At the end, *fmt is '\0' and *str isn't. end_position then is * unchanged. */ - char* end_position = NULL; - char* next_percent, *subst_location = NULL; - int scan_offset = 0; - char last_char; + char *end_position = NULL; + char *next_percent, + *subst_location = NULL; + int scan_offset = 0; + char last_char; /* are we at the end? */ - if (!*fmt) { + if (!*fmt) + { end_position = fmt; return end_position; } /* not at the end */ - while (fmt[scan_offset] == '%' && fmt[scan_offset+1]) { - /* there is no delimiter, skip to the next delimiter - * if we're reading a number and then something that is not - * a number "9:15pm", we might be able to recover with the - * strtol end pointer. Go for the next percent sign */ + while (fmt[scan_offset] == '%' && fmt[scan_offset + 1]) + { + /* + * there is no delimiter, skip to the next delimiter if we're + * reading a number and then something that is not a number + * "9:15pm", we might be able to recover with the strtol end + * pointer. Go for the next percent sign + */ scan_offset += 2; } - next_percent = strchr(fmt+scan_offset, '%'); - if (next_percent) { - /* we don't want to allocate extra memory, so we temporarily - * set the '%' sign to '\0' and call strstr - * However since we allow whitespace to float around - * everything, we have to shorten the pattern until we reach - * a non-whitespace character */ - + next_percent = strchr(fmt + scan_offset, '%'); + if (next_percent) + { + /* + * we don't want to allocate extra memory, so we temporarily set + * the '%' sign to '\0' and call strstr However since we allow + * whitespace to float around everything, we have to shorten the + * pattern until we reach a non-whitespace character + */ + subst_location = next_percent; - while(*(subst_location-1) == ' ' && subst_location-1 > fmt+scan_offset) { + while (*(subst_location - 1) == ' ' && subst_location - 1 > fmt + scan_offset) subst_location--; - } last_char = *subst_location; *subst_location = '\0'; - /* the haystack is the str and the needle is the original - * fmt but it ends at the position where the next percent - * sign would be */ - /* There is one special case. Imagine: - * str = " 2", fmt = "%d %...", - * since we want to allow blanks as "dynamic" padding we - * have to accept this. Now, we are called with a fmt of - * " %..." and look for " " in str. We find it at the first - * position and never read the 2... */ - while (*str == ' ') { str++; } - end_position = strstr(str, fmt+scan_offset); + /* + * the haystack is the str and the needle is the original fmt but + * it ends at the position where the next percent sign would be + */ + + /* + * There is one special case. Imagine: str = " 2", fmt = "%d + * %...", since we want to allow blanks as "dynamic" padding we + * have to accept this. Now, we are called with a fmt of " %..." + * and look for " " in str. We find it at the first position and + * never read the 2... + */ + while (*str == ' ') + str++; + end_position = strstr(str, fmt + scan_offset); *subst_location = last_char; - } else { - /* there is no other percent sign. So everything up to - * the end has to match. */ + } + else + { + /* + * there is no other percent sign. So everything up to the end has + * to match. + */ end_position = str + strlen(str); } - if (!end_position) { - /* maybe we have the following case: + if (!end_position) + { + /* + * maybe we have the following case: * - * str = "4:15am" - * fmt = "%M:%S %p" + * str = "4:15am" fmt = "%M:%S %p" * * at this place we could have * - * str = "15am" - * fmt = " %p" + * str = "15am" fmt = " %p" * - * and have set fmt to " " because overwrote the % sign with - * a NULL + * and have set fmt to " " because overwrote the % sign with a NULL * - * In this case where we would have to match a space but - * can't find it, set end_position to the end of the string */ - if ((fmt+scan_offset)[0] == ' ' && fmt+scan_offset+1 == subst_location) { + * In this case where we would have to match a space but can't find + * it, set end_position to the end of the string + */ + if ((fmt + scan_offset)[0] == ' ' && fmt + scan_offset + 1 == subst_location) end_position = str + strlen(str); - } } return end_position; } -static int pgtypes_defmt_scan(union un_fmt_comb* scan_val, int scan_type, char** pstr, char* pfmt) { - /* scan everything between pstr and pstr_end. - * This is not including the last character so we might set it to - * '\0' for the parsing */ - - char last_char; - int err = 0; - char* pstr_end; - char* strtol_end = NULL; - - while (**pstr == ' ') { pstr++; } +static int +pgtypes_defmt_scan(union un_fmt_comb * scan_val, int scan_type, char **pstr, char *pfmt) +{ + /* + * scan everything between pstr and pstr_end. This is not including + * the last character so we might set it to '\0' for the parsing + */ + + char last_char; + int err = 0; + char *pstr_end; + char *strtol_end = NULL; + + while (**pstr == ' ') + pstr++; pstr_end = find_end_token(*pstr, pfmt); - if (!pstr_end) { + if (!pstr_end) + { /* there was an error, no match */ err = 1; return err; @@ -2676,63 +2700,80 @@ static int pgtypes_defmt_scan(union un_fmt_comb* scan_val, int scan_type, char** last_char = *pstr_end; *pstr_end = '\0'; - switch(scan_type) { + switch (scan_type) + { case PGTYPES_TYPE_UINT: - /* numbers may be blank-padded, this is the only - * deviation from the fmt-string we accept */ - while (**pstr == ' ') { (*pstr)++; } + + /* + * numbers may be blank-padded, this is the only deviation + * from the fmt-string we accept + */ + while (**pstr == ' ') + (*pstr)++; errno = 0; scan_val->uint_val = (unsigned int) strtol(*pstr, &strtol_end, 10); - if (errno) { err = 1; } + if (errno) + err = 1; break; case PGTYPES_TYPE_UINT_LONG: - while (**pstr == ' ') { (*pstr)++; } + while (**pstr == ' ') + (*pstr)++; errno = 0; scan_val->uint_val = (unsigned long int) strtol(*pstr, &strtol_end, 10); - if (errno) { err = 1; } + if (errno) + err = 1; break; case PGTYPES_TYPE_STRING_MALLOCED: - if (pstr) { + if (pstr) scan_val->str_val = pgtypes_strdup(*pstr); - } } - if (strtol_end && *strtol_end) { + if (strtol_end && *strtol_end) *pstr = strtol_end; - } else { + else *pstr = pstr_end; - } *pstr_end = last_char; return err; } /* XXX range checking */ -int PGTYPEStimestamp_defmt_scan(char**, char*, Timestamp *, int*, int*, int*, - int*, int*, int*, int*); +int PGTYPEStimestamp_defmt_scan(char **, char *, Timestamp *, int *, int *, int *, + int *, int *, int *, int *); -int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, - int* year, int* month, int* day, - int* hour, int* minute, int* second, - int* tz) { +int +PGTYPEStimestamp_defmt_scan(char **str, char *fmt, Timestamp *d, + int *year, int *month, int *day, + int *hour, int *minute, int *second, + int *tz) +{ union un_fmt_comb scan_val; - int scan_type; + int scan_type; - char *pstr, *pfmt, *tmp; - int err = 1; - int j; - struct tm tm; + char *pstr, + *pfmt, + *tmp; + int err = 1; + int j; + struct tm tm; pfmt = fmt; pstr = *str; - - while (*pfmt) { + + while (*pfmt) + { err = 0; - while (*pfmt == ' ') { pfmt++; } - while (*pstr == ' ') { pstr++; } - if (*pfmt != '%') { - if (*pfmt == *pstr) { + while (*pfmt == ' ') + pfmt++; + while (*pstr == ' ') + pstr++; + if (*pfmt != '%') + { + if (*pfmt == *pstr) + { pfmt++; pstr++; - } else { + } + else + { /* XXX Error: no match */ err = 1; return err; @@ -2741,17 +2782,22 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, } /* here *pfmt equals '%' */ pfmt++; - switch(*pfmt) { + switch (*pfmt) + { case 'a': pfmt++; - /* we parse the day and see if it is a week - * day but we do not check if the week day - * really matches the date - * */ - err = 1; j = 0; - while(pgtypes_date_weekdays_short[j]) { + + /* + * we parse the day and see if it is a week day but we do + * not check if the week day really matches the date + */ + err = 1; + j = 0; + while (pgtypes_date_weekdays_short[j]) + { if (strncmp(pgtypes_date_weekdays_short[j], pstr, - strlen(pgtypes_date_weekdays_short[j])) == 0) { + strlen(pgtypes_date_weekdays_short[j])) == 0) + { /* found it */ err = 0; pstr += strlen(pgtypes_date_weekdays_short[j]); @@ -2763,9 +2809,12 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, case 'A': /* see note above */ pfmt++; - err = 1; j = 0; - while(days[j]) { - if (strncmp(days[j], pstr, strlen(days[j])) == 0) { + err = 1; + j = 0; + while (days[j]) + { + if (strncmp(days[j], pstr, strlen(days[j])) == 0) + { /* found it */ err = 0; pstr += strlen(days[j]); @@ -2777,13 +2826,16 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, case 'b': case 'h': pfmt++; - err = 1; j = 0; - while(months[j]) { - if (strncmp(months[j], pstr, strlen(months[j])) == 0) { + err = 1; + j = 0; + while (months[j]) + { + if (strncmp(months[j], pstr, strlen(months[j])) == 0) + { /* found it */ err = 0; pstr += strlen(months[j]); - *month = j+1; + *month = j + 1; break; } j++; @@ -2792,13 +2844,16 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, case 'B': /* see note above */ pfmt++; - err = 1; j = 0; - while(pgtypes_date_months[j]) { - if (strncmp(pgtypes_date_months[j], pstr, strlen(pgtypes_date_months[j])) == 0) { + err = 1; + j = 0; + while (pgtypes_date_months[j]) + { + if (strncmp(pgtypes_date_months[j], pstr, strlen(pgtypes_date_months[j])) == 0) + { /* found it */ err = 0; pstr += strlen(pgtypes_date_months[j]); - *month = j+1; + *month = j + 1; break; } j++; @@ -2821,9 +2876,11 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, *day = scan_val.uint_val; break; case 'D': - /* we have to concatenate the strings in - * order to be able to find the end of the - * substitution */ + + /* + * we have to concatenate the strings in order to be able + * to find the end of the substitution + */ pfmt++; tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pstr) + 1); strcpy(tmp, "%m/%d/%y"); @@ -2838,17 +2895,19 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, *month = scan_val.uint_val; break; case 'y': - case 'g': /* XXX difference to y (ISO) */ + case 'g': /* XXX difference to y (ISO) */ pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (*year < 0) { + if (*year < 0) + { /* not yet set */ *year = scan_val.uint_val; - } else { - *year += scan_val.uint_val; } - if (*year < 100) { *year += 1900; } + else + *year += scan_val.uint_val; + if (*year < 100) + *year += 1900; break; case 'G': /* XXX difference to %V (ISO) */ @@ -2870,10 +2929,12 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - /* XXX what should we do with that? - * We could say that it's sufficient if we - * have the year and the day within the year - * to get at least a specific day. */ + + /* + * XXX what should we do with that? We could say that it's + * sufficient if we have the year and the day within the + * year to get at least a specific day. + */ break; case 'M': pfmt++; @@ -2883,23 +2944,66 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, break; case 'n': pfmt++; - if (*pstr == '\n') { pstr++; } else { err = 1; } + if (*pstr == '\n') + pstr++; + else + err = 1; break; case 'p': err = 1; pfmt++; - if (strncmp(pstr, "am", 2) == 0) { *hour += 0; err = 0; pstr += 2; } - if (strncmp(pstr, "a.m.", 4) == 0) { *hour += 0; err = 0; pstr += 4; } - if (strncmp(pstr, "pm", 2) == 0) { *hour += 12; err = 0; pstr += 2; } - if (strncmp(pstr, "p.m.", 4) == 0) { *hour += 12; err = 0; pstr += 4; } - break; + if (strncmp(pstr, "am", 2) == 0) + { + *hour += 0; + err = 0; + pstr += 2; + } + if (strncmp(pstr, "a.m.", 4) == 0) + { + *hour += 0; + err = 0; + pstr += 4; + } + if (strncmp(pstr, "pm", 2) == 0) + { + *hour += 12; + err = 0; + pstr += 2; + } + if (strncmp(pstr, "p.m.", 4) == 0) + { + *hour += 12; + err = 0; + pstr += 4; + } + break; case 'P': err = 1; pfmt++; - if (strncmp(pstr, "AM", 2) == 0) { *hour += 0; err = 0; pstr += 2; } - if (strncmp(pstr, "A.M.", 4) == 0) { *hour += 0; err = 0; pstr += 4; } - if (strncmp(pstr, "PM", 2) == 0) { *hour += 12; err = 0; pstr += 2; } - if (strncmp(pstr, "P.M.", 4) == 0) { *hour += 12; err = 0; pstr += 4; } + if (strncmp(pstr, "AM", 2) == 0) + { + *hour += 0; + err = 0; + pstr += 2; + } + if (strncmp(pstr, "A.M.", 4) == 0) + { + *hour += 0; + err = 0; + pstr += 4; + } + if (strncmp(pstr, "PM", 2) == 0) + { + *hour += 12; + err = 0; + pstr += 2; + } + if (strncmp(pstr, "P.M.", 4) == 0) + { + *hour += 12; + err = 0; + pstr += 4; + } break; case 'r': pfmt++; @@ -2923,8 +3027,9 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); /* number of seconds in scan_val.luint_val */ { - struct tm *tms; - time_t et = (time_t) scan_val.luint_val; + struct tm *tms; + time_t et = (time_t) scan_val.luint_val; + tms = gmtime(&et); *year = tms->tm_year; *month = tms->tm_mon; @@ -2942,7 +3047,10 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, break; case 't': pfmt++; - if (*pstr == '\t') { pstr++; } else { err = 1; } + if (*pstr == '\t') + pstr++; + else + err = 1; break; case 'T': pfmt++; @@ -2956,31 +3064,36 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (scan_val.uint_val < 1 || scan_val.uint_val > 7) { err = 1; } + if (scan_val.uint_val < 1 || scan_val.uint_val > 7) + err = 1; break; case 'U': pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (scan_val.uint_val < 0 || scan_val.uint_val > 53) { err = 1; } + if (scan_val.uint_val < 0 || scan_val.uint_val > 53) + err = 1; break; case 'V': pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (scan_val.uint_val < 1 || scan_val.uint_val > 53) { err = 1; } + if (scan_val.uint_val < 1 || scan_val.uint_val > 53) + err = 1; break; case 'w': pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (scan_val.uint_val < 0 || scan_val.uint_val > 6) { err = 1; } + if (scan_val.uint_val < 0 || scan_val.uint_val > 6) + err = 1; break; case 'W': pfmt++; scan_type = PGTYPES_TYPE_UINT; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (scan_val.uint_val < 0 || scan_val.uint_val > 53) { err = 1; } + if (scan_val.uint_val < 0 || scan_val.uint_val > 53) + err = 1; break; case 'x': case 'X': @@ -2996,7 +3109,8 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, pfmt++; scan_type = PGTYPES_TYPE_STRING_MALLOCED; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - if (!err) { + if (!err) + { err = DecodeTimezone(scan_val.str_val, tz); free(scan_val.str_val); } @@ -3005,16 +3119,20 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, pfmt++; scan_type = PGTYPES_TYPE_STRING_MALLOCED; err = pgtypes_defmt_scan(&scan_val, scan_type, &pstr, pfmt); - /* XXX use DecodeSpecial instead ? - it's - * declared static but the arrays as well. - * :-( */ - for (j = 0; !err && j < szdatetktbl; j++) { - if (strcasecmp(datetktbl[j].token, scan_val.str_val) == 0) { - /* tz calculates the offset - * for the seconds, the - * timezone value of the - * datetktbl table is in - * quarter hours */ + + /* + * XXX use DecodeSpecial instead ? - it's declared static + * but the arrays as well. :-( + */ + for (j = 0; !err && j < szdatetktbl; j++) + { + if (strcasecmp(datetktbl[j].token, scan_val.str_val) == 0) + { + /* + * tz calculates the offset for the seconds, the + * timezone value of the datetktbl table is in + * quarter hours + */ *tz = -15 * 60 * datetktbl[j].value; break; } @@ -3026,26 +3144,62 @@ int PGTYPEStimestamp_defmt_scan(char** str, char* fmt, Timestamp *d, break; case '%': pfmt++; - if (*pstr == '%') { pstr++; } else { err = 1; } + if (*pstr == '%') + pstr++; + else + err = 1; break; default: err = 1; } } - if (!err) { - if (*second < 0) { *second = 0; } - if (*minute < 0) { *minute = 0; } - if (*hour < 0) { *hour = 0; } - if (*day < 0) { err = 1; *day = 1; } - if (*month < 0) { err = 1; *month = 1; } - if (*year < 0) { err = 1; *year = 1970; } - - if (*second > 59) { err = 1; *second = 0; } - if (*minute > 59) { err = 1; *minute = 0; } - if (*hour > 23) { err = 1; *hour = 0; } - if (*month > 12) { err = 1; *month = 1; } - if (*day > day_tab[isleap(*year)][*month-1]) { - *day = day_tab[isleap(*year)][*month-1]; + if (!err) + { + if (*second < 0) + *second = 0; + if (*minute < 0) + *minute = 0; + if (*hour < 0) + *hour = 0; + if (*day < 0) + { + err = 1; + *day = 1; + } + if (*month < 0) + { + err = 1; + *month = 1; + } + if (*year < 0) + { + err = 1; + *year = 1970; + } + + if (*second > 59) + { + err = 1; + *second = 0; + } + if (*minute > 59) + { + err = 1; + *minute = 0; + } + if (*hour > 23) + { + err = 1; + *hour = 0; + } + if (*month > 12) + { + err = 1; + *month = 1; + } + if (*day > day_tab[isleap(*year)][*month - 1]) + { + *day = day_tab[isleap(*year)][*month - 1]; err = 1; } diff --git a/src/interfaces/ecpg/pgtypeslib/extern.h b/src/interfaces/ecpg/pgtypeslib/extern.h index 8004e3d4216..110f56b5c9e 100644 --- a/src/interfaces/ecpg/pgtypeslib/extern.h +++ b/src/interfaces/ecpg/pgtypeslib/extern.h @@ -9,44 +9,46 @@ #define PGTYPES_TYPE_STRING_MALLOCED 1 #define PGTYPES_TYPE_STRING_CONSTANT 2 #define PGTYPES_TYPE_CHAR 3 -#define PGTYPES_TYPE_DOUBLE_NF 4 /* no fractional part */ +#define PGTYPES_TYPE_DOUBLE_NF 4 /* no fractional part */ #define PGTYPES_TYPE_INT64 5 #define PGTYPES_TYPE_UINT 6 -#define PGTYPES_TYPE_UINT_2_LZ 7 /* 2 digits, pad with leading zero */ -#define PGTYPES_TYPE_UINT_2_LS 8 /* 2 digits, pad with leading space */ +#define PGTYPES_TYPE_UINT_2_LZ 7 /* 2 digits, pad with + * leading zero */ +#define PGTYPES_TYPE_UINT_2_LS 8 /* 2 digits, pad with + * leading space */ #define PGTYPES_TYPE_UINT_3_LZ 9 #define PGTYPES_TYPE_UINT_4_LZ 10 #define PGTYPES_TYPE_UINT_LONG 11 #define PGTYPES_FMT_NUM_MAX_DIGITS 40 -union un_fmt_comb { - char* str_val; - unsigned int uint_val; - char char_val; - unsigned long int luint_val; - double double_val; +union un_fmt_comb +{ + char *str_val; + unsigned int uint_val; + char char_val; + unsigned long int luint_val; + double double_val; #ifdef HAVE_INT64_TIMESTAMP - int64 int64_val; + int64 int64_val; #endif }; -int pgtypes_fmt_replace(union un_fmt_comb, int, char**, int*); +int pgtypes_fmt_replace(union un_fmt_comb, int, char **, int *); -char *pgtypes_alloc(long); -char *pgtypes_strdup(char *); +char *pgtypes_alloc(long); +char *pgtypes_strdup(char *); #ifndef bool #define bool char #endif /* ndef bool */ #ifndef FALSE -#define FALSE 0 +#define FALSE 0 #endif /* FALSE */ #ifndef TRUE -#define TRUE 1 -#endif /* TRUE */ - -#endif /* __PGTYPES_COMMON_H__ */ +#define TRUE 1 +#endif /* TRUE */ +#endif /* __PGTYPES_COMMON_H__ */ diff --git a/src/interfaces/ecpg/pgtypeslib/interval.c b/src/interfaces/ecpg/pgtypeslib/interval.c index 0646f480f46..efde760c87c 100644 --- a/src/interfaces/ecpg/pgtypeslib/interval.c +++ b/src/interfaces/ecpg/pgtypeslib/interval.c @@ -743,7 +743,7 @@ tm2interval(struct tm * tm, fsec_t fsec, Interval *span) Interval * PGTYPESinterval_from_asc(char *str, char **endptr) { - Interval *result = NULL; + Interval *result = NULL; fsec_t fsec; struct tm tt, *tm = &tt; @@ -752,8 +752,8 @@ PGTYPESinterval_from_asc(char *str, char **endptr) char *field[MAXDATEFIELDS]; int ftype[MAXDATEFIELDS]; char lowstr[MAXDATELEN + MAXDATEFIELDS]; - char *realptr; - char **ptr = (endptr != NULL) ? endptr : &realptr; + char *realptr; + char **ptr = (endptr != NULL) ? endptr : &realptr; tm->tm_year = 0; tm->tm_mon = 0; @@ -791,7 +791,7 @@ PGTYPESinterval_from_asc(char *str, char **endptr) errno = PGTYPES_INTVL_BAD_INTERVAL; return NULL; } - + return result; } @@ -802,7 +802,7 @@ PGTYPESinterval_to_asc(Interval *span) *tm = &tt; fsec_t fsec; char buf[MAXDATELEN + 1]; - int DateStyle=0; + int DateStyle = 0; if (interval2tm(*span, tm, &fsec) != 0) { @@ -815,11 +815,11 @@ PGTYPESinterval_to_asc(Interval *span) errno = PGTYPES_INTVL_BAD_INTERVAL; return NULL; } - - return pgtypes_strdup(buf); + + return pgtypes_strdup(buf); } -int +int PGTYPESinterval_copy(Interval *intvlsrc, Interval *intrcldest) { intrcldest->time = intvlsrc->time; @@ -827,4 +827,3 @@ PGTYPESinterval_copy(Interval *intvlsrc, Interval *intrcldest) return 0; } - diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c index 6ac9ae48ee3..5811be61c8f 100644 --- a/src/interfaces/ecpg/pgtypeslib/numeric.c +++ b/src/interfaces/ecpg/pgtypeslib/numeric.c @@ -5,17 +5,17 @@ #include "extern.h" #include "pgtypes_error.h" -#define Max(x, y) ((x) > (y) ? (x) : (y)) -#define Min(x, y) ((x) < (y) ? (x) : (y)) +#define Max(x, y) ((x) > (y) ? (x) : (y)) +#define Min(x, y) ((x) < (y) ? (x) : (y)) -#define init_var(v) memset(v,0,sizeof(Numeric)) +#define init_var(v) memset(v,0,sizeof(Numeric)) #define digitbuf_alloc(size) ((NumericDigit *) pgtypes_alloc(size)) -#define digitbuf_free(buf) \ - do { \ - if ((buf) != NULL) \ - free(buf); \ - } while (0) +#define digitbuf_free(buf) \ + do { \ + if ((buf) != NULL) \ + free(buf); \ + } while (0) #include "pgtypes_numeric.h" @@ -27,7 +27,7 @@ * typmod field. * ---------- */ -static int +static int apply_typmod(Numeric *var, long typmod) { int precision; @@ -37,7 +37,7 @@ apply_typmod(Numeric *var, long typmod) /* Do nothing if we have a default typmod (-1) */ if (typmod < (long) (VARHDRSZ)) - return(0); + return (0); typmod -= VARHDRSZ; precision = (typmod >> 16) & 0xffff; @@ -102,9 +102,9 @@ apply_typmod(Numeric *var, long typmod) #endif /* ---------- - * alloc_var() - - * - * Allocate a digit buffer of ndigits digits (plus a spare digit for rounding) + * alloc_var() - + * + * Allocate a digit buffer of ndigits digits (plus a spare digit for rounding) * ---------- */ static int @@ -120,17 +120,16 @@ alloc_var(Numeric *var, int ndigits) return 0; } -Numeric * +Numeric * PGTYPESnumeric_new(void) { - Numeric *var; - - if ((var = (Numeric *)pgtypes_alloc(sizeof(Numeric))) == NULL) + Numeric *var; + + if ((var = (Numeric *) pgtypes_alloc(sizeof(Numeric))) == NULL) return NULL; - if (alloc_var(var, 0) < 0) { + if (alloc_var(var, 0) < 0) return NULL; - } return var; } @@ -141,11 +140,11 @@ PGTYPESnumeric_new(void) * Parse a string and put the number into a variable * ---------- */ -static int +static int set_var_from_str(char *str, char **ptr, Numeric *dest) { - bool have_dp = FALSE; - int i = 0; + bool have_dp = FALSE; + int i = 0; *ptr = str; while (*(*ptr)) @@ -182,7 +181,7 @@ set_var_from_str(char *str, char **ptr, Numeric *dest) if (!isdigit((unsigned char) *(*ptr))) { - errno=PGTYPES_NUM_BAD_NUMERIC; + errno = PGTYPES_NUM_BAD_NUMERIC; return -1; } @@ -259,7 +258,7 @@ set_var_from_str(char *str, char **ptr, Numeric *dest) dest->weight = 0; dest->rscale = dest->dscale; - return(0); + return (0); } @@ -308,7 +307,7 @@ get_str_from_var(Numeric *var, int dscale) /* * Allocate space for the result */ - if ((str = (char *)pgtypes_alloc(Max(0, dscale) + Max(0, var->weight) + 4)) == NULL) + if ((str = (char *) pgtypes_alloc(Max(0, dscale) + Max(0, var->weight) + 4)) == NULL) return NULL; cp = str; @@ -360,17 +359,18 @@ get_str_from_var(Numeric *var, int dscale) Numeric * PGTYPESnumeric_from_asc(char *str, char **endptr) { - Numeric *value = (Numeric *)pgtypes_alloc(sizeof(Numeric)); - int ret; + Numeric *value = (Numeric *) pgtypes_alloc(sizeof(Numeric)); + int ret; + #if 0 - long typmod = -1; + long typmod = -1; #endif - char *realptr; - char **ptr = (endptr != NULL) ? endptr : &realptr; - + char *realptr; + char **ptr = (endptr != NULL) ? endptr : &realptr; + if (!value) return (NULL); - + ret = set_var_from_str(str, ptr, value); if (ret) return (NULL); @@ -379,8 +379,8 @@ PGTYPESnumeric_from_asc(char *str, char **endptr) ret = apply_typmod(value, typmod); if (ret) return (NULL); -#endif - return(value); +#endif + return (value); } char * @@ -389,7 +389,7 @@ PGTYPESnumeric_to_asc(Numeric *num, int dscale) if (dscale <= 0) dscale = num->dscale; - return(get_str_from_var(num, dscale)); + return (get_str_from_var(num, dscale)); } /* ---------- @@ -410,7 +410,7 @@ zero_var(Numeric *var) var->sign = NUMERIC_POS; /* anything but NAN... */ } -void +void PGTYPESnumeric_free(Numeric *var) { digitbuf_free(var->buf); @@ -913,7 +913,7 @@ PGTYPESnumeric_sub(Numeric *var1, Numeric *var2, Numeric *result) * mul_var() - * * Multiplication on variable level. Product of var1 * var2 is stored - * in result. Accuracy of result is determined by global_rscale. + * in result. Accuracy of result is determined by global_rscale. * ---------- */ int @@ -929,7 +929,7 @@ PGTYPESnumeric_mul(Numeric *var1, Numeric *var2, Numeric *result) i1, i2; long sum = 0; - int global_rscale = var1->rscale + var2->rscale; + int global_rscale = var1->rscale + var2->rscale; res_weight = var1->weight + var2->weight + 2; res_ndigits = var1->ndigits + var2->ndigits + 1; @@ -939,7 +939,7 @@ PGTYPESnumeric_mul(Numeric *var1, Numeric *var2, Numeric *result) res_sign = NUMERIC_NEG; if ((res_buf = digitbuf_alloc(res_ndigits)) == NULL) - return -1; + return -1; res_digits = res_buf; memset(res_digits, 0, res_ndigits); @@ -1054,8 +1054,8 @@ select_div_scale(Numeric *var1, Numeric *var2, int *rscale) } /* - * Estimate weight of quotient. If the two first digits are equal, - * we can't be sure, but assume that var1 is less than var2. + * Estimate weight of quotient. If the two first digits are equal, we + * can't be sure, but assume that var1 is less than var2. */ qweight = weight1 - weight2; if (firstdigit1 <= firstdigit2) @@ -1081,8 +1081,8 @@ PGTYPESnumeric_div(Numeric *var1, Numeric *var2, Numeric *result) int res_ndigits; int res_sign; int res_weight; - Numeric dividend; - Numeric divisor[10]; + Numeric dividend; + Numeric divisor[10]; int ndigits_tmp; int weight_tmp; int rscale_tmp; @@ -1093,16 +1093,16 @@ PGTYPESnumeric_div(Numeric *var1, Numeric *var2, Numeric *result) long first_div; int first_nextdigit; int stat = 0; - int rscale; - int res_dscale = select_div_scale(var1, var2, &rscale); - + int rscale; + int res_dscale = select_div_scale(var1, var2, &rscale); + /* * First of all division by zero check */ ndigits_tmp = var2->ndigits + 1; if (ndigits_tmp == 1) { - errno= PGTYPES_NUM_DIVIDE_ZERO; + errno = PGTYPES_NUM_DIVIDE_ZERO; return -1; } @@ -1281,29 +1281,30 @@ PGTYPESnumeric_div(Numeric *var1, Numeric *var2, Numeric *result) int -PGTYPESnumeric_cmp(Numeric *var1, Numeric *var2) { +PGTYPESnumeric_cmp(Numeric *var1, Numeric *var2) +{ /* use cmp_abs function to calculate the result */ /* both are positive: normal comparation with cmp_abs */ - if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_POS) { + if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_POS) return cmp_abs(var1, var2); - } /* both are negative: return the inverse of the normal comparation */ - if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_NEG) { - /* instead of inverting the result, we invert the paramter - * ordering */ + if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_NEG) + { + /* + * instead of inverting the result, we invert the paramter + * ordering + */ return cmp_abs(var2, var1); } /* one is positive, one is negative: trivial */ - if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_NEG) { + if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_NEG) return 1; - } - if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_POS) { + if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_POS) return -1; - } errno = PGTYPES_NUM_BAD_NUMERIC; return INT_MAX; @@ -1311,67 +1312,79 @@ PGTYPESnumeric_cmp(Numeric *var1, Numeric *var2) { } int -PGTYPESnumeric_from_int(signed int int_val, Numeric *var) { +PGTYPESnumeric_from_int(signed int int_val, Numeric *var) +{ /* implicit conversion */ signed long int long_int = int_val; + return PGTYPESnumeric_from_long(long_int, var); } int -PGTYPESnumeric_from_long(signed long int long_val, Numeric *var) { +PGTYPESnumeric_from_long(signed long int long_val, Numeric *var) +{ /* calculate the size of the long int number */ /* a number n needs log_10 n digits */ - /* however we multiply by 10 each time and compare instead of - * calculating the logarithm */ - int size = 0; - int i; + /* + * however we multiply by 10 each time and compare instead of + * calculating the logarithm + */ + + int size = 0; + int i; signed long int abs_long_val = long_val; signed long int extract; signed long int reach_limit; - - if (abs_long_val < 0) { + + if (abs_long_val < 0) + { abs_long_val *= -1; var->sign = NUMERIC_NEG; - } else { - var->sign = NUMERIC_POS; } + else + var->sign = NUMERIC_POS; reach_limit = 1; - do { + do + { size++; reach_limit *= 10; - } while ((reach_limit-1) < abs_long_val); + } while ((reach_limit - 1) < abs_long_val); /* always add a .0 */ size++; - if (alloc_var(var, size) < 0) { + if (alloc_var(var, size) < 0) return -1; - } var->rscale = 1; var->dscale = 1; var->weight = size - 2; i = 0; - do { + do + { reach_limit /= 10; extract = abs_long_val - (abs_long_val % reach_limit); var->digits[i] = extract / reach_limit; abs_long_val -= extract; i++; - /* we can abandon if abs_long_val reaches 0, because the - * memory is initialized properly and filled with '0', so - * converting 10000 in only one step is no problem */ + + /* + * we can abandon if abs_long_val reaches 0, because the memory is + * initialized properly and filled with '0', so converting 10000 + * in only one step is no problem + */ } while (abs_long_val > 0); return 0; } int -PGTYPESnumeric_copy(Numeric *src, Numeric *dst) { - int i; +PGTYPESnumeric_copy(Numeric *src, Numeric *dst) +{ + int i; zero_var(dst); @@ -1383,9 +1396,8 @@ PGTYPESnumeric_copy(Numeric *src, Numeric *dst) { if (alloc_var(dst, src->ndigits) != 0) return -1; - for (i = 0; i < src->ndigits; i++) { + for (i = 0; i < src->ndigits; i++) dst->digits[i] = src->digits[i]; - } return 0; } @@ -1393,12 +1405,12 @@ PGTYPESnumeric_copy(Numeric *src, Numeric *dst) { int PGTYPESnumeric_from_double(double d, Numeric *dst) { - char buffer[100]; - Numeric *tmp; - + char buffer[100]; + Numeric *tmp; + if (sprintf(buffer, "%f", d) == 0) return -1; - + if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL) return -1; if (PGTYPESnumeric_copy(tmp, dst) != 0) @@ -1425,17 +1437,18 @@ numericvar_to_double_no_overflow(Numeric *var, double *dp) free(tmp); errno = PGTYPES_NUM_BAD_NUMERIC; return -1; - } + } *dp = val; free(tmp); return 0; } int -PGTYPESnumeric_to_double(Numeric* nv, double* dp) { - double tmp; - int i; - +PGTYPESnumeric_to_double(Numeric *nv, double *dp) +{ + double tmp; + int i; + if ((i = numericvar_to_double_no_overflow(nv, &tmp)) != 0) return -1; *dp = tmp; @@ -1443,72 +1456,79 @@ PGTYPESnumeric_to_double(Numeric* nv, double* dp) { } int -PGTYPESnumeric_to_int(Numeric* nv, int* ip) { - long l; - int i; - +PGTYPESnumeric_to_int(Numeric *nv, int *ip) +{ + long l; + int i; + if ((i = PGTYPESnumeric_to_long(nv, &l)) != 0) return i; - if (l < -INT_MAX || l > INT_MAX) { + if (l < -INT_MAX || l > INT_MAX) + { errno = PGTYPES_NUM_OVERFLOW; return -1; - } + } *ip = (int) l; return 0; } int -PGTYPESnumeric_to_long(Numeric* nv, long* lp) { - int i; - long l = 0; +PGTYPESnumeric_to_long(Numeric *nv, long *lp) +{ + int i; + long l = 0; - for (i = 1; i < nv->weight + 2; i++) { + for (i = 1; i < nv->weight + 2; i++) + { l *= 10; l += nv->buf[i]; } - if (nv->buf[i] >= 5) { + if (nv->buf[i] >= 5) + { /* round up */ l++; } - if (l > LONG_MAX || l < 0) { + if (l > LONG_MAX || l < 0) + { errno = PGTYPES_NUM_OVERFLOW; return -1; } - - if (nv->sign == NUMERIC_NEG) { + + if (nv->sign == NUMERIC_NEG) l *= -1; - } *lp = l; return 0; } int -PGTYPESnumeric_to_decimal(Numeric *src, Decimal *dst) { - int i; +PGTYPESnumeric_to_decimal(Numeric *src, Decimal * dst) +{ + int i; - if (src->ndigits > DECSIZE) { + if (src->ndigits > DECSIZE) + { errno = PGTYPES_NUM_OVERFLOW; return -1; } - + dst->weight = src->weight; dst->rscale = src->rscale; dst->dscale = src->dscale; dst->sign = src->sign; dst->ndigits = src->ndigits; - for (i = 0; i < src->ndigits; i++) { + for (i = 0; i < src->ndigits; i++) dst->digits[i] = src->digits[i]; - } return 0; } int -PGTYPESnumeric_from_decimal(Decimal *src, Numeric *dst) { - int i; +PGTYPESnumeric_from_decimal(Decimal * src, Numeric *dst) +{ + int i; zero_var(dst); @@ -1520,9 +1540,8 @@ PGTYPESnumeric_from_decimal(Decimal *src, Numeric *dst) { if (alloc_var(dst, src->ndigits) != 0) return -1; - for (i = 0; i < src->ndigits; i++) { + for (i = 0; i < src->ndigits; i++) dst->digits[i] = src->digits[i]; - } return 0; } diff --git a/src/interfaces/ecpg/pgtypeslib/timestamp.c b/src/interfaces/ecpg/pgtypeslib/timestamp.c index 4d1a13d9e5b..8a96d696b40 100644 --- a/src/interfaces/ecpg/pgtypeslib/timestamp.c +++ b/src/interfaces/ecpg/pgtypeslib/timestamp.c @@ -13,8 +13,8 @@ #include "pgtypes_date.h" #include "datetime.h" -int PGTYPEStimestamp_defmt_scan(char**, char*, Timestamp *, int*, int*, int*, - int*, int*, int*, int*); +int PGTYPEStimestamp_defmt_scan(char **, char *, Timestamp *, int *, int *, int *, + int *, int *, int *, int *); #ifdef HAVE_INT64_TIMESTAMP static int64 @@ -89,7 +89,8 @@ static Timestamp SetEpochTimestamp(void) { Timestamp dt; - struct tm tt, *tm = &tt; + struct tm tt, + *tm = &tt; GetEpochTime(tm); tm2timestamp(tm, 0, NULL, &dt); @@ -101,6 +102,7 @@ dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec) { #ifdef HAVE_INT64_TIMESTAMP int64 time; + #else double time; #endif @@ -142,8 +144,8 @@ static int timestamp2tm(Timestamp dt, int *tzp, struct tm * tm, fsec_t *fsec, char **tzn) { #ifdef HAVE_INT64_TIMESTAMP - int date, - date0; + int date, + date0; int64 time; #else @@ -255,8 +257,8 @@ timestamp2tm(Timestamp dt, int *tzp, struct tm * tm, fsec_t *fsec, char **tzn) } /* timestamp2tm() */ /* EncodeSpecialTimestamp() - * * Convert reserved timestamp data type to string. - * */ + * * Convert reserved timestamp data type to string. + * */ static int EncodeSpecialTimestamp(Timestamp dt, char *str) { @@ -274,21 +276,24 @@ Timestamp PGTYPEStimestamp_from_asc(char *str, char **endptr) { Timestamp result; + #ifdef HAVE_INT64_TIMESTAMP int64 noresult = 0; + #else double noresult = 0.0; #endif fsec_t fsec; - struct tm tt, *tm = &tt; - int tz; - int dtype; - int nf; - char *field[MAXDATEFIELDS]; - int ftype[MAXDATEFIELDS]; + struct tm tt, + *tm = &tt; + int tz; + int dtype; + int nf; + char *field[MAXDATEFIELDS]; + int ftype[MAXDATEFIELDS]; char lowstr[MAXDATELEN + MAXDATEFIELDS]; - char *realptr; - char **ptr = (endptr != NULL) ? endptr : &realptr; + char *realptr; + char **ptr = (endptr != NULL) ? endptr : &realptr; errno = 0; if (strlen(str) >= sizeof(lowstr)) @@ -298,12 +303,12 @@ PGTYPEStimestamp_from_asc(char *str, char **endptr) } if ((ParseDateTime(str, lowstr, field, ftype, MAXDATEFIELDS, &nf, ptr) != 0) - || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz, 0) != 0)) + || (DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz, 0) != 0)) { - errno = PGTYPES_TS_BAD_TIMESTAMP; - return (noresult); - } - + errno = PGTYPES_TS_BAD_TIMESTAMP; + return (noresult); + } + switch (dtype) { case DTK_DATE: @@ -343,11 +348,13 @@ PGTYPEStimestamp_from_asc(char *str, char **endptr) char * PGTYPEStimestamp_to_asc(Timestamp tstamp) { - struct tm tt, *tm = &tt; + struct tm tt, + *tm = &tt; char buf[MAXDATELEN + 1]; - char *tzn = NULL; + char *tzn = NULL; fsec_t fsec; - int DateStyle = 1; /* this defaults to ISO_DATES, shall we make it an option? */ + int DateStyle = 1; /* this defaults to ISO_DATES, shall we + * make it an option? */ if (TIMESTAMP_NOT_FINITE(tstamp)) EncodeSpecialTimestamp(tstamp, buf); @@ -362,31 +369,34 @@ PGTYPEStimestamp_to_asc(Timestamp tstamp) } void -PGTYPEStimestamp_current (Timestamp *ts) +PGTYPEStimestamp_current(Timestamp *ts) { struct tm tm; - + GetCurrentDateTime(&tm); tm2timestamp(&tm, 0, NULL, ts); return; } static int -dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, - char* output, int *pstr_len, char *fmtstr) +dttofmtasc_replace(Timestamp *ts, Date dDate, int dow, struct tm * tm, + char *output, int *pstr_len, char *fmtstr) { union un_fmt_comb replace_val; - int replace_type; - int i; - char* p = fmtstr; - char* q = output; + int replace_type; + int i; + char *p = fmtstr; + char *q = output; - while (*p) { - if (*p == '%') { + while (*p) + { + if (*p == '%') + { p++; /* fix compiler warning */ replace_type = PGTYPES_TYPE_NOTHING; - switch (*p) { + switch (*p) + { case 'a': replace_val.str_val = pgtypes_date_weekdays_short[dow]; replace_type = PGTYPES_TYPE_STRING_CONSTANT; @@ -416,19 +426,21 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, replace_type = PGTYPES_TYPE_UINT_2_LZ; break; case 'D': - /* ts, dDate, dow, tm is - * information about the timestamp + + /* + * ts, dDate, dow, tm is information about the + * timestamp * - * q is the start of the current - * output buffer + * q is the start of the current output buffer * - * pstr_len is a pointer to the - * remaining size of output, i.e. - * the size of q */ + * pstr_len is a pointer to the remaining size of output, + * i.e. the size of q + */ i = dttofmtasc_replace(ts, dDate, dow, tm, - q, pstr_len, - "%m/%d/%y"); - if (i) { return i; } + q, pstr_len, + "%m/%d/%y"); + if (i) + return i; break; case 'e': replace_val.uint_val = tm->tm_mday; @@ -436,19 +448,23 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, break; case 'E': { - char tmp[4] = "%Ex"; + char tmp[4] = "%Ex"; + p++; - if (*p == '\0') { + if (*p == '\0') return -1; - } tmp[2] = *p; /* XXX: fall back to strftime */ - /* strftime's month is 0 - * based, ours is 1 based */ + + /* + * strftime's month is 0 based, ours is 1 based + */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, tmp, tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -460,8 +476,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, /* XXX: fall back to strftime */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, "%G", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -471,12 +489,15 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, case 'g': /* XXX: fall back to strftime */ { - char *fmt = "%g"; /* Keep compiler quiet about 2-digit year */ - + char *fmt = "%g"; /* Keep compiler quiet + * about 2-digit year */ + tm->tm_mon -= 1; i = strftime(q, *pstr_len, fmt, tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -517,32 +538,32 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, replace_type = PGTYPES_TYPE_CHAR; break; case 'p': - if (tm->tm_hour < 12) { + if (tm->tm_hour < 12) replace_val.str_val = "AM"; - } else { + else replace_val.str_val = "PM"; - } replace_type = PGTYPES_TYPE_STRING_CONSTANT; break; case 'P': - if (tm->tm_hour < 12) { + if (tm->tm_hour < 12) replace_val.str_val = "am"; - } else { + else replace_val.str_val = "pm"; - } replace_type = PGTYPES_TYPE_STRING_CONSTANT; break; case 'r': i = dttofmtasc_replace(ts, dDate, dow, tm, - q, pstr_len, - "%I:%M:%S %p"); - if (i) { return i; } + q, pstr_len, + "%I:%M:%S %p"); + if (i) + return i; break; case 'R': i = dttofmtasc_replace(ts, dDate, dow, tm, - q, pstr_len, - "%H:%M"); - if (i) { return i; } + q, pstr_len, + "%H:%M"); + if (i) + return i; break; case 's': #ifdef HAVE_INT64_TIMESTAMP @@ -563,12 +584,14 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, break; case 'T': i = dttofmtasc_replace(ts, dDate, dow, tm, - q, pstr_len, - "%H:%M:%S"); - if (i) { return i; } + q, pstr_len, + "%H:%M:%S"); + if (i) + return i; break; case 'u': - if (dow == 0) { dow = 7; } + if (dow == 0) + dow = 7; replace_val.uint_val = dow; replace_type = PGTYPES_TYPE_UINT; break; @@ -576,8 +599,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, /* XXX: fall back to strftime */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, "%U", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -587,8 +612,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, case 'V': /* XXX: fall back to strftime */ i = strftime(q, *pstr_len, "%V", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -602,8 +629,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, /* XXX: fall back to strftime */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, "%U", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -613,12 +642,15 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, case 'x': /* XXX: fall back to strftime */ { - char *fmt = "%x"; /* Keep compiler quiet about 2-digit year */ + char *fmt = "%x"; /* Keep compiler quiet + * about 2-digit year */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, fmt, tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -630,8 +662,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, /* XXX: fall back to strftime */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, "%X", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -650,8 +684,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, /* XXX: fall back to strftime */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, "%z", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -662,8 +698,10 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, /* XXX: fall back to strftime */ tm->tm_mon -= 1; i = strftime(q, *pstr_len, "%Z", tm); - if (i == 0) { return -1; } - while (*q) { + if (i == 0) + return -1; + while (*q) + { q++; (*pstr_len)--; } @@ -676,41 +714,53 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, break; case '\0': /* fmtstr: blabla%' */ - /* this is not compliant to the - * specification */ + + /* + * this is not compliant to the specification + */ return -1; default: - /* if we don't know the pattern, we - * just copy it */ - if (*pstr_len > 1) { + + /* + * if we don't know the pattern, we just copy it + */ + if (*pstr_len > 1) + { *q = '%'; - q++; (*pstr_len)--; - if (*pstr_len > 1) { + q++; + (*pstr_len)--; + if (*pstr_len > 1) + { *q = *p; - q++; (*pstr_len)--; - } else { + q++; + (*pstr_len)--; + } + else + { *q = '\0'; return -1; } *q = '\0'; - } else { - return -1; } + else + return -1; break; } i = pgtypes_fmt_replace(replace_val, replace_type, &q, pstr_len); - if (i) { + if (i) return i; - } - } else { - if (*pstr_len > 1) { + } + else + { + if (*pstr_len > 1) + { *q = *p; (*pstr_len)--; q++; *q = '\0'; - } else { - return -1; } + else + return -1; } p++; } @@ -719,12 +769,12 @@ dttofmtasc_replace (Timestamp *ts, Date dDate, int dow, struct tm* tm, int -PGTYPEStimestamp_fmt_asc (Timestamp *ts, char *output, int str_len, char *fmtstr) +PGTYPEStimestamp_fmt_asc(Timestamp *ts, char *output, int str_len, char *fmtstr) { - struct tm tm; - fsec_t fsec; - Date dDate; - int dow; + struct tm tm; + fsec_t fsec; + Date dDate; + int dow; dDate = PGTYPESdate_from_timestamp(*ts); dow = PGTYPESdate_dayofweek(dDate); @@ -734,12 +784,10 @@ PGTYPEStimestamp_fmt_asc (Timestamp *ts, char *output, int str_len, char *fmtstr } int -PGTYPEStimestamp_sub (Timestamp *ts1, Timestamp *ts2, Interval *iv) +PGTYPEStimestamp_sub(Timestamp *ts1, Timestamp *ts2, Interval *iv) { if (TIMESTAMP_NOT_FINITE(*ts1) || TIMESTAMP_NOT_FINITE(*ts2)) - { return PGTYPES_TS_ERR_EINFTIME; - } else #ifdef HAVE_INT64_TIMESTAMP iv->time = (ts1 - ts2); @@ -752,28 +800,40 @@ PGTYPEStimestamp_sub (Timestamp *ts1, Timestamp *ts2, Interval *iv) return 0; } -int PGTYPEStimestamp_defmt_asc(char* str, char *fmt, Timestamp *d) { - int year, month, day; - int hour, minute, second; - int tz; - - int i; - char* mstr; - char* mfmt; - - if (!fmt) { +int +PGTYPEStimestamp_defmt_asc(char *str, char *fmt, Timestamp *d) +{ + int year, + month, + day; + int hour, + minute, + second; + int tz; + + int i; + char *mstr; + char *mfmt; + + if (!fmt) fmt = "%Y-%m-%d %H:%M:%S"; - } - if (!fmt[0]) { + if (!fmt[0]) return 1; - } mstr = pgtypes_strdup(str); mfmt = pgtypes_strdup(fmt); - /* initialize with impossible values so that we can see if the - * fields where specified at all */ + + /* + * initialize with impossible values so that we can see if the fields + * where specified at all + */ /* XXX ambiguity with 1 BC for year? */ - year = -1; month = -1; day = -1; hour = 0; minute = -1; second = -1; + year = -1; + month = -1; + day = -1; + hour = 0; + minute = -1; + second = -1; tz = 0; i = PGTYPEStimestamp_defmt_scan(&mstr, mfmt, d, &year, &month, &day, &hour, &minute, &second, &tz); @@ -781,4 +841,3 @@ int PGTYPEStimestamp_defmt_asc(char* str, char *fmt, Timestamp *d) { free(mfmt); return i; } - diff --git a/src/interfaces/ecpg/preproc/c_keywords.c b/src/interfaces/ecpg/preproc/c_keywords.c index 7b19227375c..fa2ee17f617 100644 --- a/src/interfaces/ecpg/preproc/c_keywords.c +++ b/src/interfaces/ecpg/preproc/c_keywords.c @@ -28,24 +28,24 @@ static ScanKeyword ScanKeywords[] = { {"enum", SQL_ENUM}, {"extern", S_EXTERN}, {"float", FLOAT_P}, - {"hour", HOUR_P}, + {"hour", HOUR_P}, {"int", INT_P}, {"long", SQL_LONG}, {"minute", MINUTE_P}, - {"month", MONTH_P}, + {"month", MONTH_P}, {"register", S_REGISTER}, - {"second", SECOND_P}, + {"second", SECOND_P}, {"short", SQL_SHORT}, {"signed", SQL_SIGNED}, {"static", S_STATIC}, {"struct", SQL_STRUCT}, - {"to", TO}, + {"to", TO}, {"typedef", S_TYPEDEF}, {"union", UNION}, {"unsigned", SQL_UNSIGNED}, {"varchar", VARCHAR}, {"volatile", S_VOLATILE}, - {"year", YEAR_P}, + {"year", YEAR_P}, }; ScanKeyword * diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c index fb0f6dcb859..7cf8eeebb2f 100644 --- a/src/interfaces/ecpg/preproc/ecpg.c +++ b/src/interfaces/ecpg/preproc/ecpg.c @@ -1,4 +1,4 @@ -/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.76 2003/08/01 08:21:04 meskes Exp $ */ +/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.77 2003/08/04 00:43:33 momjian Exp $ */ /* New main for ecpg, the PostgreSQL embedded SQL precompiler. */ /* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */ @@ -23,7 +23,7 @@ int ret_value = 0, system_includes = false, force_indicator = true; -enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL; +enum COMPAT_MODE compat = ECPG_COMPAT_PGSQL; struct _include_path *include_paths = NULL; struct cursor *cur = NULL; @@ -49,7 +49,7 @@ help(const char *progname) " \"INFORMIX\"\n" " \"INFORMIX_SE\"\n"); printf(" -r <option> specify runtime behaviour\n" - " option may be only \"no_indicator\" at the moment\n"); + " option may be only \"no_indicator\" at the moment\n"); printf(" -D SYMBOL define SYMBOL\n"); printf(" -I DIRECTORY search DIRECTORY for include files\n"); printf(" -o OUTFILE write result to OUTFILE\n"); @@ -65,7 +65,8 @@ help(const char *progname) static void add_include_path(char *path) { - struct _include_path *ip = include_paths, *new; + struct _include_path *ip = include_paths, + *new; new = mm_alloc(sizeof(struct _include_path)); new->path = path; @@ -75,7 +76,7 @@ add_include_path(char *path) include_paths = new; else { - for (;ip->next != NULL; ip=ip->next); + for (; ip->next != NULL; ip = ip->next); ip->next = new; } } @@ -84,21 +85,22 @@ static void add_preprocessor_define(char *define) { struct _defines *pd = defines; - char *ptr, *define_copy = mm_strdup(define); - + char *ptr, + *define_copy = mm_strdup(define); + defines = mm_alloc(sizeof(struct _defines)); - + /* look for = sign */ ptr = strchr(define_copy, '='); if (ptr != NULL) { - char *tmp; - + char *tmp; + /* symbol gets a value */ - for (tmp=ptr-1; *tmp == ' '; tmp--); + for (tmp = ptr - 1; *tmp == ' '; tmp--); tmp[1] = '\0'; defines->old = define_copy; - defines->new = ptr+1; + defines->new = ptr + 1; } else { @@ -141,11 +143,11 @@ main(int argc, char *const argv[]) switch (c) { case 'o': - if (strcmp(optarg, "-") == 0) + if (strcmp(optarg, "-") == 0) yyout = stdout; else yyout = fopen(optarg, PG_BINARY_W); - + if (yyout == NULL) perror(optarg); else @@ -179,7 +181,7 @@ main(int argc, char *const argv[]) { fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]); return ILLEGAL_OPTION; - } + } break; case 'r': if (strcmp(optarg, "no_indicator") == 0) @@ -240,7 +242,7 @@ main(int argc, char *const argv[]) /* If argv[fnr] is "-" we have to read from stdin */ if (strcmp(argv[fnr], "-") == 0) { - input_filename = mm_alloc(strlen("stdin")+1); + input_filename = mm_alloc(strlen("stdin") + 1); strcpy(input_filename, "stdin"); yyin = stdin; } @@ -265,7 +267,7 @@ main(int argc, char *const argv[]) ptr2ext[3] = 'c'; ptr2ext[4] = '\0'; } - + yyin = fopen(input_filename, PG_BINARY_R); } @@ -377,7 +379,7 @@ main(int argc, char *const argv[]) /* finally the actual connection */ connection = NULL; - + /* initialize lex */ lex_init(); @@ -387,7 +389,7 @@ main(int argc, char *const argv[]) /* add some compatibility headers */ if (INFORMIX_MODE) fprintf(yyout, "/* Needed for informix compatibility */\n#include <ecpg_informix.h>\n"); - + fprintf(yyout, "/* End of automatic include section */\n"); /* and parse the source */ diff --git a/src/interfaces/ecpg/preproc/extern.h b/src/interfaces/ecpg/preproc/extern.h index aa76930e7c7..a1ea69e1860 100644 --- a/src/interfaces/ecpg/preproc/extern.h +++ b/src/interfaces/ecpg/preproc/extern.h @@ -96,8 +96,12 @@ extern ScanKeyword *ScanKeywordLookup(char *text); #define INDICATOR_NOT_STRUCT 6 #define INDICATOR_NOT_SIMPLE 7 -enum COMPAT_MODE { ECPG_COMPAT_PGSQL = 0, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE}; +enum COMPAT_MODE +{ + ECPG_COMPAT_PGSQL = 0, ECPG_COMPAT_INFORMIX, ECPG_COMPAT_INFORMIX_SE +}; extern enum COMPAT_MODE compat; + #define INFORMIX_MODE (compat == ECPG_COMPAT_INFORMIX || compat == ECPG_COMPAT_INFORMIX_SE) -#endif /* _ECPG_PREPROC_EXTERN_H */ +#endif /* _ECPG_PREPROC_EXTERN_H */ diff --git a/src/interfaces/ecpg/preproc/type.c b/src/interfaces/ecpg/preproc/type.c index ee4bac58598..6eb1209ede4 100644 --- a/src/interfaces/ecpg/preproc/type.c +++ b/src/interfaces/ecpg/preproc/type.c @@ -220,10 +220,10 @@ static void ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, c void ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * type, - const char *ind_name, struct ECPGtype * ind_type, - const char *prefix, const char *ind_prefix, - char *arr_str_siz, const char *struct_sizeof, - const char *ind_struct_sizeof) + const char *ind_name, struct ECPGtype * ind_type, + const char *prefix, const char *ind_prefix, + char *arr_str_siz, const char *struct_sizeof, + const char *ind_struct_sizeof) { switch (type->type) { @@ -238,20 +238,20 @@ ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * type, case ECPGt_struct: case ECPGt_union: ECPGdump_a_struct(o, name, - ind_name, - type->size, - type->u.element, - (ind_type->type == ECPGt_NO_INDICATOR) ? ind_type : ind_type->u.element, - NULL, prefix, ind_prefix); + ind_name, + type->size, + type->u.element, + (ind_type->type == ECPGt_NO_INDICATOR) ? ind_type : ind_type->u.element, + NULL, prefix, ind_prefix); break; default: if (!IS_SIMPLE_TYPE(type->u.element->type)) yyerror("Internal error: unknown datatype, please inform pgsql-bugs@postgresql.org"); ECPGdump_a_simple(o, name, - type->u.element->type, + type->u.element->type, type->u.element->size, type->size, NULL, prefix); - + if (ind_type != NULL) { if (ind_type->type == ECPGt_NO_INDICATOR) @@ -259,7 +259,7 @@ ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * type, else { ECPGdump_a_simple(o, ind_name, ind_type->u.element->type, - ind_type->u.element->size, ind_type->size, NULL, prefix); + ind_type->u.element->size, ind_type->size, NULL, prefix); } } } @@ -318,22 +318,23 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, { char *variable = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 4); char *offset = (char *) mm_alloc(strlen(name) + strlen("sizeof(struct varchar_)") + 1); - + switch (type) { - /* - * we have to use the & operator except for arrays and pointers - */ - + /* + * we have to use the & operator except for arrays and + * pointers + */ + case ECPGt_varchar: /* * we have to use the pointer except for arrays with given * bounds */ - if (((atoi(arrsize) > 0) || - (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) && - siz == NULL) + if (((atoi(arrsize) > 0) || + (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) && + siz == NULL) sprintf(variable, "(%s%s)", prefix ? prefix : "", name); else sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); @@ -346,13 +347,13 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, /* * we have to use the pointer except for arrays with given - * bounds, ecpglib will distinguish between * and [] + * bounds, ecpglib will distinguish between * and [] */ if ((atoi(varcharsize) > 1 || - (atoi(arrsize) > 0) || - (atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) || - (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) - && siz == NULL) + (atoi(arrsize) > 0) || + (atoi(varcharsize) == 0 && strcmp(varcharsize, "0") != 0) || + (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) + && siz == NULL) sprintf(variable, "(%s%s)", prefix ? prefix : "", name); else sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); @@ -362,7 +363,7 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, case ECPGt_numeric: /* - * we have to use a pointer here + * we have to use a pointer here */ sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); sprintf(offset, "sizeof(Numeric)"); @@ -370,7 +371,7 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, case ECPGt_interval: /* - * we have to use a pointer here + * we have to use a pointer here */ sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); sprintf(offset, "sizeof(Interval)"); @@ -378,7 +379,8 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, case ECPGt_date: /* - * we have to use a pointer and translate the variable type + * we have to use a pointer and translate the variable + * type */ sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); sprintf(offset, "sizeof(Date)"); @@ -386,7 +388,8 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, case ECPGt_timestamp: /* - * we have to use a pointer and translate the variable type + * we have to use a pointer and translate the variable + * type */ sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); sprintf(offset, "sizeof(Date)"); @@ -394,7 +397,7 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, case ECPGt_const: /* - * just dump the const as string + * just dump the const as string */ sprintf(variable, "\"%s\"", name); sprintf(offset, "strlen(\"%s\")", name); @@ -405,9 +408,9 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, * we have to use the pointer except for arrays with given * bounds */ - if (((atoi(arrsize) > 0) || - (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) && - siz == NULL) + if (((atoi(arrsize) > 0) || + (atoi(arrsize) == 0 && strcmp(arrsize, "0") != 0)) && + siz == NULL) sprintf(variable, "(%s%s)", prefix ? prefix : "", name); else sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); @@ -418,7 +421,7 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype type, if (atoi(arrsize) < 0) strcpy(arrsize, "1"); - + if (siz == NULL || strcmp(arrsize, "0") == 0 || strcmp(arrsize, "1") == 0) fprintf(o, "\n\t%s,%s,(long)%s,(long)%s,%s, ", get_type(type), variable, varcharsize, arrsize, offset); else @@ -476,11 +479,11 @@ ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, char *arrsiz, for (p = type->u.members; p; p = p->next) { - ECPGdump_a_type(o, p->name, p->type, - (ind_p != NULL) ? ind_p->name : NULL, - (ind_p != NULL) ? ind_p->type : NULL, - prefix, ind_prefix, arrsiz, type->struct_sizeof, - (ind_p != NULL) ? ind_type->struct_sizeof : NULL); + ECPGdump_a_type(o, p->name, p->type, + (ind_p != NULL) ? ind_p->name : NULL, + (ind_p != NULL) ? ind_p->type : NULL, + prefix, ind_prefix, arrsiz, type->struct_sizeof, + (ind_p != NULL) ? ind_type->struct_sizeof : NULL); if (ind_p != NULL && ind_p != &struct_no_indicator) ind_p = ind_p->next; } diff --git a/src/interfaces/ecpg/preproc/type.h b/src/interfaces/ecpg/preproc/type.h index 9e8d6cea308..2dd511ff854 100644 --- a/src/interfaces/ecpg/preproc/type.h +++ b/src/interfaces/ecpg/preproc/type.h @@ -14,16 +14,17 @@ struct ECPGstruct_member struct ECPGtype { enum ECPGttype type; - char *size; /* For array it is the number of elements. - * For varchar it is the maxsize of the - * area. */ - char *struct_sizeof; /* For a struct this is the sizeof() type - * as string */ + char *size; /* For array it is the number of elements. + * For varchar it is the maxsize of the + * area. */ + char *struct_sizeof; /* For a struct this is the sizeof() type + * as string */ union { struct ECPGtype *element; /* For an array this is the type - * of the element */ - struct ECPGstruct_member *members; /* A pointer to a list of members. */ + * of the element */ + struct ECPGstruct_member *members; /* A pointer to a list of + * members. */ } u; }; @@ -49,9 +50,9 @@ void ECPGfree_type(struct ECPGtype *); size is the maxsize in case it is a varchar. Otherwise it is the size of the variable (required to do array fetches of structs). */ -void ECPGdump_a_type(FILE *, const char *, struct ECPGtype *, - const char *, struct ECPGtype *, const char *, - const char *, char *, const char *, const char *); +void ECPGdump_a_type(FILE *, const char *, struct ECPGtype *, + const char *, struct ECPGtype *, const char *, + const char *, char *, const char *, const char *); /* A simple struct to keep a variable and its type. */ struct ECPGtemp_type @@ -83,24 +84,24 @@ struct when struct index { - char *index1; - char *index2; - char *str; + char *index1; + char *index2; + char *str; }; struct su_symbol { - char *su; - char *symbol; + char *su; + char *symbol; }; struct this_type { enum ECPGttype type_enum; - char *type_str; - char *type_dimension; - char *type_index; - char *type_sizeof; + char *type_str; + char *type_dimension; + char *type_index; + char *type_sizeof; }; struct _include_path @@ -182,4 +183,4 @@ typedef struct ScanKeyword int value; } ScanKeyword; -#endif /* _ECPG_PREPROC_TYPE_H */ +#endif /* _ECPG_PREPROC_TYPE_H */ diff --git a/src/interfaces/ecpg/preproc/variable.c b/src/interfaces/ecpg/preproc/variable.c index f295d177d9c..d13561ab0ca 100644 --- a/src/interfaces/ecpg/preproc/variable.c +++ b/src/interfaces/ecpg/preproc/variable.c @@ -23,7 +23,8 @@ static struct variable * find_struct_member(char *name, char *str, struct ECPGstruct_member * members, int brace_level) { char *next = strpbrk(++str, ".-["), - *end, c = '\0'; + *end, + c = '\0'; if (next != NULL) { @@ -54,54 +55,64 @@ find_struct_member(char *name, char *str, struct ECPGstruct_member * members, in *next = c; if (c == '[') { - int count; + int count; - /* We don't care about what's inside the array braces - * so just eat up the character */ - for (count=1, end=next+1; count; end++) + /* + * We don't care about what's inside the array braces + * so just eat up the character + */ + for (count = 1, end = next + 1; count; end++) { - switch (*end) - { - case '[': count++; - break; - case ']': count--; - break; - default : break; - } + switch (*end) + { + case '[': + count++; + break; + case ']': + count--; + break; + default: + break; + } } } - else end = next; - + else + end = next; + switch (*end) { - case '\0': /* found the end, but this time it has to be an array element */ - if (members->type->type != ECPGt_array) - { - snprintf(errortext, sizeof(errortext), "incorrectly formed variable %s", name); - mmerror(PARSE_ERROR, ET_FATAL, errortext); - } - - switch (members->type->u.element->type) - { - case ECPGt_array: - return (new_variable(name, ECPGmake_array_type(members->type->u.element->u.element, members->type->u.element->size), brace_level)); - case ECPGt_struct: - case ECPGt_union: - return (new_variable(name, ECPGmake_struct_type(members->type->u.element->u.members, members->type->u.element->type, members->type->u.element->struct_sizeof), brace_level)); - default: - return (new_variable(name, ECPGmake_simple_type(members->type->u.element->type, members->type->u.element->size), brace_level)); - } - break; - case '-': return (find_struct_member(name, end, members->type->u.element->u.members, brace_level)); - break; - case '.': if (members->type->type != ECPGt_array) - return (find_struct_member(name, end, members->type->u.element->u.members, brace_level)); - else - return (find_struct_member(name, next, members->type->u.members, brace_level)); - break; - default : snprintf(errortext, sizeof(errortext), "incorrectly formed variable %s", name); + case '\0': /* found the end, but this time it has to + * be an array element */ + if (members->type->type != ECPGt_array) + { + snprintf(errortext, sizeof(errortext), "incorrectly formed variable %s", name); mmerror(PARSE_ERROR, ET_FATAL, errortext); - break; + } + + switch (members->type->u.element->type) + { + case ECPGt_array: + return (new_variable(name, ECPGmake_array_type(members->type->u.element->u.element, members->type->u.element->size), brace_level)); + case ECPGt_struct: + case ECPGt_union: + return (new_variable(name, ECPGmake_struct_type(members->type->u.element->u.members, members->type->u.element->type, members->type->u.element->struct_sizeof), brace_level)); + default: + return (new_variable(name, ECPGmake_simple_type(members->type->u.element->type, members->type->u.element->size), brace_level)); + } + break; + case '-': + return (find_struct_member(name, end, members->type->u.element->u.members, brace_level)); + break; + case '.': + if (members->type->type != ECPGt_array) + return (find_struct_member(name, end, members->type->u.element->u.members, brace_level)); + else + return (find_struct_member(name, next, members->type->u.members, brace_level)); + break; + default: + snprintf(errortext, sizeof(errortext), "incorrectly formed variable %s", name); + mmerror(PARSE_ERROR, ET_FATAL, errortext); + break; } } } @@ -195,33 +206,40 @@ find_simple(char *name) struct variable * find_variable(char *name) { - char *next, *end; + char *next, + *end; struct variable *p; - int count; + int count; next = strpbrk(name, ".[-"); if (next) { if (*next == '[') { - /* We don't care about what's inside the array braces - * so just eat up the character */ - for (count=1, end=next+1; count; end++) + /* + * We don't care about what's inside the array braces so just + * eat up the character + */ + for (count = 1, end = next + 1; count; end++) { - switch (*end) - { - case '[': count++; - break; - case ']': count--; - break; - default : break; - } + switch (*end) + { + case '[': + count++; + break; + case ']': + count--; + break; + default: + break; + } } - if (*end == '.') p = find_struct(name, next, end); + if (*end == '.') + p = find_struct(name, next, end); else { - char c = *next; - + char c = *next; + *next = '\0'; p = find_simple(name); *next = c; @@ -237,9 +255,11 @@ find_variable(char *name) } } } - else p = find_struct(name, next, next); + else + p = find_struct(name, next, next); } - else p = find_simple(name); + else + p = find_simple(name); if (p == NULL) { @@ -263,9 +283,10 @@ remove_variables(int brace_level) /* is it still referenced by a cursor? */ struct cursor *ptr; - for (ptr = cur; ptr != NULL; ptr = ptr->next) + for (ptr = cur; ptr != NULL; ptr = ptr->next) { - struct arguments *varptr, *prevvar; + struct arguments *varptr, + *prevvar; for (varptr = prevvar = ptr->argsinsert; varptr != NULL; varptr = varptr->next) { @@ -290,7 +311,7 @@ remove_variables(int brace_level) } } } - + /* remove it */ if (p == allvariables) prev = allvariables = p->next; @@ -373,7 +394,7 @@ dump_variables(struct arguments * list, int mode) dump_variables(list->next, mode); /* Then the current element and its indicator */ - ECPGdump_a_type(yyout, list->variable->name, list->variable->type, + ECPGdump_a_type(yyout, list->variable->name, list->variable->type, list->indicator->name, list->indicator->type, NULL, NULL, 0, NULL, NULL); @@ -512,14 +533,17 @@ adjust_array(enum ECPGttype type_enum, char **dimension, char **length, char *ty /* one index is the string length */ if (atoi(*length) < 0) { - /* make sure we return length = -1 for arrays without given bounds */ + /* + * make sure we return length = -1 for arrays without + * given bounds + */ if (atoi(*dimension) < 0) *length = make_str("1"); else if (atoi(*dimension) == 0) *length = make_str("-1"); - else + else *length = *dimension; - + *dimension = make_str("-1"); } break; |