/opencascade-6.5.1/ros/src/PCollection/PCollection_HExtendedString.cxx

https://github.com/jehc/MondocosmOS · C++ · 583 lines · 408 code · 54 blank · 121 comment · 134 complexity · 9893edb26ec4ea61827e7854ff532314 MD5 · raw file

  1. #include <PCollection_HExtendedString.ixx>
  2. #include <PCollection_HAsciiString.hxx>
  3. #include <Standard_ExtString.hxx>
  4. #include <Standard_NumericError.hxx>
  5. #include <Standard_NegativeValue.hxx>
  6. #include <Standard_OutOfRange.hxx>
  7. #ifdef HAVE_CONFIG_H
  8. # include <config.h>
  9. #endif
  10. #if defined(HAVE_STRING_H)
  11. # include <string.h>
  12. #endif
  13. #include <stdio.h>
  14. #if defined(HAVE_STDLIB_H)
  15. #include <stdlib.h>
  16. #endif
  17. #if defined(HAVE_LIBC_H)
  18. # include <libc.h>
  19. #endif
  20. //------------------------------------------------------------------------
  21. // Create from an ExtendedString of TCollection
  22. //------------------------------------------------------------------------
  23. PCollection_HExtendedString::PCollection_HExtendedString
  24. (const TCollection_ExtendedString& S):Data(S.Length())
  25. {
  26. for( Standard_Integer i = 1; i <= Data.Length() ; i++)
  27. Data.SetValue(i-1, S.Value(i)) ;
  28. }
  29. //------------------------------------------------------------------------
  30. // Create from a ExtCharacter
  31. //------------------------------------------------------------------------
  32. PCollection_HExtendedString::PCollection_HExtendedString
  33. (const Standard_ExtCharacter C): Data(1)
  34. {
  35. Data.SetValue(0, C);
  36. }
  37. //------------------------------------------------------------------------
  38. // Create from a range of an HExtendedString of PCollection
  39. //------------------------------------------------------------------------
  40. PCollection_HExtendedString::PCollection_HExtendedString
  41. (const Handle(PCollection_HExtendedString)& S,
  42. const Standard_Integer FromIndex, const Standard_Integer ToIndex) : Data(ToIndex-FromIndex+1)
  43. {
  44. for( Standard_Integer i = 0 , k = FromIndex; i < Data.Length() ; i++, k++)
  45. Data.SetValue(i, S->Value(k));
  46. }
  47. //-----------------------------------------------------------------------
  48. // Create : from a CString
  49. //-----------------------------------------------------------------------
  50. PCollection_HExtendedString::PCollection_HExtendedString(const Standard_CString S)
  51. : Data(strlen(S))
  52. {
  53. for( Standard_Integer i = 0 ; i < Data.Length() ; i++) {
  54. Standard_ExtCharacter val = ToExtCharacter(S[i]);
  55. Data.SetValue(i, val) ;
  56. }
  57. }
  58. //------------------------------------------------------------------------
  59. // Create from an HAsciiString from PCollection
  60. //------------------------------------------------------------------------
  61. PCollection_HExtendedString::PCollection_HExtendedString
  62. (const Handle(PCollection_HAsciiString)& S) : Data(S->Length())
  63. {
  64. for( Standard_Integer i = 1; i <= Data.Length() ; i++) {
  65. // convert the character i of S
  66. Standard_ExtCharacter val = ToExtCharacter(S->Value(i)) ;
  67. Data.SetValue(i-1,val );
  68. }
  69. }
  70. //------------------------------------------------------------------------
  71. // Append
  72. //------------------------------------------------------------------------
  73. void PCollection_HExtendedString::Append
  74. (const Handle(PCollection_HExtendedString)& S)
  75. {
  76. InsertAfter(Length(),S);
  77. }
  78. //------------------------------------------------------------------------
  79. // Center
  80. //------------------------------------------------------------------------
  81. void PCollection_HExtendedString::Center
  82. (const Standard_Integer Width, const Standard_ExtCharacter Filler)
  83. {
  84. if (Width < 0) Standard_NegativeValue::Raise();
  85. Standard_Integer size1 = Length();
  86. if(Width > size1) {
  87. Standard_Integer size2 = size1 + ((Width - size1)/2);
  88. LeftJustify(size2,Filler);
  89. RightJustify(Width,Filler);
  90. }
  91. }
  92. //------------------------------------------------------------------------
  93. // ChangeAll
  94. //------------------------------------------------------------------------
  95. void PCollection_HExtendedString::ChangeAll
  96. (const Standard_ExtCharacter C, const Standard_ExtCharacter NewC)
  97. {
  98. for( Standard_Integer i = 0 ; i < Data.Length(); i++) {
  99. if(Data(i) == C) Data.SetValue(i, NewC);
  100. }
  101. }
  102. //------------------------------------------------------------------------
  103. // Clear
  104. //------------------------------------------------------------------------
  105. void PCollection_HExtendedString::Clear ()
  106. {
  107. Data.Resize(0);
  108. }
  109. //------------------------------------------------------------------------
  110. // Convert
  111. //------------------------------------------------------------------------
  112. TCollection_ExtendedString PCollection_HExtendedString::Convert() const
  113. {
  114. Standard_Integer L = Length();
  115. TCollection_ExtendedString TString (L,' ');
  116. for (Standard_Integer i = 1 ; i <= L ; i++) {
  117. TString.SetValue(i,Value(i));
  118. }
  119. return TString;
  120. }
  121. //------------------------------------------------------------------------
  122. // FirstLocationInSet
  123. //------------------------------------------------------------------------
  124. Standard_Integer PCollection_HExtendedString::FirstLocationInSet
  125. (const Handle(PCollection_HExtendedString)& Set,
  126. const Standard_Integer FromIndex,
  127. const Standard_Integer ToIndex) const
  128. {
  129. if (Length() == 0 || Set->Length() == 0) return 0;
  130. if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
  131. Standard_OutOfRange::Raise();
  132. for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++)
  133. for(Standard_Integer j = 1; j <= Set->Length(); j++)
  134. if(Data(i) == Set->Value(j)) return (i+1);
  135. return 0;
  136. }
  137. //------------------------------------------------------------------------
  138. // FirstLocationNotInset
  139. //------------------------------------------------------------------------
  140. Standard_Integer PCollection_HExtendedString::FirstLocationNotInSet
  141. (const Handle(PCollection_HExtendedString)& Set, const Standard_Integer FromIndex,
  142. const Standard_Integer ToIndex) const
  143. {
  144. if (Length() == 0 || Set->Length() == 0) return 0;
  145. if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
  146. Standard_OutOfRange::Raise();
  147. Standard_Boolean find;
  148. for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++) {
  149. find = Standard_False;
  150. for(Standard_Integer j = 1; j <= Set->Length(); j++) {
  151. if (Data(i) == Set->Value(j)) find = Standard_True;
  152. }
  153. if (!find) return (i+1);
  154. }
  155. return 0;
  156. }
  157. //------------------------------------------------------------------------
  158. // InsertAfter
  159. //------------------------------------------------------------------------
  160. void PCollection_HExtendedString::InsertAfter
  161. (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
  162. {
  163. Standard_Integer i ;
  164. Standard_Integer size1 = Length();
  165. Standard_Integer size2 = S->Length();
  166. #ifndef NOBOUNDCHECK
  167. if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
  168. #endif
  169. Data.Resize(size1+size2);
  170. for( i = size1-1 ; i >= Index; i--) Data.SetValue(size2+i,Data(i));
  171. for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-1,S->Value(i));
  172. }
  173. //------------------------------------------------------------------------
  174. // InsertBefore
  175. //------------------------------------------------------------------------
  176. void PCollection_HExtendedString::InsertBefore
  177. (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
  178. {
  179. Standard_Integer i ;
  180. Standard_Integer size1 = Length();
  181. Standard_Integer size2 = S->Length();
  182. #ifndef NOBOUNDCHECK
  183. if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
  184. #endif
  185. Data.Resize(size1+size2);
  186. for( i = size1-1 ; i >= Index-1 ; i--)
  187. Data.SetValue(size2+i,Data(i));
  188. for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
  189. }
  190. //------------------------------------------------------------------------
  191. // IsAscii
  192. //------------------------------------------------------------------------
  193. Standard_Boolean PCollection_HExtendedString::IsAscii() const
  194. {
  195. for( Standard_Integer i = 0; i < Data.Length() ; i++) {
  196. if (!IsAnAscii(Data(i))) return Standard_False;
  197. }
  198. return Standard_True;
  199. }
  200. //------------------------------------------------------------------------
  201. // IsDifferent
  202. //------------------------------------------------------------------------
  203. Standard_Boolean PCollection_HExtendedString::IsDifferent
  204. (const Handle(PCollection_HExtendedString)& S) const
  205. {
  206. Standard_Integer size = Length();
  207. if( size != S->Length()) return Standard_True;
  208. Standard_Integer i = 1 ;
  209. Standard_Boolean different = Standard_False;
  210. while (i <= size && !different) {
  211. if (Data(i-1) != S->Value(i)) different = Standard_True;
  212. i++;
  213. }
  214. return different;
  215. }
  216. //------------------------------------------------------------------------
  217. // IsEmpty
  218. //------------------------------------------------------------------------
  219. Standard_Boolean PCollection_HExtendedString::IsEmpty () const
  220. {
  221. return (Data.Length() == 0);
  222. }
  223. // ----------------------------------------------------------------------------
  224. // IsLess
  225. // ----------------------------------------------------------------------------
  226. Standard_Boolean PCollection_HExtendedString::IsLess(
  227. const Handle(PCollection_HExtendedString)& other) const
  228. {
  229. Standard_Integer mysize = Data.Length();
  230. Standard_Integer size = other->Length();
  231. Standard_Integer i = 0;
  232. Standard_Integer j = 1;
  233. while (i < mysize && j <= size) {
  234. if (Data(i) < other->Value(j)) return (Standard_True);
  235. if (Data(i) > other->Value(j)) return (Standard_False);
  236. i++;
  237. j++;
  238. }
  239. if (i == mysize && j <= size) return (Standard_True);
  240. return (Standard_False);
  241. }
  242. // ----------------------------------------------------------------------------
  243. // IsGreater
  244. // ----------------------------------------------------------------------------
  245. Standard_Boolean PCollection_HExtendedString::IsGreater
  246. (const Handle(PCollection_HExtendedString)& other) const
  247. {
  248. Standard_Integer mysize = Data.Length();
  249. Standard_Integer size = other->Length();
  250. Standard_Integer i = 0;
  251. Standard_Integer j = 1;
  252. while (i < mysize && j <= size) {
  253. if (Data(i) < other->Value(j)) return (Standard_False);
  254. if (Data(i) > other->Value(j)) return (Standard_True);
  255. i++;
  256. j++;
  257. }
  258. if (j == size && j < mysize) return (Standard_True);
  259. return (Standard_False);
  260. }
  261. //------------------------------------------------------------------------
  262. // IsSameString
  263. //------------------------------------------------------------------------
  264. Standard_Boolean PCollection_HExtendedString::IsSameString
  265. (const Handle(PCollection_HExtendedString)& S) const
  266. {
  267. Standard_Integer size1 = Length();
  268. if( size1 != S->Length()) return Standard_False;
  269. for( Standard_Integer i = 1 ; i <= size1; i++) {
  270. if(Data(i-1) != S->Value(i)) return Standard_False;
  271. }
  272. return Standard_True;
  273. }
  274. //------------------------------------------------------------------------
  275. // LeftAdjust
  276. //------------------------------------------------------------------------
  277. void PCollection_HExtendedString::LeftAdjust ()
  278. {
  279. Standard_Integer i ;
  280. if (!IsAscii()) Standard_OutOfRange::Raise();
  281. for ( i = 1 ; i <= Length() ; i ++) {
  282. if (!IsSpace((Standard_Character)Value(i))) break;
  283. }
  284. if( i > 1 ) Remove(1,i-1);
  285. }
  286. //------------------------------------------------------------------------
  287. // LeftJustify
  288. //------------------------------------------------------------------------
  289. void PCollection_HExtendedString::LeftJustify
  290. (const Standard_Integer Width, const Standard_ExtCharacter Filler)
  291. {
  292. if (Width < 0) Standard_NegativeValue::Raise();
  293. Standard_Integer size1 = Length();
  294. if(Width > size1) {
  295. Data.Resize(Width);
  296. for(Standard_Integer i = size1; i < Width ; i++) Data.SetValue(i, Filler);
  297. }
  298. }
  299. //------------------------------------------------------------------------
  300. // Length
  301. //------------------------------------------------------------------------
  302. Standard_Integer PCollection_HExtendedString::Length () const
  303. {
  304. return Data.Length();
  305. }
  306. //------------------------------------------------------------------------
  307. // Location
  308. //------------------------------------------------------------------------
  309. Standard_Integer PCollection_HExtendedString::Location
  310. (const Standard_Integer N, const Standard_ExtCharacter C,
  311. const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
  312. {
  313. if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
  314. Standard_OutOfRange::Raise();
  315. for(Standard_Integer i = FromIndex-1, count = 0; i <= ToIndex-1; i++)
  316. if(Data(i) == C) {
  317. count++;
  318. if ( count == N ) return (i+1);
  319. }
  320. return 0 ;
  321. }
  322. //------------------------------------------------------------------------
  323. // Location
  324. //------------------------------------------------------------------------
  325. Standard_Integer PCollection_HExtendedString::Location
  326. (const Handle(PCollection_HExtendedString)& S, const Standard_Integer FromIndex,
  327. const Standard_Integer ToIndex) const
  328. {
  329. if (Length() == 0 || S->Length() == 0) return 0;
  330. if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
  331. Standard_OutOfRange::Raise();
  332. for(Standard_Integer i = FromIndex-1, k = 1, l = FromIndex-2; i < ToIndex; i++){
  333. if(Data(i) == S->Value(k)) {
  334. k++;
  335. if ( k > S->Length()) return l + 2;
  336. }
  337. else {
  338. k = 1;
  339. l = i;
  340. }
  341. }
  342. return 0;
  343. }
  344. //------------------------------------------------------------------------
  345. // Prepend
  346. //------------------------------------------------------------------------
  347. void PCollection_HExtendedString::Prepend
  348. (const Handle(PCollection_HExtendedString)& S)
  349. {
  350. InsertAfter(0,S);
  351. }
  352. //------------------------------------------------------------------------
  353. // Print
  354. //------------------------------------------------------------------------
  355. void PCollection_HExtendedString::Print (Standard_OStream& S) const
  356. {
  357. Standard_Integer len = Data.Length() ;
  358. for(Standard_Integer i = 0; i < len ; i++) {
  359. S.width(4);
  360. S.fill('0');
  361. S << hex << Data(i);
  362. }
  363. }
  364. //------------------------------------------------------------------------
  365. // Remove
  366. //------------------------------------------------------------------------
  367. void PCollection_HExtendedString::Remove (const Standard_Integer Index)
  368. {
  369. if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
  370. Remove(Index,Index);
  371. }
  372. //------------------------------------------------------------------------
  373. // Remove
  374. //------------------------------------------------------------------------
  375. void PCollection_HExtendedString::Remove
  376. (const Standard_Integer FromIndex, const Standard_Integer ToIndex)
  377. {
  378. if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
  379. Standard_OutOfRange::Raise();
  380. Standard_Integer size1 = Length();
  381. for( Standard_Integer i = ToIndex, j = FromIndex-1; i < size1 ; i++, j++)
  382. Data.SetValue(j,Data(i));
  383. Data.Resize(size1-(ToIndex-FromIndex+1));
  384. }
  385. //------------------------------------------------------------------------
  386. // RemoveAll
  387. //------------------------------------------------------------------------
  388. void PCollection_HExtendedString::RemoveAll (const Standard_ExtCharacter C)
  389. {
  390. Standard_Integer i ;
  391. Standard_Integer j ;
  392. Standard_Integer size1 = Length();
  393. for( i = 0, j = 0; i < size1 ; i++) {
  394. if (Data(i) == C) continue;
  395. Data.SetValue(j++, Data(i));
  396. }
  397. Data.Resize(j);
  398. }
  399. //------------------------------------------------------------------------
  400. // RightAdjust
  401. //------------------------------------------------------------------------
  402. void PCollection_HExtendedString::RightAdjust ()
  403. {
  404. Standard_Integer i ;
  405. if (! IsAscii()) Standard_OutOfRange::Raise();
  406. for ( i = Length() ; i >= 1 ; i --) {
  407. if (!IsSpace((Standard_Character)Value(i))) break;
  408. }
  409. if( i < Length() ) Remove(i+1,Length());
  410. }
  411. //------------------------------------------------------------------------
  412. // RightJustify
  413. //------------------------------------------------------------------------
  414. void PCollection_HExtendedString::RightJustify
  415. (const Standard_Integer Width, const Standard_ExtCharacter Filler)
  416. {
  417. Standard_Integer i ;
  418. Standard_Integer k ;
  419. if (Width < 0) Standard_NegativeValue::Raise();
  420. Standard_Integer size1 = Length();
  421. if(Width > size1) {
  422. Data.Resize(Width);
  423. for ( i = size1-1, k = Width-1 ; i >= 0 ; i--, k--)
  424. Data.SetValue(k, Data(i));
  425. for(; k >= 0 ; k--) Data.SetValue(k, Filler);
  426. }
  427. }
  428. //------------------------------------------------------------------------
  429. // SetValue
  430. //------------------------------------------------------------------------
  431. void PCollection_HExtendedString::SetValue
  432. (const Standard_Integer Index, const Standard_ExtCharacter C)
  433. {
  434. if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
  435. Data(Index-1) = C;
  436. }
  437. //------------------------------------------------------------------------
  438. // SetValue
  439. //------------------------------------------------------------------------
  440. void PCollection_HExtendedString::SetValue
  441. (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
  442. {
  443. Standard_Integer size1 = Length();
  444. Standard_Integer size2 = S->Length();
  445. Standard_Integer size3 = size2 + Index - 1;
  446. #ifndef NOBOUNDCHECK
  447. if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
  448. #endif
  449. if(size1 != size3) Data.Resize(size3);
  450. for( Standard_Integer i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
  451. }
  452. //------------------------------------------------------------------------
  453. // Split
  454. //------------------------------------------------------------------------
  455. Handle(PCollection_HExtendedString) PCollection_HExtendedString::Split
  456. (const Standard_Integer Index)
  457. {
  458. if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
  459. Handle(PCollection_HExtendedString) S2;
  460. if (Index != Length()) {
  461. S2 = SubString(Index+1,Length());
  462. Data.Resize(Index);
  463. }
  464. else {
  465. #ifndef OBJS
  466. Handle(PCollection_HAsciiString) s = new PCollection_HAsciiString("");
  467. S2 = new PCollection_HExtendedString(s);
  468. #else
  469. Handle(PCollection_HAsciiString) s = new (os_segment::of(this)) PCollection_HAsciiString("");
  470. S2 = new (os_segment::of(this)) PCollection_HExtendedString(s);
  471. #endif
  472. }
  473. return S2;
  474. }
  475. //------------------------------------------------------------------------
  476. // SubString
  477. //------------------------------------------------------------------------
  478. Handle(PCollection_HExtendedString) PCollection_HExtendedString::SubString
  479. (const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
  480. {
  481. if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
  482. Standard_OutOfRange::Raise();
  483. Handle(PCollection_HExtendedString) S1;
  484. Handle(PCollection_HExtendedString) S2;
  485. S2 = this;
  486. #ifndef OBJS
  487. S1 = new PCollection_HExtendedString(S2,FromIndex,ToIndex);
  488. #else
  489. S1 = new (os_segment::of(this)) PCollection_HExtendedString(S2,FromIndex,ToIndex);
  490. #endif
  491. return S1;
  492. }
  493. //------------------------------------------------------------------------
  494. // UsefullLength
  495. //------------------------------------------------------------------------
  496. Standard_Integer PCollection_HExtendedString::UsefullLength () const
  497. {
  498. Standard_Integer i ;
  499. if (! IsAscii()) Standard_OutOfRange::Raise();
  500. for( i = Length() ; i >= 1 ; i--)
  501. if (IsGraphic((Standard_Character)Value(i))) break;
  502. return (i);
  503. }
  504. //------------------------------------------------------------------------
  505. // value
  506. //------------------------------------------------------------------------
  507. Standard_ExtCharacter PCollection_HExtendedString::Value
  508. (const Standard_Integer Index) const
  509. {
  510. if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
  511. return Data(Index-1);
  512. }
  513. //------------------------------------------------------------------------
  514. // ShallowDump
  515. //------------------------------------------------------------------------
  516. void PCollection_HExtendedString::ShallowDump(Standard_OStream& S) const
  517. {
  518. S << "begin class HExtendedString " << endl;
  519. ::ShallowDump(Data, S);
  520. S << "end class HExtendedString" << endl;
  521. }
  522. //------------------------------------------------------------------------
  523. // Assign
  524. //------------------------------------------------------------------------
  525. void PCollection_HExtendedString::Assign
  526. (const DBC_VArrayOfExtCharacter& TheField)
  527. {
  528. Data = TheField;
  529. }