Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTSettings.cpp
Go to the documentation of this file.
1 /************************************************************
2 *
3 * OTSettings.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 "OTSettings.hpp"
136 #include "OTLog.hpp"
137 #include "util/OTPaths.hpp"
138 
139 #include "simpleini/SimpleIni.h"
140 
141 namespace opentxs
142 {
143 
145 {
146 private:
148  OTSettingsPvt& operator=(const OTSettingsPvt&);
149 
150 public:
151  CSimpleIniA iniSimple;
152 
154  : iniSimple()
155  {
156  }
157 };
158 
159 bool OTSettings::Load(const OTString& strConfigurationFileExactPath)
160 {
161  if (!strConfigurationFileExactPath.Exists()) {
162  otErr << __FUNCTION__ << ": Error: "
163  << "strConfigurationFileExactPath"
164  << " is Empty!\n";
165  return false;
166  }
167 
168  bool bFolderCreated(false);
169  if (!OTPaths::BuildFilePath(strConfigurationFileExactPath,
170  bFolderCreated)) {
171  OT_FAIL;
172  };
173 
174  if (!IsEmpty()) {
175  otErr << __FUNCTION__ << ": Bad: "
176  << "p_Settings"
177  << " is not Empty!\n";
178  OT_FAIL;
179  }
180 
181  int64_t lFilelength;
182  if (!OTPaths::FileExists(strConfigurationFileExactPath,
183  lFilelength)) // we don't have a config file, lets
184  // create a blank one first.
185  {
186  pvt->iniSimple.Reset(); // clean the config.
187 
188  SI_Error rc = pvt->iniSimple.SaveFile(
189  strConfigurationFileExactPath.Get()); // save a new file.
190  if (0 > rc) return false; // error!
191 
192  pvt->iniSimple.Reset(); // clean the config (again).
193  }
194 
195  SI_Error rc = pvt->iniSimple.LoadFile(strConfigurationFileExactPath.Get());
196  if (0 > rc)
197  return false;
198  else
199  return true;
200 }
201 
202 bool OTSettings::Save(const OTString& strConfigurationFileExactPath)
203 {
204  if (!strConfigurationFileExactPath.Exists()) {
205  otErr << __FUNCTION__ << ": Error: "
206  << "strConfigurationFileExactPath"
207  << " is Empty!\n";
208  return false;
209  }
210 
211  SI_Error rc = pvt->iniSimple.SaveFile(strConfigurationFileExactPath.Get());
212  if (0 > rc)
213  return false;
214  else
215  return true;
216 }
217 
218 bool OTSettings::LogChange_str(const OTString& strSection,
219  const OTString& strKey, const OTString& strValue)
220 {
221  if (!strSection.Exists()) {
222  otErr << __FUNCTION__ << ": Error: "
223  << "strSection"
224  << " is Empty!\n";
225  OT_FAIL;
226  }
227  if (!strKey.Exists()) {
228  otErr << __FUNCTION__ << ": Error: "
229  << "strKey"
230  << " is Empty!\n";
231  OT_FAIL;
232  }
233 
234  const char* const szValue = (strValue.Exists() && !strValue.Compare(""))
235  ? strValue.Get()
236  : "nullptr";
237 
238  OTString strCategory, strOption;
239  if (!OTLog::StringFill(strCategory, strSection.Get(), 12)) return false;
240  if (!OTLog::StringFill(strOption, strKey.Get(), 30, " to:")) return false;
241 
242  otWarn << "Setting " << strCategory << " " << strOption << " " << szValue
243  << " \n";
244  return true;
245 }
246 
247 OTSettings::OTSettings(const OTString& strConfigFilePath)
248  : pvt(new OTSettingsPvt())
249  , b_Loaded(false)
250  , m_strConfigurationFileExactPath(strConfigFilePath)
251 {
252  if (!m_strConfigurationFileExactPath.Exists()) {
253  otErr << __FUNCTION__ << ": Error: "
254  << "m_strConfigurationFileExactPath"
255  << " is Empty!\n";
256  OT_FAIL;
257  }
258 }
259 
260 void OTSettings::SetConfigFilePath(const OTString& strConfigFilePath)
261 {
262  m_strConfigurationFileExactPath.Set(strConfigFilePath.Get());
263 }
264 
266  : pvt(new OTSettingsPvt())
267  , b_Loaded(false)
268 {
269 }
270 
272 {
273 }
274 
276 {
277  b_Loaded = false;
278 
279  if (Load(m_strConfigurationFileExactPath)) {
280  b_Loaded = true;
281  return true;
282  }
283  else
284  return false;
285 }
286 
288 {
289  return Save(m_strConfigurationFileExactPath);
290 }
291 
292 const bool& OTSettings::IsLoaded() const
293 {
294  return b_Loaded;
295 }
296 
298 {
299  b_Loaded = false;
300  pvt->iniSimple.Reset();
301  return true;
302 }
303 
305 {
306  return pvt->iniSimple.IsEmpty();
307 }
308 
309 bool OTSettings::Check_str(const OTString& strSection, const OTString& strKey,
310  OTString& out_strResult, bool& out_bKeyExist) const
311 {
312  if (!strSection.Exists()) {
313  otErr << __FUNCTION__ << ": Error: "
314  << "strSection"
315  << " is Empty!\n";
316  OT_FAIL;
317  }
318  if (strSection.Compare("")) {
319  otErr << __FUNCTION__ << ": Error: "
320  << "strSection"
321  << " is Blank!\n";
322  OT_FAIL;
323  }
324 
325  if (!strKey.Exists()) {
326  otErr << __FUNCTION__ << ": Error: "
327  << "strKey"
328  << " is Empty!\n";
329  OT_FAIL;
330  }
331  if (strKey.Compare("")) {
332  otErr << __FUNCTION__ << ": Error: "
333  << "strKey"
334  << " is Blank!\n";
335  OT_FAIL;
336  }
337 
338  const char* szVar =
339  pvt->iniSimple.GetValue(strSection.Get(), strKey.Get(), nullptr);
340  OTString strVar(szVar);
341 
342  if (strVar.Exists() && !strVar.Compare("")) {
343  out_bKeyExist = true;
344  out_strResult = strVar;
345  }
346  else {
347  out_bKeyExist = false;
348  out_strResult = "";
349  }
350 
351  return true;
352 }
353 
354 bool OTSettings::Check_long(const OTString& strSection, const OTString& strKey,
355  int64_t& out_lResult, bool& out_bKeyExist) const
356 {
357  if (!strSection.Exists()) {
358  otErr << __FUNCTION__ << ": Error: "
359  << "strSection"
360  << " is Empty!\n";
361  OT_FAIL;
362  }
363  if (strSection.Compare("")) {
364  otErr << __FUNCTION__ << ": Error: "
365  << "strSection"
366  << " is Blank!\n";
367  OT_FAIL;
368  }
369 
370  if (!strKey.Exists()) {
371  otErr << __FUNCTION__ << ": Error: "
372  << "strKey"
373  << " is Empty!\n";
374  OT_FAIL;
375  }
376  if (strKey.Compare("")) {
377  otErr << __FUNCTION__ << ": Error: "
378  << "strKey"
379  << " is Blank!\n";
380  OT_FAIL;
381  }
382 
383  const char* szVar =
384  pvt->iniSimple.GetValue(strSection.Get(), strKey.Get(), nullptr);
385  OTString strVar(szVar);
386 
387  if (strVar.Exists() && !strVar.Compare("")) {
388  out_bKeyExist = true;
389  out_lResult =
390  pvt->iniSimple.GetLongValue(strSection.Get(), strKey.Get(), 0);
391  }
392  else {
393  out_bKeyExist = false;
394  out_lResult = 0;
395  }
396 
397  return true;
398 }
399 
400 bool OTSettings::Check_bool(const OTString& strSection, const OTString& strKey,
401  bool& out_bResult, bool& out_bKeyExist) const
402 {
403  if (!strSection.Exists()) {
404  otErr << __FUNCTION__ << ": Error: "
405  << "strSection"
406  << " is Empty!\n";
407  OT_FAIL;
408  }
409  if (strSection.Compare("")) {
410  otErr << __FUNCTION__ << ": Error: "
411  << "strSection"
412  << " is Blank!\n";
413  OT_FAIL;
414  }
415 
416  if (!strKey.Exists()) {
417  otErr << __FUNCTION__ << ": Error: "
418  << "strKey"
419  << " is Empty!\n";
420  OT_FAIL;
421  }
422  if (strKey.Compare("")) {
423  otErr << __FUNCTION__ << ": Error: "
424  << "strKey"
425  << " is Blank!\n";
426  OT_FAIL;
427  }
428 
429  const char* szVar =
430  pvt->iniSimple.GetValue(strSection.Get(), strKey.Get(), nullptr);
431  OTString strVar(szVar);
432 
433  if (strVar.Exists() &&
434  (strVar.Compare("false") || strVar.Compare("true"))) {
435  out_bKeyExist = true;
436  if (strVar.Compare("true"))
437  out_bResult = true;
438  else
439  out_bResult = false;
440  }
441  else {
442  out_bKeyExist = false;
443  out_bResult = false;
444  }
445 
446  return true;
447 }
448 
449 bool OTSettings::Set_str(const OTString& strSection, const OTString& strKey,
450  const OTString& strValue, bool& out_bNewOrUpdate,
451  const OTString& strComment)
452 {
453  if (!strSection.Exists()) {
454  otErr << __FUNCTION__ << ": Error: "
455  << "strSection"
456  << " is Empty!\n";
457  OT_FAIL;
458  }
459  if (strSection.Compare("")) {
460  otErr << __FUNCTION__ << ": Error: "
461  << "strSection"
462  << " is Blank!\n";
463  OT_FAIL;
464  }
465 
466  if (!strKey.Exists()) {
467  otErr << __FUNCTION__ << ": Error: "
468  << "strKey"
469  << " is Empty!\n";
470  OT_FAIL;
471  }
472  if (strKey.Compare("")) {
473  otErr << __FUNCTION__ << ": Error: "
474  << "strKey"
475  << " is Blank!\n";
476  OT_FAIL;
477  }
478 
479  // if (nullptr == m_strConfigurationFileExactPath){ otErr << "%s: Error:
480  // %s is a nullptr!\n", __FUNCTION__, "p_iniSimple"); OT_FAIL; }
481 
482  const char* const szValue =
483  (strValue.Exists() && !strValue.Compare("")) ? strValue.Get() : nullptr;
484  const char* const szComment =
485  (strComment.Exists() && !strComment.Compare("")) ? strComment.Get()
486  : nullptr;
487 
488  OTString strOldValue, strNewValue;
489  bool bOldKeyExist, bNewKeyExist;
490 
491  // Check if Old Key exists.
492  if (!Check_str(strSection, strKey, strOldValue, bOldKeyExist)) return false;
493 
494  if (bOldKeyExist) {
495  if (strValue.Compare(strOldValue)) {
496  out_bNewOrUpdate = false;
497  return true;
498  }
499  }
500 
501  // Log to Output Setting Change
502  if (!LogChange_str(strSection, strKey, strValue)) return false;
503 
504  // Set New Value
505  SI_Error rc = pvt->iniSimple.SetValue(strSection.Get(), strKey.Get(),
506  szValue, szComment, true);
507  if (0 > rc) return false;
508 
509  if (nullptr == szValue) // We set the key's value to null, thus removing it.
510  {
511  if (bOldKeyExist)
512  out_bNewOrUpdate = true;
513  else
514  out_bNewOrUpdate = false;
515 
516  return true;
517  }
518 
519  // Check if the new value is the same as intended.
520  if (!Check_str(strSection, strKey, strNewValue, bNewKeyExist)) return false;
521 
522  if (bNewKeyExist) {
523  if (strValue.Compare(strNewValue)) {
524  // Success
525  out_bNewOrUpdate = true;
526  return true;
527  }
528  }
529 
530  // If we get here, error!
531  OT_FAIL;
532 }
533 
534 bool OTSettings::Set_long(const OTString& strSection, const OTString& strKey,
535  const int64_t& lValue, bool& out_bNewOrUpdate,
536  const OTString& strComment)
537 {
538  if (!strSection.Exists()) {
539  otErr << __FUNCTION__ << ": Error: "
540  << "strSection"
541  << " is Empty!\n";
542  OT_FAIL;
543  }
544  if (strSection.Compare("")) {
545  otErr << __FUNCTION__ << ": Error: "
546  << "strSection"
547  << " is Blank!\n";
548  OT_FAIL;
549  }
550 
551  if (!strKey.Exists()) {
552  otErr << __FUNCTION__ << ": Error: "
553  << "strKey"
554  << " is Empty!\n";
555  OT_FAIL;
556  }
557  if (strKey.Compare("")) {
558  otErr << __FUNCTION__ << ": Error: "
559  << "strKey"
560  << " is Blank!\n";
561  OT_FAIL;
562  }
563 
564  OTString strValue;
565  strValue.Format("%lld", lValue);
566 
567  const char* const szComment =
568  (strComment.Exists() && !strComment.Compare("")) ? strComment.Get()
569  : nullptr;
570 
571  OTString strOldValue, strNewValue;
572  bool bOldKeyExist, bNewKeyExist;
573 
574  // Check if Old Key exists.
575  if (!Check_str(strSection, strKey, strOldValue, bOldKeyExist)) return false;
576 
577  if (bOldKeyExist) {
578  if (strValue.Compare(strOldValue)) {
579  out_bNewOrUpdate = false;
580  return true;
581  }
582  }
583 
584  // Log to Output Setting Change
585  if (!LogChange_str(strSection, strKey, strValue)) return false;
586 
587  // Set New Value
588  SI_Error rc = pvt->iniSimple.SetLongValue(strSection.Get(), strKey.Get(),
589  lValue, szComment, false, true);
590  if (0 > rc) return false;
591 
592  // Check if the new value is the same as intended.
593  if (!Check_str(strSection, strKey, strNewValue, bNewKeyExist)) return false;
594 
595  if (bNewKeyExist) {
596  if (strValue.Compare(strNewValue)) {
597  // Success
598  out_bNewOrUpdate = true;
599  return true;
600  }
601  }
602 
603  // If we get here, error!
604  OT_FAIL;
605 }
606 
607 bool OTSettings::Set_bool(const OTString& strSection, const OTString& strKey,
608  const bool& bValue, bool& out_bNewOrUpdate,
609  const OTString& strComment)
610 {
611  if (!strSection.Exists()) {
612  otErr << __FUNCTION__ << ": Error: "
613  << "strSection"
614  << " is Empty!\n";
615  OT_FAIL;
616  }
617  if (!strKey.Exists()) {
618  otErr << __FUNCTION__ << ": Error: "
619  << "strKey"
620  << " is Empty!\n";
621  OT_FAIL;
622  }
623  const OTString strValue(bValue ? "true" : "false");
624 
625  return Set_str(strSection, strKey, strValue, out_bNewOrUpdate, strComment);
626 }
627 
628 bool OTSettings::CheckSetSection(const OTString& strSection,
629  const OTString& strComment,
630  bool& out_bIsNewSection)
631 {
632  if (!strSection.Exists()) {
633  otErr << __FUNCTION__ << ": Error: "
634  << "strSection"
635  << " is Empty!\n";
636  OT_FAIL;
637  }
638  if (!strComment.Exists()) {
639  otErr << __FUNCTION__ << ": Error: "
640  << "strComment"
641  << " is Empty!\n";
642  OT_FAIL;
643  }
644 
645  const char* const szComment =
646  (strComment.Exists() && !strComment.Compare("")) ? strComment.Get()
647  : nullptr;
648 
649  const int64_t lSectionSize =
650  pvt->iniSimple.GetSectionSize(strSection.Get());
651 
652  if (1 > lSectionSize) {
653  out_bIsNewSection = true;
654  SI_Error rc = pvt->iniSimple.SetValue(strSection.Get(), nullptr,
655  nullptr, szComment, false);
656  if (0 > rc) return false;
657  }
658  else {
659  out_bIsNewSection = false;
660  }
661  return true;
662 }
663 
664 bool OTSettings::CheckSet_str(const OTString& strSection,
665  const OTString& strKey,
666  const OTString& strDefault,
667  OTString& out_strResult, bool& out_bIsNew,
668  const OTString& strComment)
669 {
670  if (!strSection.Exists()) {
671  otErr << __FUNCTION__ << ": Error: "
672  << "strSection"
673  << " is Empty!\n";
674  OT_FAIL;
675  }
676  if (!strKey.Exists()) {
677  otErr << __FUNCTION__ << ": Error: "
678  << "strKey"
679  << " is Empty!\n";
680  OT_FAIL;
681  }
682 
683  const char* const szDefault =
684  (strDefault.Exists() && !strDefault.Compare("")) ? strDefault.Get()
685  : nullptr;
686 
687  OTString strTempResult;
688  bool bKeyExist;
689  if (!Check_str(strSection, strKey, strTempResult, bKeyExist)) return false;
690 
691  if (bKeyExist) {
692  // Already have a key, lets use it's value.
693  out_bIsNew = false;
694  out_strResult = strTempResult;
695  return true;
696  }
697  else {
698  bool bNewKeyCheck;
699  if (!Set_str(strSection, strKey, strDefault, bNewKeyCheck, strComment))
700  return false;
701 
702  if (nullptr == szDefault) // The Default is to have no key.
703  {
704  // Success
705  out_bIsNew = false;
706  out_strResult = "";
707  return true;
708  }
709 
710  if (bNewKeyCheck) {
711  // Success
712  out_bIsNew = true;
713  out_strResult = strDefault;
714  return true;
715  }
716  }
717 
718  // If we get here, error!
719  OT_FAIL;
720 }
721 
722 bool OTSettings::CheckSet_long(const OTString& strSection,
723  const OTString& strKey, const int64_t& lDefault,
724  int64_t& out_lResult, bool& out_bIsNew,
725  const OTString& strComment)
726 {
727  if (!strSection.Exists()) {
728  otErr << __FUNCTION__ << ": Error: "
729  << "strSection"
730  << " is Empty!\n";
731  OT_FAIL;
732  }
733  if (!strKey.Exists()) {
734  otErr << __FUNCTION__ << ": Error: "
735  << "strKey"
736  << " is Empty!\n";
737  OT_FAIL;
738  }
739 
740  int64_t lTempResult;
741  bool bKeyExist;
742  if (!Check_long(strSection, strKey, lTempResult, bKeyExist)) return false;
743 
744  if (bKeyExist) {
745  // Already have a key, lets use it's value.
746  out_bIsNew = false;
747  out_lResult = lTempResult;
748  return true;
749  }
750  else {
751  bool bNewKeyCheck;
752  if (!Set_long(strSection, strKey, lDefault, bNewKeyCheck, strComment))
753  return false;
754  if (bNewKeyCheck) {
755  // Success
756  out_bIsNew = true;
757  out_lResult = lDefault;
758  return true;
759  }
760  }
761 
762  // If we get here, error!
763  OT_FAIL;
764 }
765 
766 bool OTSettings::CheckSet_bool(const OTString& strSection,
767  const OTString& strKey, const bool& bDefault,
768  bool& out_bResult, bool& out_bIsNew,
769  const OTString& strComment)
770 {
771  if (!strSection.Exists()) {
772  otErr << __FUNCTION__ << ": Error: "
773  << "strSection"
774  << " is Empty!\n";
775  OT_FAIL;
776  }
777  if (!strKey.Exists()) {
778  otErr << __FUNCTION__ << ": Error: "
779  << "strKey"
780  << " is Empty!\n";
781  OT_FAIL;
782  }
783 
784  bool bKeyExist, bTempResult;
785  if (!Check_bool(strSection, strKey, bTempResult, bKeyExist)) return false;
786 
787  if (bKeyExist) {
788  // Already have a key, lets use it's value.
789  out_bIsNew = false;
790  out_bResult = bTempResult;
791  return true;
792  }
793  else {
794  bool bNewKeyCheck;
795  if (!Set_bool(strSection, strKey, bDefault, bNewKeyCheck, strComment))
796  return false;
797  if (bNewKeyCheck) {
798  // Success
799  out_bIsNew = true;
800  out_bResult = bDefault;
801  return true;
802  }
803  }
804 
805  // If we get here, error!
806  OT_FAIL;
807 }
808 
809 bool OTSettings::SetOption_bool(const OTString& strSection,
810  const OTString& strKey, bool& bVariableName)
811 {
812  bool bNewOrUpdate;
813  return CheckSet_bool(strSection, strKey, bVariableName, bVariableName,
814  bNewOrUpdate);
815 }
816 
817 } // namespace opentxs
static EXPORT bool FileExists(const OTString &strFilePath, int64_t &nFileLength)
Definition: OTPaths.cpp:842
EXPORT bool Check_str(const OTString &strSection, const OTString &strKey, OTString &out_strResult, bool &out_bKeyExist) const
Definition: OTSettings.cpp:309
EXPORT bool Set_bool(const OTString &strSection, const OTString &strKey, const bool &bValue, bool &out_bNewOrUpdate, const OTString &strComment="")
Definition: OTSettings.cpp:607
EXPORT bool Load()
Definition: OTSettings.cpp:275
EXPORT bool Exists() const
Definition: OTString.cpp:1035
EXPORT void Format(const char *fmt,...)
Definition: OTString.cpp:1319
EXPORT bool IsEmpty() const
Definition: OTSettings.cpp:304
EXPORT bool Compare(const char *compare) const
Definition: OTString.cpp:1102
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
EXPORT bool Set_long(const OTString &strSection, const OTString &strKey, const int64_t &lValue, bool &out_bNewOrUpdate, const OTString &strComment="")
Definition: OTSettings.cpp:534
EXPORT bool Set_str(const OTString &strSection, const OTString &strKey, const OTString &strValue, bool &out_bNewOrUpdate, const OTString &strComment="")
Definition: OTSettings.cpp:449
static EXPORT bool BuildFilePath(const OTString &strFolderPath, bool &out_bFolderCreated)
Definition: OTPaths.cpp:1369
EXPORT bool Reset()
Definition: OTSettings.cpp:297
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT bool CheckSet_str(const OTString &strSection, const OTString &strKey, const OTString &strDefault, OTString &out_strResult, bool &out_bIsNew, const OTString &strComment="")
Definition: OTSettings.cpp:664
EXPORT bool Check_bool(const OTString &strSection, const OTString &strKey, bool &out_bResult, bool &out_bKeyExist) const
Definition: OTSettings.cpp:400
OTLOG_IMPORT OTLogStream otWarn
EXPORT const char * Get() const
Definition: OTString.cpp:1045
EXPORT bool SetOption_bool(const OTString &strSection, const OTString &strKey, bool &bVariableName)
Definition: OTSettings.cpp:809
OTLOG_IMPORT OTLogStream otErr
EXPORT bool CheckSet_long(const OTString &strSection, const OTString &strKey, const int64_t &lDefault, int64_t &out_lResult, bool &out_bIsNew, const OTString &strComment="")
Definition: OTSettings.cpp:722
EXPORT bool Check_long(const OTString &strSection, const OTString &strKey, int64_t &out_lResult, bool &out_bKeyExist) const
Definition: OTSettings.cpp:354
EXPORT bool CheckSetSection(const OTString &strSection, const OTString &strComment, bool &out_bIsNewSection)
Definition: OTSettings.cpp:628
EXPORT const bool & IsLoaded() const
Definition: OTSettings.cpp:292
static EXPORT bool StringFill(OTString &out_strString, const char *szString, int32_t iLength, const char *szAppend=nullptr)
Definition: OTLog.cpp:900
EXPORT void SetConfigFilePath(const OTString &strConfigFilePath)
Definition: OTSettings.cpp:260
EXPORT bool Save()
Definition: OTSettings.cpp:287
EXPORT bool CheckSet_bool(const OTString &strSection, const OTString &strKey, const bool &bDefault, bool &out_bResult, bool &out_bIsNew, const OTString &strComment="")
Definition: OTSettings.cpp:766