const HeapString& HeapString::operator=(const HeapString& stringSrc) { if (m_string == stringSrc.m_string) return *this; HeapStringData* srcStringData = stringSrc.GetStringData(); if (srcStringData->stringLen == 0) { Clear(); return *this; } HeapStringData* stringData = GetStringData(); if (srcStringData->stringLen > stringData->maxSize) AllocBuffer(srcStringData->stringLen); memcpy(m_string, stringSrc.m_string, (srcStringData->stringLen + 1) * sizeof(char)); if (srcStringData->stringLen > stringData->maxSize) stringData->Free(); GetStringData()->stringLen = srcStringData->stringLen; return *this; }
// allocate enough memory for nLen characters bool wxStringImpl::Alloc(size_t nLen) { wxStringData *pData = GetStringData(); if ( pData->nAllocLength <= nLen ) { if ( pData->IsEmpty() ) { STATISTICS_ADD(Length, nLen); nLen += EXTRA_ALLOC; pData = (wxStringData *) malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failure handled by caller return false; } pData->nRefs = 1; pData->nDataLength = 0; pData->nAllocLength = nLen; m_pchData = pData->data(); // data starts after wxStringData m_pchData[0u] = wxT('\0'); } else if ( pData->IsShared() ) { pData->Unlock(); // memory not freed because shared size_t nOldLen = pData->nDataLength; if ( !AllocBuffer(nLen) ) { // allocation failure handled by caller return false; } // +1 to copy the terminator, too memcpy(m_pchData, pData->data(), (nOldLen+1)*sizeof(wxStringCharType)); GetStringData()->nDataLength = nOldLen; } else { nLen += EXTRA_ALLOC; pData = (wxStringData *) realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failure handled by caller // keep previous data since reallocation failed return false; } // it's not important if the pointer changed or not (the check for this // is not faster than assigning to m_pchData in all cases) pData->nAllocLength = nLen; m_pchData = pData->data(); } } //else: we've already got enough return true; }
// get the pointer to writable buffer of (at least) nLen bytes wxStringCharType *wxStringImpl::DoGetWriteBuf(size_t nLen) { if ( !AllocBeforeWrite(nLen) ) { // allocation failure handled by caller return NULL; } wxASSERT( GetStringData()->nRefs == 1 ); GetStringData()->Validate(false); return m_pchData; }
HeapString HeapString::TrimLeft(const char* trimChars) const { HeapString dest; // Count trim characters. size_t trimCharsLen = 0; const char empty[] = { 0 }; for (const char* ptr = trimChars ? trimChars : empty; *ptr; ++ptr, ++trimCharsLen) { } const char* str = m_string; if (trimCharsLen == 0) { while (isspace(*str)) str++; } else { if (trimCharsLen == 1) { while (*str == *trimChars) str++; } else { const char* stringEnd = m_string + GetStringData()->stringLen; const char* lastStr = str; while (str != stringEnd) { for (const char* ptr = trimChars; *ptr; ++ptr) { if (*ptr == *str) { str++; break; } } if (lastStr == str) break; lastStr = str; } } } size_t count = GetStringData()->stringLen - (str - m_string); if (count > 0) { dest.AllocBuffer(count); memcpy(dest.m_string, str, count); } return dest; }
const HeapString& HeapString::operator=(char ch) { HeapStringData* stringData = GetStringData(); if (1 > stringData->maxSize) AllocBuffer(1); m_string[0] = ch; if (1 > stringData->maxSize) stringData->Free(); GetStringData()->stringLen = 1; return *this; }
JArray<CBPrefsManager::MacroSetInfo>* CBMacroSetTable::GetMacroList ( JIndex* firstNewID, JIndex* lastNewID ) const { assert( !IsEditing() && !itsActionTable->IsEditing() && !itsMacroTable->IsEditing() ); JStringTableData* data = GetStringData(); const JSize count = data->GetRowCount(); for (JIndex i=1; i<=count; i++) { CBPrefsManager::MacroSetInfo info = itsMacroList->GetElement(i); *(info.name) = data->GetString(i,1); } if (itsMacroIndex > 0) { CBPrefsManager::MacroSetInfo info = itsMacroList->GetElement(itsMacroIndex); itsActionTable->GetData(info.action); itsMacroTable->GetData(info.macro); } *firstNewID = itsFirstNewID; *lastNewID = itsLastNewID; itsOwnsMacroListFlag = kJFalse; return itsMacroList; }
HeapString HeapString::Sub(size_t startPos) const { size_t stringLen = GetStringData()->stringLen; if (startPos >= stringLen) return HeapString(); return Sub(startPos, stringLen - startPos); }
void CECTag::DebugPrint(int level, bool print_empty) const { if (m_dataLen || print_empty) { wxString space; for (int i = level; i--;) space += wxT(" "); wxString s1 = CFormat(wxT("%s%s %d = ")) % space % GetDebugNameECTagNames(m_tagName) % m_dataLen; wxString s2; switch (m_tagName) { case EC_TAG_DETAIL_LEVEL: s2 = GetDebugNameEC_DETAIL_LEVEL(GetInt()); break; case EC_TAG_SEARCH_TYPE: s2 = GetDebugNameEC_SEARCH_TYPE(GetInt()); break; case EC_TAG_STAT_VALUE_TYPE: s2 = GetDebugNameEC_STATTREE_NODE_VALUE_TYPE(GetInt()); break; default: switch (m_dataType) { case EC_TAGTYPE_UINT8: case EC_TAGTYPE_UINT16: case EC_TAGTYPE_UINT32: case EC_TAGTYPE_UINT64: s2 = CFormat(wxT("%d")) % GetInt(); break; case EC_TAGTYPE_STRING: s2 = GetStringData(); break; case EC_TAGTYPE_DOUBLE: s2 = CFormat(wxT("%.1f")) % GetDoubleData(); break; case EC_TAGTYPE_HASH16: s2 = GetMD4Data().Encode(); break; case EC_TAGTYPE_UINT128: // Using any non-inline function from UInt128.h would break linkage // of remote apps otherwise not using CUInt128. So just fall through // and display the value as a byte-stream. Since the value is sent // big-endian on the network, the visual result is correct, except // for the intervening spaces... //s2 = GetInt128Data().ToHexString(); break; case EC_TAGTYPE_CUSTOM: if (m_dataLen == 0) { s2 = wxT("empty"); } else { // Make a hex dump (limited to maxOutput) const uint32 maxOutput = 50; for (uint32 i = 0; i < m_dataLen; i++) { if (i == maxOutput) { s2 += wxT("..."); break; } s2 += CFormat(wxT("%02X ")) % (unsigned char) m_tagData[i]; } } break; default: s2 = GetDebugNameECTagTypes(m_dataType); } } DoECLogLine(s1 + s2); } for (TagList::const_iterator it = m_tagList.begin(); it != m_tagList.end(); ++it) { it->DebugPrint(level + 1, true); } }
// must be called before replacing contents of this string bool wxStringImpl::AllocBeforeWrite(size_t nLen) { wxASSERT( nLen != 0 ); // doesn't make any sense // must not share string and must have enough space wxStringData* pData = GetStringData(); if ( pData->IsShared() || pData->IsEmpty() ) { // can't work with old buffer, get new one pData->Unlock(); if ( !AllocBuffer(nLen) ) { // allocation failures are handled by the caller return false; } } else { if ( nLen > pData->nAllocLength ) { // realloc the buffer instead of calling malloc() again, this is more // efficient STATISTICS_ADD(Length, nLen); nLen += EXTRA_ALLOC; pData = (wxStringData*) realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failures are handled by the caller // keep previous data since reallocation failed return false; } pData->nAllocLength = nLen; m_pchData = pData->data(); } } wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner // it doesn't really matter what the string length is as it's going to be // overwritten later but, for extra safety, set it to 0 for now as we may // have some junk in m_pchData GetStringData()->nDataLength = 0; return true; }
int HeapString::Find(const char* strSearch, size_t start) const { if (start >= GetStringData()->stringLen) return -1; const char* found = strstr(m_string + start, strSearch); return found ? (int)(found - m_string) : -1; }
// must be called before changing this string bool wxStringImpl::CopyBeforeWrite() { wxStringData* pData = GetStringData(); if ( pData->IsShared() ) { pData->Unlock(); // memory not freed because shared size_t nLen = pData->nDataLength; if ( !AllocBuffer(nLen) ) { // allocation failures are handled by the caller return false; } wxStringMemcpy(m_pchData, pData->data(), nLen); } wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner return true; }
// assigns one string to another wxStringImpl& wxStringImpl::operator=(const wxStringImpl& stringSrc) { wxASSERT( stringSrc.GetStringData()->IsValid() ); // don't copy string over itself if ( m_pchData != stringSrc.m_pchData ) { if ( stringSrc.GetStringData()->IsEmpty() ) { Reinit(); } else { // adjust references GetStringData()->Unlock(); m_pchData = stringSrc.m_pchData; GetStringData()->Lock(); } } return *this; }
void wxStringImpl::DoUngetWriteBuf(size_t nLen) { wxStringData * const pData = GetStringData(); wxASSERT_MSG( nLen < pData->nAllocLength, wxT("buffer overrun") ); // the strings we store are always NUL-terminated pData->data()[nLen] = wxT('\0'); pData->nDataLength = nLen; pData->Validate(true); }
char* HeapString::GetBuffer(size_t nLen) { HeapStringData* srcData = GetStringData(); if (nLen > srcData->maxSize) { AllocBuffer(nLen); memcpy(m_string, srcData->data(), srcData->stringLen + 1); srcData->Free(); } return m_string; }
void CECTag::DebugPrint(int level, bool print_empty) const { if (m_dataLen || print_empty) { wxString space; for (int i = level; i--;) space += wxT(" "); wxString s1 = CFormat(wxT("%s%s %d = ")) % space % GetDebugNameECTagNames(m_tagName) % m_dataLen; wxString s2; switch (m_tagName) { case EC_TAG_DETAIL_LEVEL: s2 = GetDebugNameEC_DETAIL_LEVEL(GetInt()); break; case EC_TAG_SEARCH_TYPE: s2 = GetDebugNameEC_SEARCH_TYPE(GetInt()); break; case EC_TAG_STAT_VALUE_TYPE: s2 = GetDebugNameEC_STATTREE_NODE_VALUE_TYPE(GetInt()); break; default: switch (m_dataType) { case EC_TAGTYPE_UINT8: case EC_TAGTYPE_UINT16: case EC_TAGTYPE_UINT32: case EC_TAGTYPE_UINT64: s2 = CFormat(wxT("%d")) % GetInt(); break; case EC_TAGTYPE_STRING: s2 = GetStringData(); break; case EC_TAGTYPE_DOUBLE: s2 = CFormat(wxT("%.1f")) % GetDoubleData(); break; case EC_TAGTYPE_HASH16: s2 = GetMD4Data().Encode(); break; case EC_TAGTYPE_CUSTOM: if (m_dataLen == 0) { s2 = wxT("empty"); } else { // Make a hex dump (limited to maxOutput) const uint32 maxOutput = 50; for (uint32 i = 0; i < m_dataLen; i++) { if (i == maxOutput) { s2 += wxT("..."); break; } s2 += CFormat(wxT("%02X ")) % (unsigned char) m_tagData[i]; } } break; default: s2 = GetDebugNameECTagTypes(m_dataType); } } DoECLogLine(s1 + s2); } for (TagList::const_iterator it = m_tagList.begin(); it != m_tagList.end(); ++it) { it->DebugPrint(level + 1, true); } }
/* BuildTables() * ================================================================ */ void BuildTables( void ) { int i; char *bufptr; int count; char *cptr; char *ptr; InitTables(); /* NO Error Checking. if the user mucks with the DAT file, * well, that's their fault. */ for( i = 0; i < NUM_PATTERNS; i++ ) { count = 0; if( (bufptr = strstr( data_buffer, Titles[i].pattern ))!=NULL) { /* Gets us to the next line */ bgetstr( bufptr, TempString ); bufptr += ( (char)strlen( TempString) + 2 ); /* Get the first data line */ bgetstr( bufptr, TempString ); while( TempString[0] != '[' ) { /* Skip comments */ if( TempString[0] != ';' ) { /* Skip Blanks...but, get the string data */ if( ( cptr = GetStringData( TempString )) != NULL ) { /* If there is a comma, then there is a parameter */ if( (ptr = strchr( cptr, ',' )) != NULL ) { strcpy( Titles[i].fnode[count].parameter, ptr+1 ); *ptr = '\0'; } strcpy( Titles[i].fnode[count].fname, cptr ); count++; } } bufptr += ( strlen( TempString ) + 2 ); bgetstr( bufptr, TempString ); } Titles[i].count = count; } } }
void CBMacroTable::ReadData ( const JCharacter* fileName, const JBoolean replace ) { JStringTableData* data = GetStringData(); if (replace) { data->RemoveAllRows(); } JIndex firstNewRow = 0; ifstream input(fileName); JString macro, script; JIndex state = 1; while (!input.eof() && !input.fail()) { if (state == 1) { macro = JReadLine(input); if (!macro.IsEmpty()) { state = 2; } } else if (state == 2) { script = JReadLine(input); if (!script.IsEmpty()) { data->AppendRows(1); const JSize rowCount = data->GetRowCount(); data->SetString(rowCount,kMacroColumn, macro); data->SetString(rowCount,kScriptColumn, script); if (firstNewRow == 0) { firstNewRow = rowCount; } state = 1; } } } if (firstNewRow != 0) { ScrollTo((GetBounds()).bottomLeft()); BeginEditing(JPoint(kMacroColumn, firstNewRow)); } }
const HeapString& HeapString::operator=(const char* srcString) { size_t srcStringLen = StrLen(srcString); if (srcStringLen == 0) { Clear(); return *this; } HeapStringData* stringData = GetStringData(); if (srcStringLen > stringData->maxSize) AllocBuffer(srcStringLen); memcpy(m_string, srcString, srcStringLen * sizeof(char)); if (srcStringLen > stringData->maxSize) stringData->Free(); GetStringData()->stringLen = srcStringLen; return *this; }
HeapString HeapString::Sub(size_t startPos, size_t count) const { size_t stringLen = GetStringData()->stringLen; if (startPos > stringLen || count == 0) return HeapString(); if (startPos + count > stringLen) count = stringLen - startPos; HeapString dest; dest.AllocBuffer(count); memcpy(dest.m_string, m_string + startPos, count * sizeof(char)); return dest; }
wxStringImpl& wxStringImpl::append(size_t n, wxStringCharType ch) { size_type len = length(); if ( !Alloc(len + n) || !CopyBeforeWrite() ) { wxFAIL_MSG( wxT("out of memory in wxStringImpl::append") ); return *this; } GetStringData()->nDataLength = len + n; m_pchData[len + n] = '\0'; for ( size_t i = 0; i < n; ++i ) m_pchData[len + i] = ch; return *this; }
HeapString HeapString::TrimRight(const char* trimChars) const { HeapString dest; // Count trim characters. size_t trimCharsLen = 0; const char empty[] = { 0 }; for (const char* ptr = trimChars ? trimChars : empty; *ptr; ++ptr, ++trimCharsLen) { } size_t stringLen = GetStringData()->stringLen; char* str = (char*)m_string + stringLen - 1; if (trimCharsLen == 0) { while (str != m_string && isspace(*str)) str--; } else { if (trimCharsLen == 1) { while (str != m_string && *str == *trimChars) str--; } else { const char* lastStr = str; while (str != m_string) { for (const char* ptr = trimChars; *ptr; ++ptr) { if (*ptr == *str) { str--; break; } } if (lastStr == str) break; lastStr = str; } } } size_t count = str - m_string + 1; dest.AllocBuffer(count); memcpy(dest.m_string, m_string, count); return dest; }
HeapString HeapString::Replace(char oldCh, char newCh) const { HeapString dest; dest.AllocBuffer(GetStringData()->stringLen); char* destPtr = dest.m_string; for (char* srcPtr = m_string; *srcPtr; ++srcPtr, ++destPtr) { char ch = *srcPtr; if (ch == oldCh) *destPtr = newCh; else *destPtr = ch; } return dest; }
CBMacroSetTable::~CBMacroSetTable() { if (itsOwnsMacroListFlag) { const JSize count = itsMacroList->GetElementCount(); for (JIndex i=1; i<=count; i++) { CBPrefsManager::MacroSetInfo info = itsMacroList->GetElement(i); delete info.name; delete info.action; delete info.macro; } delete itsMacroList; } delete GetStringData(); }
// helper function: does real copy bool wxStringImpl::AssignCopy(size_t nSrcLen, const wxStringCharType *pszSrcData) { if ( nSrcLen == 0 ) { Reinit(); } else { if ( !AllocBeforeWrite(nSrcLen) ) { // allocation failure handled by caller return false; } memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxStringCharType)); GetStringData()->nDataLength = nSrcLen; m_pchData[nSrcLen] = wxT('\0'); } return true; }
wxString CEC_StatTree_Node_Tag::GetDisplayString() const { wxString en_label = GetStringData(); wxString my_label = wxGetTranslation(en_label); // This is needed for client names, for example if (my_label == en_label) { if (en_label.Right(4) == wxT(": %s")) { my_label = wxGetTranslation(en_label.Mid(0, en_label.Length() - 4)) + wxString(wxT(": %s")); } } CFormat label(my_label); for (const_iterator it = begin(); it != end(); it++) { if (it->GetTagName() == EC_TAG_STAT_NODE_VALUE) { FormatValue(label, &*it); } } return label.GetString(); }
void CMArray2DTable::HandleEditMenu ( const JIndex index ) { JTextEditor::CmdIndex cmd; JPoint cell; if (GetEditMenuHandler()->EditMenuIndexToCmd(index, &cmd) && cmd == JTextEditor::kCopyCmd && (GetTableSelection()).GetSingleSelectedCell(&cell)) { JXTextSelection* data = jnew JXTextSelection(GetDisplay(), GetStringData()->GetString(cell)); assert( data != NULL ); GetSelectionManager()->SetData(kJXClipboardName, data); } }
void CBMacroTable::WriteData ( const JCharacter* fileName ) const { ofstream output(fileName); const JStringTableData* data = GetStringData(); const JSize count = GetRowCount(); for (JIndex i=1; i<=count; i++) { (data->GetString(i, kMacroColumn)).Print(output); output << '\n'; (data->GetString(i, kScriptColumn)).Print(output); output << "\n\n"; } }
JBoolean CBMacroSetTable::GetCurrentMacroSetName ( JString* name ) const { if (itsMacroIndex > 0 && const_cast<CBMacroSetTable*>(this)->EndEditing()) { *name = (GetStringData())->GetString(itsMacroIndex, 1); return kJTrue; } else { name->Clear(); return kJFalse; } }
void CBMacroTable::GetData ( CBMacroManager* mgr ) const { assert( !IsEditing() ); mgr->RemoveAllMacros(); const JStringTableData* data = GetStringData(); const JSize count = GetRowCount(); for (JIndex i=1; i<=count; i++) { mgr->AddMacro(data->GetString(i, kMacroColumn), data->GetString(i, kScriptColumn)); } }
wxStringImpl& wxStringImpl::insert(size_t nPos, const wxStringCharType *sz, size_t n) { wxASSERT( nPos <= length() ); if ( n == npos ) n = wxStrlen(sz); if ( n == 0 ) return *this; if ( !Alloc(length() + n) || !CopyBeforeWrite() ) { wxFAIL_MSG( wxT("out of memory in wxStringImpl::insert") ); return *this; } memmove(m_pchData + nPos + n, m_pchData + nPos, (length() - nPos) * sizeof(wxStringCharType)); memcpy(m_pchData + nPos, sz, n * sizeof(wxStringCharType)); GetStringData()->nDataLength = length() + n; m_pchData[length()] = '\0'; return *this; }