/* * LIBML library - read, write and manipulate MATLAB MAT-files * Copyright (C) 1994 Michael J. Maurer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Michael Maurer * Durand Bldg - Room 232 * Stanford, CA 94305-4055 * (415) 723-1024 */ /****************************************************************************** mlmisc.c Some utility routines for handling matlab MAT files. Copyright (c) 1993 by Michael Maurer (maurer@nova.stanford.edu). ******************************************************************************/ /* $Log$ */ #include #include "libml.h" #include "libml.p" #include "libmisc.h" /****************************************************************************** fcopyn Copies L bytes from opened stream f1 to opened stream f2. Returns nonzero on error. ******************************************************************************/ int fcopyn(f1,f2,L) FILE *f1,*f2; long L; { static char buf[BUFSIZ]; int err=0,nb; while (L>BUFSIZ && !err) { err|=((nb=fread(buf,sizeof(char),BUFSIZ,f1))!=BUFSIZ); err|=(fwrite(buf,sizeof(char),nb,f2)!=nb); L-=nb; } if (L>0 && !err) { err|=((nb=fread(buf,sizeof(char),L,f1))!=L); err|=(fwrite(buf,sizeof(char),nb,f2)!=nb); } return err; } /****************************************************************************** mlcopy Copies a single Matlab-format object in fin to fout. If an error occurs, a nonzero value is returned, fin remains as it was after the error, but fout is seeked back to its position on entry. ******************************************************************************/ int mlcopy(fin,fout) FILE *fin,*fout; { int type,mrows,ncols,imagf,M,O,P,T,err; long dsize,offset; char pname[80]; #ifdef __STDC__ fpos_t fpos; #else long fpos; #endif if (err=mlreadh(fin, &type, pname, &mrows, &ncols, &imagf)) { fprintf(stderr,"[mlcopy] mlreadh returned %d\n",err); return 1; } dsize=mltype(type, &M, &O, &P, &T); offset = dsize*mrows*ncols; if (imagf) offset *= 2; #ifdef __STDC__ if (fgetpos(fout,&fpos)) { fprintf(stderr,"[mlcopy] fgetpos failed\n"); return 2; } #else if (fpos=ftell(fout)) { fprintf(stderr,"[mlcopy] ftell failed\n"); return 2; } #endif if (err=mlwriteh(fout, type, pname, mrows, ncols, imagf)) { fprintf(stderr,"[mlcopy] mlwriteh returned %d\n",err); #ifdef __STDC__ if (fsetpos(fout,&fpos)) { fprintf(stderr,"[mlcopy] fsetpos failed\n"); return 3; } #else if (fseek(fout,fpos,SEEK_SET)) { fprintf(stderr,"[mlcopy] fseek failed\n"); return 3; } #endif return 4; } if (fcopyn(fin,fout,offset)) { fprintf(stderr,"[mlcopy] write failed\n"); #ifdef __STDC__ if (fsetpos(fout,&fpos)) { fprintf(stderr,"[mlcopy] fsetpos failed\n"); return 5; } #else if (fseek(fout,fpos,SEEK_SET)) { fprintf(stderr,"[mlcopy] fseek failed\n"); return 5; } #endif return 6; } return 0; } /****************************************************************************** mlskip Skips forward a single Matlab-format object in fin. Returns nonzero on failure. ******************************************************************************/ int mlskip(fin) FILE *fin; { int type,mrows,ncols,imagf,M,O,P,T,err; long dsize,offset; char pname[80]; if (err=mlreadh(fin, &type, pname, &mrows, &ncols, &imagf)) { fprintf(stderr,"[mlskip] mlreadh returned %d\n",err); return 1; } dsize=mltype(type, &M, &O, &P, &T); offset = dsize*mrows*ncols; if (imagf) offset *= 2; if (fseek(fin, offset, SEEK_CUR)) { fprintf(stderr,"[mlskip] seek failed\n"); return 2; } return 0; }