// The reason we don't store stuff in a QString is because BitTorrent // b-encoded strings may contain zeroes within the string, which makes // a BString more of a buffer than a true string. void BString::init (ByteTape &tape) { QByteArray &dict(tape.data()); if (dict.find(':', tape.pos()) == -1) { kdDebug(7034) << "Can't find : for string!" << endl; return; } // Copy the part from start to :, as it will be a number // That number is the number of characters to read int length = dict.find(':', tape.pos()) - tape.pos(); char *ptr = dict.data(); ptr += tape.pos(); QByteArray buffer (length + 1); qmemmove (buffer.data(), ptr, length); buffer[length] = 0; QString numberString (buffer); bool a_isValid; ulong len = numberString.toULong (&a_isValid); if (!a_isValid) { kdDebug(7034) << "Invalid string length!" << endl; return; } // Now that we have the length, we need to advance the tape // past the colon tape += length; // Move to colon if (*tape != ':') { // Sanity check kdError(7034) << "SANITY CHECK FAILED. *tape != ':'!" << endl; return; } tape++; // Move past colon // Time to copy the data char *textBuffer = tape.at(tape.pos()); if (!m_data.resize(len + 1)) return; qmemmove (m_data.data(), textBuffer, len); m_data[len] = 0; // Null terminate for convienience tape += len; m_valid = true; }
void RKVariable::extendToLength (int length) { RK_TRACE (OBJECTS); if (length <= 0) length = 1; if (length < (myData ()->allocated_length - 1)) { dimensions[0] = length; return; } int ilength = length + 1; // be a little generous int target = myData ()->allocated_length; if (!target) target = INITIAL_ALLOC; while (target <= ilength) target = target * ALLOC_STEP; RK_DO (qDebug ("resizing from %d to %d", myData ()->allocated_length, target), OBJECTS, DL_DEBUG); // allocate new memory and copy if (getDataType () == RObject::DataCharacter) { RK_ASSERT (myData ()->cell_doubles == 0); QString *new_data = new QString[target]; if (myData ()->allocated_length) { // if not yet allocated, don't mem-move qmemmove (new_data, myData ()->cell_strings, myData ()->allocated_length * sizeof (QString)); } delete [] (myData ()->cell_strings); myData ()->cell_strings = new_data; } else { RK_ASSERT (myData ()->cell_strings == 0); double *new_data = new double[target]; if (myData ()->allocated_length) { // if not yet allocated, don't mem-move qmemmove (new_data, myData ()->cell_doubles, myData ()->allocated_length * sizeof (double)); } delete [] (myData ()->cell_doubles); myData ()->cell_doubles = new_data; } int *new_states = new int[target]; if (myData ()->allocated_length) { // if not yet allocated, don't mem-move qmemmove (new_states, myData ()->cell_states, myData ()->allocated_length * sizeof (int)); } delete [] (myData ()->cell_states); myData ()->cell_states = new_states; // set allocated but unused rows to Unknown for (int i=myData ()->allocated_length; i < target; ++i) { myData ()->cell_states[i] = RKVarEditData::Unknown; } myData ()->allocated_length = target; dimensions[0] = length; }
QCString QCString::stripWhiteSpace() const { if ( isEmpty() ) // nothing to do return *this; register const char *cs = data(); int reslen = length(); if ( !isspace((uchar)cs[0]) && !isspace((uchar)cs[reslen-1]) ) return *this; // returns a copy QCString result(cs); register char *s = result.rawData(); int start = 0; int end = reslen - 1; while ( isspace((uchar) s[start]) ) // skip white space from start start++; if ( s[start] == '\0' ) { // only white space return QCString(); } while ( end && isspace((uchar) s[end]) ) // skip white space from end end--; end -= start - 1; qmemmove( s, &s[start], end ); result.resize( end + 1 ); return result; }
void RKVariable::removeRows (int from_row, int to_row) { RK_TRACE (OBJECTS); for (int row = from_row; row <= to_row; ++row) { myData ()->invalid_fields.remove (row); } if (to_row < (myData ()->allocated_length - 1)) { // not the last rows if (myData ()->cell_strings) { qmemmove (&(myData ()->cell_strings[from_row]), &(myData ()->cell_strings[to_row+1]), (myData ()->allocated_length - to_row - 1) * sizeof (QString)); } else { qmemmove (&(myData ()->cell_doubles[from_row]), &(myData ()->cell_doubles[to_row+1]), (myData ()->allocated_length - to_row - 1) * sizeof (double)); } qmemmove (&(myData ()->cell_states[from_row]), &(myData ()->cell_states[to_row+1]), (myData ()->allocated_length - to_row - 1) * sizeof (int)); } for (int row = (myData ()->allocated_length - 1 - (to_row - from_row)); row < myData ()->allocated_length; ++row) { myData ()->cell_states[myData ()->allocated_length - 1] = RKVarEditData::Unknown; } dimensions[0] -= (to_row - from_row) + 1; downSize (); }
void RKVariable::insertRows (int row, int count) { RK_TRACE (OBJECTS); int old_len = getLength (); extendToLength (getLength () + count); for (int i=old_len; i <= row+count; ++i) { myData ()->cell_states[i] = RKVarEditData::NA; } if (row >= getLength () && (count == 1)) { // important special case if (myData ()->cell_strings) myData ()->cell_strings[row+count] = myData ()->cell_strings[row]; if (myData ()->cell_doubles) myData ()->cell_doubles[row+count] = myData ()->cell_doubles[row]; myData ()->cell_states[row+count] = myData ()->cell_states[row]; } else { if (myData ()->cell_strings) qmemmove (&(myData ()->cell_strings[row+count]), &(myData ()->cell_strings[row]), (myData ()->allocated_length - (row + count) - 1) * sizeof (QString)); if (myData ()->cell_doubles) qmemmove (&(myData ()->cell_doubles[row+count]), &(myData ()->cell_doubles[row]), (myData ()->allocated_length - (row + count) - 1) * sizeof (double)); qmemmove (&(myData ()->cell_states[row+count]), &(myData ()->cell_states[row]), (myData ()->allocated_length - (row + count) - 1) * sizeof (int)); } for (int i=row+count-1; i >= row; --i) { myData ()->cell_states[i] = RKVarEditData::NA; } }
bool SCString::stripPrefix(const char *prefix) { if (prefix==0) return FALSE; uint plen = qstrlen(prefix); if (m_data && qstrncmp(prefix,m_data,plen)==0) // prefix matches { uint len = qstrlen(m_data); uint newlen = len-plen+1; qmemmove(m_data,m_data+plen,newlen); resize(newlen); return TRUE; } return FALSE; }
QCString &QCString::remove( uint index, uint len ) { uint olen = length(); if ( index + len >= olen ) // range problems { if ( index < olen ) // index ok { resize( index+1 ); } } else if ( len != 0 ) { QCString tmp(olen-index-len+1); qmemmove( tmp.rawData(), data()+index+len, olen-index-len+1 ); resize( olen-len+1 ); memcpy( rawData()+index,tmp.data(),tmp.length() ); } return *this; }
QCString &QCString::insert( uint index, const char *s ) { int len = s ? qstrlen(s) : 0; if ( len == 0 ) return *this; int olen = length(); int nlen = olen + len; if ((int)index>=olen) { resize(nlen+index-olen+1); memset(rawData()+olen, ' ', index-olen); memcpy(rawData()+index,s, len+1); } else { resize(nlen+1); qmemmove(rawData()+index+len,data()+index,olen-index+1); memcpy(rawData()+index,s,len); } return *this; }