octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

oct2mat patch


From: Muthiah Annamalai
Subject: oct2mat patch
Date: Wed, 16 Jan 2008 00:32:33 -0600
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

Please let me know your decisions about the patches for
oct2mat conversion? I can do what you require.

Also I have problems in the driver code, in that
I am not restoring the buffer back after parsing
a script file, and the interpreter quits after
the function call. The driver routine is attached.
Can you help me with this also?

Regards,
- Muthu

/*

Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
              2005, 2006, 2007 John W. Eaton

Copyright (C) 2008, Muthiah Annamalai

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave 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 General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/


#include<string>

#include <cstdio>
#include <cstring>
#include<vector>
#include <set>
#include <string>

#include <iostream>
#include <octave/oct.h>
#include <octave/dynamic-ld.h>
#include <octave/oct-map.h>
#include <octave/oct-stream.h>
#include "parse.h"

#include <octave/file-stat.h>
#include <octave/oct-env.h>
#include <octave/Cell.h>
#include <octave/dirfns.h>
#include <octave/error.h>
#include <octave/lex.h>
#include <octave/load-path.h>
#include <octave/oct-map.h>
#include <octave/oct-obj.h>
#include <octave/ov.h>
#include <octave/ov-usr-fcn.h>
#include <octave/symtab.h>
#include <octave/toplev.h>
#include <octave/unwind-prot.h>
#include <octave/file-ops.h>
#include <octave/utils.h>
#include <octave/pt-all.h>

#include <octave/cmd-edit.h>

// 
// FIXME: somehow the parser quits the program after function
// returns on me. This is a new-bug. Also I dont want the parser
// to 'fold()' the expressions. Eeks!! This makes life easier.
// 


static void
restore_input_stream (void *f)
{
  command_editor::set_input_stream (static_cast<FILE *> (f));
}

DEFUN_DLD(oct2mat,args,,
      "converts the given file to matlab compatible script.\
\n Useage: oct2mat(filename)")
{

  if ( args.length() < 1 ) {
    print_usage();
    error("usage: oct2mat(filename,is_a_function);");
    return octave_value();
  }

  std::string fname = args(0).string_value();
  bool is_a_function = false;

  if ( args.length() >= 2 ) {
    is_a_function = args(1).is_true();
  }
  
  fname = file_ops::tilde_expand ( fname );
  fname = file_ops::canonicalize_file_name( fname );

  unwind_protect::begin_frame ("oct2mat");
  unwind_protect_bool( parser_folding_and_optimization_flag );
  parser_disable_optimization();

  if ( is_a_function )
    {
      tree_print_code o2mat(octave_stdout,"",true,true);
      octave_function * fcn = NULL;
      
      fcn = load_fcn_from_file ( fname, "" , "", "", false );
      if ( fcn ) 
        {
          fcn->accept(o2mat);
          octave_stdout<<"\n";
        } 
      else 
        {
          error(" cannot find handle to current parsed function! ");
          unwind_protect::run_frame("oct2mat");
          return octave_value();
        }
    } 
  else 
    {

      FILE *f = get_input_from_file (fname, 0);
  
      if ( !f ) {
        error("cannot open file", fname.c_str());
        return octave_value();
      }
  

      unwind_protect_str (curr_fcn_file_name);
      unwind_protect_str (curr_fcn_file_full_name);

      curr_fcn_file_name = fname;
      curr_fcn_file_full_name = fname;

      FILE *in_stream = command_editor::get_input_stream ();
      unwind_protect::add (restore_input_stream, in_stream);

      unwind_protect::begin_frame ("oct2mat_parse");  
  
      unwind_protect_ptr (ff_instream);
      unwind_protect_ptr (global_command);  
      unwind_protect_bool (line_editing);
      unwind_protect_bool (get_input_from_eval_string);
      unwind_protect_bool (parser_end_of_input);

      YY_BUFFER_STATE old_buf = current_buffer ();
      YY_BUFFER_STATE new_buf = create_buffer (f);
  
      unwind_protect::add (restore_input_buffer, old_buf);
      unwind_protect::add (delete_input_buffer, new_buf);
  
      switch_to_buffer (new_buf);

      line_editing = false;
      get_input_from_eval_string = false;
      parser_end_of_input = false;
    
      int retval;

      do
        {
          reset_parser ();
          retval = octave_parse ();
          
          if (retval == 0)
            {
              tree_print_code o2mat(octave_stdout,"",true,true);
            
              if (global_command)
                {
                  //handle the user script
                  global_command->accept(o2mat);
                  octave_stdout<<"\n";
                  global_command = 0;
                }
              else if (parser_end_of_input)
                {
                  break;
                }
              //
              // FIXME: if functions are found in a file middle,
              // generate new output file like Matlab respects, and
              // then create a handle for that.
              //
              //else if ( current_parsed_function() ) {
              //  current_parsed_function()->accept(o2mat);
              //  octave_stdout<<"\n";
              //}
              //else {
              //  octave_stdout << "no matches; possibly function undetected 
\n";
              //}
            }
          else
            {
              error("parse error!");
              break;
            }
        } while ( retval == 0 );
      
      unwind_protect::run_frame ("oct2mat_parse");
      switch_to_buffer( old_buf );
    }
  unwind_protect::run_frame ("oct2mat");
  
  return octave_value();
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]