// 高级程序员,考虑异常安全性,鲁棒性更好 CMyString& CMyString::operator=(const CMyString& str) { if (*this != str) { CMyString str_temp(str); // 依赖于局部对象的复制构造函数申请资源,和析构函数释放资源 char* ptr_temp = str_temp.m_pData; str_temp.m_pData = m_pData; m_pData = ptr_temp; } return *this; }
void MTSendDlg::on_amountEdit_editingFinished() { if (!m_myAcctId.isEmpty() && !m_bSent) { std::string str_assetId(OTAPI_Wrap::GetAccountWallet_AssetTypeID(m_myAcctId.toStdString())); QString amt = ui->amountEdit->text(); if (!amt.isEmpty() && !str_assetId.empty()) { std::string str_temp(amt.toStdString()); if (std::string::npos == str_temp.find(".")) // not found str_temp += '.'; int64_t amount = OTAPI_Wrap::It()->StringToAmount(str_assetId, str_temp); std::string str_formatted_amount = OTAPI_Wrap::It()->FormatAmount(str_assetId, static_cast<int64_t>(amount)); QString qstr_FinalAmount = QString::fromStdString(str_formatted_amount); ui->amountEdit->setText(qstr_FinalAmount); } } }
/*-------------------------------------------------------------*/ void AzBytArr::strip() { int len; const AzByte *data = point(&len); const AzByte *bp = data, *data_end = data + len; for ( ; bp < data_end; ++bp) { if (*bp > 0x20) break; } const AzByte *ep = data_end; for ( ; ep-1 >= data; --ep) { if (*(ep-1) > 0x20) break; } if (bp >= ep) { clear(); } else if (bp == data && ep == data_end); /* nop */ else { AzBytArr str_temp(bp, Az64::ptr_diff(ep - bp, "AzBytArr::strip")); clear(); concat(&str_temp); } }
/*-------------------------------------------------------------*/ void AzBytArr::concatFloat(double number, int precision, bool doScientific) { stringstream s; if (precision > 0) { s.precision(precision); } if (doScientific) s << scientific; s << number; /*--- remove leading/tailing space though I don't know if there is any ---*/ if (getLen() == 0) { concat(s.str().c_str()); strip(); } else { AzBytArr str_temp(s.str().c_str()); str_temp.strip(); concat(&str_temp); } }
/*-------------------------------------------------------------*/ void AzBytArr::concatInt(int number, int width, bool doFillWithZero) { stringstream s; if (width > 0) s.width(width); if (doFillWithZero) s.fill('0'); s << number; if (width > 0) { concat(s.str().c_str()); return; } /*--- remove leading/tailing space though I don't know if there is any ---*/ if (getLen() == 0) { concat(s.str().c_str()); strip(); } else { AzBytArr str_temp(s.str().c_str()); str_temp.strip(); concat(&str_temp); } }
// If this string starts with -----BEGIN OT ARMORED... // Then this function will load it up into an OTASCIIArmor (removing // the bookends) and then decode it back into this string. This code // has been repeated around so I'm doing this as a refactoring exercise. // // Return value: true == There is a string in here that is not armored. // (Whether I actually HAD to unarmor it or not... it's unarmored now.) // false == There was some error or the string is empty. // bool OTString::DecodeIfArmored(bool bEscapedIsAllowed/*=true*/) { if (!this->Exists()) return false; // ---------------------- bool bArmoredAndALSOescaped = false; // "- -----BEGIN OT ARMORED" bool bArmoredButNOTescaped = false; // "-----BEGIN OT ARMORED" if (this->Contains(OT_BEGIN_ARMORED_escaped)) // check this one first... { bArmoredAndALSOescaped = true; if (!bEscapedIsAllowed) { OTLog::vError("%s: Armored and escaped value passed in, but escaped are forbidden here. " "(Returning.)\n"); return false; } } else if (this->Contains(OT_BEGIN_ARMORED)) { bArmoredButNOTescaped = true; } // ---------------------------------------- const bool bArmored = (bArmoredAndALSOescaped || bArmoredButNOTescaped); // ---------------------------------------- // Whether the string is armored or not, (-----BEGIN OT ARMORED) // either way, we'll end up with the decoded version in this variable: // std::string str_Trim; // ------------------------------------------------ if (bArmored) // it's armored, we have to decode it first. { OTASCIIArmor ascTemp; if (false == (ascTemp.LoadFromString(*this, bArmoredAndALSOescaped, // if it IS escaped or not, this variable will be true or false to show it. // The below szOverride sub-string determines where the content starts, when loading. OT_BEGIN_ARMORED))) // Default is: "-----BEGIN" // We're doing this: "-----BEGIN OT ARMORED" (Should worked for escaped as well, here.) { OTLog::vError("%s: Error loading string contents from ascii-armored encoding. " "Contents: \n%s\n", __FUNCTION__, this->Get()); return false; } else // success loading the actual contents out of the ascii-armored version. { OTString strTemp(ascTemp); // <=== ascii-decoded here. std::string str_temp(strTemp.Get(), strTemp.GetLength()); str_Trim = OTString::trim(str_temp); // This is the std::string for the trim process. } } else { std::string str_temp(this->Get(), this->GetLength()); str_Trim = OTString::trim(str_temp); // This is the std::string for the trim process. (Wasn't armored, so here we use it as passed in.) } // ------------------------------------------------ // At this point, str_Trim contains the actual contents, whether they // were originally ascii-armored OR NOT. (And they are also now trimmed, either way.) // ------------------------------------------ this->Release(); // ------------------------------------------ if (str_Trim.size() > 0) this->Set(str_Trim.c_str()); // ------------------------------------------ return this->Exists(); }
// static -- class factory. // OTTransactionType * OTTransactionType::TransactionFactory(const OTString & strInput) { static char buf[45] = ""; OTTransactionType * pItem = NULL; if (!strInput.Exists()) return NULL; // -------------------------------------------------------------------- // // To support legacy data, we check here to see if it's armored or not. // If it's not, we support it. But if it IS, we ALSO support it (we de-armor it here.) // bool bArmoredAndALSOescaped = false; // "- -----BEGIN OT ARMORED" bool bArmoredButNOTescaped = false; // "-----BEGIN OT ARMORED" if (strInput.Contains(OT_BEGIN_ARMORED_escaped)) // check this one first... { bArmoredAndALSOescaped = true; OTLog::Error("OTTransactionType::TransactionFactory: Armored and escaped value passed in, " "but escaped are forbidden here. (Returning NULL.)\n"); return NULL; } else if (strInput.Contains(OT_BEGIN_ARMORED)) { bArmoredButNOTescaped = true; } // ---------------------------------------- const bool bArmored = (bArmoredAndALSOescaped || bArmoredButNOTescaped); // ---------------------------------------- // Whether the string is armored or not, (-----BEGIN OT ARMORED) // either way, we'll end up with the decoded version in this variable: // std::string str_Trim; // ------------------------------------------------ if (bArmored) // it's armored, we have to decode it first. { OTASCIIArmor ascTemp; OTString strInputTemp(strInput); if (false == (ascTemp.LoadFromString(strInputTemp, bArmoredAndALSOescaped, // if it IS escaped or not, this variable will be true or false to show it. // The below szOverride sub-string determines where the content starts, when loading. OT_BEGIN_ARMORED))) // Default is: "-----BEGIN" // We're doing this: "-----BEGIN OT ARMORED" (Should worked for escaped as well, here.) { OTLog::vError("OTTransactionType::TransactionFactory: Error " "loading string contents from ascii-armored encoding. Contents: \n%s\n", strInput.Get()); return NULL; } else // success loading the actual contents out of the ascii-armored version. { OTString strTemp(ascTemp); // <=== ascii-decoded here. std::string str_temp(strTemp.Get(), strTemp.GetLength()); str_Trim = OTString::trim(str_temp); // This is the std::string for the trim process. } } else { std::string str_temp(strInput.Get(), strInput.GetLength()); str_Trim = OTString::trim(str_temp); // This is the std::string for the trim process. (Wasn't armored, so here we use it as passed in.) } // ------------------------------------------------ // At this point, str_Trim contains the actual contents, whether they // were originally ascii-armored OR NOT. (And they are also now trimmed, either way.) // ------------------------------------------ OTString strContract(str_Trim.c_str()); strContract.reset(); // for sgets buf[0] = 0; // probably unnecessary. bool bGotLine = strContract.sgets(buf, 40); if (!bGotLine) return NULL; OTString strFirstLine(buf); strContract.reset(); // set the "file" pointer within this string back to index 0. // Now I feel pretty safe -- the string I'm examining is within // the first 45 characters of the beginning of the contract, and // it will NOT contain the escape "- " sequence. From there, if // it contains the proper sequence, I will instantiate that type. if (!strFirstLine.Exists() || strFirstLine.Contains("- -")) return NULL; if (strFirstLine.Contains("-----BEGIN SIGNED TRANSACTION-----")) // this string is 34 chars long. { pItem = new OTTransaction(); OT_ASSERT(NULL != pItem); } else if (strFirstLine.Contains("-----BEGIN SIGNED TRANSACTION ITEM-----")) // this string is 39 chars long. { pItem = new OTItem(); OT_ASSERT(NULL != pItem); } else if (strFirstLine.Contains("-----BEGIN SIGNED LEDGER-----")) // this string is 29 chars long. { pItem = new OTLedger(); OT_ASSERT(NULL != pItem); } else if (strFirstLine.Contains("-----BEGIN SIGNED ACCOUNT-----")) // this string is 30 chars long. { pItem = new OTAccount(); OT_ASSERT(NULL != pItem); } // The string didn't match any of the options in the factory. if (NULL == pItem) return NULL; // This causes pItem to load ASSUMING that the PurportedAcctID and PurportedServerID are correct. // The object is still expected to be internally consistent with its sub-items, regarding those IDs, // but the big difference is that it will SET the Real Acct and Real Server IDs based on the purported // values. This way you can load a transaction without knowing the account in advance. // pItem->m_bLoadSecurely = false; // OTLog::Error("\n\nTESTING DEBUGGING LOL LOL LOL LOL \n\n\n"); // Does the contract successfully load from the string passed in? if (pItem->LoadContractFromString(strContract)) { // NOTE: this already happens in OTTransaction::ProcessXMLNode and OTLedger::ProcessXMLNode. // Specifically, it happens when m_bLoadSecurely is set to false. // // pItem->SetRealServerID(pItem->GetPurportedServerID()); // pItem->SetRealAccountID(pItem->GetPurportedAccountID()); // return pItem; } else delete pItem; return NULL; }
//获取此分站范围内的设备IP std::string CEasyssh::GetDeviceMAC(void) { if (m_bIsErr) { //自动重连 ConnectAP(); return std::string("Error"); } std::string str_ip_pool = ""; char *exitsignal=(char *)"none"; int rc = 0; const char *commandline = "/usr/www/wstalist ath0"; /* Exec non-blocking on the remove host */ LIBSSH2_CHANNEL *channel; while( (channel = libssh2_channel_open_session(m_ssh_session)) == NULL && libssh2_session_last_error(m_ssh_session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN) { waitsocket(m_sock, m_ssh_session); } if(channel == NULL) { TRACE(_T("libssh2_channel_open_session Error\n")); return std::string("Error"); } while( (rc = libssh2_channel_exec(channel, commandline)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(m_sock, m_ssh_session); } if(rc != 0) { return std::string("Error"); } for( ;; ) { /* loop until we block */ int rc; do { char buffer[0x4000]; rc = libssh2_channel_read(channel, buffer, sizeof(buffer)); if(rc > 0) { std::string str_temp(buffer, 0, rc); str_ip_pool += str_temp; } } while( rc > 0 ); /* this is due to blocking that would occur otherwise so we loop on this condition */ if( rc == LIBSSH2_ERROR_EAGAIN ) { waitsocket(m_sock, m_ssh_session); } else break; } while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) waitsocket(m_sock, m_ssh_session); if( rc == 0 ) { libssh2_channel_get_exit_status(channel); libssh2_channel_get_exit_signal(channel, &exitsignal, NULL, NULL, NULL, NULL, NULL); } libssh2_channel_free(channel); channel = NULL; return str_ip_pool; }