Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTString Class Reference

#include <OTString.hpp>

Inheritance diagram for opentxs::OTString:

Public Types

typedef std::list< std::string > List
 
typedef std::map< std::string,
std::string > 
Map
 

Public Member Functions

EXPORT OTString ()
 
EXPORT OTString (const OTString &value)
 
EXPORT OTString (const OTASCIIArmor &value)
 
 OTString (const OTSignature &value)
 
EXPORT OTString (const OTContract &value)
 
EXPORT OTString (const OTIdentifier &value)
 
 OTString (OTPseudonym &value)
 
EXPORT OTString (const char *value)
 
 OTString (const char *value, size_t size)
 
EXPORT OTString (const std::string &value)
 
virtual EXPORT ~OTString ()
 
virtual EXPORT void Release ()
 
void Initialize ()
 
EXPORT OTStringoperator= (OTString rhs)
 
void swap (OTString &rhs)
 
bool operator> (const OTString &rhs) const
 
bool operator< (const OTString &rhs) const
 
bool operator<= (const OTString &rhs) const
 
bool operator>= (const OTString &rhs) const
 
EXPORT bool operator== (const OTString &rhs) const
 
EXPORT int64_t ToLong () const
 
EXPORT uint64_t ToUlong () const
 
EXPORT bool At (uint32_t index, char &c) const
 
EXPORT bool Exists () const
 
EXPORT bool DecodeIfArmored (bool escapedIsAllowed=true)
 
EXPORT uint32_t GetLength () const
 
EXPORT bool Compare (const char *compare) const
 
EXPORT bool Compare (const OTString &compare) const
 
EXPORT bool Contains (const char *compare) const
 
bool Contains (const OTString &compare) const
 
EXPORT const char * Get () const
 
EXPORT void Set (const char *data, uint32_t enforcedMaxLength=0)
 
EXPORT void Set (const OTString &data)
 
EXPORT bool MemSet (const char *mem, uint32_t size)
 
EXPORT void Concatenate (const char *arg,...)
 
void Concatenate (const OTString &data)
 
void Truncate (uint32_t index)
 
EXPORT void Format (const char *fmt,...)
 
void ConvertToLowerCase () const
 
void ConvertToUpperCase () const
 
EXPORT bool TokenizeIntoKeyValuePairs (Map &map) const
 
EXPORT void OTfgets (std::istream &ofs)
 
bool sgets (char *buffer, uint32_t size)
 
char sgetc ()
 
void sungetc ()
 
void reset ()
 
void WriteToFile (std::ostream &ofs) const
 
void Release_String ()
 
EXPORT void zeroMemory () const
 

Static Public Member Functions

static bool vformat (const char *fmt, std::va_list *pvl, std::string &s)
 
static EXPORT std::string & trim (std::string &str)
 
static EXPORT std::string replace_chars (const std::string &str, const std::string &charsFrom, const char &charTo)
 
static EXPORT bool safe_strcpy (char *dest, const char *src, size_t destSize, bool zeroSource=false)
 
static size_t safe_strlen (const char *s, size_t max)
 
static EXPORT int64_t StringToLong (const std::string &number)
 
static EXPORT uint64_t StringToUlong (const std::string &number)
 

Protected Attributes

uint32_t length_
 
uint32_t position_
 
char * data_
 

Friends

EXPORT friend std::ostream & operator<< (std::ostream &os, const OTString &obj)
 

Detailed Description

Definition at line 158 of file OTString.hpp.

Member Typedef Documentation

typedef std::list<std::string> opentxs::OTString::List

Definition at line 161 of file OTString.hpp.

typedef std::map<std::string, std::string> opentxs::OTString::Map

Definition at line 162 of file OTString.hpp.

Constructor & Destructor Documentation

opentxs::OTString::OTString ( )

Definition at line 786 of file OTString.cpp.

787  : length_(0)
788  , position_(0)
789  , data_(nullptr)
790 {
791  // Initialize();
792 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const OTString value)

Definition at line 857 of file OTString.cpp.

858  : length_(0)
859  , position_(0)
860  , data_(nullptr)
861 {
862  // Initialize();
863  LowLevelSetStr(strValue);
864 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const OTASCIIArmor value)

Definition at line 819 of file OTString.cpp.

820  : length_(0)
821  , position_(0)
822  , data_(nullptr)
823 {
824  // Initialize();
825 
826  if (strValue.Exists()) strValue.GetString(*this);
827 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const OTSignature value)

Definition at line 837 of file OTString.cpp.

838  : length_(0)
839  , position_(0)
840  , data_(nullptr)
841 {
842  // Initialize();
843 
844  if (strValue.Exists()) strValue.GetString(*this);
845 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const OTContract value)

Definition at line 807 of file OTString.cpp.

808  : length_(0)
809  , position_(0)
810  , data_(nullptr)
811 {
812  // Initialize();
813 
814  (const_cast<OTContract&>(theValue)).SaveContractRaw(*this);
815 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const OTIdentifier value)

Definition at line 797 of file OTString.cpp.

798  : length_(0)
799  , position_(0)
800  , data_(nullptr)
801 {
802  // Initialize();
803 
804  if (theValue.GetSize() > 0) theValue.GetString(*this);
805 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( OTPseudonym value)

Definition at line 847 of file OTString.cpp.

848  : length_(0)
849  , position_(0)
850  , data_(nullptr)
851 {
852  // Initialize();
853 
854  theValue.SavePseudonym(*this);
855 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const char *  value)

Definition at line 866 of file OTString.cpp.

867  : length_(0)
868  , position_(0)
869  , data_(nullptr)
870 {
871  // Initialize();
872  LowLevelSet(new_string, 0);
873 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const char *  value,
size_t  size 
)

Definition at line 875 of file OTString.cpp.

876  : length_(0)
877  , position_(0)
878  , data_(nullptr)
879 {
880  // Initialize();
881  LowLevelSet(new_string, static_cast<uint32_t>(sizeLength));
882 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::OTString ( const std::string &  value)

Definition at line 884 of file OTString.cpp.

885  : length_(0)
886  , position_(0)
887  , data_(nullptr)
888 {
889  // Initialize();
890  LowLevelSet(new_string.c_str(), static_cast<uint32_t>(new_string.length()));
891 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
opentxs::OTString::~OTString ( )
virtual

Definition at line 774 of file OTString.cpp.

775 {
776  Release_String();
777 }
void Release_String()
Definition: OTString.cpp:751

Member Function Documentation

bool opentxs::OTString::At ( uint32_t  index,
char &  c 
) const

Definition at line 1025 of file OTString.cpp.

1026 {
1027  if (lIndex < length_) {
1028  c = data_[lIndex];
1029  return true;
1030  }
1031  else
1032  return false;
1033 }
uint32_t length_
Definition: OTString.hpp:275
bool opentxs::OTString::Compare ( const char *  compare) const

Definition at line 1102 of file OTString.cpp.

1103 {
1104  if (nullptr == data_ || nullptr == strCompare) {
1105  return false;
1106  }
1107 
1108  char* s1 = data_;
1109  char* s2 = (char*)strCompare;
1110 
1111  for (; *s1 && *s2; s1++, s2++)
1112  if (*s1 != *s2) return false;
1113 
1114  if (*s1 != *s2) return false;
1115 
1116  return true;
1117 }
bool opentxs::OTString::Compare ( const OTString compare) const

Definition at line 1119 of file OTString.cpp.

1120 {
1121  if (nullptr == data_ || !strCompare.Exists()) {
1122  return false;
1123  }
1124 
1125  char* s1 = data_;
1126  const char* s2 = strCompare.Get();
1127 
1128  for (; *s1 && *s1 != ' '; s1++, s2++)
1129  if (*s1 != *s2) return false;
1130 
1131  return true;
1132 }
void opentxs::OTString::Concatenate ( const char *  arg,
  ... 
)

Definition at line 1334 of file OTString.cpp.

1335 {
1336  va_list vl;
1337  va_start(vl, fmt);
1338 
1339  std::string str_output;
1340 
1341  const bool bSuccess = OTString::vformat(fmt, &vl, str_output);
1342 
1343  va_end(vl);
1344 
1345  if (bSuccess) {
1346  const OTString strConcat(str_output);
1347 
1348  Concatenate(strConcat);
1349  }
1350 }
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT void Concatenate(const char *arg,...)
Definition: OTString.cpp:1334
static bool vformat(const char *fmt, std::va_list *pvl, std::string &s)
Definition: OTString.cpp:191
void opentxs::OTString::Concatenate ( const OTString data)

Definition at line 1353 of file OTString.cpp.

1354 {
1355  std::string str_output;
1356 
1357  if ((length_ > 0) && (nullptr != data_)) str_output += data_;
1358 
1359  if (strBuf.Exists() && (strBuf.GetLength() > 0)) str_output += strBuf.Get();
1360 
1361  Set(str_output.c_str());
1362 }
uint32_t length_
Definition: OTString.hpp:275
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
bool opentxs::OTString::Contains ( const char *  compare) const

Definition at line 1137 of file OTString.cpp.

1138 {
1139  if (nullptr == data_ || nullptr == strCompare) {
1140  return false;
1141  }
1142 
1143  if (strstr(data_, strCompare)) return true;
1144 
1145  return false;
1146 }
bool opentxs::OTString::Contains ( const OTString compare) const

Definition at line 1148 of file OTString.cpp.

1149 {
1150  if (nullptr == data_ || !strCompare.Exists()) {
1151  return false;
1152  }
1153 
1154  if (strstr(data_, strCompare.Get())) return true;
1155 
1156  return false;
1157 }
void opentxs::OTString::ConvertToLowerCase ( ) const

Definition at line 1171 of file OTString.cpp.

1172 {
1173  if (data_ == nullptr) {
1174  return;
1175  }
1176 
1177  for (char* s1 = data_; *s1; s1++) {
1178  *s1 = static_cast<char>(tolower(*s1));
1179  }
1180 }
void opentxs::OTString::ConvertToUpperCase ( ) const

Definition at line 1182 of file OTString.cpp.

1183 {
1184  if (data_ == nullptr) {
1185  return;
1186  }
1187 
1188  for (char* s1 = data_; *s1; s1++) {
1189  *s1 = static_cast<char>(toupper(*s1));
1190  }
1191 }
bool opentxs::OTString::DecodeIfArmored ( bool  escapedIsAllowed = true)

Definition at line 1212 of file OTString.cpp.

1213 {
1214  if (!Exists()) return false;
1215 
1216  bool bArmoredAndALSOescaped = false; // "- -----BEGIN OT ARMORED"
1217  bool bArmoredButNOTescaped = false; // "-----BEGIN OT ARMORED"
1218 
1219  if (Contains(OT_BEGIN_ARMORED_escaped)) // check this one first...
1220  {
1221  bArmoredAndALSOescaped = true;
1222 
1223  if (!bEscapedIsAllowed) {
1224  otErr << __FUNCTION__ << ": Armored and escaped value passed in, "
1225  "but escaped are forbidden here. "
1226  "(Returning.)\n";
1227  return false;
1228  }
1229  }
1230  else if (Contains(OT_BEGIN_ARMORED)) {
1231  bArmoredButNOTescaped = true;
1232  }
1233 
1234  const bool bArmored = (bArmoredAndALSOescaped || bArmoredButNOTescaped);
1235 
1236  // Whether the string is armored or not, (-----BEGIN OT ARMORED)
1237  // either way, we'll end up with the decoded version in this variable:
1238  //
1239  std::string str_Trim;
1240 
1241  if (bArmored) // it's armored, we have to decode it first.
1242  {
1243  OTASCIIArmor ascTemp;
1244  if (false ==
1245  (ascTemp.LoadFromString(
1246  *this, bArmoredAndALSOescaped, // if it IS escaped or not, this
1247  // variable will be true or false
1248  // to show it.
1249  // The below szOverride sub-string determines where the content
1250  // starts, when loading.
1251  OT_BEGIN_ARMORED))) // Default is: "-----BEGIN"
1252  // We're doing this: "-----BEGIN OT ARMORED" (Should worked for
1253  // escaped as well, here.)
1254  {
1255  otErr << __FUNCTION__ << ": Error loading string contents from "
1256  "ascii-armored encoding. "
1257  "Contents: \n" << Get() << "\n";
1258  return false;
1259  }
1260  else // success loading the actual contents out of the ascii-armored
1261  // version.
1262  {
1263  OTString strTemp(ascTemp); // <=== ascii-decoded here.
1264  std::string str_temp(strTemp.Get(), strTemp.GetLength());
1265  str_Trim = OTString::trim(
1266  str_temp); // This is the std::string for the trim process.
1267  }
1268  }
1269  else {
1270  std::string str_temp(Get(), GetLength());
1271  str_Trim = OTString::trim(str_temp); // This is the std::string for the
1272  // trim process. (Wasn't armored,
1273  // so here we use it as passed in.)
1274  }
1275 
1276  // At this point, str_Trim contains the actual contents, whether they
1277  // were originally ascii-armored OR NOT. (And they are also now trimmed,
1278  // either way.)
1279 
1280  Release();
1281 
1282  if (str_Trim.size() > 0) Set(str_Trim.c_str());
1283 
1284  return Exists();
1285 }
const char * OT_BEGIN_ARMORED_escaped
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT uint32_t GetLength() const
Definition: OTString.cpp:1040
EXPORT bool Exists() const
Definition: OTString.cpp:1035
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
EXPORT bool Contains(const char *compare) const
Definition: OTString.cpp:1137
static EXPORT std::string & trim(std::string &str)
Definition: OTString.cpp:398
virtual EXPORT void Release()
Definition: OTString.cpp:765
const char * OT_BEGIN_ARMORED
bool opentxs::OTString::Exists ( void  ) const

Definition at line 1035 of file OTString.cpp.

1036 {
1037  return (nullptr != data_) ? true : false;
1038 }
void opentxs::OTString::Format ( const char *  fmt,
  ... 
)

Definition at line 1319 of file OTString.cpp.

1320 {
1321  va_list vl;
1322  va_start(vl, fmt);
1323 
1324  std::string str_output;
1325 
1326  const bool bSuccess = OTString::vformat(fmt, &vl, str_output);
1327 
1328  va_end(vl);
1329 
1330  if (bSuccess) Set(str_output.c_str());
1331 }
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
static bool vformat(const char *fmt, std::va_list *pvl, std::string &s)
Definition: OTString.cpp:191
const char * opentxs::OTString::Get ( void  ) const

Definition at line 1045 of file OTString.cpp.

1046 {
1047  return (nullptr != data_) ? const_cast<const char*>(data_) : "";
1048 }
uint32_t opentxs::OTString::GetLength ( void  ) const

Definition at line 1040 of file OTString.cpp.

1041 {
1042  return length_;
1043 }
uint32_t length_
Definition: OTString.hpp:275
void opentxs::OTString::Initialize ( )

Definition at line 779 of file OTString.cpp.

780 {
781  length_ = 0;
782  position_ = 0;
783  data_ = nullptr;
784 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
bool opentxs::OTString::MemSet ( const char *  mem,
uint32_t  size 
)

Definition at line 967 of file OTString.cpp.

968 {
969  Release();
970  // -------------------
971  if ((nullptr == pMem) || (theSize < 1)) return true;
972 
973  char* str_new = new char[theSize + 1]; // then we allocate 11
974  OT_ASSERT(nullptr != str_new);
975  // -------------------
976  OTPassword::zeroMemory(str_new, theSize + 1);
977 
978  // void * OTPassword::safe_memcpy(void* dest,
979  // uint32_t dest_size,
980  // const
981  // void * src,
982  // uint32_t src_length,
983  // bool bZeroSource)
984 
985  OTPassword::safe_memcpy(static_cast<void*>(str_new), theSize + 1, pMem,
986  theSize);
987  // memcpy(static_cast<void*>(str_new), pMem, theSize); // then we copy 10
988  // bytes
989 
990  // todo optimize: This is probably superfluous due to the zeroMemory above.
991  // Then again, we might want to remove that, and then keep this.
992  str_new[theSize] = '\0'; // add null-terminator. (I deliberately made this
993  // buffer 1 byte larger so I could put the 0 at the
994  // end.) Here the index[10] is the 11th byte, since
995  // we're counting from 0.
996 
997  // Calculate the length (in case there was a null terminator in the
998  // middle...)
999  // This way we're guaranteed to have the correct length.
1000  //
1001  uint32_t nLength = static_cast<uint32_t>(
1002  OTString::safe_strlen(str_new, static_cast<size_t>(theSize)));
1003  str_new[nLength] = '\0'; // This SHOULD be superfluous as well...
1004 
1005  length_ = nLength; // the length doesn't count the 0.
1006  data_ = str_new;
1007 
1008  return true;
1009 }
uint32_t length_
Definition: OTString.hpp:275
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
static size_t safe_strlen(const char *s, size_t max)
Definition: OTString.cpp:388
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
virtual EXPORT void Release()
Definition: OTString.cpp:765
bool opentxs::OTString::operator< ( const OTString rhs) const

Definition at line 477 of file OTString.cpp.

478 {
479  if (length_ == 0) {
480  return (true);
481  }
482  if (s2.length_ == 0) {
483  return (false);
484  }
485  if (strcmp(data_, s2.data_) >= 0) {
486  return (false);
487  }
488  return (true);
489 }
uint32_t length_
Definition: OTString.hpp:275
bool opentxs::OTString::operator<= ( const OTString rhs) const

Definition at line 491 of file OTString.cpp.

492 {
493  if (length_ == 0) {
494  return (true);
495  }
496  if (s2.length_ == 0) {
497  return (false);
498  }
499  if (strcmp(data_, s2.data_) > 0) {
500  return (false);
501  }
502  return (true);
503 }
uint32_t length_
Definition: OTString.hpp:275
OTString & opentxs::OTString::operator= ( OTString  rhs)

Definition at line 1011 of file OTString.cpp.

1012 {
1013  if (this != &rhs) // Compare addresses.
1014  swap(rhs); // Only swap if they are different objects.
1015  return *this;
1016 }
void swap(OTString &rhs)
Definition: OTString.cpp:1018
bool opentxs::OTString::operator== ( const OTString rhs) const

Definition at line 1077 of file OTString.cpp.

1078 {
1079  // If they are not the same length, return false
1080  if (length_ != s2.length_) {
1081  return (false);
1082  }
1083 
1084  // At this point we know they are at least the same length.
1085  // Next--are they both 0? If they are both 0, return true
1086  if (length_ == 0 && s2.length_ == 0) {
1087  return (true);
1088  }
1089 
1090  // At this point we have 2 identical-length strings.
1091  // Now we call strcmp and convert it to true or false.
1092  if (strcmp(data_, s2.data_) ==
1093  0) { // TODO security: use a replacement for strcmp.
1094  return (true);
1095  }
1096  return (false);
1097 }
uint32_t length_
Definition: OTString.hpp:275
bool opentxs::OTString::operator> ( const OTString rhs) const

Definition at line 463 of file OTString.cpp.

464 {
465  if (s2.length_ == 0) {
466  return (true);
467  }
468  if (length_ == 0) {
469  return (false);
470  }
471  if (strcmp(data_, s2.data_) <= 0) {
472  return (false);
473  }
474  return (true);
475 }
uint32_t length_
Definition: OTString.hpp:275
bool opentxs::OTString::operator>= ( const OTString rhs) const

Definition at line 505 of file OTString.cpp.

506 {
507  if (s2.length_ == 0) {
508  return (true);
509  }
510  if (length_ == 0) {
511  return (false);
512  }
513  if (strcmp(data_, s2.data_) < 0) {
514  return (false);
515  }
516  return (true);
517 }
uint32_t length_
Definition: OTString.hpp:275
void opentxs::OTString::OTfgets ( std::istream &  ofs)

Definition at line 1159 of file OTString.cpp.

1160 {
1161  std::stringbuf sb;
1162  ifs.get(sb); // delimiter defaults to '\n'
1163 
1164  if (ifs.good()) {
1165  const std::string str_output = sb.str();
1166 
1167  Set(str_output.c_str());
1168  }
1169 }
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
void opentxs::OTString::Release ( void  )
virtual

Definition at line 765 of file OTString.cpp.

766 {
767  Release_String();
768 
769  // no need to use ot_super here, since OTString is a "base class."
770 }
void Release_String()
Definition: OTString.cpp:751
void opentxs::OTString::Release_String ( void  )

Definition at line 751 of file OTString.cpp.

752 {
753  if (nullptr != data_) {
754  // for security purposes.
755  //
757  // memset(data_, 0, length_);
758  delete[] data_;
759  }
760  data_ = nullptr;
761  position_ = 0;
762  length_ = 0;
763 }
uint32_t length_
Definition: OTString.hpp:275
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
uint32_t position_
Definition: OTString.hpp:276
std::string opentxs::OTString::replace_chars ( const std::string &  str,
const std::string &  charsFrom,
const char &  charTo 
)
static

Definition at line 420 of file OTString.cpp.

423 {
424  std::string l_str(str);
425  size_t found;
426 
427  found = str.find_first_of(charsFrom);
428  while (found != std::string::npos) {
429  l_str[found] = charTo;
430  found = str.find_first_of(charsFrom, found + 1);
431  }
432  return l_str;
433 }
void opentxs::OTString::reset ( void  )

Definition at line 1464 of file OTString.cpp.

1465 {
1466  position_ = 0;
1467 }
uint32_t position_
Definition: OTString.hpp:276
bool opentxs::OTString::safe_strcpy ( char *  dest,
const char *  src,
size_t  destSize,
bool  zeroSource = false 
)
static

Definition at line 353 of file OTString.cpp.

358 {
359  // Make sure they don't overlap.
360  //
361  OT_ASSERT_MSG(false == ((src > dest) && (src < (dest + dest_size))),
362  "ASSERT: safe_strcpy: Unexpected memory overlap.\n");
363 
364  const size_t src_length = OTString::safe_strlen(src, MAX_STRING_LENGTH);
365 
366  OT_ASSERT_MSG(dest_size > src_length, "OTString::safe_strcpy: ASSERT: "
367  "src_length must be less than "
368  "dest_size.\n");
369 
370 #ifdef _WIN32
371  bool bSuccess = (0 == strcpy_s(dest, dest_size, src));
372 #else
373  size_t src_cpy_length = strlcpy(dest, src, dest_size);
374  bool bSuccess = (src_length == src_cpy_length);
375 #endif
376 
377  // Notice: we don't zero out the source unless we were successful (AND
378  // unless we were asked to.)
379  //
380  if (bSuccess && bZeroSource)
381  OTPassword::zeroMemory(const_cast<char*>(src),
382  static_cast<uint32_t>(src_length));
383 
384  return bSuccess;
385 }
size_t strlcpy(char *dst, const char *src, size_t siz)
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
#define MAX_STRING_LENGTH
Definition: OTString.hpp:147
static size_t safe_strlen(const char *s, size_t max)
Definition: OTString.cpp:388
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
size_t opentxs::OTString::safe_strlen ( const char *  s,
size_t  max 
)
static

Definition at line 388 of file OTString.cpp.

389 {
390  OT_ASSERT_MSG(max <= MAX_STRING_LENGTH, "OT_String::safe_strlen: ASSERT: "
391  "max length passed in is longer "
392  "than allowed.\n");
393 
394  return strnlen(s, max);
395 }
#define MAX_STRING_LENGTH
Definition: OTString.hpp:147
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
void opentxs::OTString::Set ( const char *  data,
uint32_t  enforcedMaxLength = 0 
)

Definition at line 1055 of file OTString.cpp.

1056 {
1057  if (new_string == data_) // Already the same string.
1058  return;
1059 
1060  Release();
1061 
1062  if (nullptr == new_string) return;
1063 
1064  LowLevelSet(new_string, nEnforcedMaxLength);
1065 }
virtual EXPORT void Release()
Definition: OTString.cpp:765
void opentxs::OTString::Set ( const OTString data)

Definition at line 1067 of file OTString.cpp.

1068 {
1069  if (this == &strBuf) // Already the same string.
1070  return;
1071 
1072  Release();
1073 
1074  LowLevelSetStr(strBuf);
1075 }
virtual EXPORT void Release()
Definition: OTString.cpp:765
char opentxs::OTString::sgetc ( void  )

Definition at line 1443 of file OTString.cpp.

1444 {
1445  char answer;
1446 
1447  if (position_ >= length_) {
1448  return EOF;
1449  }
1450  answer = (char)(*(data_ + position_));
1451 
1452  ++position_;
1453 
1454  return answer;
1455 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
bool opentxs::OTString::sgets ( char *  buffer,
uint32_t  size 
)

Definition at line 1380 of file OTString.cpp.

1381 {
1382  if (nullptr == szBuffer) {
1383  return false;
1384  }
1385 
1386  if (position_ >= length_) return false;
1387 
1388  uint32_t lIndex = 0;
1389  char* pChar = (char*)(data_ + position_);
1390 
1391  // while *pChar isn't at the end of the source string,
1392  // and lIndex hasn't reached the end of the destination buffer,
1393  //
1394  while (0 != *pChar && (position_ < length_) &&
1395  lIndex < (nBufSize - 1)) // the -1 leaves room for a forced null
1396  // terminator.
1397  {
1398  // If the current character isn't a newline, then copy it...
1399  if ('\n' != *pChar) {
1400  szBuffer[lIndex] = *pChar;
1401  lIndex++; // increment the buffer
1402  position_++; // increment the string's internal memory of where it
1403  // stopped.
1404  pChar++; // increment this for convenience (could calcuate from
1405  // position)
1406  }
1407  // Until we reach a newline...
1408  else {
1409  // IT'S A NEWLINE!
1410 
1411  szBuffer[lIndex] =
1412  0; // destination buffer, this is the end of the line for him.
1413  position_++; // This still moves past the newline, so the next
1414  // call will get the next
1415  // string.
1416  // lIndex does NOT increment here because we reach the end of this
1417  // string.
1418  // neither does pChar. These local variables go away since we are
1419  // returning.
1420 
1421  if (0 == *(pChar + 1))
1422  return false;
1423  else
1424  return true; // there was more to read, but we stopped at the
1425  // newline.
1426  }
1427  }
1428 
1429  // Need to add the nullptr terminator.
1430  szBuffer[lIndex] = 0;
1431 
1432  // We reached the end of the string.
1433  // EOF. So we return false to tell the caller not to bother calling again.
1434  if (0 == *pChar) {
1435  return false;
1436  }
1437 
1438  // Obviously if *pChar isn't at the end, then there was more to read,
1439  // but the buffer was full, so we return true.
1440  return true;
1441 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
int64_t opentxs::OTString::StringToLong ( const std::string &  number)
static

Definition at line 677 of file OTString.cpp.

678 {
679  if (strNumber.size() == 0) return 0;
680 
681  int64_t v = 0;
682  size_t i = 0;
683 
684  char sign = (strNumber[0] == '-' || strNumber[0] == '+')
685  ? (++i, strNumber[0])
686  : '+';
687 
688  for (; i < strNumber.size(); ++i) {
689  if (strNumber[i] < '0' || strNumber[i] > '9') break;
690  v = ((v * 10) + (strNumber[i] - '0'));
691  }
692  return ((0 == v) ? 0 : ((sign == '-') ? -v : v));
693 }
uint64_t opentxs::OTString::StringToUlong ( const std::string &  number)
static

Definition at line 662 of file OTString.cpp.

663 {
664  if (strNumber.size() == 0) return 0;
665 
666  uint64_t v = 0;
667  size_t i = 0;
668 
669  for (; i < strNumber.size(); ++i) {
670  if (strNumber[i] < '0' || strNumber[i] > '9') break;
671  v = ((v * 10) + (strNumber[i] - '0'));
672  }
673  return ((0 == v) ? 0 : v);
674 }
void opentxs::OTString::sungetc ( void  )

Definition at line 1457 of file OTString.cpp.

1458 {
1459  if (position_ > 0) {
1460  --position_;
1461  }
1462 }
uint32_t position_
Definition: OTString.hpp:276
void opentxs::OTString::swap ( OTString rhs)

Definition at line 1018 of file OTString.cpp.

1019 {
1020  std::swap(length_, rhs.length_);
1021  std::swap(position_, rhs.position_);
1022  std::swap(data_, rhs.data_);
1023 }
uint32_t length_
Definition: OTString.hpp:275
uint32_t position_
Definition: OTString.hpp:276
bool opentxs::OTString::TokenizeIntoKeyValuePairs ( Map map) const

Definition at line 550 of file OTString.cpp.

552 {
553 #if !(defined(_WIN32) || defined(TARGET_OS_IPHONE) || defined(ANDROID))
554  // fabcy-pansy parser that allows for multiple level of quotes nesting and
555  // escaped quotes
556  if (!Exists()) return true;
557 
558  wordexp_t exp_result;
559 
560  exp_result.we_wordc = 0;
561  exp_result.we_wordv = nullptr;
562  exp_result.we_offs = 0;
563 
564  if (wordexp(Get(), &exp_result, 0)) // non-zero == failure.
565  {
566  otErr << "OTString::TokenizeIntoKeyValuePairs: Error calling wordexp() "
567  "(to expand user-defined script args.)\nData: " << *this
568  << "\n";
569  // wordfree(&exp_result);
570  return false;
571  }
572 
573  if ((exp_result.we_wordc > 0) && (nullptr != exp_result.we_wordv)) {
574  // wordexp tokenizes by space (as well as expands, which is why I'm
575  // using it.)
576  // Therefore we need to iterate through the tokens, and create a single
577  // string
578  // with spaces between the tokens.
579  //
580  for (uint32_t i = 0;
581  (i < (exp_result.we_wordc - 1)) &&
582  (exp_result.we_wordv[i] != nullptr) &&
583  (exp_result.we_wordv[i + 1] !=
584  nullptr); // odd man out. Only PAIRS of strings are processed!
585  i += 2) {
586  const std::string str_key = exp_result.we_wordv[i];
587  const std::string str_val = exp_result.we_wordv[i + 1];
588 
589  otInfo << __FUNCTION__ << ":Parsed: " << str_key << " = " << str_val
590  << "\n";
591  mapOutput.insert(
592  std::pair<std::string, std::string>(str_key, str_val));
593  }
594 
595  wordfree(&exp_result);
596  }
597 
598  return true;
599 #else
600  // simple parser that allows for one level of quotes nesting but no escaped
601  // quotes
602  if (!Exists()) return true;
603 
604  const char* txt = Get();
605  std::string buf = txt;
606  for (int32_t i = 0; txt[i] != 0;) {
607  while (txt[i] == ' ') i++;
608  int32_t k = i;
609  int32_t k2 = i;
610  if (txt[i] == '\'' || txt[i] == '"') {
611  // quoted string
612  char quote = txt[i++];
613  k = i;
614  while (txt[i] != quote && txt[i] != 0) i++;
615  if (txt[i] != quote) {
616  otErr << __FUNCTION__ << ": Unmatched quotes in: " << txt
617  << "\n";
618  return false;
619  }
620  k2 = i;
621  i++;
622  }
623  else {
624  while (txt[i] != ' ' && txt[i] != 0) i++;
625  k2 = i;
626  }
627  const std::string key = buf.substr(k, k2 - k);
628 
629  while (txt[i] == ' ') i++;
630  int32_t v = i;
631  int32_t v2 = i;
632  if (txt[i] == '\'' || txt[i] == '"') {
633  // quoted string
634  char quote = txt[i++];
635  v = i;
636  while (txt[i] != quote && txt[i] != 0) i++;
637  if (txt[i] != quote) {
638  otErr << __FUNCTION__ << ": Unmatched quotes in: " << txt
639  << "\n";
640  return false;
641  }
642  v2 = i;
643  i++;
644  }
645  else {
646  while (txt[i] != ' ' && txt[i] != 0) i++;
647  v2 = i;
648  }
649  const std::string value = buf.substr(v, v2 - v);
650 
651  if (key.length() != 0 && value.length() != 0) {
652  otInfo << __FUNCTION__ << ":Parsed: " << key << " = " << value
653  << "\n";
654  mapOutput.insert(std::pair<std::string, std::string>(key, value));
655  }
656  }
657  return true;
658 #endif
659 }
EXPORT bool Exists() const
Definition: OTString.cpp:1035
OTLOG_IMPORT OTLogStream otInfo
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
int64_t opentxs::OTString::ToLong ( ) const

Definition at line 702 of file OTString.cpp.

703 {
704  const std::string str_number(Get());
705 
706  return OTString::StringToLong(str_number);
707 }
static EXPORT int64_t StringToLong(const std::string &number)
Definition: OTString.cpp:677
EXPORT const char * Get() const
Definition: OTString.cpp:1045
uint64_t opentxs::OTString::ToUlong ( ) const

Definition at line 695 of file OTString.cpp.

696 {
697  const std::string str_number(Get());
698 
699  return OTString::StringToUlong(str_number);
700 }
static EXPORT uint64_t StringToUlong(const std::string &number)
Definition: OTString.cpp:662
EXPORT const char * Get() const
Definition: OTString.cpp:1045
std::string & opentxs::OTString::trim ( std::string &  str)
static

Definition at line 398 of file OTString.cpp.

399 {
400  std::string whitespaces(" \t\f\v\n\r");
401 
402  size_t found = str.find_first_not_of(whitespaces);
403 
404  if (found != std::string::npos) {
405  str.erase(0, found);
406  }
407 
408  found = str.find_last_not_of(whitespaces);
409 
410  if (found != std::string::npos) {
411  str.erase(found + 1);
412  }
413 
414  // otErr << "(DEBUGGING OTString.cpp) CONTRACT HAS BEEN TRIMMED!!!
415  // RESULT: \n\n***BEGIN TRIM DATA:%s******END TRIM DATA\n\n",
416  // str.c_str()); // todo temp remove
417  return str;
418 }
void opentxs::OTString::Truncate ( uint32_t  index)

Definition at line 1193 of file OTString.cpp.

1194 {
1195  OTString strTruncated;
1196 
1197  strTruncated.Set(Get(), lAt);
1198 
1199  Set(strTruncated);
1200 }
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
EXPORT const char * Get() const
Definition: OTString.cpp:1045
bool opentxs::OTString::vformat ( const char *  fmt,
std::va_list *  pvl,
std::string &  s 
)
static

Definition at line 191 of file OTString.cpp.

192 {
193  OT_ASSERT(nullptr != fmt);
194  OT_ASSERT(nullptr != pvl);
195 
196  int32_t size = 0;
197  int32_t nsize = 0;
198  char* buffer = nullptr;
199  va_list args;
200 
201 #ifdef _WIN32
202  va_list args_2 = *pvl; // windows only.
203 
204  args = *pvl;
205  size = _vscprintf(fmt, args) + 1;
206 #else
207  va_copy(args, *pvl);
208  size = 512;
209 #endif
210 
211  buffer = new char[size + 100];
212  OT_ASSERT(nullptr != buffer);
213  OTPassword::zeroMemory(buffer, size + 100);
214 
215 #ifdef _WIN32
216  nsize = vsnprintf_s(buffer, size, size, fmt, args_2);
217 #else
218  nsize = vsnprintf(buffer, size, fmt, args);
219  va_end(args);
220 #endif
221 
222  OT_ASSERT(nsize >= 0);
223 
224  // fail -- delete buffer and try again
225  // If nsize was 1024 bytes, then that would mean that it printed 1024
226  // characters,
227  // even though the actual string must be 1025 in length (to have room for
228  // the null
229  // terminator.)
230  // If size, the ACTUAL buffer, was 1024 (that is, if size <= nsize) then
231  // size would
232  // LACK the necessary space to store the 1025th byte containing the null
233  // terminator.
234  // Therefore we are forced to delete the buffer and make one that is
235  // nsize+1, so that
236  // it will be 1025 bytes and thus have the necessary space for the
237  // terminator
238  //
239  if (size <= nsize) {
240  size = nsize + 1;
241  delete[] buffer;
242  buffer = new char[size + 100];
243  OT_ASSERT(nullptr != buffer);
244  OTPassword::zeroMemory(buffer, size + 100);
245 
246 #ifdef _WIN32
247  nsize = vsnprintf_s(buffer, size, size, fmt, *pvl);
248  va_end(args);
249  va_end(args_2);
250 #else
251  nsize = vsnprintf(buffer, size, fmt, *pvl);
252 #endif
253 
254  OT_ASSERT(nsize >= 0);
255  }
256  OT_ASSERT(size > nsize);
257 
258  str_Output = buffer;
259  delete[] buffer;
260  buffer = nullptr;
261  return true;
262 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
#define OT_ASSERT(x)
Definition: Assert.hpp:150
void opentxs::OTString::WriteToFile ( std::ostream &  ofs) const

Definition at line 1364 of file OTString.cpp.

1365 {
1366  fwrite_string(ofs, data_);
1367 }
void fwrite_string(std::ostream &ofs, const char *str)
Definition: OTString.cpp:519
void opentxs::OTString::zeroMemory ( ) const

Definition at line 744 of file OTString.cpp.

745 {
746  if (nullptr != data_) {
748  }
749 }
uint32_t length_
Definition: OTString.hpp:275
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281

Friends And Related Function Documentation

EXPORT friend std::ostream& operator<< ( std::ostream &  os,
const OTString obj 
)
friend

Definition at line 150 of file OTString.cpp.

151 {
152  os << obj.Get();
153  return os;
154 }

Member Data Documentation

char* opentxs::OTString::data_
protected

Definition at line 277 of file OTString.hpp.

uint32_t opentxs::OTString::length_
protected

Definition at line 275 of file OTString.hpp.

uint32_t opentxs::OTString::position_
protected

Definition at line 276 of file OTString.hpp.


The documentation for this class was generated from the following files: