Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
script_call_string.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * xml Mission Scripting written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <time.h>
30 #include <ctype.h>
31 #include <assert.h>
32 #include "cmd/unit_generic.h"
33 #ifndef WIN32
34 //this file isn't available on my system (all win32 machines?) i dun even know what it has or if we need it as I can compile without it
35 #include <unistd.h>
36 #endif
37 
38 #include <expat.h>
39 #include "xml_support.h"
40 
41 #include "vegastrike.h"
42 
43 #include "cmd/unit_generic.h"
44 #include "mission.h"
45 #include "easydom.h"
46 
47 
48 varInst* Mission::call_string( missionNode *node, int mode )
49 {
50  varInst *viret = NULL;
51  if (mode == SCRIPT_PARSE) {
52  string cmd = node->attr_value( "name" );
53  node->script.method_id = module_string_map[cmd];
54  }
56  if (method_id == CMT_STRING_new) {
57  viret = call_string_new( node, mode, "" );
58 
59  return viret;
60  } else {
61  varInst *ovi = getObjectArg( node, mode );
62  string *my_string = getStringObject( node, mode, ovi );
63  if (method_id == CMT_STRING_delete) {
64  if (mode == SCRIPT_RUN) {
65  delete my_string;
66  string_counter--;
67  }
68  viret = newVarInst( VI_TEMP );
69  viret->type = VAR_VOID;
70  } else if (method_id == CMT_STRING_print) {
71  if (mode == SCRIPT_RUN)
72  call_string_print( node, mode, ovi );
73  viret = newVarInst( VI_TEMP );
74  viret->type = VAR_VOID;
75  } else if (method_id == CMT_STRING_equal) {
76  missionNode *other_node = getArgument( node, mode, 1 );
77  varInst *other_vi = checkObjectExpr( other_node, mode );
78 
79  bool res = false;
80  if (mode == SCRIPT_RUN) {
81  string s1 = call_string_getstring( node, mode, ovi );
82  string s2 = call_string_getstring( node, mode, other_vi );
83  if (s1 == s2)
84  res = true;
85  }
86  deleteVarInst( other_vi );
87  viret = newVarInst( VI_TEMP );
88  viret->type = VAR_BOOL;
89  viret->bool_val = res;
90  } else if (method_id == CMT_STRING_begins) {
91  //test if s1 begins with s2
92  missionNode *other_node = getArgument( node, mode, 1 );
93  varInst *other_vi = checkObjectExpr( other_node, mode );
94 
95  bool res = false;
96  if (mode == SCRIPT_RUN) {
97  string s1 = call_string_getstring( node, mode, ovi );
98  string s2 = call_string_getstring( node, mode, other_vi );
99  if (s1.find( s2, 0 ) == 0)
100  res = true;
101  }
102  deleteVarInst( other_vi );
103  viret = newVarInst( VI_TEMP );
104  viret->type = VAR_BOOL;
105  viret->bool_val = res;
106  } else {
107  fatalError( node, mode, "unknown command "+node->script.name+" for callback string" );
108  assert( 0 );
109  }
110  deleteVarInst( ovi );
111  return viret;
112  } //else objects
113  return NULL; //never reach
114 }
115 
116 string Mission::getStringArgument( missionNode *node, int mode, int arg_nr )
117 {
118  missionNode *arg_node = getArgument( node, mode, arg_nr );
119  varInst *arg_vi = checkObjectExpr( arg_node, mode );
120 
121  string retstr;
122  if (mode == SCRIPT_RUN)
123  retstr = call_string_getstring( arg_node, mode, arg_vi );
124  return retstr;
125 }
126 
127 string Mission::call_string_getstring( missionNode *node, int mode, varInst *ovi )
128 {
129  if ( ovi->type != VAR_OBJECT || (ovi->type == VAR_OBJECT && ovi->objectname != "string") ) {
130  fatalError( node, mode, "call_string_getstring needs string object as arg" );
131  assert( 0 );
132  }
133  string *my_string = getStringObject( node, mode, ovi );
134 
135  string ret = *my_string;
136 
137  return ret;
138 }
139 
140 void Mission::call_string_print( missionNode *node, int mode, varInst *ovi )
141 {
142  string *my_string = getStringObject( node, mode, ovi );
143 
144  std::cout<<*my_string;
145 }
146 
147 varInst* Mission::call_string_new( missionNode *node, int mode, string initstring )
148 {
149  debug( 10, node, mode, "call_string" );
150 
151  varInst *viret = newVarInst( VI_TEMP );
152 
153  string *my_string = new string( initstring );
154  string_counter++;
155 
156  viret->type = VAR_OBJECT;
157  viret->objectname = "string";
158  viret->object = (void*) my_string;
159 
160  return viret;
161 }
162 
163 string* Mission::getStringObject( missionNode *node, int mode, varInst *ovi )
164 {
165  string *my_object = NULL;
166  if (mode == SCRIPT_RUN) {
167  my_object = (string*) ovi->object;
168  if (my_object == NULL) {
169  fatalError( node, mode, "string: no object" );
170  assert( 0 );
171  }
172  }
173  return my_object;
174 }
175