Merge with trunk r37757.
[blender.git] / source / gameengine / Expressions / StringValue.cpp
1 /** \file gameengine/Expressions/StringValue.cpp
2  *  \ingroup expressions
3  */
4 // StringValue.cpp: implementation of the CStringValue class.
5 /*
6  * Copyright (c) 1996-2000 Erwin Coumans <coockie@acm.org>
7  *
8  * Permission to use, copy, modify, distribute and sell this software
9  * and its documentation for any purpose is hereby granted without fee,
10  * provided that the above copyright notice appear in all copies and
11  * that both that copyright notice and this permission notice appear
12  * in supporting documentation.  Erwin Coumans makes no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  */
17
18 #include "StringValue.h"
19 #include "BoolValue.h"
20 #include "ErrorValue.h"
21 #include "VoidValue.h"
22
23 //////////////////////////////////////////////////////////////////////
24 // Construction/Destruction
25 //////////////////////////////////////////////////////////////////////
26
27 CStringValue::CStringValue()
28 /*
29 pre: false
30 effect: constructs a new CStringValue
31 */
32 {
33         m_strString = "[Illegal String constructor call]";
34 }
35
36 CStringValue::CStringValue(const char *txt,const char *name,AllocationTYPE alloctype)
37 /*
38 pre:
39 effect: constructs a new CStringValue containing text txt
40 */
41 {
42         m_strString = txt;
43         SetName(name);
44         
45         if (alloctype==CValue::STACKVALUE)
46         {
47                 CValue::DisableRefCount();
48                 
49         }
50         
51         
52 }
53
54
55 CValue* CStringValue::Calc(VALUE_OPERATOR op, CValue *val)
56 /*
57 pre:
58 ret: a new object containing the result of applying operator op to this
59 object and val
60 */
61 {
62         //return val->CalrcString(op, this);
63         return val->CalcFinal(VALUE_STRING_TYPE, op, this);
64 }
65
66 CValue* CStringValue::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
67 /*
68 pre: the type of val is dtype
69 ret: a new object containing the result of applying operator op to val and
70 this object
71 */
72 {
73         CValue *ret;
74         
75         if (op == VALUE_ADD_OPERATOR) {
76                 if (dtype == VALUE_ERROR_TYPE)
77                         ret = new CErrorValue(val->GetText() + op2str(op) +     GetText());
78                 else
79                         ret = new CStringValue(val->GetText() + GetText(),"");
80         } else {
81                 
82                 if (dtype == VALUE_STRING_TYPE || dtype == VALUE_EMPTY_TYPE) {
83                         switch(op) {
84                         case VALUE_EQL_OPERATOR:
85                                 ret = new CBoolValue(val->GetText() == GetText());
86                                 break;
87                         case VALUE_NEQ_OPERATOR:
88                                 ret = new CBoolValue(val->GetText() != GetText());
89                                 break;
90                         case VALUE_GRE_OPERATOR:
91                                 ret = new CBoolValue(val->GetText() > GetText());
92                                 break;
93                         case VALUE_LES_OPERATOR:
94                                 ret = new CBoolValue(val->GetText() < GetText());
95                                 break;
96                         case VALUE_GEQ_OPERATOR:
97                                 ret = new CBoolValue(val->GetText() >= GetText());
98                                 break;
99                         case VALUE_LEQ_OPERATOR:
100                                 ret = new CBoolValue(val->GetText() <= GetText());
101                                 break;
102                         default:
103                                 ret =  new CErrorValue(val->GetText() + op2str(op) + "[operator not allowed on strings]");
104                                 break;
105                         }
106                 } else {
107                         ret =  new CErrorValue(val->GetText() + op2str(op) + "[operator not allowed on strings]");
108                 }
109         }
110         return ret;
111 }
112
113
114
115 double CStringValue::GetNumber()
116 {
117         return -1;
118 }
119
120
121
122 const STR_String & CStringValue::GetText()
123 {
124         return m_strString;
125 }
126
127 bool CStringValue::IsEqual(const STR_String & other)
128 {
129         return (m_strString == other);
130 }
131
132 CValue* CStringValue::GetReplica()
133
134         CStringValue* replica = new CStringValue(*this);
135         replica->ProcessReplica();
136         return replica;
137 };
138
139