hit counter for tumblr
Real Time Web Analytics

purefractalsolutions.com

STL-like string library for plain C

Updated: 2011-10-07 15:17:14
If you found mistake, simply select it
and press Control+Enter

pfs_string is a small STL-like string library written in plain C.

Introduction

This small string library was written for Borland Turbo C 3.0 for use in small embedded application with rich text manipulations. Libraries for that device has no standard C string functions, and standard C functions are not usefull and safe. This library possesses the more possibilities, and, I hope, it is more safe.

Supported compilers

Originally written for Turbo C, this library can also be compiled with "grown-up" compilers such as MSVC or GNU C.

Memory allocation

String objects can use memory from dynamic heap and in this case they can grow-up as needed. Also, they can use static buffers and in this case string lenght is limited to size of that buffers.

Usage samples

Usage samples of pfs-string library are showed in listing above.
[-]
/*** Initialization ***/

/* Initialize string with static buffer */
pfs_str_t str;
char buf[256];
pfs_str_init_static( &str, sizeof(buf), &buf[0] );

/* More convenient way to initialization with static buffer */
PFS_STR_INIT_STATIC( &str, buf );

/* Initialize dynamic string */
pfs_str_init_dynamic( &str, 256 /* initial size */ );

/* Initialize dynamic string with limited growth */
pfs_str_init_dynamic_ex( &str
                       , 256  /* initial size */
                       , 1024 /* can grow up to 1024 bytes max */
                       ); 

/* Deleting string object */
/* do nothing for static buffers */
pfs_str_delete(&str);

/* erasing all content */
pfs_str_clear(&str);


/*** Assignments ***/
/* Assign from another string */
pfs_str_assign( &str, &anotherStr );

/* Assign from const char* */
int pfs_str_assign_str( &str, constCharPtr );

/* Assign data (can contain zeros) */
pfs_str_assign_data( &str, pData, dataLen);

/* Assign single char */
int pfs_str_assign_char( &str, 'C' );

/* Assign 15 chars */
pfs_str_assign_chars( &str, 15, 'C' );


/*** Concatenations ***/
/*
   For concatenations (appending) use pfs_str_append* functions
   with same prototypes as pfs_str_assign*
*/

pfsstr.h

pfs-string library header file.
[-]
#ifndef UTILS_PFSSTR_H
#define UTILS_PFSSTR_H

/* add this lines to your scr
#ifndef UTILS_PFSSTR_H
    #include "../utils/pfsstr.h"
#endif
*/

#ifndef UTILS_PFSUCONF_H
    #include "pfsuconf.h"
#endif

#ifndef UTILS_PFSALLOC_H
    #include "pfsalloc.h"
#endif

#ifndef PFS_SIZE_T_DEFINED
    #define PFS_SIZE_T_DEFINED
        #if defined(__TURBOC__)
            typedef unsigned      pfs_size_t;
            typedef int           pfs_ssize_t;
            typedef int           pfs_ptrdiff_t;        
        #else
            typedef unsigned long pfs_size_t;
            typedef          long pfs_ssize_t;
            typedef int           pfs_ptrdiff_t;
        #endif

#endif

#if !defined(STRING_NPOS)
    #define STRING_NPOS ((pfs_size_t)-1)
#endif


typedef struct tag_pfs_string_t
{
    char         *pData;
    pfs_size_t   _str_size;
    pfs_size_t   _buf_size;
    /* _is_dynamic == 1 - buf can be reallocated and max size is unlimited
       _is_dynamic > 1  - buf can be reallocated but max size = (_is_dynamic&1)
     */
    pfs_size_t   _is_dynamic;
} pfs_string_t;

typedef pfs_string_t pfs_str_t;


/* Helpers */


void swap_chars( char *pc1, char *pc2 );
/*void swap_ints( int *pc1, int *pc2 );*/
void swap_pfs_sizes( pfs_size_t *ps1, pfs_size_t *ps2 );
void swap_char_ptrs( char **pc1, char **pc2 );
void swap_pfs_strings( pfs_str_t *ps1, pfs_str_t *ps2 );


char char_touppercasecopy( char ch );
char char_tolowercasecopy( char ch );
void char_touppercase( char *pch );
void char_tolowercase( char *pch );

unsigned pfs_isalphalower(char ch);
unsigned pfs_isalphaupper(char ch);
unsigned pfs_isalpha(char ch);      /* isalphalower || isalphaupper */
unsigned pfs_isdigit(char ch);
unsigned pfs_isalnum(char ch);      /* isalpha || isdigit */
unsigned pfs_isspace(char ch);      /* (0x09 - 0x0D or 0x20). */

pfs_size_t str_copy_impl( char *buf, const char *p, pfs_size_t bufMaxSize );
pfs_size_t str_copy_data_impl( char *buf, const char *p, pfs_size_t bufMaxSize, pfs_size_t numBytesCopy );
pfs_size_t str_fill_chars_impl( char *buf, pfs_size_t bufMaxSize, pfs_size_t cnt, char ch );
pfs_size_t str_len_impl( const char *p );
void pfs_memcpy_impl( void* dst, const void* src, pfs_size_t len );


/* iterator version */
char* str_reverse_impl( char *b, char *e );
/* classic version */
char* str_reverse_len_impl( char *b, pfs_size_t len);

int  str_compare_impl( const char *p1, const char *p2, pfs_size_t numCharsCmp );
int  str_compare_impl2( const char *p1, pfs_size_t size1, const char *p2, pfs_size_t size2 );

int  str_compare_ci_impl( const char *p1, const char *p2, pfs_size_t numCharsCmp );
int  str_compare_ci_impl2( const char *p1, pfs_size_t size1, const char *p2, pfs_size_t size2 );

pfs_size_t str_find_char_impl( const char *strFindIn, pfs_size_t strFindInSize, char ch );
pfs_size_t str_find_str_impl( const char *strFindIn, pfs_size_t strFindInSize, const char *findStr, pfs_size_t strLen);
pfs_size_t str_find_char_ci_impl( const char *strFindIn, pfs_size_t strFindInSize, char ch );
pfs_size_t str_find_str_ci_impl( const char *strFindIn, pfs_size_t strFindInSize, const char *findStr, pfs_size_t strLen);



int pfs_str_init_static( pfs_str_t *pStr, pfs_size_t bufSize, char *pBuf);
#define PFS_STR_INIT_STATIC( pStr, staticBuf ) pfs_str_init_static( pStr, sizeof(staticBuf)/sizeof(staticBuf[0]), &staticBuf[0] )


int pfs_str_delete( pfs_str_t *pStr);

#if defined(HAS_PFS_ALLOC_IMPL)
/* return -1 if fail */
int pfs_str_init_dynamic( pfs_str_t *pStr, pfs_size_t bufInitialSize);
int pfs_str_init_dynamic_ex( pfs_str_t *pStr, pfs_size_t bufInitialSize, pfs_size_t bufMaxSize);
#endif

int pfs_str_reserve( pfs_str_t *pStr, pfs_size_t requiredSize);


void pfs_str_clear( pfs_str_t *pStr );

int pfs_str_assign( pfs_str_t *pStr, const pfs_str_t * pStrAssignFrom);
int pfs_str_assign_str( pfs_str_t *pStr, const char * p);
int pfs_str_assign_data( pfs_str_t *pStr, const char * pData, pfs_size_t dataLen);
int pfs_str_assign_char( pfs_str_t *pStr, char ch );
int pfs_str_assign_chars( pfs_str_t *pStr, pfs_size_t numChars, char ch );

int pfs_str_append( pfs_str_t *pStr, const pfs_str_t * pStrApendFrom);
int pfs_str_append_str( pfs_str_t *pStr, const char * p);
int pfs_str_append_data( pfs_str_t *pStr, const char * pData, pfs_size_t dataLen);
int pfs_str_append_char( pfs_str_t *pStr, char ch );
int pfs_str_append_chars( pfs_str_t *pStr, pfs_size_t numChars, char ch );

int pfs_str_erase( pfs_str_t *pStr, pfs_size_t pos, pfs_size_t len );
int pfs_str_insert_data( pfs_str_t *pStr, pfs_size_t pos, const char * pData, pfs_size_t dataLen );
int pfs_str_insert_str( pfs_str_t *pStr, pfs_size_t pos, const char * pData );
int pfs_str_insert( pfs_str_t *pStr, pfs_size_t pos, const pfs_str_t *pStrIns );

int pfs_str_replace_with_data( pfs_str_t *pStr, pfs_size_t replacePos, pfs_size_t replaceLen, const char * pData, pfs_size_t dataLen );
int pfs_str_replace_with_str( pfs_str_t *pStr, pfs_size_t replacePos, pfs_size_t replaceLen, const char * pData );
int pfs_str_replace_with( pfs_str_t *pStr, pfs_size_t replacePos, pfs_size_t replaceLen, const pfs_str_t *pStrReplace );

unsigned pfs_str_empty ( const pfs_str_t *pStr );
pfs_size_t  pfs_str_size ( const pfs_str_t *pStr );
const char* pfs_str_data ( const pfs_str_t *pStr );
const char* pfs_str_c_str( const pfs_str_t *pStr );
char pfs_str_at( const pfs_str_t *pStr, pfs_size_t pos );

void pfs_str_toupper_ex ( const pfs_str_t *pStr, pfs_size_t offs, pfs_size_t len );
void pfs_str_toupper    ( const pfs_str_t *pStr );
void pfs_str_tolower_ex ( const pfs_str_t *pStr, pfs_size_t offs, pfs_size_t len );
void pfs_str_tolower    ( const pfs_str_t *pStr );

int pfs_str_compare_data ( const pfs_str_t *pStr, pfs_size_t dataLen, const char * pData );
int pfs_str_compare_str  ( const pfs_str_t *pStr, const char * p );
int pfs_str_compare      ( const pfs_str_t *pStrLeft, const pfs_str_t *pStrRight );
int pfs_str_compare_data_ex ( const pfs_str_t *pStr    , pfs_size_t offs, pfs_size_t cmpLen, const char * pData );
int pfs_str_compare_str_ex  ( const pfs_str_t *pStr    , pfs_size_t offs, const char * p );
int pfs_str_compare_ex      ( const pfs_str_t *pStrLeft, pfs_size_t offs, pfs_size_t cmpLen, const pfs_str_t *pStrRight );

pfs_size_t pfs_str_find_char   ( const pfs_str_t *pStr, char ch, pfs_size_t posStart );
pfs_size_t pfs_str_find_char_ci( const pfs_str_t *pStr, char ch, pfs_size_t posStart );
pfs_size_t pfs_str_find_first_of_char( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart );
pfs_size_t pfs_str_find_first_not_of_char( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart );
pfs_size_t pfs_str_find_first_of_char_ci( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart );
pfs_size_t pfs_str_find_first_not_of_char_ci( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart );
pfs_size_t pfs_str_find_str    ( const pfs_str_t *pStr, const char* p, pfs_size_t posStart );
pfs_size_t pfs_str_find_str_ci ( const pfs_str_t *pStr, const char* p, pfs_size_t posStart );
pfs_size_t pfs_str_find     ( const pfs_str_t *pStr, const pfs_str_t *pStrToFind, pfs_size_t posStart );
pfs_size_t pfs_str_find_ci  ( const pfs_str_t *pStr, const pfs_str_t *pStrToFind, pfs_size_t posStart );

/* return pos after insertion or STRING_NPOS if no strFind string was found */
pfs_size_t pfs_str_find_replace_with_str( pfs_str_t *pStr, pfs_size_t posStart, const char *strFind, const char *strReplace );
pfs_size_t pfs_str_find_replace_with( pfs_str_t *pStr, pfs_size_t posStart, const char *strFind, pfs_str_t *pStrReplace );

void pfs_str_find_replace_all_with_str( pfs_str_t *pStr, const char *strFind, const char *strReplace );
void pfs_str_find_replace_all_with( pfs_str_t *pStr, const char *strFind, pfs_str_t *pStrReplace );

void pfs_str_split_to_pair(const pfs_str_t *pStr, pfs_str_t *pFirst, pfs_str_t *pSecond, const char *separator);

void pfs_str_trim_copy(const pfs_str_t *pStr, pfs_str_t *pTrimTo);
void pfs_str_rtrim_copy(const pfs_str_t *pStr, pfs_str_t *pTrimTo);
void pfs_str_ltrim_copy(const pfs_str_t *pStr, pfs_str_t *pTrimTo);




/*
pfs_size_t pfs_str_append_str( char *buf, const char *p, pfs_size_t bufMaxSize );
pfs_size_t pfs_str_append_data( char *buf, const char *p, pfs_size_t bufMaxSize, pfs_size_t numBytesCopy );
void pfs_str_append_chars( char *buf, pfs_size_t bufMaxSize, pfs_size_t cnt, char ch );
*/



#endif /* UTILS_PFSSTR_H */

pfsstr.c

pfs-string library implementation code.
[-]
#include "pfsstr.h"
/*
pfs_size_t str_copy_impl( char *buf, const char *p, pfs_size_t bufMaxSize );
pfs_size_t str_copy_data_impl( char *buf, const char *p, pfs_size_t bufMaxSize, pfs_size_t numBytesCopy );
void str_fill_chars_impl( char *buf, pfs_size_t bufMaxSize, pfs_size_t cnt, char ch );
pfs_size_t pfs_str_len_impl( const char *p );
*/

/*-----------------------------------------------------------------------------*/
void pfs_memcpy_impl( void* _dst, const void* _src, pfs_size_t len )
{
    unsigned char *dst;
    const unsigned char *src;
    const unsigned char *srcEnd;

    dst    = (unsigned char*)_dst;
    src    = (const unsigned char*)_src;
    srcEnd = src+(pfs_ptrdiff_t)len;
    for(; src!=srcEnd; )
       *dst++ = *src++;
}

/*-----------------------------------------------------------------------------*/
void swap_chars( char *pc1, char *pc2 )
   {
    char ch = *pc1;
    *pc1 = *pc2;
    *pc2 = ch;
   }

/*-----------------------------------------------------------------------------*/
void swap_pfs_sizes( pfs_size_t *ps1, pfs_size_t *ps2 )
   {
    pfs_size_t s = *ps1;
    *ps1 = *ps2;
    *ps2 = s;
   }

/*-----------------------------------------------------------------------------*/
void swap_char_ptrs( char **pc1, char **pc2 )
   {
    char *p = *pc1;
    *pc1 = *pc2;
    *pc2 = p;
   }

/*-----------------------------------------------------------------------------*/
void swap_pfs_strings( pfs_str_t *ps1, pfs_str_t *ps2 )
   {
    swap_char_ptrs( &(ps1->pData), &(ps2->pData) );
    swap_pfs_sizes( &(ps1->_str_size), &(ps2->_str_size) );
    swap_pfs_sizes( &(ps1->_buf_size), &(ps2->_buf_size) );
    swap_pfs_sizes( &(ps1->_is_dynamic), &(ps2->_is_dynamic) );
   }

/*-----------------------------------------------------------------------------*/
char char_touppercasecopy( char ch )
   {
    if ( ch>='a' && ch<='z') return ch-'a'+'A';
    return ch;
   }

/*-----------------------------------------------------------------------------*/
char char_tolowercasecopy( char ch )
   {
    if ( ch>='A' && ch<='Z') return ch-'A'+'a';
    return ch;
   }

/*-----------------------------------------------------------------------------*/
void char_touppercase( char *pch )
   {
    if ( (*pch)>='a' && (*pch)<='z') *pch = (*pch)-'a'+'A';
   }

/*-----------------------------------------------------------------------------*/
void char_tolowercase( char *pch )
   {
    if ( (*pch)>='A' && (*pch)<='Z') *pch = (*pch)-'A'+'a';
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_isalphalower(char ch)
   {
    if ( (ch)>='a' && (ch)<='z') return 1;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_isalphaupper(char ch)
   {
    if ( (ch)>='A' && (ch)<='Z') return 1;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_isalpha(char ch)
   {
    if ( (ch)>='a' && (ch)<='z') return 1;
    if ( (ch)>='A' && (ch)<='Z') return 1;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_isdigit(char ch)
   {
    if ( (ch)>='0' && (ch)<='9') return 1;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_isalnum(char ch)      /* isalpha || isdigit */
   {
    if ( (ch)>='a' && (ch)<='z') return 1;
    if ( (ch)>='A' && (ch)<='Z') return 1;
    if ( (ch)>='0' && (ch)<='9') return 1;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_isspace(char ch)      /* (0x09 - 0x0D or 0x20). */
   {
    if ( (ch>=0x09 && ch<=0x0D) || ch==0x20 ) return 1;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
/* iterator version */
char* str_reverse_impl( char *b, char *e )
   {
    pfs_size_t i = 0, numIters;
    char *r = b;
    numIters = (pfs_size_t)(e - b)/2;
    --e;
    for( i=0; i!=numIters; ++i)
       {
        swap_chars(b,e);
        ++b; --e;
       }
    return r;
   }

/*-----------------------------------------------------------------------------*/
/* classic version */
char* str_reverse_len_impl( char *b, pfs_size_t len)
   {
    return str_reverse_impl( b, b+(pfs_ptrdiff_t)len );
    /*
    char *e;
    size_t i = 0, numIters;
    char *r = b;
    e = b + len;
    numIters = len/2;
    --e;
    for( i=0; i!=numIters; ++i)
       {
        swap_chars(b,e);
        ++b; --e;
       }
    return r;
    */
   }

/*-----------------------------------------------------------------------------*/
int  str_compare_impl( const char *p1, const char *p2, pfs_size_t numCharsCmp )
   {
    pfs_size_t i = 0;
    for(; i!=numCharsCmp; ++i, ++p1, ++p2 )
       {
        if ((*p1) < (*p2)) return -1;
        if ((*p1) > (*p2)) return  1;
       }
    return 0;
   }

/*-----------------------------------------------------------------------------*/
int  str_compare_impl2( const char *p1, pfs_size_t size1, const char *p2, pfs_size_t size2 )
   {
    int cmp = str_compare_impl( p1, p2, (size1 < size2 ? size1 : size2) );
    if (cmp==0) 
       {
        if (size1<size2) return -1;
        else if (size1>size2) return  1;
        else return 0;
       }
    return cmp;
   }

/*-----------------------------------------------------------------------------*/
int  str_compare_ci_impl( const char *p1, const char *p2, pfs_size_t numCharsCmp )
   {
    pfs_size_t i = 0;
    for(; i!=numCharsCmp; ++i, ++p1, ++p2 )
       {
        if (char_touppercasecopy(*p1) < char_touppercasecopy(*p2)) return -1;
        if (char_touppercasecopy(*p1) > char_touppercasecopy(*p2)) return  1;
       }
    return 0;
   }

/*-----------------------------------------------------------------------------*/
int  str_compare_ci_impl2( const char *p1, pfs_size_t size1, const char *p2, pfs_size_t size2 )
   {
    int cmp = str_compare_ci_impl( p1, p2, (size1 < size2 ? size1 : size2) );
    if (cmp==0) 
       {
        if (size1<size2) return -1;
        else if (size1>size2) return  1;
        else return 0;
       }
    return cmp;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t str_find_char_impl( const char *strFindIn, pfs_size_t strFindInSize, char ch )
   {
    pfs_size_t chNo = 0;
    for( ; chNo!=strFindInSize; ++chNo, ++strFindIn)
       if (*strFindIn==ch) return chNo;
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t str_find_str_impl( const char *strFindIn, pfs_size_t strFindInSize, const char *findStr, pfs_size_t strLen)
   {
    pfs_size_t pos = 0;
    if (strLen>strFindInSize) return 0;
    for(; pos!=(strFindInSize-strLen+1); ++pos, ++strFindIn)
       {
        if (str_compare_impl(strFindIn,findStr,strLen)==0) return pos;
       }
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t str_find_char_ci_impl( const char *strFindIn, pfs_size_t strFindInSize, char ch )
   {
    pfs_size_t chNo = 0;
    ch = char_touppercasecopy(ch);
    for( ; chNo!=strFindInSize; ++chNo, ++strFindIn)
       if (char_touppercasecopy(*strFindIn)==ch) return chNo;
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t str_find_str_ci_impl( const char *strFindIn, pfs_size_t strFindInSize, const char *findStr, pfs_size_t strLen)
   {
    pfs_size_t pos = 0;
    if (strLen>strFindInSize) return 0;
    for(; pos!=(strFindInSize-strLen+1); ++pos, ++strFindIn)
       {
        if (str_compare_ci_impl(strFindIn,findStr,strLen)==0) return pos;
       }
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
/*static*/
pfs_size_t str_copy_impl( char *buf, const char *p, pfs_size_t bufMaxSize )
   {
    pfs_size_t cnt = 0;
    for( ; *p && cnt!=(bufMaxSize-1); ++p, ++buf, ++cnt)
       *buf = *p;
    *buf = 0;
    return cnt;
   }

/*-----------------------------------------------------------------------------*/
/*static*/
pfs_size_t str_copy_data_impl( char *buf, const char *p, pfs_size_t bufMaxSize, pfs_size_t numBytesCopy )
   {
    pfs_size_t chNo = 0;
    for( ; /* *p */ chNo!=numBytesCopy && chNo!=(bufMaxSize-1); ++p, ++buf, ++chNo)
       *buf = *p;
    *buf = 0;
    return chNo;
   }

/*-----------------------------------------------------------------------------*/
/*static*/
pfs_size_t str_fill_chars_impl( char *buf, pfs_size_t bufMaxSize, pfs_size_t cnt, char ch )
   {
    pfs_size_t chNo = 0;
    for( ; /* *p */ chNo!=cnt && chNo!=(bufMaxSize-1); ++chNo, ++buf)
       *buf = ch;
    *buf = 0;
    return chNo;
   }

/*-----------------------------------------------------------------------------*/
/*static*/
pfs_size_t str_len_impl( const char *p )
   {
    pfs_size_t res = 0;
    while(*p) { ++p; ++res; }
    return res;
   }




/*
typedef struct tag_pfs_string_t
{
    char         *pData;
    pfs_size_t   _str_size;
    pfs_size_t   _buf_size;
    pfs_size_t   _is_dynamic;
} pfs_string_t;
*/

/*-----------------------------------------------------------------------------*/
int pfs_str_init_static( pfs_str_t *pStr, pfs_size_t bufSize, char *pBuf)
   {
    pStr->pData       = pBuf;
    pStr->_buf_size   = bufSize;
    pStr->_str_size   = 0;
    pStr->_is_dynamic = 0;
    return 0;
   }

#if defined(HAS_PFS_ALLOC_IMPL)
extern void* pfs_alloc( pfs_size_t numBytes );
extern void pfs_free ( void* p );
#endif

/*-----------------------------------------------------------------------------*/
int pfs_str_delete( pfs_str_t *pStr)
   {
    #if defined(HAS_PFS_ALLOC_IMPL)
    if (pStr->_is_dynamic)
       pfs_free( (void*)pStr->pData );
    #endif
    return 0;
   }

/*-----------------------------------------------------------------------------*/
#if defined(HAS_PFS_ALLOC_IMPL)
/* return -1 if fail */
int pfs_str_init_dynamic( pfs_str_t *pStr, pfs_size_t bufInitialSize)
   {
    pStr->pData       = (char*)pfs_alloc( bufInitialSize );
    pStr->_buf_size   = bufInitialSize;
    pStr->_str_size   = 0;
    pStr->_is_dynamic = 1;
    if (!pStr->pData) { pStr->_buf_size = 0; return -1; }
    return 0;   
   }

int pfs_str_init_dynamic_ex( pfs_str_t *pStr, pfs_size_t bufInitialSize, pfs_size_t bufMaxSize)
   {
    pStr->pData       = (char*)pfs_alloc( bufInitialSize );
    pStr->_buf_size   = bufInitialSize;
    pStr->_str_size   = 0;
    pStr->_is_dynamic = bufMaxSize | 1;
    if (!pStr->pData) { pStr->_buf_size = 0; return -1; }
    return 0;   
   }

#endif

/*-----------------------------------------------------------------------------*/
int pfs_str_reserve( pfs_str_t *pStr, pfs_size_t requiredSize)
   {
    #if defined(HAS_PFS_ALLOC_IMPL)
    pfs_size_t newSize;
    pfs_size_t maxSize;
    char *pNewBuf = 0;
    #endif

    if (pStr->_buf_size < requiredSize) return 0; /* no need to realloc */

    #if defined(HAS_PFS_ALLOC_IMPL)
    if (pStr->_is_dynamic)
       {
        maxSize = pStr->_is_dynamic & 1;
        newSize = (pStr->_buf_size << 1);
        if (maxSize && newSize>maxSize)
           {
            if (requiredSize<=maxSize)
               { /* default resize strategy reach limit but we can allocate required space */
                newSize = requiredSize;
               }
            else
               { /* we cant allocate required space */
                return -1;
               }
           }
        pNewBuf = (char*)pfs_alloc( newSize );
        if (!pNewBuf) return -1;
        str_copy_data_impl( pNewBuf, pStr->pData, newSize, pStr->_str_size );
        pfs_free( (void*)pStr->pData );
        pStr->pData      = pNewBuf;
        pStr->_buf_size  = newSize;
        return 0;
       }
    #endif

    return -1;
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_append( pfs_str_t *pStr, const pfs_str_t * pStrApendFrom)
   {
    return pfs_str_append_data( pStr, pStrApendFrom->pData, pStrApendFrom->_str_size );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_append_str( pfs_str_t *pStr, const char * p)
   {
    return pfs_str_append_data( pStr, p, str_len_impl(p) );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_append_data( pfs_str_t *pStr, const char * pData, pfs_size_t dataLen)
   {
    pfs_str_reserve( pStr, pStr->_str_size + dataLen + 1);
    /*if ( pStr->_str_size >= (pStr->_buf_size-1) ) return -1;*/ /* wrong */
    /*if ( pStr->_str_size > (pStr->_buf_size-1) ) return -1;*/ /* wrong */
    pStr->_str_size += str_copy_data_impl( pStr->pData + (pfs_ptrdiff_t)pStr->_str_size
                                             , pData
                                             , pStr->_buf_size - pStr->_str_size
                                             , dataLen
                                             );
    return 0;
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_append_char( pfs_str_t *pStr, char ch )
   {
    pfs_str_reserve( pStr, pStr->_str_size + 1 + 1);
    if ( pStr->_str_size >= (pStr->_buf_size-1) ) return -1;
    pStr->pData[(pfs_ptrdiff_t)(pStr->_str_size++)] = ch;
    pStr->pData[(pfs_ptrdiff_t)(pStr->_str_size)] = 0;
    return 0;
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_append_chars( pfs_str_t *pStr, pfs_size_t numChars, char ch )
   {
    pfs_str_reserve( pStr, pStr->_str_size + numChars + 1 );
    pStr->_str_size += str_fill_chars_impl( pStr->pData + (pfs_ptrdiff_t)pStr->_str_size
                                              , pStr->_buf_size - pStr->_str_size
                                              , numChars
                                              , ch
                                              );
    return 0;
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_clear( pfs_str_t *pStr )
   {
    pStr->_str_size = 0;
    if (pStr->pData) pStr->pData[0] = 0;
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_assign( pfs_str_t *pStr, const pfs_str_t * pStrAssignFrom)
   {
    pStr->_str_size = 0;
    return pfs_str_append_data( pStr, pStrAssignFrom->pData, pStrAssignFrom->_str_size );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_assign_str( pfs_str_t *pStr, const char * p)
   {
    pStr->_str_size = 0;
    return pfs_str_append_str( pStr, p );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_assign_data( pfs_str_t *pStr, const char * pData, pfs_size_t dataLen)
   {
    pStr->_str_size = 0;
    return pfs_str_append_data( pStr, pData, dataLen );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_assign_char( pfs_str_t *pStr, char ch )
   {
    pStr->_str_size = 0;
    return pfs_str_append_char( pStr, ch );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_assign_chars( pfs_str_t *pStr, pfs_size_t numChars, char ch )
   {
    pStr->_str_size = 0;
    return pfs_str_append_chars( pStr, numChars, ch );
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t  pfs_str_size ( const pfs_str_t *pStr )
   {
    return pStr->_str_size;
   }

/*-----------------------------------------------------------------------------*/
unsigned pfs_str_empty ( const pfs_str_t *pStr )
   {
    if (pStr->_str_size) return 0;
    return 1;
   }

/*-----------------------------------------------------------------------------*/
const char* pfs_str_data ( const pfs_str_t *pStr )
   {
    return pStr->pData;
   }

/*-----------------------------------------------------------------------------*/
const char* pfs_str_c_str( const pfs_str_t *pStr )
   {
    return pStr->pData;
   }

/*-----------------------------------------------------------------------------*/
char pfs_str_at( const pfs_str_t *pStr, pfs_size_t pos )
   {
    if (pos>=pStr->_str_size) return 0;
    return pStr->pData[(pfs_ptrdiff_t)pos];
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_toupper_ex ( const pfs_str_t *pStr, pfs_size_t offs, pfs_size_t len )
   {
    pfs_size_t i = 0, end;
    if (offs >= pStr->_str_size) return; /* offset is out of range */
    if (len == STRING_NPOS) len = pStr->_str_size - offs;
    end = offs + len; i = offs;
    for(; i!=end; ++i)
       char_touppercase( &pStr->pData[(pfs_ptrdiff_t)i] );
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_toupper    ( const pfs_str_t *pStr )
   {
    pfs_str_toupper_ex( pStr, 0, STRING_NPOS );
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_tolower_ex ( const pfs_str_t *pStr, pfs_size_t offs, pfs_size_t len )
   {
    pfs_size_t i = 0, end;
    if (offs >= pStr->_str_size) return; /* offset is out of range */
    if (len == STRING_NPOS) len = pStr->_str_size - offs;
    end = offs + len; i = offs;
    for(; i!=end; ++i)
       char_tolowercase( &pStr->pData[(pfs_ptrdiff_t)i] );
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_tolower    ( const pfs_str_t *pStr )
   {
    pfs_str_tolower_ex( pStr, 0, STRING_NPOS );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_compare_data ( const pfs_str_t *pStr, pfs_size_t dataLen, const char * pData )
   {
    return str_compare_impl2( pStr->pData, pStr->_str_size, pData, dataLen );
    /*return pfs_str_compare_data_ex ( pStr, 0, dataLen, pData );*/
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_compare_str  ( const pfs_str_t *pStr, const char * p )
   {
    return str_compare_impl2( pStr->pData, pStr->_str_size, p, str_len_impl(p) );
    /*
    pfs_size_t strLen;
    int cmpRes;
    strLen = str_len_impl(p);
    cmpRes = str_compare_impl2( pStr->pData, pStr->_str_size, p, strLen );
    if (cmpRes) return cmpRes;
    if (pStr->_str_size == strLen) return 0;
    if (pStr->_str_size >  strLen) return 1;
    return -1;
    */
    /*return pfs_str_compare_data_ex ( pStr, 0, dataLen, pData );*/
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_compare      ( const pfs_str_t *pStrLeft, const pfs_str_t *pStrRight )
   {
    return str_compare_impl2( pStrLeft->pData, pStrLeft->_str_size, pStrRight->pData, pStrRight->_str_size );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_compare_data_ex ( const pfs_str_t *pStr    , pfs_size_t offs, pfs_size_t cmpLen, const char * pData )
   {
    if (offs >= (pStr->_str_size)) return 0; /* nothing to compare, offset is out of string, assuming equality */
    if (cmpLen==STRING_NPOS) cmpLen = pStr->_str_size;
    if (cmpLen > (pStr->_str_size-offs)) cmpLen = pStr->_str_size-offs;
    return str_compare_impl( pStr->pData+(pfs_ptrdiff_t)offs, pData, cmpLen );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_compare_str_ex  ( const pfs_str_t *pStr    , pfs_size_t offs, const char * p )
   {
    return pfs_str_compare_data_ex( pStr, offs, str_len_impl(p), p);
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_compare_ex      ( const pfs_str_t *pStrLeft, pfs_size_t offs, pfs_size_t cmpLen, const pfs_str_t *pStrRight )
   {
    return pfs_str_compare_data_ex( pStrLeft, offs
                                  , cmpLen < pStrRight->_str_size ? cmpLen : pStrRight->_str_size
                                  , pStrRight->pData
                                  );
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_char( const pfs_str_t *pStr, char ch, pfs_size_t posStart )
   {
    pfs_size_t findRes = STRING_NPOS;
    if (posStart >= (pStr->_str_size)) return findRes;
    findRes = str_find_char_impl( pStr->pData + (pfs_ptrdiff_t)posStart
                                , pStr->_str_size - posStart
                                , ch
                                );
    if (findRes==STRING_NPOS) return findRes;
    else return findRes + posStart;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_char_ci( const pfs_str_t *pStr, char ch, pfs_size_t posStart )
   {
    pfs_size_t findRes = STRING_NPOS;
    if (posStart >= (pStr->_str_size)) return findRes;
    findRes = str_find_char_ci_impl( pStr->pData + (pfs_ptrdiff_t)posStart
                             , pStr->_str_size - posStart
                             , ch
                             );
    if (findRes==STRING_NPOS) return findRes;
    else return findRes + posStart;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_first_of_char( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart )
   {
    pfs_size_t i = 0;
    const char* p; /* = pStrChars; */
    if (posStart >= (pStr->_str_size)) return STRING_NPOS;
    for(i=posStart; i!=pStr->_str_size; ++i)
       {
        for(p = pStrChars; *p && *p!= pStr->pData[(pfs_ptrdiff_t)i]; ++p) {}
        if (*p) /* found */
           return i;
       }
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_first_not_of_char( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart )
   {
    pfs_size_t i = 0;
    const char* p = pStrChars;
    if (posStart >= (pStr->_str_size)) return STRING_NPOS;
    for(i=posStart; i!=pStr->_str_size; ++i)
       {
        for(; *p && *p!= pStr->pData[(pfs_ptrdiff_t)i]; ++p) {}
        if (!(*p)) /* not found */
           return i;
       }
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_first_of_char_ci( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart )
   {
    pfs_size_t i = 0;
    const char* p = pStrChars;
    if (posStart >= pStr->_str_size) return STRING_NPOS;
    for(i=posStart; i!=pStr->_str_size; ++i)
       {
        for(; *p && char_touppercasecopy(*p)!= char_touppercasecopy(pStr->pData[(pfs_ptrdiff_t)i]); ++p) {}
        if (*p) /* found */
           return i;
       }
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_first_not_of_char_ci( const pfs_str_t *pStr, const char* pStrChars, pfs_size_t posStart )
   {
    pfs_size_t i = 0;
    const char* p = pStrChars;
    if (posStart >= pStr->_str_size) return STRING_NPOS;
    for(i=posStart; i!=pStr->_str_size; ++i)
       {
        for(; *p && char_touppercasecopy(*p)!= char_touppercasecopy(pStr->pData[(pfs_ptrdiff_t)i]); ++p) {}
        if (!(*p)) /* not found */
           return i;
       }
    return STRING_NPOS;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_str ( const pfs_str_t *pStr, const char* p, pfs_size_t posStart )
   {
    pfs_size_t res = STRING_NPOS;
    if (posStart >= pStr->_str_size) return STRING_NPOS;
    res = str_find_str_impl( pStr->pData+(pfs_ptrdiff_t)posStart, pStr->_str_size-posStart, p, str_len_impl(p) );
    if (res==STRING_NPOS) return res;
    return res + posStart;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_str_ci ( const pfs_str_t *pStr, const char* p, pfs_size_t posStart )
   {
    pfs_size_t res = STRING_NPOS;
    if (posStart >= pStr->_str_size) return STRING_NPOS;
    res = str_find_str_ci_impl( pStr->pData+(pfs_ptrdiff_t)posStart, pStr->_str_size-posStart, p, str_len_impl(p) );
    if (res==STRING_NPOS) return res;
    return res + posStart;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find     ( const pfs_str_t *pStr, const pfs_str_t *pStrToFind, pfs_size_t posStart )
   {
    pfs_size_t res = STRING_NPOS;
    if (posStart >= pStr->_str_size) return STRING_NPOS;
    res = str_find_str_impl( pStr->pData+(pfs_ptrdiff_t)posStart, pStr->_str_size-posStart, pStrToFind->pData, pStrToFind->_str_size );
    if (res==STRING_NPOS) return res;
    return res + posStart;
   }

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_ci   ( const pfs_str_t *pStr, const pfs_str_t *pStrToFind, pfs_size_t posStart )
   {
    pfs_size_t res = STRING_NPOS;
    if (posStart >= pStr->_str_size) return STRING_NPOS;
    res = str_find_str_ci_impl( pStr->pData+(pfs_ptrdiff_t)posStart, pStr->_str_size-posStart, pStrToFind->pData, pStrToFind->_str_size );
    if (res==STRING_NPOS) return res;
    return res + posStart;
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_split_to_pair(const pfs_str_t *pStr, pfs_str_t *pFirst, pfs_str_t *pSecond, const char *separator)
   {
    pfs_size_t pos, sepLen;
    pos = pfs_str_find_str( pStr, separator, 0 );
    if (pos==STRING_NPOS)
       {
        pfs_str_assign_data( pFirst, pfs_str_data(pStr), pfs_str_size(pStr) );
        pfs_str_clear(pSecond);
       }
    else
       {
        sepLen = str_len_impl(separator);
        pfs_str_assign_data( pFirst, pfs_str_data(pStr), pos );
        pfs_str_assign_data( pSecond, pfs_str_data(pStr) + (pfs_ptrdiff_t)(pos + sepLen), pfs_str_size(pStr) - pos - sepLen);
       }
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_trim_copy(const pfs_str_t *pStr, pfs_str_t *pTrimTo)
   {
    pfs_size_t startPos = 0, endPos;
    endPos = pfs_str_size(pStr);
    for(; startPos!=endPos; ++startPos)
       {
        if (!pfs_isspace(pfs_str_at(pStr, startPos))) break;
       }
    /* found start */
    for(; startPos!=endPos; --endPos)
       {
        if (!pfs_isspace(pfs_str_at(pStr, endPos-1))) break;
       }
    pfs_str_assign_data( pTrimTo, pfs_str_data(pStr)+(pfs_ptrdiff_t)startPos, endPos-startPos );
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_rtrim_copy(const pfs_str_t *pStr, pfs_str_t *pTrimTo)
   {
    pfs_size_t startPos = 0, endPos;
    endPos = pfs_str_size(pStr);
    for(; startPos!=endPos; --endPos)
       {
        if (!pfs_isspace(pfs_str_at(pStr, endPos-1))) break;
       }
    pfs_str_assign_data( pTrimTo, pfs_str_data(pStr)+(pfs_ptrdiff_t)startPos, endPos-startPos );
   }

/*-----------------------------------------------------------------------------*/
void pfs_str_ltrim_copy(const pfs_str_t *pStr, pfs_str_t *pTrimTo)
   {
    pfs_size_t startPos = 0, endPos;
    endPos = pfs_str_size(pStr);
    for(; startPos!=endPos; ++startPos)
       {
        if (!pfs_isspace(pfs_str_at(pStr, startPos))) break;
       }
    pfs_str_assign_data( pTrimTo, pfs_str_data(pStr)+(pfs_ptrdiff_t)startPos, endPos-startPos );
   }

/*-----------------------------------------------------------------------------*/
int pfs_str_erase( pfs_str_t *pStr, pfs_size_t pos, pfs_size_t len )
{
    pfs_size_t posFrom/*, numCharToMove*/;
    if (pos >= pStr->_str_size) return 0;
    if (len==STRING_NPOS)
       {
        len = pStr->_str_size - pos;
       }
    if (len==0) return 0;
    posFrom = pos + len;
    #if 0
    if (posFrom >= pStr->_str_size)
       { /* nothing to move */
        pStr->_str_size = pos;
        pStr->pData[pos] = 0;
        return 0;
       }
    #endif
    /*numCharToMove = pStr->_str_size - posFrom;*/
    for(; posFrom!=pStr->_str_size; ++pos, ++posFrom)
       pStr->pData[(pfs_ptrdiff_t)pos] = pStr->pData[(pfs_ptrdiff_t)posFrom];
    pStr->pData[pos] = 0;
    pStr->_str_size = pos;
    return 0;
}

/*-----------------------------------------------------------------------------*/
int pfs_str_insert_str( pfs_str_t *pStr, pfs_size_t pos, const char * pData )
{
    return pfs_str_insert_data( pStr, pos, pData, str_len_impl(pData) );
}

/*-----------------------------------------------------------------------------*/
int pfs_str_insert_data( pfs_str_t *pStr, pfs_size_t pos, const char * pData, pfs_size_t dataLen )
{
    pfs_size_t posTo, posFrom, i, reqSize, moveLen;
   
    if (pos >= pStr->_str_size) return 0;

    reqSize = pStr->_str_size + dataLen + 1;
    pfs_str_reserve( pStr, reqSize );
    if (pStr->_buf_size < reqSize) return -1; /* can't insert, keep str untouched */

    
    posTo = pStr->_str_size+dataLen;
    posFrom = pStr->_str_size;
    i = 0;
    moveLen = pStr->_str_size - pos;
    for(; i<=moveLen; ++i, --posTo, --posFrom )
       {
        pStr->pData[(pfs_ptrdiff_t)posTo] = pStr->pData[(pfs_ptrdiff_t)posFrom];
       }
    for(i=0, posTo=pos; i!=dataLen; ++i, ++posTo)
       {
        pStr->pData[(pfs_ptrdiff_t)posTo] = pData[(pfs_ptrdiff_t)i];
       }
    pStr->_str_size += dataLen;
    return 0;
}

/*-----------------------------------------------------------------------------*/
int pfs_str_insert( pfs_str_t *pStr, pfs_size_t pos, const pfs_str_t *pStrIns )
{
    return pfs_str_insert_data( pStr, pos, pStrIns->pData, pStrIns->_str_size );
}

/*-----------------------------------------------------------------------------*/
int pfs_str_replace_with_data( pfs_str_t *pStr, pfs_size_t replacePos, pfs_size_t replaceLen, const char * pData, pfs_size_t dataLen )
{
    pfs_str_erase( pStr, replacePos, replaceLen );
    return pfs_str_insert_data( pStr, replacePos, pData, dataLen );
}

/*-----------------------------------------------------------------------------*/
int pfs_str_replace_with_str( pfs_str_t *pStr, pfs_size_t replacePos, pfs_size_t replaceLen, const char * pData )
{
    return pfs_str_replace_with_data( pStr, replacePos, replaceLen, pData, str_len_impl(pData) );
}

/*-----------------------------------------------------------------------------*/
int pfs_str_replace_with( pfs_str_t *pStr, pfs_size_t replacePos, pfs_size_t replaceLen, const pfs_str_t *pStrReplace )
{
    return pfs_str_replace_with_data( pStr, replacePos, replaceLen, pStrReplace->pData, pStrReplace->_str_size );
}

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_replace_with_str( pfs_str_t *pStr, pfs_size_t posStart, const char *strFind, const char *strReplace )
{
    pfs_size_t foundAt;
    pfs_size_t strReplaceLen; 
    pfs_size_t 
    strFindLen    = str_len_impl(strFind);
    strReplaceLen = str_len_impl(strReplace);
    foundAt = pfs_str_find_str( pStr, strFind, posStart );
    if (foundAt==STRING_NPOS) return foundAt;

    pfs_str_replace_with_str( pStr, foundAt, strFindLen, strReplace );
    return foundAt + strReplaceLen /*  - strFindLen */ ;
}

/*-----------------------------------------------------------------------------*/
pfs_size_t pfs_str_find_replace_with( pfs_str_t *pStr, pfs_size_t posStart, const char *strFind, pfs_str_t *pStrReplace )
{
    pfs_size_t foundAt;
    pfs_size_t 
    strFindLen    = str_len_impl(strFind);
    foundAt = pfs_str_find_str( pStr, strFind, posStart );
    if (foundAt==STRING_NPOS) return foundAt;

    pfs_str_replace_with( pStr, foundAt, strFindLen, pStrReplace );
    return foundAt + pStrReplace->_str_size /*  - strFindLen */ ;
}

/*-----------------------------------------------------------------------------*/
void pfs_str_find_replace_all_with_str( pfs_str_t *pStr, const char *strFind, const char *strReplace )
{
    pfs_size_t pNextStartPos = pfs_str_find_replace_with_str( pStr, (pfs_size_t)0, strFind, strReplace );
    while(pNextStartPos!=STRING_NPOS)
       {
        pNextStartPos = pfs_str_find_replace_with_str( pStr, pNextStartPos, strFind, strReplace );
       }
}

/*-----------------------------------------------------------------------------*/
void pfs_str_find_replace_all_with( pfs_str_t *pStr, const char *strFind, pfs_str_t *pStrReplace )
{
    pfs_size_t pNextStartPos = pfs_str_find_replace_with( pStr, (pfs_size_t)0, strFind, pStrReplace );
    while(pNextStartPos!=STRING_NPOS)
       {
        pNextStartPos = pfs_str_find_replace_with( pStr, pNextStartPos, strFind, pStrReplace );
       }
}








pfsalloc.h

Memory allocation functions used in pfs-string library.
[-]
#ifndef UTILS_PFSALLOC_H
#define UTILS_PFSALLOC_H

/* add this lines to your scr
#ifndef UTILS_PFSALLOC_H
    #include "../utils/pfsalloc.h"
#endif
*/

#ifndef UTILS_PFSUCONF_H
    #include "pfsuconf.h"
#endif

#ifndef PFS_SIZE_T_DEFINED
    #define PFS_SIZE_T_DEFINED
        #if defined(__TURBOC__)
            typedef unsigned      pfs_size_t;
            typedef int           pfs_ssize_t;
            typedef int           pfs_ptrdiff_t;        
        #else
            typedef unsigned long pfs_size_t;
            typedef          long pfs_ssize_t;
            typedef int           pfs_ptrdiff_t;
        #endif

#endif


#if defined(HAS_PFS_ALLOC_IMPL)
void* pfs_alloc( pfs_size_t numBytes );
void  pfs_free ( void* p );
#endif

/*
create file pfsalloc.c and put next code to it
#if defined(WIN32) || defined(_WIN32)
extern void *malloc( size_t size );
extern void free( void *memblock );
#else
extern void *malloc( unsigned size );
extern void free( void *memblock );
#endif
 
void* pfs_alloc( pfs_size_t numBytes )
{
#if !defined(DEVICE)
    #if defined(WIN32) || defined(_WIN32)
    return malloc( (size_t)numBytes );
    #else
    return malloc( (unsigned)numBytes );
    #endif
#else
    return sys_malloc( (unsigned)numBytes );
#endif
}

void pfs_free ( void* p )
{
#if !defined(DEVICE)
    free( p );
#else
    sys_free( p );
#endif
}
*/

#endif /* UTILS_PFSALLOC_H */

pfsuconf.h

pfs-string library configuration file.
[-]
#ifndef UTILS_PFSUCONF_H
#define UTILS_PFSUCONF_H

/* add this lines to your src
#ifndef UTILS_PFSUCONF_H
    #include "../utils/pfsuconf.h"
#endif
*/

/* String library configuration.
 * On embedded devices we can find 
 * that there is no dynamic memory at all,
 * so string library can use only static buffers
 */
#define HAS_PFS_ALLOC_IMPL

#endif /* UTILS_PFSUCONF_H */