/*
 * MATLAB Compiler: 3.0
 * Date: Mon Sep 20 09:40:08 2004
 * Arguments: "-B" "macro_default" "-O" "all" "-O" "fold_scalar_mxarrays:on"
 * "-O" "fold_non_scalar_mxarrays:on" "-O" "optimize_integer_for_loops:on" "-O"
 * "array_indexing:on" "-O" "optimize_conditionals:on" "-B" "sgl" "-m" "-W"
 * "main" "-L" "C" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg"
 * "libmwsglm.mlib" "recarga_multiple" 
 */
#include "textread.h"
#include "dataread_mex_interface.h"
#include "libmatlbm.h"
#include "libmmfile.h"
static mxArray * _mxarray0_;
static double _ieee_plusinf_;
static mxArray * _mxarray1_;
static mxArray * _mxarray2_;

static mxChar _array4_[15] = { 'F', 'i', 'l', 'e', ' ', 'n', 'o', 't',
                               ' ', 'f', 'o', 'u', 'n', 'd', '.' };
static mxArray * _mxarray3_;

static mxChar _array6_[4] = { 'f', 'i', 'l', 'e' };
static mxArray * _mxarray5_;

void InitializeModule_textread(void) {
    _mxarray0_ = mclInitializeDouble(1.0);
    _ieee_plusinf_ = mclGetInf();
    _mxarray1_ = mclInitializeDouble(_ieee_plusinf_);
    _mxarray2_ = mclInitializeDouble(2.0);
    _mxarray3_ = mclInitializeString(15, _array4_);
    _mxarray5_ = mclInitializeString(4, _array6_);
}

void TerminateModule_textread(void) {
    mxDestroyArray(_mxarray5_);
    mxDestroyArray(_mxarray3_);
    mxDestroyArray(_mxarray2_);
    mxDestroyArray(_mxarray1_);
    mxDestroyArray(_mxarray0_);
}

static mxArray * Mtextread(int nargout_, mxArray * varargin);

_mexLocalFunctionTable _local_function_table_textread
  = { 0, (mexFunctionTableEntry *)NULL };

/*
 * The function "mlfNTextread" contains the nargout interface for the
 * "textread" M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\textread.m" (lines 1-173).
 * This interface is only produced if the M-function uses the special variable
 * "nargout". The nargout interface allows the number of requested outputs to
 * be specified via the nargout argument, as opposed to the normal interface
 * which dynamically calculates the number of outputs based on the number of
 * non-NULL inputs it receives. This function processes any input arguments and
 * passes them to the implementation version of the function, appearing above.
 */
mxArray * mlfNTextread(int nargout, mlfVarargoutList * varargout, ...) {
    mxArray * varargin = NULL;
    mlfVarargin(&varargin, varargout, 0);
    mlfEnterNewContext(0, -1, varargin);
    nargout += mclNargout(varargout);
    *mlfGetVarargoutCellPtr(varargout) = Mtextread(nargout, varargin);
    mlfRestorePreviousContext(0, 0);
    mxDestroyArray(varargin);
    return mlfAssignOutputs(varargout);
}

/*
 * The function "mlfTextread" contains the normal interface for the "textread"
 * M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\textread.m" (lines 1-173).
 * This function processes any input arguments and passes them to the
 * implementation version of the function, appearing above.
 */
mxArray * mlfTextread(mlfVarargoutList * varargout, ...) {
    mxArray * varargin = NULL;
    int nargout = 0;
    mlfVarargin(&varargin, varargout, 0);
    mlfEnterNewContext(0, -1, varargin);
    nargout += mclNargout(varargout);
    *mlfGetVarargoutCellPtr(varargout) = Mtextread(nargout, varargin);
    mlfRestorePreviousContext(0, 0);
    mxDestroyArray(varargin);
    return mlfAssignOutputs(varargout);
}

/*
 * The function "mlfVTextread" contains the void interface for the "textread"
 * M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\textread.m" (lines 1-173). The
 * void interface is only produced if the M-function uses the special variable
 * "nargout", and has at least one output. The void interface function
 * specifies zero output arguments to the implementation version of the
 * function, and in the event that the implementation version still returns an
 * output (which, in MATLAB, would be assigned to the "ans" variable), it
 * deallocates the output. This function processes any input arguments and
 * passes them to the implementation version of the function, appearing above.
 */
void mlfVTextread(mxArray * synthetic_varargin_argument, ...) {
    mxArray * varargin = NULL;
    mxArray * varargout = NULL;
    mlfVarargin(&varargin, synthetic_varargin_argument, 1);
    mlfEnterNewContext(0, -1, varargin);
    varargout = Mtextread(0, synthetic_varargin_argument);
    mlfRestorePreviousContext(0, 0);
    mxDestroyArray(varargin);
}

/*
 * The function "mlxTextread" contains the feval interface for the "textread"
 * M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\textread.m" (lines 1-173). The
 * feval function calls the implementation version of textread through this
 * function. This function processes any input arguments and passes them to the
 * implementation version of the function, appearing above.
 */
void mlxTextread(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    mxArray * mprhs[1];
    mxArray * mplhs[1];
    int i;
    for (i = 0; i < 1; ++i) {
        mplhs[i] = NULL;
    }
    mlfEnterNewContext(0, 0);
    mprhs[0] = NULL;
    mlfAssign(&mprhs[0], mclCreateVararginCell(nrhs, prhs));
    mplhs[0] = Mtextread(nlhs, mprhs[0]);
    mclAssignVarargoutCell(0, nlhs, plhs, mplhs[0]);
    mlfRestorePreviousContext(0, 0);
    mxDestroyArray(mprhs[0]);
}

/*
 * The function "Mtextread" is the implementation version of the "textread"
 * M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\textread.m" (lines 1-173). It
 * contains the actual compiled code for that M-function. It is a static
 * function and must only be called from one of the interface functions,
 * appearing below.
 */
/*
 * function varargout = textread(varargin);
 */
static mxArray * Mtextread(int nargout_, mxArray * varargin) {
    mexLocalFunctionTable save_local_function_table_
      = mclSetCurrentLocalFunctionTable(&_local_function_table_textread);
    int nargin_ = mclNargin(-1, varargin, NULL);
    mxArray * varargout = NULL;
    mxArray * _T0_ = NULL;
    mxArray * nlhs = NULL;
    mxArray * ans = NULL;
    mclCopyArray(&varargin);
    /*
     * %TEXTREAD Read formatted data from text file.
     * %    A = TEXTREAD('FILENAME')
     * %    A = TEXTREAD('FILENAME','',N)
     * %    A = TEXTREAD('FILENAME','',param,value, ...)
     * %    A = TEXTREAD('FILENAME','',N,param,value, ...) reads numeric data from
     * %    the file FILENAME into a single variable.  If the file contains any
     * %    text data, an error is produced.
     * %
     * %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT')
     * %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N)
     * %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',param,value, ...)
     * %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N,param,value, ...) reads
     * %    data from the file FILENAME into the variables A,B,C,etc.  The type of
     * %    each return argument is given by the FORMAT string.  The number of
     * %    return arguments must match the number of conversion specifiers in the
     * %    FORMAT string.  If there are fewer fields in the file than in the
     * %    format string, an error is produced.  See FORMAT STRINGS below for
     * %    more information.
     * %
     * %    If N is specified, the format string is reused N times.  If N is -1 (or
     * %    not specified) TEXTREAD reads the entire file.
     * %
     * %    If param,value pairs are supplied, user configurable options customize
     * %    the behavior of TEXTREAD.  See USER CONFIGURABLE OPTIONS below.
     * %
     * %    TEXTREAD works by matching and converting groups of characters from the
     * %    file. An input field is defined as a string of non-whitespace
     * %    characters extending to the next whitespace or delimiter character
     * %    or until the field width is exhausted.  Repeated delimiter characters
     * %    are significant while repeated whitespace characters are treated as
     * %    one.
     * %
     * %    FORMAT STRINGS
     * %
     * %    If the FORMAT string is empty, TEXTREAD will only numeric data.
     * %
     * %    The FORMAT string can contain whitespace characters (which are
     * %    ignored), ordinary characters (which are expected to match the next
     * %    non-whitespace character in the input), or conversion specifications.
     * %
     * %    If whitespace is set to '' and format types are %s,%q,%[...] and %[^...]. 
     * %    Else whitespace characters are ignored.
     * %
     * %    Supported conversion specifications:
     * %        %n - read a number - float or integer (returns double array)
     * %             %5n reads up to 5 digits or until next delimiter
     * %        %d - read a signed integer value (returns double array)
     * %             %5d reads up to 5 digits or until next delimiter
     * %        %u - read an integer value (returns double array)
     * %             %5u reads up to 5 digits or until next delimiter
     * %        %f - read a floating point value (returns double array)
     * %             %5f reads up to 5 digits or until next delimiter
     * %        %s - read a whitespace separated string (returns cellstr)
     * %             %5s reads up to 5 characters or until whitespace
     * %        %q - read a (possibly double quoted) string (returns cellstr)
     * %             %5q reads up to 5 non-quote characters or until whitespace
     * %        %c - read character or whitespace (returns char array)
     * %             %5c reads up to 5 characters including whitespace
     * %        %[...]  - reads characters matching characters between the
     * %                  brackets until first non-matching character or
     * %                  whitespace (returns cellstr)
     * %                  use %[]...] to include ]
     * %             %5[...] reads up to 5 characters
     * %        %[^...] - reads characters not matching characters between the
     * %                  brackets until first matching character or whitespace
     * %                  (returns cellstr)
     * %                  use %[^]...] to exclude ]
     * %             %5[^...] reads up to 5 characters
     * %
     * %    Note: Format strings are interpreted as with sprintf before parsing.
     * %    For example, textread('mydata.dat','%s\t') will search for a tab not
     * %    the character '\' followed by the character 't'.  See the Language
     * %    Reference Guide or a C manual for complete details.
     * %
     * %    Using %* instead of % in a conversion causes TEXTREAD to skip the
     * %    matching characters in the input (and no output is created for this
     * %    conversion).  The % can be followed by an optional field width to
     * %    handle fixed width fields. For example %5d reads a 5 digit integer. In
     * %    addition the %f format supports the form %<width>.<prec>f.
     * %
     * %    USER CONFIGURABLE OPTIONS
     * %
     * %    Possible param/value options are:
     * %         'bufsize'      - maximum string length in bytes (default is 4095)
     * %         'commentstyle' - one of 
     * %              'matlab'  -- characters after % are ignored
     * %              'shell'   -- characters after # are ignored
     * %              'c'       -- characters between /* and *| are ignored
     * %              'c++'    -- characters after // are ignored
     * %         'delimiter'    - delimiter characters (default is none)
     * %         'emptyvalue'   - empty cell value in delimited files (default is 0)
     * %         'endofline'    - end of line character (default determined from file)
     * %         'expchars'     - exponent characters (default is 'eEdD')
     * %         'headerlines'  - number of lines at beginning of file to skip
     * %         'whitespace'   - whitespace characters (default is ' \b\t')
     * %    
     * %    TEXTREAD is useful for reading text files with a known format.  Both
     * %    fixed and free format files can be handled.
     * %
     * %    Examples:
     * %     Suppose the text file mydata.dat contains data in the following form:
     * %        Sally    Type1 12.34 45 Yes
     * %        Joe      Type2 23.54 60 No
     * %        Bill     Type1 34.90 12 No
     * %          
     * %     Read each column into a variable
     * %       [names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');
     * %
     * %     Read first column into a cell array (skipping rest of line)
     * %       [names]=textread('mydata.dat','%s%*[^\n]')
     * %
     * %     Read first character into char array (skipping rest of line)
     * %       [initials]=textread('mydata.dat','%c%*[^\n]')
     * %
     * %     Read file as a fixed format file while skipping the doubles
     * %       [names,types,y,answer] = textread('mydata.dat','%9c%5s%*f%2d%3s');
     * %
     * %     Read file and match Type literal
     * %       [names,typenum,x,y,answer]=textread('mydata.dat','%sType%d%f%d%s');
     * %
     * %     Read m-file into cell array of strings
     * %       file = textread('fft.m','%s','delimiter','\n','whitespace','');
     * %
     * %     To read all numeric data from a delimited text file, use a single output
     * %     argument, empty format string, and the appropriate delimiter. For 
     * %     example, suppose data.csv contains:
     * %       1,2,3,4
     * %       5,6,7,8
     * %       9,10,11,12
     * %
     * %     Read the whole matrix into a single variable:
     * %       [data] = textread('data.csv','','delimiter',',');
     * %
     * %     Read the first two columns into two variables:
     * %       [col1, col2] = textread('data.csv','%n%n%*[^\n]','delimiter',',');
     * %
     * %     For files with empty cells, use the emptyvalue parameter.  Suppose
     * %     data.csv contains:
     * %       1,2,3,4,,6
     * %       7,8,9,,11,12
     * %
     * %     Read the file like this, using NaN in empty cells:
     * %       [data] = textread('data.csv','','delimiter',',','emptyvalue',NaN);
     * %
     * %     
     * %   See also DLMREAD, LOAD, STRREAD, SSCANF, XLSREAD.
     * 
     * %   Clay M. Thompson 3-3-98
     * %   Copyright 1984-2002 The MathWorks, Inc.
     * %   $Revision: 1.16 $ $Date: 2002/06/05 20:10:18 $
     * 
     * %   Implemented as a mex file.
     * 
     * % do some preliminary error checking
     * error(nargchk(1,inf,nargin));
     */
    mlfError(mlfNargchk(_mxarray0_, _mxarray1_, mlfScalar(nargin_)), NULL);
    /*
     * 
     * if (exist(varargin{1}) ~= 2 | exist(fullfile(cd,varargin{1})) ~= 2) & ~isempty(which(varargin{1}))
     */
    {
        mxArray * a_
          = mclInitialize(
              mclNe(
                mclFeval(
                  mclValueVarargout(),
                  mlxExist,
                  mlfIndexRef(mclVa(varargin, "varargin"), "{?}", _mxarray0_),
                  NULL),
                _mxarray2_));
        if (mlfTobool(a_)) {
            mlfAssign(&a_, mlfScalar(1));
        } else {
            mlfAssign(
              &a_,
              mclOr(
                a_,
                mclNe(
                  mlfExist(
                    mlfFullfile(
                      mlfNCd(1, NULL),
                      mlfIndexRef(
                        mclVa(varargin, "varargin"), "{?}", _mxarray0_),
                      NULL),
                    NULL),
                  _mxarray2_)));
        }
        if (mlfTobool(a_)
            && mlfTobool(
                 mclAnd(
                   a_,
                   mclNot(
                     mlfIsempty(
                       mlfWhich(
                         mlfIndexRef(
                           mclVa(varargin, "varargin"), "{?}", _mxarray0_),
                         NULL)))))) {
            mxDestroyArray(a_);
            /*
             * varargin{1} = which(varargin{1});
             */
            mlfIndexAssign(
              &varargin,
              "{?}",
              _mxarray0_,
              mlfWhich(
                mlfIndexRef(mclVa(varargin, "varargin"), "{?}", _mxarray0_),
                NULL));
        } else {
            mxDestroyArray(a_);
        }
    /*
     * end
     */
    }
    /*
     * 
     * if exist(varargin{1}) ~= 2
     */
    if (mclNeBool(
          mclFeval(
            mclValueVarargout(),
            mlxExist,
            mlfIndexRef(mclVa(varargin, "varargin"), "{?}", _mxarray0_),
            NULL),
          _mxarray2_)) {
        /*
         * error('File not found.');
         */
        mlfError(_mxarray3_, NULL);
    /*
     * end
     */
    }
    /*
     * 
     * if nargout == 0
     */
    if (nargout_ == 0) {
        /*
         * nlhs = 1;
         */
        mlfAssign(&nlhs, _mxarray0_);
    /*
     * else
     */
    } else {
        /*
         * nlhs = nargout;
         */
        mlfAssign(&nlhs, mlfScalar(nargout_));
    /*
     * end
     */
    }
    mlfAssign(&_T0_, mlfColon(_mxarray0_, mclVv(nlhs, "nlhs"), NULL));
    /*
     * 
     * [varargout{1:nlhs}]=dataread('file',varargin{:});
     */
    mlfNDataread(
      0,
      mlfIndexVarargout(&varargout, "{?}", _T0_, NULL),
      _mxarray5_,
      mlfIndexRef(mclVa(varargin, "varargin"), "{?}", mlfCreateColonIndex()),
      NULL);
    mxDestroyArray(ans);
    mxDestroyArray(nlhs);
    mxDestroyArray(_T0_);
    mxDestroyArray(varargin);
    mclSetCurrentLocalFunctionTable(save_local_function_table_);
    return varargout;
}
