// Append status_t Path::Append(const char* leaf) { if (!leaf) return B_BAD_VALUE; if (fLength == 0) return SetTo(leaf); // get the leaf len int32 leafLen = strlen(leaf); if (leafLen == 0) return B_BAD_VALUE; // check, if we need a separator bool insertSeparator = (fBuffer[fLength - 1] != '/' && leaf[0] != '/'); // compute the resulting length and resize the buffer int32 wholeLen = fLength + leafLen + (insertSeparator ? 1 : 0); status_t error = _Resize(wholeLen + 1); if (error != B_OK) return error; // insert separator if (insertSeparator) fBuffer[fLength++] = '/'; // append leaf if (leafLen > 0) memcpy(fBuffer + fLength, leaf, leafLen + 1); fLength = wholeLen; return B_OK; }
/*! Makes this string writable, and resizes the buffer to \a length bytes (not including the terminating null). @param length The length of the new buffer in bytes. @param copy If true, the current string will be copied into the new string. */ status_t BString::_MakeWritable(int32 length, bool copy) { char* newData = NULL; if (atomic_get(&_ReferenceCount()) > 1) { // we might share our data with someone else if (copy) newData = _Clone(fPrivateData, length); else newData = _Allocate(length); if (newData == NULL) return B_NO_MEMORY; if (atomic_add(&_ReferenceCount(), -1) == 1) { // someone else left, we were the last owner _FreePrivateData(); } } else { // we don't share our data with someone else newData = _Resize(length); if (newData == NULL) return B_NO_MEMORY; } fPrivateData = newData; return B_OK; }
// Resize status_t DataContainer::Resize(off_t newSize) { status_t error = B_OK; if (newSize < 0) newSize = 0; if (newSize != fSize) { // Shrinking should never fail. Growing can fail, if we run out of // memory. Then we try to shrink back to the original size. off_t oldSize = fSize; error = _Resize(newSize); if (error == B_NO_MEMORY && newSize > fSize) _Resize(oldSize); } return error; }
bool Append(const char* toAppend, size_t length) { size_t oldLength = fLength; if (!_Resize(fLength + length)) return false; memcpy(fBuffer + oldLength, toAppend, length); return true; }
char* BString::_ShrinkAtBy(int32 offset, int32 length) { int32 oldLength = Length(); if (_MakeWritable() != B_OK) return NULL; memmove(fPrivateData + offset, fPrivateData + offset + length, oldLength - offset - length); return _Resize(oldLength - length); }
BString& BString::_DoCharacterEscape(const char* string, const char* setOfCharsToEscape, char escapeChar) { if (_MakeWritable(string_length(string), false) != B_OK) return *this; memcpy(fPrivateData, string, Length()); PosVect positions; int32 length = Length(); int32 pos; for (int32 offset = 0; offset < length; offset += pos + 1) { pos = strcspn(fPrivateData + offset, setOfCharsToEscape); if (pos < length - offset && !positions.Add(offset + pos)) return *this; } uint32 count = positions.CountItems(); int32 newLength = length + count; if (!newLength) { _Resize(0); return *this; } char* newData = _Allocate(newLength); if (newData) { char* oldString = fPrivateData; char* newString = newData; int32 lastPos = 0; for (uint32 i = 0; i < count; ++i) { pos = positions.ItemAt(i); length = pos - lastPos; if (length > 0) { memcpy(newString, oldString, length); oldString += length; newString += length; } *newString++ = escapeChar; *newString++ = *oldString++; lastPos = pos + 1; } length = Length() + 1 - lastPos; if (length > 0) memcpy(newString, oldString, length); _FreePrivateData(); fPrivateData = newData; } return *this; }
BString& BString::UnlockBuffer(int32 length) { if (length > 0) length = min_clamp0(length, Length()); else length = fPrivateData == NULL ? 0 : strlen(fPrivateData); if (_Resize(length) != NULL) { fPrivateData[length] = '\0'; _ReferenceCount() = 1; // mark shareable again } return *this; }
bool CTemplateManager::Register( const char* pTemplateName, CDBTemplate* pTemplate, int iDataLen ) { if (!pTemplateName || !pTemplate) { return false; } if (m_TemplateCount >= m_MaxTemplateCount) { if (!_Resize()) { return true; } } m_pTemplateArray[m_TemplateCount] = *pTemplate; pTemplate->SetInfo(pTemplateName, iDataLen); m_TemplateCount++; }
// SetTo status_t Path::SetTo(const char* path, const char* leaf) { if (!path) return B_BAD_VALUE; // get the base path len int32 len = strlen(path); if (len == 0) return B_BAD_VALUE; // get leaf len and check, if a separator needs to be inserted bool insertSeparator = false; int32 leafLen = 0; if (leaf) { leafLen = strlen(leaf); if (leafLen > 0) insertSeparator = (path[len - 1] != '/' && leaf[0] != '/'); } // compute the resulting length and resize the buffer int32 wholeLen = len + leafLen + (insertSeparator ? 1 : 0); status_t error = _Resize(wholeLen + 1); if (error != B_OK) return error; // copy path memcpy(fBuffer, path, len); // insert separator if (insertSeparator) fBuffer[len++] = '/'; // append leaf if (leafLen > 0) memcpy(fBuffer + len, leaf, leafLen); // null terminate fBuffer[wholeLen] = '\0'; fLength = wholeLen; return B_OK; }
void BString::_ReplaceAtPositions(const PosVect* positions, int32 searchLength, const char* with, int32 withLength) { int32 length = Length(); uint32 count = positions->CountItems(); int32 newLength = length + count * (withLength - searchLength); if (!newLength) { _Resize(0); return; } char* newData = _Allocate(newLength); if (newData == NULL) return; char* oldString = fPrivateData; char* newString = newData; int32 lastPos = 0; for (uint32 i = 0; i < count; ++i) { int32 pos = positions->ItemAt(i); length = pos - lastPos; if (length > 0) { memcpy(newString, oldString, length); oldString += length; newString += length; } memcpy(newString, with, withLength); oldString += searchLength; newString += withLength; lastPos = pos + searchLength; } length = Length() + 1 - lastPos; if (length > 0) memcpy(newString, oldString, length); _FreePrivateData(); fPrivateData = newData; }
bool CDBTemplateStruct::RegInt( BOOL Primary, const char* pName, BOOL NewColumn, int Default, BOOL IsUnsigned, BOOL IsIndex, BOOL IsEncrypt, const char* pEnctyptKey ) { if (FindColumn(pName)) { Assert(false); return false; } if (IsFull() && !_Resize()) { Assert(false); return false; } ColumnInfo& ci = m_pColumnInfo[m_ColumnInfoCount]; ci.m_Type = EM_TYPE_DB_COLUMN_INT; ci.m_Len = sizeof(int); ci.m_Pos = m_CurOffset; ci.m_Primary = Primary; ci.m_IsUnsigned = IsUnsigned; ci.m_NewColumn = NewColumn; ci.m_pTemplate = 0; ci.m_IsIndex = IsIndex; ci.m_IsEncrypt = IsEncrypt; CTools::Strcpy(ci.m_Name, pName, sizeof(ci.m_Name)); std::string strName = pName; m_HashColumnInfo[strName] = &ci; if (IsEncrypt) { CTools::Strcpy(ci.m_EncryptKey, pEnctyptKey, sizeof(ci.m_EncryptKey)); } ci.m_DefaultLen = sizeof(Default); memcpy(ci.m_Default, &Default, ci.m_DefaultLen); CTools::Strcpy(ci.m_ColumnDefault, DB_SQL_FORMAT_DEFAULT_INT, sizeof(ci.m_ColumnDefault)); m_CurOffset += (int)sizeof(int); m_ColumnInfoCount++; }
void ScreenWidget::resizeGL(int width, int height) { _Resize(width, height); }