Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// CIL.cpp: implementation of the CCIL class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
e280ae
#ifdef _MSC_VER
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
Shinya Kitaoka 12c444
#include <algorithm></algorithm>
Shinya Kitaoka 12c444
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CCIL::isRange(const char *s) const {
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)strlen(s); i++)
Shinya Kitaoka 120a6e
    if (s[i] == '-') return true;
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CCIL::getRangeBegin(const char *s) const {
Shinya Kitaoka 120a6e
  char ss[100];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  strcpy(ss, s);
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)strlen(ss); i++)
Shinya Kitaoka 120a6e
    if (ss[i] == '-') {
Shinya Kitaoka 120a6e
      ss[i] = '\0';
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  if (strlen(ss) == 0) return -1;
Shinya Kitaoka 120a6e
  return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CCIL::getRangeEnd(const char *s) const {
Shinya Kitaoka 120a6e
  char ss[100];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int i = 0;
Shinya Kitaoka 120a6e
  for (i = strlen(s) - 1; i >= 0; i--)
Shinya Kitaoka 120a6e
    if (s[i] == '-') break;
Shinya Kitaoka 120a6e
  strcpy(ss, &s[i + 1]);
Shinya Kitaoka 120a6e
  if (strlen(ss) == 0) return -1;
Shinya Kitaoka 120a6e
  return atoi(ss);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CCIL::strToColorIndex(const char *s, CCIL &cil, const int maxIndex) {
Shinya Kitaoka 120a6e
  /* If s=="-1", all of the indices are put into the ColorIndexList
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  if (strcmp(s, "-1") == 0) {
Shinya Kitaoka 120a6e
    for (int i             = 0; i <= maxIndex && cil.m_nb < MAXNBCI; i++)
Shinya Kitaoka 120a6e
      cil.m_ci[cil.m_nb++] = i;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (isRange(s)) {
Shinya Kitaoka 120a6e
    int begin = getRangeBegin(s);
Shinya Kitaoka 120a6e
    int end   = getRangeEnd(s);
Shinya Kitaoka 120a6e
    if (begin >= 0 && end >= 0) {
Shinya Kitaoka 120a6e
      begin = std::min(begin, maxIndex);
Shinya Kitaoka 120a6e
      end   = std::min(end, maxIndex);
Shinya Kitaoka 120a6e
      for (int i = std::min(begin, end);
Shinya Kitaoka 120a6e
           i <= std::max(begin, end) && cil.m_nb < MAXNBCI; i++)
Shinya Kitaoka 120a6e
        cil.m_ci[cil.m_nb++] = i;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (cil.m_nb < MAXNBCI) {
Shinya Kitaoka 120a6e
      int q                                             = atoi(s);
Shinya Kitaoka 120a6e
      if (q >= 0 && q <= maxIndex) cil.m_ci[cil.m_nb++] = q;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton b3bd84
static int cilCompare(const void *a, const void *b) {
Shinya Kitaoka 120a6e
  int *aa;
Shinya Kitaoka 120a6e
  int *bb;
Shinya Kitaoka 120a6e
  aa = (int *)a;
Shinya Kitaoka 120a6e
  bb = (int *)b;
Shinya Kitaoka 120a6e
  if (*aa == *bb) return 0;
Shinya Kitaoka 120a6e
  if (*aa < *bb) return -1;
Shinya Kitaoka 120a6e
  return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CCIL::set(const char *s, const int maxIndex) {
Shinya Kitaoka 120a6e
  int ln = strlen(s);
Shinya Kitaoka 120a6e
  int i, j;
Shinya Kitaoka 120a6e
  char ss[100];
Shinya Kitaoka 120a6e
  CCIL tmpCil;
Shinya Kitaoka 120a6e
  m_nb = tmpCil.m_nb = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (strlen(s) == 0 || strstr(s, "all") != 0 || strstr(s, "ALL") != 0)
Shinya Kitaoka 120a6e
    strToColorIndex("-1", tmpCil, maxIndex);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    for (i = 0; i < ln; i++) {
Shinya Kitaoka 120a6e
      if (s[i] != ',') {
Shinya Kitaoka 120a6e
        strcpy(ss, &s[i]);
Shinya Kitaoka 120a6e
        for (j = 0; j < (int)strlen(ss); j++)
Shinya Kitaoka 120a6e
          if (ss[j] == ',') {
Shinya Kitaoka 120a6e
            ss[j] = '\0';
Shinya Kitaoka 120a6e
            break;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        i += (strlen(ss) - 1);
Shinya Kitaoka 120a6e
        strToColorIndex(ss, tmpCil, maxIndex);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  qsort(tmpCil.m_ci, tmpCil.m_nb, sizeof(int), cilCompare);
Shinya Kitaoka 120a6e
  if (tmpCil.m_nb > 0) {
Shinya Kitaoka 120a6e
    m_ci[0] = tmpCil.m_ci[0];
Shinya Kitaoka 120a6e
    m_nb    = 1;
Shinya Kitaoka 120a6e
    for (i = j = 1; i < tmpCil.m_nb; i++)
Shinya Kitaoka 120a6e
      if (tmpCil.m_ci[i] != tmpCil.m_ci[i - 1]) m_ci[m_nb++] = tmpCil.m_ci[i];
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    m_nb = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CCIL::isIn(const int ci) {
Shinya Kitaoka 120a6e
  for (int i = 0; i < m_nb && ci >= m_ci[i]; i++)
Shinya Kitaoka 120a6e
    if (ci == m_ci[i]) return true;
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CCIL::print() { /*
Shinya Kitaoka 120a6e
                           tmsg_info("Nb=%d",m_nb);
Shinya Kitaoka 120a6e
                           for( int i=0; i
Shinya Kitaoka 120a6e
                                   tmsg_info("ci[%d]=%d",i,m_ci[i]);*/
Toshihiro Shimizu 890ddd
}