Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// CIL.cpp: implementation of the CCIL class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include "SDef.h"
Toshihiro Shimizu 890ddd
#include "CIL.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CCIL::isRange(const char *s) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)strlen(s); i++)
Toshihiro Shimizu 890ddd
		if (s[i] == '-')
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CCIL::getRangeBegin(const char *s) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char ss[100];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	strcpy(ss, s);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)strlen(ss); i++)
Toshihiro Shimizu 890ddd
		if (ss[i] == '-') {
Toshihiro Shimizu 890ddd
			ss[i] = '\0';
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	if (strlen(ss) == 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CCIL::getRangeEnd(const char *s) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char ss[100];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
	for (i = strlen(s) - 1; i >= 0; i--)
Toshihiro Shimizu 890ddd
		if (s[i] == '-')
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	strcpy(ss, &s[i + 1]);
Toshihiro Shimizu 890ddd
	if (strlen(ss) == 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CCIL::strToColorIndex(const char *s, CCIL &cil,
Toshihiro Shimizu 890ddd
						   const int maxIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* If s=="-1", all of the indices are put into the ColorIndexList
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	if (strcmp(s, "-1") == 0) {
Toshihiro Shimizu 890ddd
		for (int i = 0; i <= maxIndex && cil.m_nb < MAXNBCI; i++)
Toshihiro Shimizu 890ddd
			cil.m_ci[cil.m_nb++] = i;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (isRange(s)) {
Toshihiro Shimizu 890ddd
		int begin = getRangeBegin(s);
Toshihiro Shimizu 890ddd
		int end = getRangeEnd(s);
Toshihiro Shimizu 890ddd
		if (begin >= 0 && end >= 0) {
Toshihiro Shimizu 890ddd
			begin = MIN(begin, maxIndex);
Toshihiro Shimizu 890ddd
			end = MIN(end, maxIndex);
Toshihiro Shimizu 890ddd
			for (int i = MIN(begin, end); i <= MAX(begin, end) && cil.m_nb < MAXNBCI; i++)
Toshihiro Shimizu 890ddd
				cil.m_ci[cil.m_nb++] = i;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (cil.m_nb < MAXNBCI) {
Toshihiro Shimizu 890ddd
			int q = atoi(s);
Toshihiro Shimizu 890ddd
			if (q >= 0 && q <= maxIndex)
Toshihiro Shimizu 890ddd
				cil.m_ci[cil.m_nb++] = q;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int cilCompare(const void *a, const void *b)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int *aa;
Toshihiro Shimizu 890ddd
	int *bb;
Toshihiro Shimizu 890ddd
	aa = (int *)a;
Toshihiro Shimizu 890ddd
	bb = (int *)b;
Toshihiro Shimizu 890ddd
	if (*aa == *bb)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	if (*aa < *bb)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CCIL::set(const char *s, const int maxIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int ln = strlen(s);
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	char ss[100];
Toshihiro Shimizu 890ddd
	CCIL tmpCil;
Toshihiro Shimizu 890ddd
	m_nb = tmpCil.m_nb = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (strlen(s) == 0 || strstr(s, "all") != 0 || strstr(s, "ALL") != 0)
Toshihiro Shimizu 890ddd
		strToColorIndex("-1", tmpCil, maxIndex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		for (i = 0; i < ln; i++) {
Toshihiro Shimizu 890ddd
			if (s[i] != ',') {
Toshihiro Shimizu 890ddd
				strcpy(ss, &s[i]);
Toshihiro Shimizu 890ddd
				for (j = 0; j < (int)strlen(ss); j++)
Toshihiro Shimizu 890ddd
					if (ss[j] == ',') {
Toshihiro Shimizu 890ddd
						ss[j] = '\0';
Toshihiro Shimizu 890ddd
						break;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				i += (strlen(ss) - 1);
Toshihiro Shimizu 890ddd
				strToColorIndex(ss, tmpCil, maxIndex);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	qsort(tmpCil.m_ci, tmpCil.m_nb, sizeof(int), cilCompare);
Toshihiro Shimizu 890ddd
	if (tmpCil.m_nb > 0) {
Toshihiro Shimizu 890ddd
		m_ci[0] = tmpCil.m_ci[0];
Toshihiro Shimizu 890ddd
		m_nb = 1;
Toshihiro Shimizu 890ddd
		for (i = j = 1; i < tmpCil.m_nb; i++)
Toshihiro Shimizu 890ddd
			if (tmpCil.m_ci[i] != tmpCil.m_ci[i - 1])
Toshihiro Shimizu 890ddd
				m_ci[m_nb++] = tmpCil.m_ci[i];
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		m_nb = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CCIL::isIn(const int ci)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < m_nb && ci >= m_ci[i]; i++)
Toshihiro Shimizu 890ddd
		if (ci == m_ci[i])
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CCIL::print()
Toshihiro Shimizu 890ddd
{ /*
Toshihiro Shimizu 890ddd
	tmsg_info("Nb=%d",m_nb);
Toshihiro Shimizu 890ddd
	for( int i=0; i
Toshihiro Shimizu 890ddd
		tmsg_info("ci[%d]=%d",i,m_ci[i]);*/
Toshihiro Shimizu 890ddd
}