提交 7f0d77469b3c4404afabc3ef9b3ef9cb12bc187e

作者 LJH 李佳桓
1 个父辈 8f0b5bcf

add

正在显示 1 个修改的文件 包含 296 行增加0 行删除
  1 +/*
  2 + * Copyright 2007 Stephen Liu
  3 + * For license terms, see the file COPYING along with this library.
  4 + */
  5 +
  6 +#include <stdlib.h>
  7 +#include <assert.h>
  8 +#include <string.h>
  9 +#include <ctype.h>
  10 +
  11 +#include "sputils.hpp"
  12 +
  13 +const int SP_ArrayList::LAST_INDEX = -1;
  14 +
  15 +SP_ArrayList :: SP_ArrayList( int initCount )
  16 +{
  17 + mMaxCount = initCount <= 0 ? 2 : initCount;
  18 + mCount = 0;
  19 + mFirst = (void**)malloc( sizeof( void * ) * mMaxCount );
  20 +}
  21 +
  22 +SP_ArrayList :: ~SP_ArrayList()
  23 +{
  24 + free( mFirst );
  25 + mFirst = NULL;
  26 +}
  27 +
  28 +int SP_ArrayList :: getCount() const
  29 +{
  30 + return mCount;
  31 +}
  32 +
  33 +int SP_ArrayList :: append( void * value )
  34 +{
  35 + if( NULL == value ) return -1;
  36 +
  37 + if( mCount >= mMaxCount ) {
  38 + mMaxCount = ( mMaxCount * 3 ) / 2 + 1;
  39 + mFirst = (void**)realloc( mFirst, sizeof( void * ) * mMaxCount );
  40 + assert( NULL != mFirst );
  41 + memset( mFirst + mCount, 0, ( mMaxCount - mCount ) * sizeof( void * ) );
  42 + }
  43 +
  44 + mFirst[ mCount++ ] = value;
  45 +
  46 + return 0;
  47 +}
  48 +
  49 +void * SP_ArrayList :: takeItem( int index )
  50 +{
  51 + void * ret = NULL;
  52 +
  53 + if( LAST_INDEX == index ) index = mCount -1;
  54 + if( index < 0 || index >= mCount ) return ret;
  55 +
  56 + ret = mFirst[ index ];
  57 +
  58 + mCount--;
  59 +
  60 + if( ( index + 1 ) < mMaxCount ) {
  61 + memmove( mFirst + index, mFirst + index + 1,
  62 + ( mMaxCount - index - 1 ) * sizeof( void * ) );
  63 + } else {
  64 + mFirst[ index ] = NULL;
  65 + }
  66 +
  67 + return ret;
  68 +}
  69 +
  70 +const void * SP_ArrayList :: getItem( int index ) const
  71 +{
  72 + const void * ret = NULL;
  73 +
  74 + if( LAST_INDEX == index ) index = mCount - 1;
  75 + if( index < 0 || index >= mCount ) return ret;
  76 +
  77 + ret = mFirst[ index ];
  78 +
  79 + return ret;
  80 +}
  81 +
  82 +void SP_ArrayList :: clean()
  83 +{
  84 + mCount = 0;
  85 + memset( mFirst, 0, sizeof( void * ) * mMaxCount );
  86 +}
  87 +
  88 +//-------------------------------------------------------------------
  89 +
  90 +SP_CircleQueue :: SP_CircleQueue()
  91 +{
  92 + mMaxCount = 8;
  93 + mEntries = (void**)malloc( sizeof( void * ) * mMaxCount );
  94 +
  95 + mHead = mTail = mCount = 0;
  96 +}
  97 +
  98 +SP_CircleQueue :: ~SP_CircleQueue()
  99 +{
  100 + free( mEntries );
  101 + mEntries = NULL;
  102 +}
  103 +
  104 +void SP_CircleQueue :: push( void * item )
  105 +{
  106 + if( mCount >= mMaxCount ) {
  107 + mMaxCount = ( mMaxCount * 3 ) / 2 + 1;
  108 + void ** newEntries = (void**)malloc( sizeof( void * ) * mMaxCount );
  109 +
  110 + unsigned int headLen = 0, tailLen = 0;
  111 + if( mHead < mTail ) {
  112 + headLen = mTail - mHead;
  113 + } else {
  114 + headLen = mCount - mTail;
  115 + tailLen = mTail;
  116 + }
  117 +
  118 + memcpy( newEntries, &( mEntries[ mHead ] ), sizeof( void * ) * headLen );
  119 + if( tailLen ) {
  120 + memcpy( &( newEntries[ headLen ] ), mEntries, sizeof( void * ) * tailLen );
  121 + }
  122 +
  123 + mHead = 0;
  124 + mTail = headLen + tailLen;
  125 +
  126 + free( mEntries );
  127 + mEntries = newEntries;
  128 + }
  129 +
  130 + mEntries[ mTail++ ] = item;
  131 + mTail = mTail % mMaxCount;
  132 + mCount++;
  133 +}
  134 +
  135 +void * SP_CircleQueue :: pop()
  136 +{
  137 + void * ret = NULL;
  138 +
  139 + if( mCount > 0 ) {
  140 + ret = mEntries[ mHead++ ];
  141 + mHead = mHead % mMaxCount;
  142 + mCount--;
  143 + }
  144 +
  145 + return ret;
  146 +}
  147 +
  148 +void * SP_CircleQueue :: top()
  149 +{
  150 + return mCount > 0 ? mEntries[ mHead ] : NULL;
  151 +}
  152 +
  153 +int SP_CircleQueue :: getLength()
  154 +{
  155 + return mCount;
  156 +}
  157 +
  158 +//-------------------------------------------------------------------
  159 +
  160 +SP_BlockingQueue :: SP_BlockingQueue()
  161 +{
  162 + mQueue = new SP_CircleQueue();
  163 + sp_thread_mutex_init( &mMutex, NULL );
  164 + sp_thread_cond_init( &mCond, NULL );
  165 +}
  166 +
  167 +SP_BlockingQueue :: ~SP_BlockingQueue()
  168 +{
  169 + delete mQueue;
  170 + sp_thread_mutex_destroy( &mMutex );
  171 + sp_thread_cond_destroy( &mCond );
  172 +}
  173 +
  174 +void SP_BlockingQueue :: push( void * item )
  175 +{
  176 + sp_thread_mutex_lock( &mMutex );
  177 +
  178 + mQueue->push( item );
  179 +
  180 + sp_thread_cond_signal( &mCond );
  181 +
  182 + sp_thread_mutex_unlock( &mMutex );
  183 +}
  184 +
  185 +void * SP_BlockingQueue :: pop()
  186 +{
  187 + void * ret = NULL;
  188 +
  189 + sp_thread_mutex_lock( &mMutex );
  190 +
  191 + if( mQueue->getLength() == 0 ) {
  192 + sp_thread_cond_wait( &mCond, &mMutex );
  193 + }
  194 +
  195 + ret = mQueue->pop();
  196 +
  197 + sp_thread_mutex_unlock( &mMutex );
  198 +
  199 + return ret;
  200 +}
  201 +
  202 +void * SP_BlockingQueue :: top()
  203 +{
  204 + void * ret = NULL;
  205 +
  206 + sp_thread_mutex_lock( &mMutex );
  207 +
  208 + ret = mQueue->top();
  209 +
  210 + sp_thread_mutex_unlock( &mMutex );
  211 +
  212 + return ret;
  213 +}
  214 +
  215 +int SP_BlockingQueue :: getLength()
  216 +{
  217 + int len = 0;
  218 +
  219 + sp_thread_mutex_lock( &mMutex );
  220 +
  221 + len = mQueue->getLength();
  222 +
  223 + sp_thread_mutex_unlock( &mMutex );
  224 +
  225 + return len;
  226 +}
  227 +
  228 +//-------------------------------------------------------------------
  229 +
  230 +int sp_strtok( const char * src, int index, char * dest, int len,
  231 + char delimiter, const char ** next )
  232 +{
  233 + int ret = 0;
  234 +
  235 + const char * pos1 = src, * pos2 = NULL;
  236 +
  237 + if( isspace( delimiter ) ) delimiter = 0;
  238 +
  239 + for( ; isspace( *pos1 ); ) pos1++;
  240 +
  241 + for ( int i = 0; i < index; i++ ) {
  242 + if( 0 == delimiter ) {
  243 + for( ; '\0' != *pos1 && !isspace( *pos1 ); ) pos1++;
  244 + if( '\0' == *pos1 ) pos1 = NULL;
  245 + } else {
  246 + pos1 = strchr ( pos1, delimiter );
  247 + }
  248 + if ( NULL == pos1 ) break;
  249 +
  250 + pos1++;
  251 + for( ; isspace( *pos1 ); ) pos1++;
  252 + }
  253 +
  254 + *dest = '\0';
  255 + if( NULL != next ) *next = NULL;
  256 +
  257 + if ( NULL != pos1 && '\0' != * pos1 ) {
  258 + if( 0 == delimiter ) {
  259 + for( pos2 = pos1; '\0' != *pos2 && !isspace( *pos2 ); ) pos2++;
  260 + if( '\0' == *pos2 ) pos2 = NULL;
  261 + } else {
  262 + pos2 = strchr ( pos1, delimiter );
  263 + }
  264 + if ( NULL == pos2 ) {
  265 + strncpy ( dest, pos1, len );
  266 + if ( ((int)strlen(pos1)) >= len ) ret = -2;
  267 + } else {
  268 + if( pos2 - pos1 >= len ) ret = -2;
  269 + len = ( pos2 - pos1 + 1 ) > len ? len : ( pos2 - pos1 + 1 );
  270 + strncpy( dest, pos1, len );
  271 +
  272 + for( pos2++; isspace( *pos2 ); ) pos2++;
  273 + if( NULL != next && '\0' != *pos2 ) *next = pos2;
  274 + }
  275 + } else {
  276 + ret = -1;
  277 + }
  278 +
  279 + dest[ len - 1 ] = '\0';
  280 + len = strlen( dest );
  281 +
  282 + // remove tailing space
  283 + for( ; len > 0 && isspace( dest[ len - 1 ] ); ) len--;
  284 + dest[ len ] = '\0';
  285 +
  286 + return ret;
  287 +}
  288 +
  289 +char * sp_strlcpy( char * dest, const char * src, int n )
  290 +{
  291 + strncpy( dest, src, n );
  292 + dest[ n - 1 ] = '\0';
  293 +
  294 + return dest;
  295 +}
  296 +
... ...
注册登录 后发表评论