Subversion Repositories gelsvn

Rev

Rev 30 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "LinAlgIO.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

namespace
{

        bool Comp(istreambuf_iterator<char>& Itt,const string str)
        {
                
                int cChar=0;
                bool Ret=true;
                while(cChar<str.size() && Ret)
                {
                        Ret=(*Itt==str[cChar]);
                        ++Itt;
                        cChar++;
                }
                
                return Ret;
        }
        
        bool FindVar(istreambuf_iterator<char>& Itt,
                istreambuf_iterator<char> End,
                const string VarName)
        {
                bool bFound=false;
                
                while(!bFound)
                {
                        Itt=find(Itt,End,VarName[0]);
                        if(Itt==End)
                                return false;
                        bFound=Comp(Itt,VarName);
                }
                
                return true;
        }
        

}

namespace LinAlg
{
void ToMatlab(const CMatrix& A,
                          const std::string& VarName,
                          const std::string& FileName,
                          const bool append,
                          const std::string& Comment)
{
        long nMode;
        if(append)
                nMode=std::ios::app;
        else
                nMode=std::ios::trunc;
        std::ofstream Mfile(FileName.c_str(), nMode);
        
        
        if(!Comment.empty())
                Mfile << "% " << Comment.c_str() << "\n";
        Mfile << VarName.c_str() << "=[";
        

        const double* pRow;

        for(int cRow=0;cRow<A.Rows();cRow++)
        {
                pRow=A[cRow];
                for(int cCol=0;cCol<A.Cols();cCol++)
                {
                        Mfile << "\t" << pRow[cCol];
                }
                Mfile << ";\n";
        }
        
        Mfile << "];\n\n";
}


void FromMatlab(CMatrix& A,
                                const std::string& VarName,
                                const std::string& FileName)
{
        ifstream file(FileName.c_str());
        istreambuf_iterator<char> Itt(file);
        istreambuf_iterator<char> End;

        string VarId(VarName);
        VarId+="=[";

        FindVar(Itt,End,VarId);

        vector<double> data;
        double elem;
        int nCols=0;

        while((*Itt)!=';')      
        {
                Itt=(file >> elem );
                data.push_back(elem);
                nCols++;
        }

        Itt++;
        Itt++;
        int nElems=nCols;

        while((*Itt)!=']')
        {
                while((*Itt)!=';')      
                {
                        Itt=(file >> elem );
                        data.push_back(elem);
                        nElems++;
                }
                Itt++;
                Itt++;
        }

        A.Resize(nElems/nCols,nCols);

        memcpy(A[0],&(data[0]),sizeof(double)*data.size());


/*      for(int cI=0;cI<20;cI++)
        {
                cout << *Itt;
                ++Itt;
        }
*/
/*      for(int cI=0;cI<data.size();cI++)
                cout << data[cI] << "\t";

        cout << endl << nCols << endl << nElems << endl;
*/
        
}

}