Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTVariable.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTVariable.cpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131  **************************************************************/
132 
133 #include "stdafx.hpp"
134 
135 #include "OTVariable.hpp"
136 
137 #include "crypto/OTASCIIArmor.hpp"
138 #include "OTLog.hpp"
139 #include "OTScript.hpp"
140 
141 namespace opentxs
142 {
143 
144 void OTVariable::Serialize(OTString& strAppend, bool bCalculatingID) const
145 {
146 
147  std::string str_access("");
148 
149  switch (m_Access) {
150  case OTVariable::Var_Constant: // This cannot be changed from inside the
151  // script.
152  str_access = "constant";
153  break;
154  case OTVariable::Var_Persistent: // This can be changed without notifying
155  // the parties.
156  str_access = "persistent";
157  break;
158  case OTVariable::Var_Important: // This cannot be changed without notifying
159  // the parties.
160  str_access = "important";
161  break;
162  default:
163  otErr << "OTVariable::Serialize: ERROR: Bad variable type.\n";
164  break;
165  }
166 
167  std::string str_type;
168 
169  switch (m_Type) {
170  case OTVariable::Var_String: {
171  str_type = "string";
172 
173  if ((false == bCalculatingID) && // we don't serialize the variable's
174  // value when calculating the
175  (m_str_Value.size() > 0)) // owner OTScriptable's ID, since the
176  // value can change over time.
177  {
178  OTString strVal(m_str_Value.c_str());
179  OTASCIIArmor ascVal(strVal);
180  strAppend.Concatenate("<variable\n name=\"%s\"\n"
181  " value=\"%s\"\n"
182  " type=\"%s\"\n"
183  " access=\"%s\" >\n%s</variable>\n\n",
184  m_strName.Get(), "exists", str_type.c_str(),
185  str_access.c_str(), ascVal.Get());
186  }
187  else {
188  strAppend.Concatenate("<variable\n name=\"%s\"\n"
189  " value=\"%s\"\n"
190  " type=\"%s\"\n"
191  " access=\"%s\" />\n\n",
192  m_strName.Get(), "none", // value
193  str_type.c_str(), str_access.c_str());
194  }
195  } break;
197  str_type = "integer";
198  strAppend.Concatenate(
199  "<variable\n name=\"%s\"\n"
200  " value=\"%d\"\n"
201  " type=\"%s\"\n"
202  " access=\"%s\" />\n\n",
203  m_strName.Get(),
204  bCalculatingID ? 0 : m_nValue, // we don't serialize the variable's
205  // value when calculating the smart
206  // contract's ID.
207  str_type.c_str(), str_access.c_str());
208  break;
210  str_type = "bool";
211  strAppend.Concatenate(
212  "<variable\n name=\"%s\"\n"
213  " value=\"%s\"\n"
214  " type=\"%s\"\n"
215  " access=\"%s\" />\n\n",
216  m_strName.Get(),
217  bCalculatingID
218  ? "false"
219  : (m_bValue ? "true" : "false"), // we don't serialize the
220  // variable's value when
221  // calculating the smart
222  // contract's ID.
223  str_type.c_str(),
224  str_access.c_str());
225  break;
226  default:
227  otErr
228  << "OTVariable::Serialize: Error, Wrong Type -- not serializing.\n";
229  break;
230  }
231 }
232 
233 // NO TYPE (YET)
235  : m_nValue(0)
236  , m_bValue(false)
237  , m_nValueBackup(0)
238  , m_bValueBackup(false)
239  , m_pBylaw(nullptr)
240  , m_Type(OTVariable::Var_Error_Type)
241  , m_Access(Var_Error_Access)
242  , m_pScript(nullptr)
243 {
244 }
245 
246 // STRING
247 OTVariable::OTVariable(std::string str_Name, std::string str_Value,
248  OTVariable_Access theAccess)
249  : m_strName(str_Name.c_str())
250  , m_str_Value(str_Value)
251  , m_nValue(0)
252  , m_bValue(false)
253  , m_str_ValueBackup(str_Value)
254  , m_nValueBackup(0)
255  , m_bValueBackup(false)
256  , m_pBylaw(nullptr)
257  , m_Type(OTVariable::Var_String)
258  , m_Access(theAccess)
259  , m_pScript(nullptr)
260 {
261  if (m_str_Value.empty()) m_str_Value = "";
262  if (m_str_ValueBackup.empty()) m_str_ValueBackup = "";
263 }
264 
265 // INT
266 OTVariable::OTVariable(std::string str_Name, int32_t nValue,
267  OTVariable_Access theAccess)
268  : m_strName(str_Name.c_str())
269  , m_nValue(nValue)
270  , m_bValue(false)
271  , m_nValueBackup(nValue)
272  , m_bValueBackup(false)
273  , m_pBylaw(nullptr)
274  , m_Type(OTVariable::Var_Integer)
275  , m_Access(theAccess)
276  , m_pScript(nullptr)
277 {
278 }
279 
280 // BOOL
281 OTVariable::OTVariable(std::string str_Name, bool bValue,
282  OTVariable_Access theAccess)
283  : m_strName(str_Name.c_str())
284  , m_nValue(0)
285  , m_bValue(bValue)
286  , m_nValueBackup(0)
287  , m_bValueBackup(bValue)
288  , m_pBylaw(nullptr)
289  , m_Type(OTVariable::Var_Bool)
290  , m_Access(theAccess)
291  , m_pScript(nullptr)
292 {
293 }
294 
296 {
297  if (nullptr != m_pScript) {
298  m_pScript->RemoveVariable(*this);
299  }
300 
301  m_pScript =
302  nullptr; // I wasn't the owner, it was a pointer for convenience only.
303  m_pBylaw =
304  nullptr; // I wasn't the owner, it was a pointer for convenience only.
305 }
306 
307 bool OTVariable::SetValue(const int32_t& nValue)
308 {
309  if (!IsInteger()) {
310  otErr << "OTVariable::SetValue(int64_t): Error: This variable ("
311  << m_strName << ") is not an integer.\n";
312  return false;
313  }
314 
315  m_nValue = m_nValueBackup = nValue;
316 
317  return true;
318 }
319 
320 bool OTVariable::SetValue(bool bValue)
321 {
322  if (!IsBool()) {
323  otErr << "OTVariable::SetValue(bool): Error: This variable ("
324  << m_strName << ") is not a bool.\n";
325  return false;
326  }
327 
328  m_bValue = m_bValueBackup = bValue;
329 
330  return true;
331 }
332 
333 bool OTVariable::SetValue(const std::string& str_Value)
334 {
335  if (!IsString()) {
336  otErr << "OTVariable::SetValue(std::string): Error: This variable ("
337  << m_strName << ") is not a string.\n";
338  return false;
339  }
340 
341  m_str_Value = m_str_ValueBackup = str_Value;
342 
343  if (m_str_Value.empty()) m_str_Value = "";
344  if (m_str_ValueBackup.empty()) m_str_ValueBackup = "";
345 
346  return true;
347 }
348 
349 // So you can tell if the variable has CHANGED since it was last set clean.
350 // (Usually you set clean just before running the script, and you check dirty
351 // just AFTER running the script.)
352 //
354 {
355  bool bReturnVal = false;
356 
357  switch (m_Type) {
359  if (0 != m_str_Value.compare(m_str_ValueBackup)) // If they do NOT
360  // match, then it's
361  // dirty.
362  bReturnVal = true;
363  break;
365  if (m_nValue !=
366  m_nValueBackup) // If they do NOT match, then it's dirty.
367  bReturnVal = true;
368  break;
370  if (m_bValue !=
371  m_bValueBackup) // If they do NOT match, then it's dirty.
372  bReturnVal = true;
373  break;
374  default:
375  otErr << "OTVariable::IsDirty: Error: unknown type for variable: "
376  << m_strName << "\n";
377  break;
378  }
379 
380  return bReturnVal;
381 }
382 
383 // Sets the variable as clean, so you can check it later and see if it's been
384 // changed (if it's DIRTY again.)
386 {
387  switch (m_Type) {
389  m_str_ValueBackup = m_str_Value; // Save a copy of the current value, so
390  // we can check later and see if
391  // they're different.
392  break;
394  m_nValueBackup = m_nValue; // Save a copy of the current value, so we
395  // can check later and see if they're
396  // different.
397  break;
399  m_bValueBackup = m_bValue; // Save a copy of the current value, so we
400  // can check later and see if they're
401  // different.
402  break;
403  default:
404  otErr << "OTVariable::SetAsClean: Error: unknown type for variable: "
405  << m_strName << "\n";
406  m_str_ValueBackup = m_str_Value;
407  m_nValueBackup = m_nValue;
408  m_bValueBackup = m_bValue;
409  break;
410  }
411 }
412 
413 // If the script destructs before the variable does, it unregisters
414 // itself here, so the variable isn't stuck with a bad pointer.
415 //
417 {
418  m_pScript = nullptr;
419 }
420 
421 // We keep an internal script pointer here, so if we destruct,
422 // we can remove ourselves from the script.
423 //
425 {
426  SetAsClean(); // so we can check for dirtiness after execution.
427 
428  const std::string str_var_name = m_strName.Get();
429 
430  theScript.AddVariable(str_var_name, *this);
431 
432  m_pScript = &theScript; // So later, if the variable destructs, and
433  // this pointer is set, the variable can
434  // remove itself from the script.
435 }
436 
437 // Done
439 {
440  if (!(GetName().Compare(rhs.GetName()))) {
441  otOut << "OTVariable::Compare: Names don't match: " << GetName()
442  << " / " << rhs.GetName() << " \n";
443  return false;
444  }
445  if (!(GetType() == rhs.GetType())) {
446  otOut << "OTVariable::Compare: Type doesn't match: " << GetName()
447  << " \n";
448  return false;
449  }
450  if (!(GetAccess() == rhs.GetAccess())) {
451  otOut << "OTVariable::Compare: Access types don't match: " << GetName()
452  << " \n";
453  return false;
454  }
455 
456  bool bMatch = false;
457 
458  switch (GetType()) {
460  bMatch = (GetValueInteger() == rhs.GetValueInteger());
461  break;
463  bMatch = (GetValueBool() == rhs.GetValueBool());
464  break;
466  bMatch = (GetValueString().compare(rhs.GetValueString()) == 0);
467  break;
468  default:
469  otErr << "OTVariable::Compare: Unknown type in variable " << m_strName
470  << ".\n";
471  break;
472  }
473 
474  return bMatch;
475 }
476 
477 } // namespace opentxs
bool IsString() const
Definition: OTVariable.hpp:241
OTVariable_Type GetType() const
Definition: OTVariable.hpp:224
EXPORT void RegisterForExecution(OTScript &theScript)
Definition: OTVariable.cpp:424
EXPORT void RemoveVariable(OTVariable &theVar)
Definition: OTScript.cpp:405
bool IsDirty() const
Definition: OTVariable.cpp:353
OTLOG_IMPORT OTLogStream otOut
EXPORT void Concatenate(const char *arg,...)
Definition: OTString.cpp:1334
bool IsInteger() const
Definition: OTVariable.hpp:233
EXPORT void UnregisterScript()
Definition: OTVariable.cpp:416
EXPORT const OTString & GetName() const
Definition: OTVariable.hpp:220
bool SetValue(const int32_t &nValue)
Definition: OTVariable.cpp:307
void Serialize(OTString &strAppend, bool bCalculatingID=false) const
Definition: OTVariable.cpp:144
bool IsBool() const
Definition: OTVariable.hpp:237
OTVariable_Access GetAccess() const
Definition: OTVariable.hpp:228
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
virtual EXPORT ~OTVariable()
Definition: OTVariable.cpp:295
EXPORT void AddVariable(std::string str_var_name, OTVariable &theVar)
Definition: OTScript.cpp:380
std::string & GetValueString()
Definition: OTVariable.hpp:267
int32_t & GetValueInteger()
Definition: OTVariable.hpp:259
bool Compare(OTVariable &rhs)
Definition: OTVariable.cpp:438