⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpw_str.h

📁 关于游戏手柄的驱动编程
💻 H
📖 第 1 页 / 共 2 页
字号:
    memcpy( sub->str, sc->str+start, len ); 
    
    return sub;
}

/* returns a new context containing a copy of the sub-string 
   starting at start, ending at the end of pStr */

static pStr 
subStrShort( pStr sc, uint_32 start ) 
{
    pStr sub = null;
    int_32 len;

    if ( sc == null ) return newString();

    if ( start > sc->len ) return newString();

    len = sc->len - start;

    if ( len < 0 ) len = 0;

    sizeStr( &sub, len ); /* adds hidden char on the end! */

    memcpy( sub->str, sc->str+start, len ); 
    
    return sub;
}

/* returns a new context containing a copy of the sub-string 
   starting at start for length of len */

static pStr 
subStrLen( pStr sc, uint_32 start, uint_32 len ) 
{
    pStr sub = null;

    //start = 0, len = 10, sc->len = 5  : len = 10
    //start = 5, len = 10, sc->len = 10 : len = 5

    if ( sc == null ) return newString();

    if ( start > sc->len ) return newString();

    if ( (start + len) > sc->len  ) len = sc->len - start;

    sizeStr( &sub, len ); /* adds hidden char on the end! */

    memcpy( sub->str, sc->str+start, len ); 
    
    return sub;
}

static pStr 
strToken( pStr * string, char* key )
{
    int_32 index;
    pStr tmp;
    pStr token;

    if ( string == null ) return false;
    if ( *string == 0 ) return false;
    if ( (*string)->len == 0 ) return false;
    if ( key == null ) return false;

    /* get the last chunk which i assumed to not have a key on the end */

    if ( ( index = slFind( str( *string ), key ) ) <= 0 ) {
        tmp = strCpy( *string );
        freeStr( string );
        return tmp;
    }

    token = subStr( *string, 0, index );
    tmp = subStrShort( *string, index+1 );
    setStr( string, tmp );
    freeStr( &tmp );
    return token;
}

/* concatinate src onto the end of target */

static bool
concatStr( pStr* target, pStr src )
{
    uint_32 tlen;
    uint_32 slen;
    char * p;

    if ( target == null || src == null ) return false;

    if ( *target == null ) *target = newString();

    tlen = (*target)->len;
    slen = src->len;

    if ( slen <= 0 ) return true;

    if ( tlen == 0 ) {
        *target = strCpy( src );
    } else {
        p = slAppend( (*target)->str, src->str );
        freeStr( target );
        setString( target, p );
        slFree( p );  
    }

    return true;
}

/* concatinate src onto the end of target */

static bool
concatString( pStr* target, char * src )
{
    uint_32 tlen;
    uint_32 slen;
    char * p;

    if ( target == null || src == null ) return false;

    if ( *target == null ) *target = newString();

    tlen = (*target)->len;
    slen = slLength( src );

    if ( slen == 0 ) return true;

    if ( tlen == 0 ) {
        freeStr( target );
        *target = cpyString( src );
    } else {
        p = slAppend( (*target)->str, src ); /* creates a new string in p */
        freeStr( target );
        setString( target, p ); /* creates a new string in target */
        slFree( p );  
    }

    return true;
}

/* concatinate src(s) onto the end of target. You must
   have the last string = "\0" */

static bool
concatStrings( pStr* target, char * src, ... )
{
    va_list marker;
    uint_32 tlen;
    uint_32 slen;
    char * p;

    if ( target == null || src == null ) return false;

    if ( *target == null ) *target = newString();

    va_start( marker, src );     /* Initialize variable arguments. */
    while( src != null )
    {
        slen = slLength( src );
        tlen = (*target)->len;

        if ( slen == 0 ) break;

        if ( (*target)->len == 0 ) {
            freeStr( target );
            *target = cpyString( src );
        } else {
            //sizeStr( target, tlen+slen ); /* adds hidden char on the end. */
            //memcpy( (*target)->str + tlen, src, slen );
            p = slAppend( (*target)->str, src );
            freeStr( target );
            setString( target, p );
            slFree( p );  
        }
        src = va_arg( marker, char* );
    }
    va_end( marker );              /* Reset variable arguments.      */

    return true;
}

/* concatinate src onto the end of target for a total len */

static bool
concatStringLen( pStr* target, char * src, uint_32 len )
{
    uint_32 tlen;
    uint_32 slen;

    if ( target == null || src == null ) return false;

    if ( *target == null ) *target = newString();

    tlen = (*target)->len;
    slen = slLength( src );

    if ( slen <= 0 ) return true;

    sizeStr( target, tlen+slen ); /* adds hidden char on the end. */
    
    memcpy( (*target)->str + tlen, src, slen );

    return true;
}

/* concatinate a charcter onto the end of target */

static bool 
concatChar( pStr* target, char c )
{
    char buf[1];

    buf[0] = c;
    buf[1] = 0;

    return concatString( target, buf );
}

#define appendStr concatStr
#define appendString concatString

/* memcmp wrapper with a little bit o fudge - case sensitive */

static int_32 
compareStrCs( pStr a, pStr b )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b->str == null ) return 0;

    if ( a->str == null || b->str == null ) return -1;

    return memcmp( a->str, b->str, a->len );
}

/* strncmp wrapper - case sensitive */

static int_32 
compareStrLenCs( pStr a, pStr b, uint_32 len )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b->str == null ) return 0;

    if ( a->str == null || b->str == null ) return -1;

    return memcmp( a->str, b->str, len );
}

/* strncmp wrapper - case sensitive */

static int_32 
compareStringLenCs( pStr a, char* b, uint_32 len )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b == null ) return 0;

    if ( a->str == null || b == null ) return -1;

    return memcmp( a->str, b, len );
}

/* strcmp wrapper with a little bit o fudge - case sensitive */

static int_32 
compareStrNc( pStr a, pStr b )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b->str == null ) return 0;

    if ( a->str == null || b->str == null ) return -1;

    return _memicmp( a->str, b->str, a->len );
}

/* strncmp wrapper - case insensitive */

static int_32 
compareStringNc( pStr a, char* b )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b == null ) return 0;

    if ( a->str == null || b == null ) return -1;

    return _memicmp( a->str, b, a->len );
}

/* strncmp wrapper - case insensitive */

static int_32 
compareStrLenNc( pStr a, pStr b, uint_32 len )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b->str == null ) return 0;

    if ( a->str == null || b->str == null ) return -1;

    return _memicmp( a->str, b->str, len );
}

/* strncmp wrapper - case insensitive */

static int_32 
compareStringLenNc( pStr a, char* b, uint_32 len )
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return -1;

    if ( a->str == null && b == null ) return 0;

    if ( a->str == null || b == null ) return -1;

    return _memicmp( a->str, b, len );
}

/* replace all instances of token with replace in target */

static int_32 
replaceString( pStr* target, char* token, char* replace )
{
    char* newstr;
    
    if ( token == null || replace == null ) 
        return -1;

    if ( target == null || (*target)->len <= 0 ) 
        return 0;

    if ( memcmp( token, replace, (slLength(token) > slLength(replace) ? slLength(token):slLength(replace)) ) == 0 ) {
        return -1;
    } 

    newstr = slReplace( (*target)->str, token, replace ); 
    setString( target, newstr );
    strFree( newstr );

    return 1;
}

/* Simple, straight forward sub string search.  
   returns -1 if not found, 'starting character 
   position' for sub string is returned. */

static int_32
containsStr( pStr a, pStr b )		
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return 0;

    if ( a->str == null || b->str == null ) return 0;

    return slFind( str( a ), str( b ) );
}

/* Simple, straight forward sub string search.  
   returns -1 if not found, 'starting character 
   position' for sub string is returned. */

static int_32
containsString( pStr a, char* b )		
{
    if ( a == null && b == null ) return 0;

    if ( a == null || b == null ) return 0;

    if ( a->str == null ) return 0;

    return slFind( str( a ), b );
}

/* convert pStr to int32 value */

static int_32 
strToInt32( pStr a )		
{
    if ( a == null ) return 0;

    if ( a->str == null ) return 0;

    return atoi( str( a ) );
}

/* convert pStr to double value */

static float_64 
strToFloat64( pStr a )		
{
    if ( a == null ) return 0;

    if ( a->str == null ) return 0;

    return atof( str( a ) );
}

/* convert int32 value to pStr string */

static pStr 
int32ToStr( int_32 value )		
{
    pStr str;
    char buf[STRBUF];

    str = newString();
    sprintf( buf, "%i", value );

    setString( &str, buf ); 
    return str;
}

/* convert double value to pStr string */

static pStr 
float64ToStr( float_64 value )		
{
    pStr str;
    char buf[STRBUF];

    str = newString();
    sprintf( buf, "%i", value );

    setString( &str, buf ); 
    return str;
}

/* internal : string context allocation function */

static pStr 
newString( void )
{
    pStr p;
    p = (pStr)strMalloc( sizeof( StringContext ) );
    if ( p == null ) return null;
    return (pStr)memset( (void*)p, 0, sizeof( StringContext ) );
}

/* don't clutter or conflict with anything - string lib use only */
#undef strMalloc
#undef strRealloc
#undef strFree

CPW_END_HEADER
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -