Guy Richey #include #include #include #include #include #ifdef IB5LIB #define DYN_ALLOC_RET 1 #else #undef DYN_ALLOC_RET #endif #define EXPORTFN __declspec(dllexport) #define STDCALL __stdcall #define BUFFLEN 256 #ifndef DYN_ALLOC_RET char buff[BUFFLEN]; #endif /* HELPER FUNCTIONS */ char *strscan(char *sub, char *str, int count) { char *s, *w; int i; w = str; for (i = 0; i < count; i++) { s = strstr(w, sub); if (s) w = s + 1; else break; }; return s; } char *strrscan(char *sub, char *str, int count, int *counter) { char *s, *r = NULL; s = strstr(str, sub); if (s) r = strrscan(sub, s + 1, count, counter); else { *counter = 0; }; if (*counter == count) r = s; (*counter)++; return r; } /* Date functions */ EXPORTFN int fn_year(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_year + 1900; } EXPORTFN int fn_month(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_mon + 1; } EXPORTFN int fn_day(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_mday; } EXPORTFN int fn_doy(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_yday + 1; } EXPORTFN int fn_dow(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_wday + 1; } EXPORTFN int fn_hour(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_hour; } EXPORTFN int fn_minute(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_min; } EXPORTFN int fn_second(ISC_QUAD *ibdate) { struct tm tm_date; isc_decode_date(ibdate, &tm_date); return tm_date.tm_sec; } /* String functions */ EXPORTFN int fn_strlen(char *str) { return strlen(str); } EXPORTFN char *fn_substr(char *str, int *from, int *count) { int f, c, l = strlen(str); #ifdef DYN_ALLOC_RET char *buff = (char *)malloc(BUFFLEN); #endif if (*from == 0 || *count == 0) buff[0] = 0; else { f = (*from > 0) ? (*from - 1) : (l + *from); if (*count > 0) c = *count; else { f += *count + 1; c = -(*count); }; if (f < 0) { c += f; f = 0; }; if (f >= l || c <= 0) buff[0] = 0; else { strncpy(buff, str + f, c); buff[c] = 0; }; }; return buff; } EXPORTFN int fn_pos(char *sub, char *str, int *count) { int cntr; char *found; if (*count == 0 || !sub || strlen(sub) == 0) return 0; else { if (*count > 0) found = strscan(sub, str, *count); else found = strrscan(sub, str, -(*count), &cntr); }; if (found) return found - str + 1; else return 0; } EXPORTFN char *fn_rtrim(char *str) { int i = strlen(str); #ifdef DYN_ALLOC_RET char *buff = (char *)malloc(BUFFLEN); #endif while (i > 0 && isspace(str[i - 1])) i--; strncpy(buff, str, i); buff[i] = 0; return buff; } EXPORTFN char *fn_ltrim(char *str) { int i = 0; #ifdef DYN_ALLOC_RET char *buff = (char *)malloc(BUFFLEN); #endif while (str[i] && isspace(str[i])) i++; strcpy(buff, str + i); return buff; } EXPORTFN char *fn_trim(char *str) { int i = 0; #ifdef DYN_ALLOC_RET char *buff = (char *)malloc(BUFFLEN); #endif while (str[i] && isspace(str[i])) i++; strcpy(buff, str + i); i = strlen(buff); while (i > 0 && isspace(buff[i - 1])) i--; buff[i] = 0; return buff; } EXPORTFN char *fn_replace(char *sub, char* str, char *rep, int *count) { char *s, *w; int i; #ifdef DYN_ALLOC_RET char *buff = (char *)malloc(BUFFLEN); #endif if (!str || strlen(str) == 0) { buff[0] = 0; return buff; }; if (!sub || strlen(sub) == 0) { strcpy(buff, str); return buff; }; if (*count != 0) { if (*count > 0) s = strscan(sub, str, *count); else s = strrscan(sub, str, -(*count), &i); if (s) { strncpy(buff, str, s - str); buff[s - str] = 0; strcat(buff, rep); strcat(buff, s + strlen(sub)); } else strcpy(buff, str); } else { buff[0] = 0; w = str; for (;;) { s = strstr(w, sub); if (s) { i = strlen(buff); strncpy(buff + i, w, s - w); buff[i + (s - w)] = 0; strcat(buff, rep); w = s + strlen(sub); } else { strcat(buff, w); break; }; }; }; return buff; }