KSGSText.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "KSGSText.h"
  2. #include "KSGSTyp.h"
  3. #include "../Error/Error.h"
  4. #include "KSGSBool.h"
  5. #include "KSGSInt.h"
  6. #include "KSGSDouble.h"
  7. using namespace KSGScript;
  8. // Inhalt der KSGSTextKlasse Klasse aus KSGSText.h
  9. // Konstruktor
  10. KSGSTextKlasse::KSGSTextKlasse( KSGScriptObj *zObj, char *std )
  11. : KSGSKlasseInstanz( KSGS_TEXT, 0, 0, zObj )
  12. {
  13. val = new Text( std );
  14. }
  15. // Destruktor
  16. KSGSTextKlasse::~KSGSTextKlasse()
  17. {
  18. val->release();
  19. }
  20. // nicht constant
  21. KSGSVariable *KSGSTextKlasse::startFunktion( int id, bool access, RCArray< KSGSVariable > *parameter )
  22. {
  23. KSGSVariable *ret = 0;
  24. switch( id )
  25. {
  26. case 0: // void setSuchGrenzen( int, int )
  27. if( parameter->getEintragAnzahl() < 2 )
  28. error( 20, {}, obj );
  29. val->setSuchGrenzen( (char)( parameter->z( 0 ) ? parameter->z( 0 )->getInt() : 0 ),
  30. (char)( parameter->z( 1 ) ? parameter->z( 1 )->getInt() : 0 ) );
  31. break;
  32. case 1: // void setText( Text )
  33. if( parameter->getEintragAnzahl() < 1 )
  34. error( 20, {}, obj );
  35. val->setText( parameter->z( 0 ) ? parameter->z( 0 )->getText() : new Text( "" ) );
  36. break;
  37. case 2: // void anhängen( Text )
  38. if( parameter->getEintragAnzahl() < 1 )
  39. error( 20, {}, obj );
  40. val->append( parameter->z( 0 ) ? parameter->z( 0 )->getText() : new Text( "" ) );
  41. break;
  42. case 3: // void einfügen( int, Text )
  43. if( parameter->getEintragAnzahl() < 2 )
  44. error( 20, {}, obj );
  45. val->insert( parameter->z( 0 ) ? parameter->z( 0 )->getInt() : 0,
  46. parameter->z( 1 ) ? parameter->z( 1 )->getText() : new Text( "" ) );
  47. break;
  48. case 4: // void ersetzen( int, int, Text )
  49. if( parameter->getEintragAnzahl() < 3 )
  50. error( 20, {}, obj );
  51. val->ersetzen( parameter->z( 0 ) ? parameter->z( 0 )->getInt() : 0,
  52. parameter->z( 1 ) ? parameter->z( 1 )->getInt() : 0,
  53. parameter->z( 2 ) ? parameter->z( 2 )->getText() : new Text( "" ) );
  54. break;
  55. case 5: // void löschen( int, int )
  56. if( parameter->getEintragAnzahl() < 2 )
  57. error( 20, {}, obj );
  58. val->remove( parameter->z( 0 ) ? parameter->z( 0 )->getInt() : 0,
  59. parameter->z( 1 ) ? parameter->z( 1 )->getInt() : 0 );
  60. break;
  61. case 6: // int getLänge()
  62. ret = new KSGSIntKlasse( obj, val->getLength() );
  63. break;
  64. case 7: // bool hat( Text )
  65. if( parameter->getEintragAnzahl() < 1 )
  66. error( 20, {}, obj );
  67. ret = new KSGSBoolKlasse( obj, val->hat( parameter->z( 0 ) ? parameter->z( 0 )->getText() : new Text( "" ) ) );
  68. break;
  69. case 8: // bool istGleich( Text )
  70. if( parameter->getEintragAnzahl() < 1 )
  71. error( 20, {}, obj );
  72. ret = new KSGSBoolKlasse( obj, val->istGleich( parameter->z( 0 ) ? parameter->z( 0 )->getText() : new Text( "" ) ) );
  73. break;
  74. case 9: // int anzahlVon( Text )
  75. if( parameter->getEintragAnzahl() < 1 )
  76. error( 20, {}, obj );
  77. ret = new KSGSIntKlasse( obj, val->anzahlVon( parameter->z( 0 ) ? parameter->z( 0 )->getText() : new Text( "" ) ) );
  78. break;
  79. case 10: // int positionVon( Text, int )
  80. if( parameter->getEintragAnzahl() < 2 )
  81. error( 20, {}, obj );
  82. ret = new KSGSIntKlasse( obj, val->positionVon( parameter->z( 0 ) ? parameter->z( 0 )->getText() : new Text( "" ),
  83. parameter->z( 1 ) ? parameter->z( 1 )->getInt() : 0 ) );
  84. break;
  85. case 11: // Text getTeilText( int, int )
  86. if( 1 )
  87. {
  88. Text *txt = val->getTeilText( parameter->z( 0 ) ? parameter->z( 0 )->getInt() : 0,
  89. parameter->z( 1 ) ? parameter->z( 1 )->getInt() : 0 );
  90. if( !txt )
  91. txt = new Text();
  92. ret = new KSGSTextKlasse( obj, *txt );
  93. txt->release();
  94. }
  95. break;
  96. default: // unbekannt
  97. error( 19, {}, obj );
  98. break;
  99. }
  100. parameter->release();
  101. return ret;
  102. }
  103. KSGSVariable *KSGSTextKlasse::doOperator( int id, KSGSVariable *rechts )
  104. {
  105. if( !rechts )
  106. {
  107. error( 3, {}, obj );
  108. return 0;
  109. }
  110. KSGSVariable *ret = 0;
  111. switch( id )
  112. {
  113. case KSGS_O_SET:
  114. val->setText( rechts->getText() );
  115. ret = getThis();
  116. break;
  117. case KSGS_O_PLUSSET:
  118. val->append( rechts->getText() );
  119. ret = getThis();
  120. break;
  121. case KSGS_O_MINUSSET:
  122. val->append( rechts->getText() );
  123. ret = getThis();
  124. break;
  125. case KSGS_O_GLEICH:
  126. ret = new KSGSBoolKlasse( obj, val->istGleich( rechts->getText() ) );
  127. break;
  128. case KSGS_O_KLEINERGLEICH:
  129. if( 1 )
  130. {
  131. Text *txt = rechts->getText();
  132. ret = new KSGSBoolKlasse( obj, val->istGleich( rechts->getText() ) || *val < *txt );
  133. txt->release();
  134. }
  135. break;
  136. case KSGS_O_GRÖßERGLEICH:
  137. if( 1 )
  138. {
  139. Text *txt = rechts->getText();
  140. ret = new KSGSBoolKlasse( obj, val->istGleich( rechts->getText() ) || *val > *txt );
  141. txt->release();
  142. }
  143. break;
  144. case KSGS_O_UNGLEICH:
  145. ret = new KSGSBoolKlasse( obj, !val->istGleich( rechts->getText() ) );
  146. break;
  147. case KSGS_O_KLEINER:
  148. if( 1 )
  149. {
  150. Text *txt = rechts->getText();
  151. ret = new KSGSBoolKlasse( obj, *val < *txt );
  152. txt->release();
  153. }
  154. break;
  155. case KSGS_O_GRÖßER:
  156. if( 1 )
  157. {
  158. Text *txt = rechts->getText();
  159. ret = new KSGSBoolKlasse( obj, *val > *txt );
  160. txt->release();
  161. }
  162. break;
  163. case KSGS_O_PLUS:
  164. if( 1 )
  165. {
  166. Text *txt = rechts->getText();
  167. ret = new KSGSTextKlasse( obj, Text( (char*)val ) += (char*)*txt );
  168. txt->release();
  169. }
  170. break;
  171. case KSGS_O_MINUS:
  172. if( 1 )
  173. {
  174. Text t( (char*)val );
  175. t.remove( rechts->getText() );
  176. ret = new KSGSTextKlasse( obj, t );
  177. }
  178. break;
  179. }
  180. if( !ret )
  181. error( 21, {}, obj );
  182. if( rechts )
  183. rechts->release();
  184. return ret;
  185. }
  186. // constant
  187. Text *KSGSTextKlasse::getVal() const
  188. {
  189. return val->getThis();
  190. }
  191. Text *KSGSTextKlasse::zVal() const
  192. {
  193. return val;
  194. }
  195. KSGSVariable *KSGSTextKlasse::umwandelnIn( int typ ) const
  196. {
  197. switch( typ )
  198. {
  199. case KSGS_BOOL:
  200. if( val->istGleich( "true" ) )
  201. return new KSGSBoolKlasse( obj, 1 );
  202. if( val->istGleich( "false" ) )
  203. return new KSGSBoolKlasse( obj, 0 );
  204. return new KSGSBoolKlasse( obj, (int)*val != 0 );
  205. case KSGS_INT:
  206. return new KSGSIntKlasse( obj, *val );
  207. case KSGS_DOUBLE:
  208. return new KSGSDoubleKlasse( obj, *val );
  209. }
  210. error( 16, {}, obj );
  211. return 0;
  212. }
  213. // Reference Counting
  214. KSGSVariable *KSGSTextKlasse::release()
  215. {
  216. ref--;
  217. if( !ref )
  218. delete this;
  219. return 0;
  220. }