Synesis Software

cstring/cstring.h

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:    cstring/cstring.h
00003  *
00004  * Purpose: Definition of the cstring API.
00005  *
00006  * Created: 16th June 1994
00007  * Updated: 14th February 2012
00008  *
00009  * Home:    http://synesis.com.au/software/
00010  *
00011  * Copyright (c) 1994-2012, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the names of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * ////////////////////////////////////////////////////////////////////// */
00039 
00040 
00044 #ifndef CSTRING_INCL_CSTRING_H_CSTRING
00045 #define CSTRING_INCL_CSTRING_H_CSTRING
00046 
00047 /* /////////////////////////////////////////////////////////////////////////
00048  * Version
00049  */
00050 
00051 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00052 # define CSTRING_VER_CSTRING_H_CSTRING_MAJOR    3
00053 # define CSTRING_VER_CSTRING_H_CSTRING_MINOR    8
00054 # define CSTRING_VER_CSTRING_H_CSTRING_REVISION 2
00055 # define CSTRING_VER_CSTRING_H_CSTRING_EDIT     62
00056 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00057 
00074 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00075 # define CSTRING_VER_1_0_1      0x01000100
00076 # define CSTRING_VER_1_0_2      0x01000200
00077 # define CSTRING_VER_2_0_1      0x02000100
00078 # define CSTRING_VER_2_1_1      0x02010100
00079 # define CSTRING_VER_2_2_1      0x02020100
00080 # define CSTRING_VER_3_0_1      0x03000100
00081 # define CSTRING_VER_3_1_1      0x03010100
00082 # define CSTRING_VER_3_1_2      0x03010200
00083 # define CSTRING_VER_3_1_3      0x03010300
00084 # define CSTRING_VER_3_2_1      0x03020100
00085 # define CSTRING_VER_3_3_1      0x03030100
00086 # define CSTRING_VER_3_3_2      0x03030200
00087 # define CSTRING_VER_3_4_1      0x03040100
00088 # define CSTRING_VER_3_4_2      0x03040200
00089 # define CSTRING_VER_3_4_3      0x03040300
00090 # define CSTRING_VER_3_4_4      0x03040400
00091 # define CSTRING_VER_3_5_1      0x03050100
00092 # define CSTRING_VER_3_5_2      0x03050200
00093 # define CSTRING_VER_3_5_3      0x03050300
00094 # define CSTRING_VER_3_5_4      0x03050400
00095 # define CSTRING_VER_3_6_1      0x03060100
00096 # define CSTRING_VER_3_6_2      0x030602ff
00097 
00098 # define CSTRING_VER            CSTRING_VER_3_6_2
00099 #else /* ? CSTRING_DOCUMENTATION_SKIP_SECTION */
00100 # define CSTRING_VER            0x030602ff
00101 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00102 
00103 #define CSTRING_VER_MAJOR       3
00104 #define CSTRING_VER_MINOR       6
00105 #define CSTRING_VER_REVISION    2
00106 
00107 /* /////////////////////////////////////////////////////////////////////////
00108  * Includes
00109  */
00110 
00111 #include <stddef.h>
00112 #include <stdio.h>
00113 
00114 /* /////////////////////////////////////////////////////////////////////////
00115  * Documentation
00116  */
00117 
00125 /* /////////////////////////////////////////////////////////////////////////
00126  * Storage class
00127  */
00128 
00129 #ifdef __cplusplus
00130 # define CSTRING_EXTERN_C       extern "C"
00131 #else /* ? __cplusplus */
00132 # define CSTRING_EXTERN_C       extern
00133 #endif /* ? __cplusplus */
00134 
00135 /* /////////////////////////////////////////////////////////////////////////
00136  * Constants & definitions
00137  */
00138 
00142 enum CSTRING_RC
00143 {
00144         CSTRING_RC_SUCCESS               =   0  
00145     ,   CSTRING_RC_OUTOFMEMORY                  
00146     ,   CSTRING_RC_FIXED                        
00147     ,   CSTRING_RC_BORROWED                     
00148     ,   CSTRING_RC_READONLY                     
00149     ,   CSTRING_RC_INVALIDARENA                 
00150     ,   CSTRING_RC_CUSTOMARENANOTSUPPORTED      
00151     ,   CSTRING_RC_EXCEEDFIXEDCAPACITY          
00152     ,   CSTRING_RC_EXCEEDBORROWEDCAPACITY       
00153     ,   CSTRING_RC_CANNOTYIELDFROMSO            
00154     ,   CSTRING_RC_ARENAOVERLOADED              
00155     /* cstring 3.5+ */
00156     ,   CSTRING_RC_INVALIDSTREAM                
00157     ,   CSTRING_RC_EOF                          
00158     ,   CSTRING_RC_INVALIDSECTION               
00159     ,   CSTRING_RC_IOERROR                      
00160     /* cstring 3.6.2+ */
00161     ,   CSTRING_RC_SYSTEMSPECIFICFAILURE        
00162 };
00163 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00164 typedef enum CSTRING_RC     CSTRING_RC;
00165 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00166 
00167 /* /////////////////////////////////////////////////////////////////////////
00168  * Typedefs
00169  */
00170 
00174 typedef int cstring_flags_t;
00175 
00183 struct cstring_t
00184 {
00185     size_t          len;        
00186     char*           ptr;        
00188     size_t          capacity;   
00189     cstring_flags_t flags;      
00191 };
00192 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00193 typedef struct cstring_t    cstring_t;
00194 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00195 
00200 #define cstring_t_DEFAULT  { 0, NULL, 0, 0 }
00201 
00202 /* /////////////////////////////////////////////////////////////////////////
00203  * Custom settings
00204  */
00205 
00206 #ifndef CSTRING_NO_USE_SYNESIS_APIS
00207 # if defined(_SYNSOFT_INCL_H_MLMEMORY)
00208 #  define CSTRING_USE_SYNESIS_APIS
00209 # endif /* _SYNSOFT_INCL_H_MLMEMORY */
00210 #endif /* !CSTRING_NO_USE_SYNESIS_APIS */
00211 
00212 /* /////////////////////////////////////////////////////////////////////////
00213  * Macros
00214  */
00215 
00226 #define CSTRING_FROM_END(x)     (-1 - (int)(x))
00227 
00228 /* /////////////////////////////////////////////////////////////////////////
00229  * Flags
00230  */
00231 
00240 #define CSTRING_F_TYPE_MASK                     (0x003f)
00241 #define CSTRING_F_MEMORY_IS_INTERNAL_HEAP       (0x0000)    
00243 #define CSTRING_F_MEMORY_IS_BORROWED            (0x0001)    
00246 #define CSTRING_F_MEMORY_IS_FIXED               (0x0002)    
00248 #define CSTRING_F_MEMORY_IS_READONLY            (0x0004)    
00249 #define CSTRING_F_MEMORY_IS_CONST               CSTRING_F_MEMORY_IS_READONLY 
00251 #define CSTRING_F_MEMORY_IS_OFFSET              (0x0040)    
00254 #define CSTRING_F_MEMORY_CAN_GROW_TO_HEAP       (0x0080)    
00256 #define CSTRING_F_ARENA_MASK                    (0xff00)
00257 #define CSTRING_F_USE_REALLOC                   (0x0000)    
00259 #if defined(WIN32) || \
00260     defined(WIN64)
00261 # define CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY    (0x0100)    
00264 # define CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY (0x0200)  
00267 # define CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY  (0x0400)    
00270 #endif /* WIN32 || WIN64 */
00271 #if defined(CSTRING_USE_SYNESIS_APIS)
00272 # define CSTRING_F_USE_SYNESIS_HATOR            (0x1000)    
00274 #endif /* CSTRING_USE_SYNESIS_APIS */
00275 #define CSTRING_F_USE_CUSTOMARENAFUNCTIONS      (0x2000)    
00278 #define CSTRING_F_CUSTOMARENA_CC_CDECL          (0x0000)    
00281 #define CSTRING_F_CUSTOMARENA_CC_STDCALL        (0x0040)    
00287 /* /////////////////////////////////////////////////////////////////////////
00288  * API functions
00289  */
00290 
00299 CSTRING_EXTERN_C char const* cstring_getStatusCodeString(
00300     CSTRING_RC  rc
00301 );
00302 
00312 CSTRING_EXTERN_C size_t cstring_getStatusCodeStringLength(
00313     CSTRING_RC  rc
00314 );
00315 
00324 #if defined(_MSC_VER) && \
00325     _MSC_VER >= 1400
00326 __declspec(deprecated("cstring_error() is deprecated, and may be removed in a future version: instead use cstring_getStatusCodeString()"))
00327 #endif /* compiler */
00328 CSTRING_EXTERN_C char const* cstring_error(
00329     CSTRING_RC  rc
00330 );
00331 
00339 CSTRING_EXTERN_C CSTRING_RC cstring_init(
00340     struct cstring_t*       pcs
00341 );
00342 
00354 CSTRING_EXTERN_C CSTRING_RC cstring_create(
00355     struct cstring_t*   pcs
00356 ,   char const*         s
00357 );
00358 
00372 CSTRING_EXTERN_C CSTRING_RC cstring_createLen(
00373     struct cstring_t*       pcs
00374 ,   char const*             s
00375 ,   size_t                  cch
00376 );
00377 
00391 CSTRING_EXTERN_C CSTRING_RC cstring_createN(
00392     struct cstring_t*       pcs
00393 ,   char                    ch
00394 ,   size_t                  n
00395 );
00396 
00397 
00418 CSTRING_EXTERN_C CSTRING_RC cstring_createEx(
00419     struct cstring_t*       pcs
00420 ,   char const*             s
00421 ,   cstring_flags_t         flags
00422 ,   void*                   arena
00423 ,   size_t                  capacity
00424 );
00425 
00448 CSTRING_EXTERN_C CSTRING_RC cstring_createLenEx(
00449     struct cstring_t*       pcs
00450 ,   char const*             s
00451 ,   size_t                  cch
00452 ,   cstring_flags_t         flags
00453 ,   void*                   arena
00454 ,   size_t                  capacity
00455 );
00456 
00467 CSTRING_EXTERN_C CSTRING_RC cstring_destroy(
00468     struct cstring_t*       pcs
00469 );
00470 
00471 
00490 CSTRING_EXTERN_C CSTRING_RC cstring_yield2(
00491     struct cstring_t*       pcs
00492 ,   char**                  pPayload
00493 ,   void**                  pRaw
00494 );
00495 
00496 #ifdef CSTRING_OBSOLETE
00497 CSTRING_EXTERN_C CSTRING_RC cstring_yield(
00498     struct cstring_t*       pcs
00499 ,   char**                  ppBuff
00500 );
00501 #endif /* CSTRING_OBSOLETE */
00502 
00519 CSTRING_EXTERN_C CSTRING_RC cstring_setCapacity(
00520     struct cstring_t*       pcs
00521 ,   size_t                  capacity
00522 );
00523 
00524 
00541 CSTRING_EXTERN_C CSTRING_RC cstring_assign(
00542     struct cstring_t*       pcs
00543 ,   char const*             s
00544 );
00545 
00564 CSTRING_EXTERN_C CSTRING_RC cstring_assignLen(
00565     struct cstring_t*       pcs
00566 ,   char const*             s
00567 ,   size_t                  cch
00568 );
00569 
00582 CSTRING_EXTERN_C CSTRING_RC cstring_copy(
00583     struct cstring_t*       pcs
00584 ,   struct cstring_t const* pcsSrc
00585 );
00586 
00599 CSTRING_EXTERN_C CSTRING_RC cstring_append(
00600     struct cstring_t*       pcs
00601 ,   char const*             s
00602 );
00603 
00622 CSTRING_EXTERN_C CSTRING_RC cstring_appendLen(
00623     struct cstring_t*       pcs
00624 ,   char const*             s
00625 ,   size_t                  cch
00626 );
00627 
00639 CSTRING_EXTERN_C CSTRING_RC cstring_truncate(
00640     struct cstring_t*       pcs
00641 ,   size_t                  len
00642 );
00643 
00656 CSTRING_EXTERN_C CSTRING_RC cstring_swap(
00657     struct cstring_t*       pcs1
00658 ,   struct cstring_t*       pcs2
00659 );
00660 
00661 
00679 CSTRING_EXTERN_C CSTRING_RC cstring_readline(
00680     FILE*               stm
00681 ,   struct cstring_t*   pcs
00682 ,   size_t*             numRead /* = NULL */
00683 );
00684 
00701 CSTRING_EXTERN_C CSTRING_RC cstring_writeline(
00702     FILE*                   stm
00703 ,   struct cstring_t const* pcs
00704 ,   size_t*                 numWritten /* = NULL */
00705 );
00706 
00707 
00713 CSTRING_EXTERN_C CSTRING_RC cstring_insert(
00714     struct cstring_t*       pcs
00715 ,   int                     index
00716 ,   char const*             s
00717 );
00718 
00724 CSTRING_EXTERN_C CSTRING_RC cstring_insertLen(
00725     struct cstring_t*       pcs
00726 ,   int                     index
00727 ,   char const*             s
00728 ,   size_t                  cch
00729 );
00730 
00745 CSTRING_EXTERN_C CSTRING_RC cstring_replace(
00746     struct cstring_t*       pcs
00747 ,   int                     index
00748 ,   size_t                  len
00749 ,   char const*             s
00750 );
00751 
00767 CSTRING_EXTERN_C CSTRING_RC cstring_replaceLen(
00768     struct cstring_t*       pcs
00769 ,   int                     index
00770 ,   size_t                  len
00771 ,   char const*             s
00772 ,   size_t                  cch
00773 );
00774 
00788 CSTRING_EXTERN_C CSTRING_RC cstring_replaceAll(
00789     struct cstring_t*       pcs
00790 ,   char const*             f
00791 ,   char const*             t
00792 ,   size_t*                 numReplaced /* = NULL */
00793 );
00794 
00795 /* ////////////////////////////////////////////////////////////////////// */
00796 
00797 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00798 typedef int (*cstring_allocFailHandler)(void* pv, size_t cb, cstring_flags_t flags, void* param);
00799 
00800 
00802 CSTRING_EXTERN_C CSTRING_RC cstring_createLenFn(
00803     struct cstring_t*           pcs
00804 ,   char const*                 s
00805 ,   size_t                      cch
00806 ,   cstring_flags_t             flags
00807 ,   void*                       arena
00808 ,   size_t                      capacity
00809 ,   cstring_allocFailHandler    pfnAllocFailHandler
00810 ,   void*                       param
00811 );
00812 
00814 CSTRING_EXTERN_C CSTRING_RC cstring_setCapacityFn(
00815     struct cstring_t*           pcs
00816 ,   size_t                      capacity
00817 ,   cstring_allocFailHandler    pfnAllocFailHandler
00818 ,   void*                       param
00819 );
00820 
00822 CSTRING_EXTERN_C CSTRING_RC cstring_assignFn(
00823     struct cstring_t*           pcs
00824 ,   char const*                 s
00825 ,   cstring_allocFailHandler    pfnAllocFailHandler
00826 ,   void*                       param
00827 );
00829 CSTRING_EXTERN_C CSTRING_RC cstring_assignLenFn(
00830     struct cstring_t*           pcs
00831 ,   char const*                 s
00832 ,   size_t                      cch
00833 ,   cstring_allocFailHandler    pfnAllocFailHandler
00834 ,   void*                       param
00835 );
00836 
00838 CSTRING_EXTERN_C CSTRING_RC cstring_appendFn(
00839     struct cstring_t*           pcs
00840 ,   char const*                 s
00841 ,   cstring_allocFailHandler    pfnAllocFailHandler
00842 ,   void*                       param
00843 );
00844 
00846 CSTRING_EXTERN_C CSTRING_RC cstring_appendLenFn(
00847     struct cstring_t*           pcs
00848 ,   char const*                 s
00849 ,   size_t                      cch
00850 ,   cstring_allocFailHandler    pfnAllocFailHandler
00851 ,   void*                       param
00852 );
00853 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00854 
00855 /* /////////////////////////////////////////////////////////////////////////
00856  * Compiler warnings
00857  */
00858 
00859 #if defined(_MSC_VER)
00860 # if _MSC_VER >= 1200
00861 #  pragma warning(push)
00862 # endif /* compiler */
00863 # pragma warning(disable : 4514) /* unreferenced inline function has been removed */
00864 #endif /* compiler */
00865 
00866 /* /////////////////////////////////////////////////////////////////////////
00867  * String Access Shims
00868  */
00869 
00870 #ifdef __cplusplus
00871 
00872 inline char const* c_str_data_a(struct cstring_t const* pcs)
00873 {
00874     return (NULL == pcs || 0u == pcs->len) ? "" : pcs->ptr;
00875 }
00876 inline char const* c_str_data(struct cstring_t const* pcs)
00877 {
00878     return c_str_data_a(pcs);
00879 }
00880 inline size_t c_str_len_a(struct cstring_t const* pcs)
00881 {
00882     return (NULL == pcs) ? 0u : pcs->len;
00883 }
00884 inline size_t c_str_len(struct cstring_t const* pcs)
00885 {
00886     return c_str_len_a(pcs);
00887 }
00888 inline char const* c_str_ptr_a(struct cstring_t const* pcs)
00889 {
00890     return c_str_data_a(pcs);
00891 }
00892 inline char const* c_str_ptr(struct cstring_t const* pcs)
00893 {
00894     return c_str_data_a(pcs);
00895 }
00896 
00897 inline char const* c_str_data_a(struct cstring_t const& cs)
00898 {
00899     return (0u == cs.len) ? "" : cs.ptr;
00900 }
00901 inline char const* c_str_data(struct cstring_t const& cs)
00902 {
00903     return c_str_data_a(cs);
00904 }
00905 inline size_t c_str_len_a(struct cstring_t const& cs)
00906 {
00907     return cs.len;
00908 }
00909 inline size_t c_str_len(struct cstring_t const& cs)
00910 {
00911     return c_str_len_a(cs);
00912 }
00913 inline char const* c_str_ptr_a(struct cstring_t const& cs)
00914 {
00915     return c_str_data_a(cs);
00916 }
00917 inline char const* c_str_ptr(struct cstring_t const& cs)
00918 {
00919     return c_str_data_a(cs);
00920 }
00921 
00922 inline char const *c_str_data_a(CSTRING_RC rc)
00923 {
00924     return cstring_getStatusCodeString(rc);
00925 }
00926 inline char const *c_str_data(CSTRING_RC rc)
00927 {
00928     return cstring_getStatusCodeString(rc);
00929 }
00930 
00931 inline size_t c_str_len_a(CSTRING_RC rc)
00932 {
00933     return cstring_getStatusCodeStringLength(rc);
00934 }
00935 inline size_t c_str_len(CSTRING_RC rc)
00936 {
00937     return cstring_getStatusCodeStringLength(rc);
00938 }
00939 
00940 inline char const *c_str_ptr_a(CSTRING_RC rc)
00941 {
00942     return cstring_getStatusCodeString(rc);
00943 }
00944 inline char const *c_str_ptr(CSTRING_RC rc)
00945 {
00946     return cstring_getStatusCodeString(rc);
00947 }
00948 
00949 # ifndef _STLSOFT_NO_NAMESPACE
00950 namespace stlsoft
00951 {
00952     using ::c_str_data_a;
00953     using ::c_str_len_a;
00954     using ::c_str_ptr_a;
00955     using ::c_str_data;
00956     using ::c_str_len;
00957     using ::c_str_ptr;
00958 
00959 } /* namespace stlsoft */
00960 # endif /* !_STLSOFT_NO_NAMESPACE */
00961 
00962 #endif /* __cplusplus */
00963 
00964 /* /////////////////////////////////////////////////////////////////////////
00965  * Compiler warnings
00966  */
00967 
00968 #if defined(_MSC_VER)
00969 # if _MSC_VER >= 1200
00970 /* #  pragma warning(pop) */
00971 # endif /* compiler */
00972 #endif /* compiler */
00973 
00974 /* /////////////////////////////////////////////////////////////////////////
00975  * Obsolete
00976  */
00977 
00978 #ifdef CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY
00979 # define CSTRING_F_USE_WIN32_GLOBAL_MEMORY      CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY
00980 #endif
00981 
00982 #ifdef CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY
00983 # define CSTRING_F_USE_WIN32_PROCESSHEAP_MEMORY CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY
00984 #endif
00985 
00986 #ifdef CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY
00987 # define CSTRING_F_USE_WIN32_COM_TASK_MEMORY    CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY
00988 #endif
00989 
00990 /* ////////////////////////////////////////////////////////////////////// */
00991 
00992 #endif /* CSTRING_INCL_CSTRING_H_CSTRING */
00993 
00994 /* ////////////////////////////////////////////////////////////////////// */

cstring Library documentation (c) Matthew Wilson and Synesis Software Pty Ltd, 1994-2009