Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTNumList.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTNumList.cpp
4  *
5  * Base class for many of the OT classes.
6  */
7 
8 /************************************************************
9  -----BEGIN PGP SIGNED MESSAGE-----
10  Hash: SHA1
11 
12  * OPEN TRANSACTIONS
13  *
14  * Financial Cryptography and Digital Cash
15  * Library, Protocol, API, Server, CLI, GUI
16  *
17  * -- Anonymous Numbered Accounts.
18  * -- Untraceable Digital Cash.
19  * -- Triple-Signed Receipts.
20  * -- Cheques, Vouchers, Transfers, Inboxes.
21  * -- Basket Currencies, Markets, Payment Plans.
22  * -- Signed, XML, Ricardian-style Contracts.
23  * -- Scripted smart contracts.
24  *
25  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
26  *
27  * EMAIL:
29  *
30  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
31  *
32  * KEY FINGERPRINT (PGP Key in license file):
33  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
34  *
35  * OFFICIAL PROJECT WIKI(s):
36  * https://github.com/FellowTraveler/Moneychanger
37  * https://github.com/FellowTraveler/Open-Transactions/wiki
38  *
39  * WEBSITE:
40  * http://www.OpenTransactions.org/
41  *
42  * Components and licensing:
43  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
44  * -- otlib.........A class library.......LICENSE:...LAGPLv3
45  * -- otapi.........A client API..........LICENSE:...LAGPLv3
46  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
47  * -- otserver......Server Application....LICENSE:....AGPLv3
48  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
49  *
50  * All of the above OT components were designed and written by
51  * Fellow Traveler, with the exception of Moneychanger, which
52  * was contracted out to Vicky C ([email protected]).
53  * The open-source community has since actively contributed.
54  *
55  * -----------------------------------------------------
56  *
57  * LICENSE:
58  * This program is free software: you can redistribute it
59  * and/or modify it under the terms of the GNU Affero
60  * General Public License as published by the Free Software
61  * Foundation, either version 3 of the License, or (at your
62  * option) any later version.
63  *
64  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
65  * section 7: (This paragraph applies only to the LAGPLv3
66  * components listed above.) If you modify this Program, or
67  * any covered work, by linking or combining it with other
68  * code, such other code is not for that reason alone subject
69  * to any of the requirements of the GNU Affero GPL version 3.
70  * (==> This means if you are only using the OT API, then you
71  * don't have to open-source your code--only your changes to
72  * Open-Transactions itself must be open source. Similar to
73  * LGPLv3, except it applies to software-as-a-service, not
74  * just to distributing binaries.)
75  *
76  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
77  * used by Open Transactions: This program is released under
78  * the AGPL with the additional exemption that compiling,
79  * linking, and/or using OpenSSL is allowed. The same is true
80  * for any other open source libraries included in this
81  * project: complete waiver from the AGPL is hereby granted to
82  * compile, link, and/or use them with Open-Transactions,
83  * according to their own terms, as long as the rest of the
84  * Open-Transactions terms remain respected, with regard to
85  * the Open-Transactions code itself.
86  *
87  * Lucre License:
88  * This code is also "dual-license", meaning that Ben Lau-
89  * rie's license must also be included and respected, since
90  * the code for Lucre is also included with Open Transactions.
91  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
92  * The Laurie requirements are light, but if there is any
93  * problem with his license, simply remove the Lucre code.
94  * Although there are no other blind token algorithms in Open
95  * Transactions (yet. credlib is coming), the other functions
96  * will continue to operate.
97  * See Lucre on Github: https://github.com/benlaurie/lucre
98  * -----------------------------------------------------
99  * You should have received a copy of the GNU Affero General
100  * Public License along with this program. If not, see:
101  * http://www.gnu.org/licenses/
102  *
103  * If you would like to use this software outside of the free
104  * software license, please contact FellowTraveler.
105  * (Unfortunately many will run anonymously and untraceably,
106  * so who could really stop them?)
107  *
108  * DISCLAIMER:
109  * This program is distributed in the hope that it will be
110  * useful, but WITHOUT ANY WARRANTY; without even the implied
111  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
112  * PURPOSE. See the GNU Affero General Public License for
113  * more details.
114 
115  -----BEGIN PGP SIGNATURE-----
116  Version: GnuPG v1.4.9 (Darwin)
117 
118  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
119  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
120  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
121  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
122  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
123  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
124  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
125  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
126  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
127  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
128  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
129  kamH0Y/n11lCvo1oQxM+
130  =uSzz
131  -----END PGP SIGNATURE-----
132  **************************************************************/
133 
134 #include "stdafx.hpp"
135 
136 #include "OTNumList.hpp"
137 
138 #include "OTLog.hpp"
139 #include "OTStorage.hpp"
140 
141 #include <locale>
142 
143 // OTNumList (helper class.)
144 
145 namespace opentxs
146 {
147 
148 OTNumList::OTNumList(const std::set<int64_t>& theNumbers)
149 {
150  Add(theNumbers);
151 }
152 
153 OTNumList::OTNumList(int64_t lInput)
154 {
155  Add(lInput);
156 }
157 
158 // removed, security reasons.
159 // OTNumList::OTNumList(const char* szNumbers)
160 //{
161 // Add(szNumbers);
162 //}
163 
164 OTNumList::OTNumList(const OTString& strNumbers)
165 {
166  Add(strNumbers);
167 }
168 
169 OTNumList::OTNumList(const std::string& strNumbers)
170 {
171  Add(strNumbers);
172 }
173 
175 {
176 }
177 
179 {
180 }
181 
182 bool OTNumList::Add(const OTString& strNumbers) // if false, means the numbers
183  // were already there. (At least
184  // one of them.)
185 {
186  return Add(strNumbers.Get());
187 }
188 
189 bool OTNumList::Add(const std::string& strNumbers) // if false, means the
190  // numbers were already
191  // there. (At least one of
192  // them.)
193 {
194  return Add(strNumbers.c_str());
195 }
196 
197 // This function is private, so you can't use it without passing an OTString.
198 // (For security reasons.) It takes a comma-separated list of numbers, and adds
199 // them to *this.
200 //
201 bool OTNumList::Add(const char* szNumbers) // if false, means the numbers were
202  // already there. (At least one of
203  // them.)
204 {
205  OT_ASSERT(nullptr != szNumbers); // Should never happen.
206 
207  bool bSuccess = true;
208  int64_t lNum = 0;
209  const char* pChar = szNumbers;
210  std::locale loc;
211 
212  // Skip any whitespace.
213  while (std::isspace(*pChar, loc)) pChar++;
214 
215  bool bStartedANumber =
216  false; // During the loop, set this to true when processing a digit, and
217  // set to false when anything else. That way when we go to add
218  // the number to the list, and it's "0", we'll know it's a real
219  // number we're supposed to add, and not just a default value.
220 
221  for (;;) // We already know it's not null, due to the assert. (So at least
222  // one iteration will happen.)
223  {
224  if (std::isdigit(*pChar, loc)) {
225  bStartedANumber = true;
226 
227  int32_t nDigit = (*pChar - '0');
228 
229  lNum *= 10; // Move it up a decimal place.
230  lNum += nDigit;
231  }
232  // if separator, or end of string, either way, add lNum to *this.
233  else if ((',' == *pChar) || ('\0' == *pChar) ||
234  std::isspace(*pChar, loc)) // first sign of a space, and we are
235  // done with current number. (On to
236  // the next.)
237  {
238  if ((lNum > 0) || (bStartedANumber && (0 == lNum))) {
239  if (!Add(lNum)) // <=========
240  {
241  bSuccess = false; // We still go ahead and try to add them
242  // all, and then return this sort of
243  // status when it's all done.
244  }
245  }
246 
247  lNum = 0; // reset for the next transaction number (in the
248  // comma-separated list.)
249  bStartedANumber = false; // reset
250  }
251  else {
252  otErr << "OTNumList::Add: Error: Unexpected character found in "
253  "erstwhile comma-separated list of longs: " << *pChar
254  << "\n";
255  bSuccess = false;
256  break;
257  }
258 
259  // End of the road.
260  if ('\0' == *pChar) break;
261 
262  pChar++;
263 
264  // Skip any whitespace.
265  while (std::isspace(*pChar, loc)) pChar++;
266 
267  } // while
268 
269  return bSuccess;
270 }
271 
272 bool OTNumList::Add(const int64_t& theValue) // if false, means the value was
273  // already there.
274 {
275  auto it = m_setData.find(theValue);
276 
277  if (m_setData.end() == it) // it's not already there, so add it.
278  {
279  m_setData.insert(theValue);
280  return true;
281  }
282  return false; // it was already there.
283 }
284 
285 bool OTNumList::Peek(int64_t& lPeek) const
286 {
287  auto it = m_setData.begin();
288 
289  if (m_setData.end() != it) // it's there.
290  {
291  lPeek = *it;
292  return true;
293  }
294  return false;
295 }
296 
298 {
299  auto it = m_setData.begin();
300 
301  if (m_setData.end() != it) // it's there.
302  {
303  m_setData.erase(it);
304  return true;
305  }
306  return false;
307 }
308 
309 bool OTNumList::Remove(const int64_t& theValue) // if false, means the value was
310  // NOT already there.
311 {
312  auto it = m_setData.find(theValue);
313 
314  if (m_setData.end() != it) // it's there.
315  {
316  m_setData.erase(it);
317  return true;
318  }
319  return false; // it wasn't there (so how could you remove it then?)
320 }
321 
322 bool OTNumList::Verify(const int64_t& theValue) const // returns true/false
323  // (whether value is
324  // already there.)
325 {
326  auto it = m_setData.find(theValue);
327 
328  return (m_setData.end() == it) ? false : true;
329 }
330 
331 // True/False, based on whether values are already there.
332 // (ALL theNumbersmust be present.)
333 // So if *this contains "3,4,5,6" and rhs contains "4,5" then match is TRUE.
334 //
335 bool OTNumList::Verify(const std::set<int64_t>& theNumbers) const
336 {
337  bool bSuccess = true;
338 
339  for (const auto& it : theNumbers) {
340  if (!Verify(it)) // It must have NOT already been there.
341  bSuccess = false;
342  }
343 
344  return bSuccess;
345 }
346 
350 bool OTNumList::Verify(const OTNumList& rhs) const
351 {
352  // Verify they have the same number of elements.
353  //
354  if (Count() != rhs.Count()) return false;
355 
356  // Verify each value on *this is also found on rhs.
357  //
358  for (auto& it : m_setData) {
359  if (!rhs.Verify(it)) return false;
360  }
361 
362  return true;
363 }
364 
367 bool OTNumList::VerifyAny(const OTNumList& rhs) const
368 {
369  return rhs.VerifyAny(m_setData);
370 }
371 
374 bool OTNumList::VerifyAny(const std::set<int64_t>& setData) const
375 {
376  for (const auto& it : m_setData) {
377  auto it_find = setData.find(it);
378 
379  if (it_find != setData.end()) // found a match.
380  return true;
381  }
382 
383  return false;
384 }
385 
386 bool OTNumList::Add(const OTNumList& theNumList) // if false, means the numbers
387  // were already there. (At
388  // least one of them.)
389 {
390  std::set<int64_t> theOutput;
391  theNumList.Output(theOutput); // returns false if the numlist was empty.
392 
393  return Add(theOutput);
394 }
395 
396 bool OTNumList::Add(const std::set<int64_t>& theNumbers) // if false, means the
397  // numbers were already
398  // there. (At least one
399  // of them.)
400 {
401  bool bSuccess = true;
402 
403  for (const auto& it : theNumbers) {
404  if (!Add(it)) // It must have already been there.
405  bSuccess = false;
406  }
407 
408  return bSuccess;
409 }
410 
411 bool OTNumList::Remove(const std::set<int64_t>& theNumbers) // if false, means
412  // the numbers were
413  // NOT already
414  // there. (At least
415  // one of them.)
416 {
417  bool bSuccess = true;
418 
419  for (const auto& it : theNumbers) {
420  if (!Remove(it)) // It must have NOT already been there.
421  bSuccess = false;
422  }
423 
424  return bSuccess;
425 }
426 
427 // Outputs the numlist as a set of numbers.
428 // (To iterate OTNumList, call this, then iterate the output.)
429 //
430 bool OTNumList::Output(std::set<int64_t>& theOutput) const // returns false if
431  // the numlist was
432  // empty.
433 {
434  theOutput = m_setData;
435 
436  return !m_setData.empty();
437 }
438 
439 // Outputs the numlist as a comma-separated string (for serialization, usually.)
440 //
441 bool OTNumList::Output(OTString& strOutput) const // returns false if the
442  // numlist was empty.
443 {
444  int32_t nIterationCount = 0;
445 
446  for (auto& it : m_setData) {
447  nIterationCount++;
448 
449  strOutput.Concatenate(
450  "%s%lld",
451  // If first iteration, prepend a blank string (instead of a comma.)
452  // Like this: "%lld"
453  // But for all subsequent iterations, concatenate: ",%lld"
454  (1 == nIterationCount) ? "" : ",", it);
455  }
456 
457  return !m_setData.empty();
458 }
459 
460 int32_t OTNumList::Count() const
461 {
462  return static_cast<int32_t>(m_setData.size());
463 }
464 
466 {
467  m_setData.clear();
468 }
469 
470 } // namespace opentxs
EXPORT void Release()
Definition: OTNumList.cpp:465
EXPORT bool Remove(const int64_t &theValue)
Definition: OTNumList.cpp:309
EXPORT bool Peek(int64_t &lPeek) const
Definition: OTNumList.cpp:285
EXPORT bool Output(std::set< int64_t > &theOutput) const
Definition: OTNumList.cpp:430
#define OT_ASSERT(x)
Definition: Assert.hpp:150
EXPORT int32_t Count() const
Definition: OTNumList.cpp:460
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
EXPORT bool VerifyAny(const OTNumList &rhs) const
Definition: OTNumList.cpp:367
EXPORT bool Verify(const int64_t &theValue) const
Definition: OTNumList.cpp:322
EXPORT bool Pop()
Definition: OTNumList.cpp:297