void BufferAppend(Buffer *buffer, const char *bytes, unsigned int length) { assert(buffer); assert(bytes); if (length == 0) { return; } switch (buffer->mode) { case BUFFER_BEHAVIOR_CSTRING: { size_t actual_length = strnlen(bytes, length); ExpandIfNeeded(buffer, buffer->used + actual_length + 1); memcpy(buffer->buffer + buffer->used, bytes, actual_length); buffer->used += actual_length; buffer->buffer[buffer->used] = '\0'; } break; case BUFFER_BEHAVIOR_BYTEARRAY: ExpandIfNeeded(buffer, buffer->used + length); memcpy(buffer->buffer + buffer->used, bytes, length); buffer->used += length; break; } }
MxStatus MxArrayListInsertAt(MxArrayListRef list, const void *item, int index) { if (list == NULL) return MxStatusNullArgument; if (index < 0) return MxStatusIllegalArgument; if (index >= list->count) return MxStatusIndexOutOfRange; MxStatus result = ExpandIfNeeded(list); if (result != MxStatusOK) return result; void *moveSource = list->items + index; void *moveDest = list->items + (index + 1); size_t moveAmt = (list->count - index) * sizeof(void *); memmove(moveDest, moveSource, moveAmt); list->items[index] = (void *)item; list->count += 1; return MxStatusOK; }
// NB! Make sure to sanitize format if taken from user input int BufferVPrintf(Buffer *buffer, const char *format, va_list ap) { assert(buffer); assert(format); va_list aq; va_copy(aq, ap); /* * We don't know how big of a buffer we will need. It might be that we have enough space * or it might be that we don't have enough space. Unfortunately, we cannot reiterate over * a va_list, so our only solution is to tell the caller to retry the call. We signal this * by returning zero. Before doing that we increase the buffer to a suitable size. * The tricky part is the implicit sharing and the reference counting, if we are not shared then * everything is easy, however if we are shared then we need a different strategy. */ int printed = vsnprintf(buffer->buffer, buffer->capacity, format, aq); if (printed >= buffer->capacity) { ExpandIfNeeded(buffer, printed); buffer->used = 0; printed = vsnprintf(buffer->buffer, buffer->capacity, format, ap); buffer->used = printed; } else { buffer->used = printed; } va_end(aq); return printed; }
void BufferAppendF(Buffer *buffer, const char *format, ...) { assert(buffer); assert(format); va_list ap; va_list aq; va_start(ap, format); va_copy(aq, ap); int printed = vsnprintf(buffer->buffer + buffer->used, buffer->capacity - buffer->used, format, aq); if (printed >= (buffer->capacity - buffer->used)) { /* * Allocate a larger buffer and retry. * Now is when having a copy of the list pays off :-) */ ExpandIfNeeded(buffer, buffer->used + printed); printed = vsnprintf(buffer->buffer + buffer->used, buffer->capacity - buffer->used, format, ap); buffer->used += printed; } else { buffer->used += printed; } va_end(aq); va_end(ap); }
void BufferAppendString(Buffer *buffer, const char *str) { size_t len = strlen(str); ExpandIfNeeded(buffer, buffer->used + len + 1); memcpy(buffer->buffer + buffer->used, str, len); buffer->used += len; buffer->buffer[buffer->used] = '\0'; }
csStringBase& csStringBase::Append (char c) { ExpandIfNeeded(Size+1); char *p = GetDataMutable(); CS_ASSERT(p!=0); p[Size++]=c; p[Size]='\0'; return *this; }
csStringBase& csStringBase::PadRight (size_t iNewSize, char iChar) { if (iNewSize > Size) { ExpandIfNeeded (iNewSize); char* p = GetDataMutable(); // GLOBAL NOTE *2* CS_ASSERT(p != 0); for (size_t x = Size; x < iNewSize; x++) p [x] = iChar; Size = iNewSize; p [Size] = '\0'; } return *this; }
MxStatus MxArrayListPush(MxArrayListRef list, const void *item) { if (list == NULL) return MxStatusNullArgument; MxStatus result = ExpandIfNeeded(list); if (result != MxStatusOK) return result; memmove(list->items + 1, list->items, (sizeof(void *) * list->count)); list->items[0] = (void *)item; return MxStatusOK; }
MxStatus MxArrayListAppend(MxArrayListRef list, const void *item) { if (list == NULL) return MxStatusNullArgument; MxStatus result = ExpandIfNeeded(list); if (result != MxStatusOK) return result; list->items[list->count] = (void *)item; list->count += 1; return MxStatusOK; }
csStringBase &csStringBase::PadLeft (size_t iNewSize, char iChar) { if (iNewSize > Size) { ExpandIfNeeded (iNewSize); char* p = GetDataMutable(); // GLOBAL NOTE *2* CS_ASSERT(p != 0); const size_t toInsert = iNewSize - Size; memmove (p + toInsert, p, Size + 1); // GLOBAL NOTE *1* for (size_t x = 0; x < toInsert; x++) p [x] = iChar; Size = iNewSize; } return *this; }
csStringBase &csStringBase::Overwrite (size_t iPos, const csStringBase &iStr) { CS_ASSERT (iPos <= Size); if (GetData() == 0 || iPos == Size) return Append (iStr); size_t const sl = iStr.Length (); size_t const NewSize = iPos + sl; ExpandIfNeeded (NewSize); char* p = GetDataMutable(); // GLOBAL NOTE *2* memcpy (p + iPos, iStr.GetData (), sl + 1); // GLOBAL NOTE *1* Size = NewSize; return *this; }
csStringBase &csStringBase::Insert (size_t iPos, const char* str) { CS_ASSERT(iPos <= Size); if (GetData() == 0 || iPos == Size) return Append (str); size_t const sl = strlen (str); size_t const NewSize = sl + Size; ExpandIfNeeded (NewSize); char* p = GetDataMutable(); // GLOBAL NOTE *2* memmove (p + iPos + sl, p + iPos, Size - iPos + 1); // GLOBAL NOTE *1* memcpy (p + iPos, str, sl); Size = NewSize; return *this; }
csStringBase &csStringBase::Append (const char *iStr, size_t iCount) { /* Since "" is not the same as 0, make an empty string if a non-null empty string is appended to a 0 string. */ if (iStr == 0 || ((iCount == 0) && (Size != 0))) return *this; if (iCount == (size_t)-1) iCount = strlen (iStr); size_t const NewSize = Size + iCount; ExpandIfNeeded (NewSize); char* p = GetDataMutable(); // GLOBAL NOTE *2* CS_ASSERT(p != 0); memcpy (p + Size, iStr, iCount); Size = NewSize; p [Size] = '\0'; return *this; }
/************************************************************* * Function : dictADD * Author : bulldozer.ma * Date : 2015-11-01 * Input : char *str, dict *pHeader, unsigned int len * Output : N/A * Return : int * Other : add **************************************************************/ int dictADD(char *str, dict *pHeader, unsigned int len) { int iRet = DICT_ERROR; if (NULL == str || 0 >= len || NULL == pHeader) { return DICT_ERROR; } if (DICT_EXIST == dictfind(pHeader, str, len)) { return DICT_OK; } if (ExpandIfNeeded(pHeader)) { Rehash(pHeader, 100); } iRet = AddNode(pHeader, str, len); return iRet; }
csStringBase& csStringBase::PadCenter (size_t iNewSize, char iChar) { if (iNewSize > Size) { ExpandIfNeeded (iNewSize); char* p = GetDataMutable(); // GLOBAL NOTE *2* CS_ASSERT(p != 0); const size_t toInsert = iNewSize - Size; const size_t halfInsert = toInsert / 2; if (Size > 0) memmove (p + halfInsert, p, Size); size_t x; for (x = 0; x < halfInsert; x++) p [x] = iChar; for (x = halfInsert + Size; x < iNewSize; x++) p [x] = iChar; Size = iNewSize; p [Size] = '\0'; } return *this; }