"Fossies" - the Fresh Open Source Software Archive

Member "pfstools-2.2.0/src/octave/pfswrite.cpp" (12 Aug 2021, 3623 Bytes) of package /linux/privat/pfstools-2.2.0.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * @brief GNU Octave wrapper - write matrices as channels to the PFS stream
    3  * 
    4  * This file is a part of PFSTOOLS package.
    5  * ---------------------------------------------------------------------- 
    6  * Copyright (C) 2003,2004 Rafal Mantiuk and Grzegorz Krawczyk
    7  * 
    8  *  This program is free software; you can redistribute it and/or modify
    9  *  it under the terms of the GNU General Public License as published by
   10  *  the Free Software Foundation; either version 2 of the License, or
   11  *  (at your option) any later version.
   12  *
   13  *  This program is distributed in the hope that it will be useful,
   14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  *  GNU General Public License for more details.
   17  *
   18  *  You should have received a copy of the GNU General Public License
   19  *  along with this program; if not, write to the Free Software
   20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21  * ---------------------------------------------------------------------- 
   22  *
   23  * @author Rafal Mantiuk, <mantiuk@mpi-sb.mpg.de>
   24  *
   25  * $Id: pfswrite.cpp,v 1.1 2005/06/15 13:36:54 rafm Exp $
   26  */
   27 #include <string>
   28 
   29 #include <octave/oct.h>
   30 #include <octave/oct-stream.h>
   31 
   32 #include <pfs.h>
   33 
   34 #define SCRIPT_NAME "pfswrite"
   35 
   36 static const char *helpString =
   37 #include "pfswrite_help.h"
   38 
   39 DEFUN_DLD( pfswrite, args, , helpString)
   40 {
   41     octave_value_list retval;
   42     
   43     int nargin = args.length();
   44     
   45     if( nargin < 3 )
   46     {
   47         error( SCRIPT_NAME ": Improper usage!");
   48         return retval;
   49     }
   50 
   51     // Check params
   52     if( !args(0).is_string() ) {
   53       error( SCRIPT_NAME ": expected file name as the first argument!");
   54       return retval;
   55     }
   56     for( int i = 1; i < nargin; i += 2 ) 
   57       if( (i+1) >= nargin || !args(i).is_string() || !args(i+1).is_real_matrix() ) {
   58         error( SCRIPT_NAME ": expected (channelName, matrix) pair argument!");
   59         return retval;
   60       }
   61 
   62     FILE *fh;
   63     const char *fileName = args(0).string_value().c_str();
   64     if( !strcasecmp( "stdout", fileName ) ) 
   65       fh = stdout;
   66     else {
   67       fh = fopen( fileName, "ab" );
   68       if( fh == NULL ) {
   69         error( SCRIPT_NAME ": cannot open file for writing!");
   70         return retval;
   71       }
   72     }
   73     
   74     try {
   75       
   76         pfs::DOMIO ctx;
   77         pfs::Frame *frame = NULL;
   78 
   79         int rows, cols;
   80         
   81         for( int i = 1; i < nargin; i += 2 ) {
   82       
   83             Matrix mat(args(i+1).matrix_value());
   84             if( frame == NULL ) { // Fist channel
   85                 frame = ctx.createFrame(mat.cols(), mat.rows());
   86                 rows = mat.rows();
   87                 cols = mat.cols();
   88             }
   89             else if( mat.rows() != rows || mat.cols() != cols ) {
   90               // If size of this and the first channel differ
   91               error( SCRIPT_NAME ": all matrices must be of the same size");
   92               continue;
   93             }
   94             
   95       
   96             const char *channelName = args(i).string_value().c_str();
   97             pfs::Channel *channel = frame->createChannel( channelName );
   98       
   99             int index = 0;
  100             for( int r = 0; r < channel->getRows(); r++ ) // Copy octave matrix to channel
  101                 for( int c = 0; c < channel->getCols(); c++ ) {
  102                     (*channel)(index++) = mat(r,c);
  103                 }
  104         }
  105 
  106         ctx.writeFrame( frame, fh );
  107         ctx.freeFrame( frame );
  108   
  109     }
  110     catch( pfs::Exception ex )
  111     {
  112         error( "%s: %s", SCRIPT_NAME, ex.getMessage() );
  113     }
  114 
  115     if( fh != stdout ) fclose( fh );
  116   
  117     return retval;
  118 }