/*\ * libLB - The LBPP support library * Copyright (C) 2001 Anthony Liguori * * libLB is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * libLB is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA \*/ #include #include #include #include #include #include #include #include #include "lb.h" double LB_ABS_1(LBContext *cxt, double d) { return (d < 0.0) ? -d : d; } double LB_ACS_1(LBContext *cxt, double d) { return acos(d); } double LB_ASC_1(LBContext *cxt, LBString str) { double ret = (double)(str->data[0]); lbstr_destroy(str); return ret; } double LB_ASN_1(LBContext *cxt, double d) { return asin(d); } double LB_ATN_1(LBContext *cxt, double d) { return atan(d); } LBString LB_CHR$_1(LBContext *cxt, double val) { LBString ret = lbstr_str("1"); ret->data[0] = (char)val; return ret; } double LB_COS_1(LBContext *cxt, double d) { return cos(d); } LBString LB_DATE$_0(LBContext *cxt) { time_t t = time(0); char data[128]; struct tm *tv = localtime(&t); sprintf(data, "%d/%d/%d", tv->tm_mon + 1, tv->tm_mday, tv->tm_year + 1900); return lbstr_str(data); } double LB_EOF_1(LBContext *cxt, LBHandle hnd) { assert(hnd.dev->dev_type & LB_DEV_STREAM); return hnd.dev->eof(cxt, hnd.data) ? -1 : 0; } double LB_EXP_1(LBContext *cxt, double d) { return exp(d); } // DECLARE FUNCTION INPUT$ handle double (TBD) LBString LB_INPUT$_2(LBContext *cxt, LBHandle hnd, double d) { return lbstr_cstr(""); } double LB_INSTR_3(LBContext *cxt, LBString haystack, LBString needle, double _pos) { int pos = (int)_pos; int ret = -1; if (pos < haystack->size) { char *results = strstr(haystack->data + pos, needle->data); if (results) { ret = results - haystack->data; } } return ret; } // This probably isn't right since it will chop // off big numbers... I am little wary of using // a long long though... double LB_INT_1(LBContext *cxt, double d) { return (double)((int)d); } LBString LB_LEFT$_2(LBContext *cxt, LBString str, double size) { LBString ret = lbstr_dirty(str); ret->size = (int)size; ret->data[ret->size] = 0; return ret; } double LB_LEN_1(LBContext *cxt, LBString str) { double len = str->size; lbstr_destroy(str); return len; } // I know this works on Win32 and Unix. // I'm sure an fstat would also work but // it probably doesn't work to well on Win32. double LB_LOF_1(LBContext *cxt, LBHandle hnd) { return 0.0; } LBString LB_LOWER$_1(LBContext *cxt, LBString str) { int i = 0; LBString ret = lbstr_dirty(str); for (i = 0; i < ret->size; i++) { ret->data[i] = tolower(ret->data[i]); } return str; } LBString LB_MID$_3(LBContext *cxt, LBString str, double pos, double len) { LBString ret = lbstr_str(&(str->data[(int)pos])); if (len < ret->size) { ret->size = (int)len; ret->data[ret->size] = 0; } lbstr_destroy(str); return ret; } LBString LB_RIGHT$_2(LBContext *cxt, LBString str, double len) { LBString ret = lbstr_str(&str->data[str->size - (int)len]); lbstr_destroy(str); return ret; } double LB_RND_1(LBContext *cxt, double d) { return ((double)random() / (double)RAND_MAX); } double LB_SIN_1(LBContext *cxt, double d) { return sin(d); } LBString LB_STR$_1(LBContext *cxt, double val) { char buf[20]; sprintf(buf, "%f", val); return lbstr_str(buf); } double LB_TAN_1(LBContext *cxt, double d) { return tan(d); } LBString LB_TIME$_0(LBContext *cxt) { time_t t = time(0); char data[128]; struct tm *tv = localtime(&t); sprintf(data, "%d:%d:%d", tv->tm_hour, tv->tm_min, tv->tm_sec); return lbstr_str(data); } LBString LB_TRIM$_1(LBContext *cxt, LBString str) { char *ptr = str->data; LBString ret; while (isspace(*ptr)) { ptr++; } ret = lbstr_str(ptr); while (ret->size && isspace(ret->data[ret->size - 1])) { ret->size--; } ret->data[ret->size] = 0; lbstr_destroy(str); return ret; } LBString LB_UPPER$_1(LBContext *cxt, LBString str) { int i = 0; LBString ret = lbstr_dirty(str); for (i = 0; i < ret->size; i++) { ret->data[i] = toupper(ret->data[i]); } return ret; } double LB_VAL_1(LBContext *cxt, LBString str) { double d = atof(str->data); lbstr_destroy(str); return d; } LBString LB_WORD$_2(LBContext *cxt, LBString str, double d) { int word = (int)d; char *ptr = str->data; int i; LBString ret; while (isspace(*ptr)) { ptr++; } for (i = 1; i < word; i++) { while (*ptr && !isspace(*ptr)) { ptr++; } while (isspace(*ptr)) { ptr++; } } ret = lbstr_str(ptr); lbstr_destroy(str); ptr = ret->data; while (*ptr && !isspace(*ptr)) { ptr++; } *ptr = 0; ret->size = ptr - ret->data; return ret; }