Newer
Older
TB_Chris / TbUT / src / alibava / .svn / text-base / ChanList.cpp.svn-base
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include "ChanList.h"
  8.  
  9. static char svStr[8192];
  10.  
  11. //////////////////////////////////////////////////////////////////////////
  12. // ChanList class
  13. //
  14. // Created: Mon Sep 7 18:50:38 1998 Author: Carlos Lacasta
  15. // Purpose:
  16. //
  17. //////////////////////////////////////////////////////////////////////////
  18. int cmp(const void *x1, const void *x2)
  19. {
  20. double d1 = *(double *) x1 - *(double *) x2;
  21. return (d1 == 0. ? 0 : (d1 < 0. ? -1 : 1));
  22. }
  23. int icmp(const void *x1, const void *x2)
  24. {
  25. int d1 = *(int *) x1 - *(int *) x2;
  26. return (d1 == 0 ? 0 : (d1 < 0 ? -1 : 1));
  27. }
  28.  
  29. ChanList::ChanList(int i1, int /*i2*/) :
  30. nch(0), cm(0.), noise(0.)
  31. {
  32. for (int i = 0; i < nch; i++)
  33. ch.push_back(i1 + i);
  34.  
  35. nch = ch.size();
  36. }
  37. ChanList::ChanList(const char *str) :
  38. cm(0.), noise(0.)
  39. {
  40. if (str == 0)
  41. return;
  42. Set(str);
  43. }
  44.  
  45. ChanList::ChanList(const ChanList &cl)
  46. {
  47. copy(cl);
  48. }
  49.  
  50. ChanList &ChanList::operator=(const ChanList &cl)
  51. {
  52. if (&cl == this)
  53. return *this;
  54. copy(cl);
  55. return *this;
  56. }
  57.  
  58. void ChanList::copy(const ChanList &cl)
  59. {
  60. if ( &cl != this )
  61. {
  62. hits = cl.hits;
  63. ch = cl.ch;
  64. nch = ch.size();
  65. cm = cl.cm;
  66. noise = cl.noise;
  67. }
  68. }
  69. int ChanList::Set(const char *str)
  70. {
  71. char *p, *q;
  72. int i, is;
  73. int ival1, ival2, wRange;
  74.  
  75. ch.clear();
  76. strcpy(svStr, str);
  77. p = q = svStr;
  78. wRange = 0;
  79. while (*p)
  80. {
  81. switch (*p)
  82. {
  83. case ',':
  84. *p = 0;
  85. if (wRange)
  86. {
  87. is = sscanf(q, "%d", &ival2);
  88. for (i = ival1 + 1; i <= ival2; i++)
  89. ch.push_back(i);
  90. }
  91. else
  92. {
  93. is = sscanf(q, "%d", &ival1);
  94. ch.push_back(ival1);
  95. }
  96. wRange = 0;
  97. q = p + 1;
  98. p += is;
  99. break;
  100. case '-':
  101. *p = 0;
  102. wRange = 1;
  103. is = sscanf(q, "%d", &ival1);
  104. ch.push_back(ival1);
  105. q = p + 1;
  106. p += is;
  107. break;
  108. }
  109. p++;
  110. }
  111. if (wRange)
  112. {
  113. is = sscanf(q, "%d", &ival2);
  114. for (i = ival1 + 1; i <= ival2; i++)
  115. ch.push_back(i);
  116. }
  117. else
  118. {
  119. is = sscanf(q, "%d", &ival2);
  120. ch.push_back(ival2);
  121. }
  122. // sort
  123. std::sort(ch.begin(), ch.end());
  124. nch = ch.size();
  125. return nch;
  126. }
  127.  
  128. std::ostream &operator<<(std::ostream &os, ChanList &cl)
  129. {
  130. for (int i = 0; i < cl.Nch(); i++)
  131. {
  132. if (i && !(i % 15))
  133. os << std::endl;
  134. os << std::setw(5) << cl[i];
  135. }
  136. return os;
  137. }
  138.  
  139. int ChanList::ParseChanList(const char *str, ChanList **cl)
  140. {
  141. int ireg, nreg = 0;
  142. std::vector<char *> clst;
  143. char *q, *p, *ptr;
  144.  
  145. if (str == 0)
  146. return 0;
  147. if (cl == 0)
  148. return 0;
  149. p = ptr = strdup(str);
  150. do
  151. {
  152. clst.push_back(p);
  153. q = strchr(p, ';');
  154. if (q == 0)
  155. break;
  156. *q++ = 0;
  157. p = q;
  158. } while (*p);
  159.  
  160. nreg = clst.size();
  161. *cl = new ChanList[nreg];
  162. for (ireg = 0; ireg < nreg; ireg++)
  163. (*cl)[ireg].Set(clst[ireg]);
  164.  
  165. free(ptr);
  166. return nreg;
  167. }