@q $Id: dynamic_model.cweb 431 2005-08-16 15:41:01Z kamenik $ @>
@q Copyright 2005, Ondra Kamenik @>

@ Start of {\tt dynamic\_model.cpp} file.

@c
#include "dynamic_model.h"

@<|NameList::print| code@>;
@<|NameList::writeMat| code@>;
@<|NameList::writeMatIndices| code@>;

@ 
@<|NameList::print| code@>=
void NameList::print() const
{
	for (int i = 0; i < getNum(); i++)
		printf("%s\n", getName(i));
}

@ 
@<|NameList::writeMat| code@>=
void NameList::writeMat(mat_t* fd, const char* vname) const
{
	int maxlen = 0;
	for (int i = 0; i < getNum(); i++)
		if (maxlen < (int)strlen(getName(i)))
			maxlen = (int)strlen(getName(i));

	if (maxlen == 0)
		return;

  char *m = new char[getNum()*maxlen];

	for (int i = 0; i < getNum(); i++)
		for (int j = 0; j < maxlen; j++)
			if (j < (int)strlen(getName(i)))
				m[j*getNum()+i] = getName(i)[j];
			else
				m[j*getNum()+i] = ' ';

# if MATIO_MAJOR_VERSION > 1 || (MATIO_MAJOR_VERSION == 1 && MATIO_MINOR_VERSION >= 5)
  size_t dims[2];
  const matio_compression compression = MAT_COMPRESSION_NONE;
# else
  int dims[2];
  const int compression = COMPRESSION_NONE;
# endif
  dims[0] = getNum();
  dims[1] = maxlen;

  matvar_t *v = Mat_VarCreate(vname, MAT_C_CHAR, MAT_T_UINT8, 2, dims, m, 0);

  Mat_VarWrite(fd, v, compression);

  Mat_VarFree(v);
  delete[] m;
}

@ 
@<|NameList::writeMatIndices| code@>=
void NameList::writeMatIndices(mat_t* fd, const char* prefix) const
{
	char tmp[100];
	TwoDMatrix aux(1,1);
	for (int i = 0; i < getNum(); i++) {
		sprintf(tmp, "%s_i_%s", prefix, getName(i));
		aux.get(0,0) = i+1;
		aux.writeMat(fd, tmp);
	}
}

@ End of {\tt dynamic\_model.cpp} file.
