/*
 * 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 "iofun_private_guessdelim.h"
#include "libmatlbm.h"
static mxArray * _mxarray0_;

static mxChar _array2_[2] = { 0x005c, 'n' };
static mxArray * _mxarray1_;

static mxChar _array4_[2] = { 0x005c, 't' };
static mxArray * _mxarray3_;

static mxChar _array6_[1] = { ',' };
static mxArray * _mxarray5_;

static mxChar _array8_[1] = { ';' };
static mxArray * _mxarray7_;

static mxChar _array10_[1] = { ':' };
static mxArray * _mxarray9_;

static mxChar _array12_[1] = { '|' };
static mxArray * _mxarray11_;

static mxChar _array14_[1] = { ' ' };
static mxArray * _mxarray13_;
static mxArray * _mxarray15_;
static mxArray * _mxarray16_;
static mxArray * _mxarray17_;
static mxArray * _mxarray18_;

void InitializeModule_iofun_private_guessdelim(void) {
    _mxarray0_ = mclInitializeCharVector(0, 0, (mxChar *)NULL);
    _mxarray1_ = mclInitializeString(2, _array2_);
    _mxarray3_ = mclInitializeString(2, _array4_);
    _mxarray5_ = mclInitializeString(1, _array6_);
    _mxarray7_ = mclInitializeString(1, _array8_);
    _mxarray9_ = mclInitializeString(1, _array10_);
    _mxarray11_ = mclInitializeString(1, _array12_);
    _mxarray13_ = mclInitializeString(1, _array14_);
    _mxarray15_ = mclInitializeCellVector(0, 0, (mxArray * *)NULL);
    _mxarray16_ = mclInitializeDoubleVector(0, 0, (double *)NULL);
    _mxarray17_ = mclInitializeDouble(0.0);
    _mxarray18_ = mclInitializeDouble(1.0);
}

void TerminateModule_iofun_private_guessdelim(void) {
    mxDestroyArray(_mxarray18_);
    mxDestroyArray(_mxarray17_);
    mxDestroyArray(_mxarray16_);
    mxDestroyArray(_mxarray15_);
    mxDestroyArray(_mxarray13_);
    mxDestroyArray(_mxarray11_);
    mxDestroyArray(_mxarray9_);
    mxDestroyArray(_mxarray7_);
    mxDestroyArray(_mxarray5_);
    mxDestroyArray(_mxarray3_);
    mxDestroyArray(_mxarray1_);
    mxDestroyArray(_mxarray0_);
}

static mxArray * Miofun_private_guessdelim(int nargout_, mxArray * str);

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

/*
 * The function "mlfIofun_private_guessdelim" contains the normal interface for
 * the "iofun/private/guessdelim" M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\private\guessdelim.m" (lines
 * 1-50). This function processes any input arguments and passes them to the
 * implementation version of the function, appearing above.
 */
mxArray * mlfIofun_private_guessdelim(mxArray * str) {
    int nargout = 1;
    mxArray * delim = NULL;
    mlfEnterNewContext(0, 1, str);
    delim = Miofun_private_guessdelim(nargout, str);
    mlfRestorePreviousContext(0, 1, str);
    return mlfReturnValue(delim);
}

/*
 * The function "mlxIofun_private_guessdelim" contains the feval interface for
 * the "iofun/private/guessdelim" M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\private\guessdelim.m" (lines
 * 1-50). The feval function calls the implementation version of
 * iofun/private/guessdelim through this function. This function processes any
 * input arguments and passes them to the implementation version of the
 * function, appearing above.
 */
void mlxIofun_private_guessdelim(int nlhs,
                                 mxArray * plhs[],
                                 int nrhs,
                                 mxArray * prhs[]) {
    mxArray * mprhs[1];
    mxArray * mplhs[1];
    int i;
    if (nlhs > 1) {
        mlfError(
          mxCreateString(
            "Run-time Error: File: iofun/private/guessdelim Line: 1 Co"
            "lumn: 1 The function \"iofun/private/guessdelim\" was cal"
            "led with more than the declared number of outputs (1)."),
          NULL);
    }
    if (nrhs > 1) {
        mlfError(
          mxCreateString(
            "Run-time Error: File: iofun/private/guessdelim Line: 1 Co"
            "lumn: 1 The function \"iofun/private/guessdelim\" was cal"
            "led with more than the declared number of inputs (1)."),
          NULL);
    }
    for (i = 0; i < 1; ++i) {
        mplhs[i] = NULL;
    }
    for (i = 0; i < 1 && i < nrhs; ++i) {
        mprhs[i] = prhs[i];
    }
    for (; i < 1; ++i) {
        mprhs[i] = NULL;
    }
    mlfEnterNewContext(0, 1, mprhs[0]);
    mplhs[0] = Miofun_private_guessdelim(nlhs, mprhs[0]);
    mlfRestorePreviousContext(0, 1, mprhs[0]);
    plhs[0] = mplhs[0];
}

/*
 * The function "Miofun_private_guessdelim" is the implementation version of
 * the "iofun/private/guessdelim" M-function from file
 * "g:\win32\matlab_r13_sp1\toolbox\matlab\iofun\private\guessdelim.m" (lines
 * 1-50). 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 delim = guessdelim(str)
 */
static mxArray * Miofun_private_guessdelim(int nargout_, mxArray * str) {
    mexLocalFunctionTable save_local_function_table_
      = mclSetCurrentLocalFunctionTable(
          &_local_function_table_iofun_private_guessdelim);
    mxArray * delim = NULL;
    mxArray * numDelims = NULL;
    mxArray * i = NULL;
    mxArray * goodDelimCounts = NULL;
    mxArray * goodDelims = NULL;
    mxArray * delims = NULL;
    mxArray * numLines = NULL;
    mclCopyArray(&str);
    /*
     * %GUESSDELIM Take stab at default delim for this string.
     * 
     * %   Copyright 1984-2002 The MathWorks, Inc. 
     * %   $Revision: 1.5 $  $Date: 2002/06/05 20:08:52 $
     * 
     * % bail if str is empty
     * if isempty(str)
     */
    if (mlfTobool(mlfIsempty(mclVa(str, "str")))) {
        /*
         * delim = '';
         */
        mlfAssign(&delim, _mxarray0_);
        /*
         * return;
         */
        goto return_;
    /*
     * end
     */
    }
    /*
     * 
     * % count num lines
     * numLines = length(find(str == sprintf('\n')));
     */
    mlfAssign(
      &numLines,
      mlfScalar(
        mclLengthInt(
          mlfFind(
            NULL,
            NULL,
            mclEq(mclVa(str, "str"), mlfSprintf(NULL, _mxarray1_, NULL))))));
    /*
     * 
     * % set of column delimiters to try - ordered by quality as delim
     * delims = {sprintf('\t'), ',', ';', ':', '|', ' '};
     */
    mlfAssign(
      &delims,
      mlfCellhcat(
        mlfSprintf(NULL, _mxarray3_, NULL),
        _mxarray5_,
        _mxarray7_,
        _mxarray9_,
        _mxarray11_,
        _mxarray13_,
        NULL));
    /*
     * 
     * % remove any delims which don't appear at all
     * % need to rethink based on headers and footers which are plain text
     * goodDelims = {};
     */
    mlfAssign(&goodDelims, _mxarray15_);
    /*
     * goodDelimCounts = [];
     */
    mlfAssign(&goodDelimCounts, _mxarray16_);
    /*
     * for i = 1:length(delims)
     */
    {
        int v_ = mclForIntStart(1);
        int e_ = mclLengthInt(mclVv(delims, "delims"));
        if (v_ > e_) {
            mlfAssign(&i, _mxarray16_);
        } else {
            /*
             * numDelims(i) = length(find(str == sprintf(delims{i})));
             * if numDelims(i) ~= 0
             * % this could be a delim
             * goodDelims{end+1} = delims{i};
             * end
             * end
             */
            for (; ; ) {
                mclIntArrayAssign1(
                  &numDelims,
                  mlfScalar(
                    mclLengthInt(
                      mlfFind(
                        NULL,
                        NULL,
                        mclEq(
                          mclVa(str, "str"),
                          mclFeval(
                            mclValueVarargout(),
                            mlxSprintf,
                            mlfIndexRef(
                              mclVv(delims, "delims"), "{?}", mlfScalar(v_)),
                            NULL))))),
                  v_);
                if (mclNeBool(
                      mclIntArrayRef1(mclVv(numDelims, "numDelims"), v_),
                      _mxarray17_)) {
                    mlfIndexAssign(
                      &goodDelims,
                      "{?}",
                      mclPlus(
                        mlfEnd(
                          mclVv(goodDelims, "goodDelims"),
                          _mxarray18_,
                          _mxarray18_),
                        _mxarray18_),
                      mlfIndexRef(
                        mclVv(delims, "delims"), "{?}", mlfScalar(v_)));
                }
                if (v_ == e_) {
                    break;
                }
                ++v_;
            }
            mlfAssign(&i, mlfScalar(v_));
        }
    }
    /*
     * 
     * % if no delims were found, return empty string
     * if isempty(goodDelims)
     */
    if (mlfTobool(mlfIsempty(mclVv(goodDelims, "goodDelims")))) {
        /*
         * delim = '';
         */
        mlfAssign(&delim, _mxarray0_);
        /*
         * return;
         */
        goto return_;
    /*
     * end
     */
    }
    /*
     * 
     * % if the num delims is greater or equal to num lines,
     * % this will be the default (so return)
     * for i = 1:length(delims)
     */
    {
        int v_ = mclForIntStart(1);
        int e_ = mclLengthInt(mclVv(delims, "delims"));
        if (v_ > e_) {
            mlfAssign(&i, _mxarray16_);
        } else {
            /*
             * delim = delims{i};
             * if numDelims(i) > numLines
             * return;
             * end
             * end
             */
            for (; ; ) {
                mlfAssign(
                  &delim,
                  mlfIndexRef(mclVv(delims, "delims"), "{?}", mlfScalar(v_)));
                if (mclGtBool(
                      mclIntArrayRef1(mclVv(numDelims, "numDelims"), v_),
                      mclVv(numLines, "numLines"))) {
                    goto return_;
                }
                if (v_ == e_) {
                    break;
                }
                ++v_;
            }
            mlfAssign(&i, mlfScalar(v_));
        }
    }
    /*
     * 
     * % no delimiter was a clear win from above, choose the first
     * % in the delimiter list
     * delim = goodDelims{1};
     */
    mlfAssign(
      &delim, mlfIndexRef(mclVv(goodDelims, "goodDelims"), "{?}", _mxarray18_));
    /*
     * 
     */
    return_:
    mclValidateOutput(delim, 1, nargout_, "delim", "iofun/private/guessdelim");
    mxDestroyArray(numLines);
    mxDestroyArray(delims);
    mxDestroyArray(goodDelims);
    mxDestroyArray(goodDelimCounts);
    mxDestroyArray(i);
    mxDestroyArray(numDelims);
    mxDestroyArray(str);
    mclSetCurrentLocalFunctionTable(save_local_function_table_);
    return delim;
}
