• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/io_eegtools.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Matthias Ihrke   *
00003  *   ihrke@nld.ds.mpg.de
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "io_eegtools.h"
00022 #include <stdlib.h>
00023 #include <stdint.h>
00024 
00025 
00026 /* -------------- READER ---------------- */
00037 Array* array_from_file( FILE *in ){
00038   uint16_t dtype;
00039   uint16_t size_dtype;
00040   uint16_t ndim;
00041   uint16_t size;
00042   uint32_t nbytes;
00043   uint *dims;
00044   int i;
00045   
00046   ffread( &dtype, 2, 1, in );
00047   ffread( &size_dtype, 2, 1, in );
00048   ffread( &ndim, 2, 1, in );
00049   MALLOC( dims, ndim, uint );
00050   for( i=0; i<ndim; i++ ){
00051      ffread( &size, 2, 1, in );
00052      dims[i]=(uint)size;
00053   }
00054   Array *a=array_new( dtype, ndim, dims );
00055   ffread( &nbytes, 4, 1, in );
00056   if( nbytes!=a->nbytes ){
00057      errprintf("Wrong number of bytes, continuing (probably dieing)\n");
00058   }
00059   ffread( a->data, 1, nbytes, in );
00060 
00061   free( dims );
00062 
00063   return a;
00064 }
00065 
00066 /* -------------- WRITER ---------------- */
00067 
00078 void array_to_file( FILE *out, const Array *a ){
00079   uint8_t  ui8;
00080   uint16_t ui16;
00081   uint32_t ui32;
00082   int i;
00083 
00084   ui16=(uint16_t)a->dtype;
00085   ffwrite( &ui16, 2, 1, out );
00086   ui16=(uint16_t)a->dtype_size;
00087   ffwrite( &ui16, 2, 1, out );
00088   ui16=(uint16_t)a->ndim;
00089   ffwrite( &ui16, 2, 1, out );
00090   for( i=0; i<a->ndim; i++ ){
00091      ui16=(uint16_t)a->size[i];
00092      ffwrite( &ui16, 2, 1, out );    
00093   }
00094   ui32=(uint32_t)a->nbytes;
00095   ffwrite( &ui32, 4, 1, out );   
00096   /* data */
00097   ffwrite( a->data, 1, a->nbytes, out );
00098 }

Generated on Fri Jun 25 2010 14:10:19 for libeegtools by  doxygen 1.7.0