/*\ * 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 "lb.h" #include "basic_io.h" void LB_INITIO_0(LBContext *cxt) { LBDevice *dev = 0; dev = lb_register_dev(cxt, "input", "stdio"); dev->dev_type = LB_DEV_STREAM | LB_DEV_RDONLY; dev->open = (LBDevOpen)&lb_open_file_input; dev->close = (LBDevClose)&lb_close_file; dev->eof = (LBDevEOF)&lb_eof_file; dev->lof = (LBDevLOF)&lb_lof_file; dev->read_str = (LBDevReadStr)&lb_read_file_str; dev->read_db = (LBDevReadDb)&lb_read_file_db; dev = lb_register_dev(cxt, "output", "stdio"); dev->dev_type = LB_DEV_STREAM | LB_DEV_RDWR; dev->open = (LBDevOpen)&lb_open_file_output; dev->close = (LBDevClose)&lb_close_file; dev->eof = (LBDevEOF)&lb_eof_file; dev->lof = (LBDevLOF)&lb_lof_file; dev->read_str = (LBDevReadStr)&lb_read_file_str; dev->read_db = (LBDevReadDb)&lb_read_file_db; dev->write_str = (LBDevWriteStr)&lb_write_file_str; dev->write_db = (LBDevWriteDb)&lb_write_file_db; } int lb_open_file_input(LBContext *cxt, char *name, Variable *handle) { handle->data.hnd.data = fopen(name, "r"); return (handle->data.hnd.data != 0); } int lb_open_file_output(LBContext *cxt, char *name, Variable *handle) { handle->data.hnd.data = fopen(name, "w+"); return (handle->data.hnd.data != 0); } void lb_close_file(LBContext *cxt, FILE *f) { fclose(f); } int lb_eof_file(LBContext *cxt, FILE *f) { return feof(f); } int lb_lof_file(LBContext *cxt, FILE *f) { return 0; } LBString lb_read_file_str(LBContext *cxt, FILE *f, size_t size) { LBString ret; if (size > 0) { ret = lbstr_empty(size); fread(ret->data, ret->size, 1, f); } else { ret = lbstr_empty(1024); fgets(ret->data, ret->size + 1, f); ret->size = strlen(ret->data); if (ret->data[ret->size - 1] == '\n') { ret->size--; if (ret->data[ret->size - 1] == '\r') { ret->size--; } ret->data[ret->size] = 0; } } return ret; } int lb_write_file_str(LBContext *cxt, FILE *f, LBString str) { fwrite(str->data, str->size, 1, f); return 0; } double lb_read_file_db(LBContext *cxt, FILE *f) { char buf[30]; int i = 0; while (i < (sizeof(buf) - 1) && (isdigit((buf[i] = getc(f))) || buf[i] == '.')); if (i == (sizeof(buf) - 1)) { ungetc(buf[i], f); } buf[i] = 0; return atof(buf); } int lb_write_file_db(LBContext *cxt, FILE *f, double db) { fprintf(f, "%f", db); return 4; }