143 #if !(defined(_WIN32) || defined(TARGET_OS_IPHONE) || defined(ANDROID))
198 char* buffer =
nullptr;
202 va_list args_2 = *pvl;
205 size = _vscprintf(fmt, args) + 1;
211 buffer =
new char[size + 100];
216 nsize = vsnprintf_s(buffer, size, size, fmt, args_2);
218 nsize = vsnprintf(buffer, size, fmt, args);
242 buffer =
new char[size + 100];
247 nsize = vsnprintf_s(buffer, size, size, fmt, *pvl);
251 nsize = vsnprintf(buffer, size, fmt, *pvl);
361 OT_ASSERT_MSG(
false == ((src > dest) && (src < (dest + dest_size))),
362 "ASSERT: safe_strcpy: Unexpected memory overlap.\n");
366 OT_ASSERT_MSG(dest_size > src_length,
"OTString::safe_strcpy: ASSERT: "
367 "src_length must be less than "
371 bool bSuccess = (0 == strcpy_s(dest, dest_size, src));
373 size_t src_cpy_length =
strlcpy(dest, src, dest_size);
374 bool bSuccess = (src_length == src_cpy_length);
380 if (bSuccess && bZeroSource)
382 static_cast<uint32_t>(src_length));
391 "max length passed in is longer "
394 return strnlen(s, max);
400 std::string whitespaces(
" \t\f\v\n\r");
402 size_t found = str.find_first_not_of(whitespaces);
404 if (found != std::string::npos) {
408 found = str.find_last_not_of(whitespaces);
410 if (found != std::string::npos) {
411 str.erase(found + 1);
421 const std::string& charsFrom,
424 std::string l_str(str);
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);
437 std::wstring OTString::s2ws(
const std::string& s)
440 int32_t slength = (int32_t)s.length() + 1;
441 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
442 std::wstring r(len, L
'\0');
443 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
447 std::string OTString::ws2s(
const std::wstring& s)
450 int32_t slength = (int32_t)s.length() + 1;
451 len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
452 std::string r(len,
'\0');
453 WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, &r[0], len, 0, 0);
527 if (*pchar !=
'\r') ofs << *pchar;
551 std::map<std::string, std::string>& mapOutput)
const
553 #if !(defined(_WIN32) || defined(TARGET_OS_IPHONE) || defined(ANDROID))
556 if (!
Exists())
return true;
558 wordexp_t exp_result;
560 exp_result.we_wordc = 0;
561 exp_result.we_wordv =
nullptr;
562 exp_result.we_offs = 0;
564 if (wordexp(
Get(), &exp_result, 0))
566 otErr <<
"OTString::TokenizeIntoKeyValuePairs: Error calling wordexp() "
567 "(to expand user-defined script args.)\nData: " << *
this
573 if ((exp_result.we_wordc > 0) && (
nullptr != exp_result.we_wordv)) {
581 (i < (exp_result.we_wordc - 1)) &&
582 (exp_result.we_wordv[i] !=
nullptr) &&
583 (exp_result.we_wordv[i + 1] !=
586 const std::string str_key = exp_result.we_wordv[i];
587 const std::string str_val = exp_result.we_wordv[i + 1];
589 otInfo << __FUNCTION__ <<
":Parsed: " << str_key <<
" = " << str_val
592 std::pair<std::string, std::string>(str_key, str_val));
595 wordfree(&exp_result);
602 if (!
Exists())
return true;
604 const char* txt =
Get();
605 std::string buf = txt;
606 for (int32_t i = 0; txt[i] != 0;) {
607 while (txt[i] ==
' ') i++;
610 if (txt[i] ==
'\'' || txt[i] ==
'"') {
612 char quote = txt[i++];
614 while (txt[i] != quote && txt[i] != 0) i++;
615 if (txt[i] != quote) {
616 otErr << __FUNCTION__ <<
": Unmatched quotes in: " << txt
624 while (txt[i] !=
' ' && txt[i] != 0) i++;
627 const std::string key = buf.substr(k, k2 - k);
629 while (txt[i] ==
' ') i++;
632 if (txt[i] ==
'\'' || txt[i] ==
'"') {
634 char quote = txt[i++];
636 while (txt[i] != quote && txt[i] != 0) i++;
637 if (txt[i] != quote) {
638 otErr << __FUNCTION__ <<
": Unmatched quotes in: " << txt
646 while (txt[i] !=
' ' && txt[i] != 0) i++;
649 const std::string value = buf.substr(v, v2 - v);
651 if (key.length() != 0 && value.length() != 0) {
652 otInfo << __FUNCTION__ <<
":Parsed: " << key <<
" = " << value
654 mapOutput.insert(std::pair<std::string, std::string>(key, value));
664 if (strNumber.size() == 0)
return 0;
669 for (; i < strNumber.size(); ++i) {
670 if (strNumber[i] <
'0' || strNumber[i] >
'9')
break;
671 v = ((v * 10) + (strNumber[i] -
'0'));
673 return ((0 == v) ? 0 : v);
679 if (strNumber.size() == 0)
return 0;
684 char sign = (strNumber[0] ==
'-' || strNumber[0] ==
'+')
685 ? (++i, strNumber[0])
688 for (; i < strNumber.size(); ++i) {
689 if (strNumber[i] <
'0' || strNumber[i] >
'9')
break;
690 v = ((v * 10) + (strNumber[i] -
'0'));
692 return ((0 == v) ? 0 : ((sign ==
'-') ? -v : v));
697 const std::string str_number(
Get());
704 const std::string str_number(
Get());
746 if (
nullptr !=
data_) {
753 if (
nullptr !=
data_) {
814 (
const_cast<OTContract&
>(theValue)).SaveContractRaw(*
this);
863 LowLevelSetStr(strValue);
872 LowLevelSet(new_string, 0);
881 LowLevelSet(new_string, static_cast<uint32_t>(sizeLength));
890 LowLevelSet(new_string.c_str(),
static_cast<uint32_t
>(new_string.length()));
893 void OTString::LowLevelSetStr(
const OTString& strBuf)
903 "ASSERT: OTString::LowLevelSetStr: Exceeded "
904 "MAX_STRING_LENGTH! (String would not have fully fit "
905 "anyway--it would have been truncated here, potentially "
906 "causing data corruption.)");
924 void OTString::LowLevelSet(
const char* new_string, uint32_t nEnforcedMaxLength)
928 if (
nullptr != new_string) {
930 (nEnforcedMaxLength > 0)
932 new_string, static_cast<size_t>(nEnforcedMaxLength)))
938 if (0 == nLength)
return;
941 "ASSERT: OTString::LowLevelSet: Exceeded "
942 "MAX_STRING_LENGTH! (String would not have fully fit "
943 "anyway--it would have been truncated here, potentially "
944 "causing data corruption.)");
957 if (
nullptr !=
data_)
971 if ((
nullptr == pMem) || (theSize < 1))
return true;
973 char* str_new =
new char[theSize + 1];
992 str_new[theSize] =
'\0';
1001 uint32_t nLength =
static_cast<uint32_t
>(
1003 str_new[nLength] =
'\0';
1037 return (
nullptr !=
data_) ?
true :
false;
1047 return (
nullptr !=
data_) ?
const_cast<const char*
>(
data_) :
"";
1057 if (new_string ==
data_)
1062 if (
nullptr == new_string)
return;
1064 LowLevelSet(new_string, nEnforcedMaxLength);
1069 if (
this == &strBuf)
1074 LowLevelSetStr(strBuf);
1104 if (
nullptr ==
data_ ||
nullptr == strCompare) {
1109 char* s2 = (
char*)strCompare;
1111 for (; *s1 && *s2; s1++, s2++)
1112 if (*s1 != *s2)
return false;
1114 if (*s1 != *s2)
return false;
1126 const char* s2 = strCompare.
Get();
1128 for (; *s1 && *s1 !=
' '; s1++, s2++)
1129 if (*s1 != *s2)
return false;
1139 if (
nullptr ==
data_ ||
nullptr == strCompare) {
1143 if (strstr(
data_, strCompare))
return true;
1154 if (strstr(
data_, strCompare.
Get()))
return true;
1165 const std::string str_output = sb.str();
1167 Set(str_output.c_str());
1173 if (
data_ ==
nullptr) {
1177 for (
char* s1 =
data_; *s1; s1++) {
1178 *s1 =
static_cast<char>(tolower(*s1));
1184 if (
data_ ==
nullptr) {
1188 for (
char* s1 =
data_; *s1; s1++) {
1189 *s1 =
static_cast<char>(toupper(*s1));
1197 strTruncated.
Set(
Get(), lAt);
1214 if (!
Exists())
return false;
1216 bool bArmoredAndALSOescaped =
false;
1217 bool bArmoredButNOTescaped =
false;
1221 bArmoredAndALSOescaped =
true;
1223 if (!bEscapedIsAllowed) {
1224 otErr << __FUNCTION__ <<
": Armored and escaped value passed in, "
1225 "but escaped are forbidden here. "
1231 bArmoredButNOTescaped =
true;
1234 const bool bArmored = (bArmoredAndALSOescaped || bArmoredButNOTescaped);
1239 std::string str_Trim;
1246 *
this, bArmoredAndALSOescaped,
1255 otErr << __FUNCTION__ <<
": Error loading string contents from "
1256 "ascii-armored encoding. "
1257 "Contents: \n" <<
Get() <<
"\n";
1264 std::string str_temp(strTemp.
Get(), strTemp.
GetLength());
1282 if (str_Trim.size() > 0)
Set(str_Trim.c_str());
1324 std::string str_output;
1330 if (bSuccess)
Set(str_output.c_str());
1339 std::string str_output;
1346 const OTString strConcat(str_output);
1355 std::string str_output;
1361 Set(str_output.c_str());
1382 if (
nullptr == szBuffer) {
1388 uint32_t lIndex = 0;
1395 lIndex < (nBufSize - 1))
1399 if (
'\n' != *pChar) {
1400 szBuffer[lIndex] = *pChar;
1421 if (0 == *(pChar + 1))
1430 szBuffer[lIndex] = 0;
void Truncate(uint32_t index)
EXPORT OTString & operator=(OTString rhs)
static EXPORT int64_t StringToLong(const std::string &number)
std::ostream & operator<<(std::ostream &os, const OTIdentifier &obj)
static EXPORT uint64_t StringToUlong(const std::string &number)
const char * OT_BEGIN_ARMORED_escaped
size_t strlcpy(char *dst, const char *src, size_t siz)
static EXPORT bool safe_strcpy(char *dest, const char *src, size_t destSize, bool zeroSource=false)
void ConvertToUpperCase() const
EXPORT int64_t ToLong() const
EXPORT void OTfgets(std::istream &ofs)
#define MAX_STRING_LENGTH
EXPORT uint32_t GetLength() const
char * str_dup2(const char *str, uint32_t length)
EXPORT void Concatenate(const char *arg,...)
bool operator<(const OTString &rhs) const
void fwrite_string(std::ostream &ofs, const char *str)
EXPORT bool DecodeIfArmored(bool escapedIsAllowed=true)
EXPORT bool Exists() const
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
EXPORT void Format(const char *fmt,...)
EXPORT bool Compare(const char *compare) const
static size_t safe_strlen(const char *s, size_t max)
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
EXPORT bool TokenizeIntoKeyValuePairs(Map &map) const
bool sgets(char *buffer, uint32_t size)
bool operator>=(const OTString &rhs) const
#define OT_ASSERT_MSG(x, s)
OTLOG_IMPORT OTLogStream otInfo
EXPORT bool At(uint32_t index, char &c) const
EXPORT bool SavePseudonym()
EXPORT const char * Get() const
OTLOG_IMPORT OTLogStream otErr
EXPORT bool operator==(const OTString &rhs) const
EXPORT void zeroMemory() const
bool operator<=(const OTString &rhs) const
EXPORT void GetString(OTString &theStr) const
EXPORT uint64_t ToUlong() const
EXPORT bool Contains(const char *compare) const
static EXPORT std::string & trim(std::string &str)
EXPORT bool GetString(OTString &theData, bool bLineBreaks=true) const
bool operator>(const OTString &rhs) const
virtual EXPORT ~OTString()
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
EXPORT bool MemSet(const char *mem, uint32_t size)
static bool vformat(const char *fmt, std::va_list *pvl, std::string &s)
virtual EXPORT void Release()
void WriteToFile(std::ostream &ofs) const
const char * OT_BEGIN_ARMORED
static EXPORT std::string replace_chars(const std::string &str, const std::string &charsFrom, const char &charTo)
void ConvertToLowerCase() const