package com.jmatio.types;

import java.util.Arrays;

public class MLChar extends MLArray implements GenericArrayCreator<Character>
{
    Character[] chars;
    public MLChar(String name, String value )
    {
        this( name, new int[] { 1, value.length() } , MLArray.mxCHAR_CLASS, 0);
        set(value);
    }
    public MLChar(String name, int[] dims, int type, int attributes)
    {
        super(name, dims, type, attributes);
        chars = createArray(getM(), getN());
    }

    public Character[] createArray(int m, int n)
    {
        return new Character[m*n];
    }
    public void setChar(char ch, int index)
    {
        chars[index] = new Character(ch);
    }
    public void set(String value)
    {
        char[] cha = value.toCharArray();
        for ( int i = 0; i < getN() &&  i < value.length(); i++ )
        {
            setChar(cha[i], i);
        }
    }
    
    public Character getChar(int m, int n)
    {
        return chars[getIndex(m,n)];
    }
    public Character[] exportChar()
    {
        return chars;
    }
    
    @Override
    public boolean equals(Object o)
    {
        if ( o instanceof MLChar )
        {
            return Arrays.equals( chars, ((MLChar)o).chars );
        }
        return super.equals( o );
    }
    
    /**
     * Gets the m-th character matrix's row as <code>String</code>.
     * 
     * @param m - row number
     * @return - <code>String</code>
     */
    public String getString( int m )
    {
        StringBuffer charbuff = new StringBuffer();
        
        for (int n = 0; n < getN(); n++)
        {
            charbuff.append(getChar(m, n));
        }
        
        return charbuff.toString();
    }
    
    public String contentToString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append(name + " = \n");
        
        for ( int m = 0; m < getM(); m++ )
        {
           sb.append("\t");
           StringBuffer charbuff = new StringBuffer();
           charbuff.append("'");
           for ( int n = 0; n < getN(); n++ )
           {
               charbuff.append( getChar(m,n) );
           }
           charbuff.append("'");
           sb.append(charbuff);
           sb.append("\n");
        }
        return sb.toString();
        
    }
}

@@ -299,32 +299,470 @@
 t(value);%0A    %7D%0A
+    /**%0A     * Added method to allow initialization of a char array representing %0A     * an array of strings.%0A     * %0A     * @param name%0A     * @param values%0A     * @param maxlen%0A     */%0A    public MLChar(String name, String%5B%5D values, int maxlen)%0A    %7B%0A    %09this( name, new int%5B%5D %7B values.length, maxlen %7D, MLArray.mxCHAR_CLASS, 0 );%0A    %09int idx = 0;%0A    %09for (String v : values)%0A    %09%7B%0A    %09%09set(v, idx);%0A    %09%09idx++;%0A    %09%7D%0A    %7D%0A    %0A
     public MLCha
@@ -1314,32 +1314,523 @@
    %7D%0A    %7D%0A    %0A
+    /** %0A     * Set one row, specifying the row.%0A     * %0A     * @param value%0A     * @param idx%0A     */%0A    public void set(String value, int idx)%0A    %7B%0A        char%5B%5D cha = value.toCharArray();%0A        int rowOffset = getM();%0A        for ( int i = 0; i %3C getN(); i++ )%0A        %7B%0A        %09if ( i %3C value.length())%0A        %09%7B%0A        %09%09setChar(cha%5Bi%5D, idx + (rowOffset * i));%0A        %09%7D%0A        %09else %0A        %09%7B%0A        %09%09setChar(' ',  idx + (rowOffset * i));%0A        %09%7D%0A        %7D%0A    %7D%0A    %0A
     public Chara

Update MLChar to support multi-dimensional char arrays, in addition to single strings