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: 28th July 2011
00008  *
00009  * Home:    http://synesis.com.au/software/
00010  *
00011  * Copyright (c) 1994-2011, 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 1
00055 # define CSTRING_VER_CSTRING_H_CSTRING_EDIT     60
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 
00097 # define CSTRING_VER            CSTRING_VER_3_6_1
00098 #else /* ? CSTRING_DOCUMENTATION_SKIP_SECTION */
00099 # define CSTRING_VER            0x03060100
00100 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00101 
00102 #define CSTRING_VER_MAJOR       3
00103 #define CSTRING_VER_MINOR       6
00104 #define CSTRING_VER_REVISION    1
00105 
00106 /* /////////////////////////////////////////////////////////////////////////
00107  * Includes
00108  */
00109 
00110 #include <stddef.h>
00111 #include <stdio.h>
00112 
00113 /* /////////////////////////////////////////////////////////////////////////
00114  * Documentation
00115  */
00116 
00124 /* /////////////////////////////////////////////////////////////////////////
00125  * Storage class
00126  */
00127 
00128 #ifdef __cplusplus
00129 # define CSTRING_EXTERN_C       extern "C"
00130 #else /* ? __cplusplus */
00131 # define CSTRING_EXTERN_C       extern
00132 #endif /* ? __cplusplus */
00133 
00134 /* /////////////////////////////////////////////////////////////////////////
00135  * Constants & definitions
00136  */
00137 
00141 enum CSTRING_RC
00142 {
00143         CSTRING_RC_SUCCESS               =   0  
00144     ,   CSTRING_RC_OUTOFMEMORY                  
00145     ,   CSTRING_RC_FIXED                        
00146     ,   CSTRING_RC_BORROWED                     
00147     ,   CSTRING_RC_READONLY                     
00148     ,   CSTRING_RC_INVALIDARENA                 
00149     ,   CSTRING_RC_CUSTOMARENANOTSUPPORTED      
00150     ,   CSTRING_RC_EXCEEDFIXEDCAPACITY          
00151     ,   CSTRING_RC_EXCEEDBORROWEDCAPACITY       
00152     ,   CSTRING_RC_CANNOTYIELDFROMSO            
00153     ,   CSTRING_RC_ARENAOVERLOADED              
00154     /* cstring 3.5+ */
00155     ,   CSTRING_RC_INVALIDSTREAM                
00156     ,   CSTRING_RC_EOF                          
00157     ,   CSTRING_RC_INVALIDSECTION               
00158     ,   CSTRING_RC_IOERROR                      
00159 };
00160 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00161 typedef enum CSTRING_RC     CSTRING_RC;
00162 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00163 
00164 /* /////////////////////////////////////////////////////////////////////////
00165  * Typedefs
00166  */
00167 
00171 typedef int cstring_flags_t;
00172 
00180 struct cstring_t
00181 {
00182     size_t          len;        
00183     char*           ptr;        
00185     size_t          capacity;   
00186     cstring_flags_t flags;      
00188 };
00189 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00190 typedef struct cstring_t    cstring_t;
00191 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00192 
00197 #define cstring_t_DEFAULT  { 0, NULL, 0, 0 }
00198 
00199 /* /////////////////////////////////////////////////////////////////////////
00200  * Custom settings
00201  */
00202 
00203 #ifndef CSTRING_NO_USE_SYNESIS_APIS
00204 # if defined(_SYNSOFT_INCL_H_MLMEMORY)
00205 #  define CSTRING_USE_SYNESIS_APIS
00206 # endif /* _SYNSOFT_INCL_H_MLMEMORY */
00207 #endif /* !CSTRING_NO_USE_SYNESIS_APIS */
00208 
00209 /* /////////////////////////////////////////////////////////////////////////
00210  * Macros
00211  */
00212 
00223 #define CSTRING_FROM_END(x)     (-1 - (int)(x))
00224 
00225 /* /////////////////////////////////////////////////////////////////////////
00226  * Flags
00227  */
00228 
00237 #define CSTRING_F_TYPE_MASK                     (0x003f)
00238 #define CSTRING_F_MEMORY_IS_INTERNAL_HEAP       (0x0000)    
00240 #define CSTRING_F_MEMORY_IS_BORROWED            (0x0001)    
00243 #define CSTRING_F_MEMORY_IS_FIXED               (0x0002)    
00245 #define CSTRING_F_MEMORY_IS_READONLY            (0x0004)    
00246 #define CSTRING_F_MEMORY_IS_CONST               CSTRING_F_MEMORY_IS_READONLY 
00248 #define CSTRING_F_MEMORY_IS_OFFSET              (0x0040)    
00251 #define CSTRING_F_MEMORY_CAN_GROW_TO_HEAP       (0x0080)    
00253 #define CSTRING_F_ARENA_MASK                    (0xff00)
00254 #define CSTRING_F_USE_REALLOC                   (0x0000)    
00256 #if defined(WIN32) || \
00257     defined(WIN64)
00258 # define CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY    (0x0100)    
00261 # define CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY (0x0200)  
00264 # define CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY  (0x0400)    
00267 #endif /* WIN32 || WIN64 */
00268 #if defined(CSTRING_USE_SYNESIS_APIS)
00269 # define CSTRING_F_USE_SYNESIS_HATOR            (0x1000)    
00271 #endif /* CSTRING_USE_SYNESIS_APIS */
00272 #define CSTRING_F_USE_CUSTOMARENAFUNCTIONS      (0x2000)    
00275 #define CSTRING_F_CUSTOMARENA_CC_CDECL          (0x0000)    
00278 #define CSTRING_F_CUSTOMARENA_CC_STDCALL        (0x0040)    
00284 /* /////////////////////////////////////////////////////////////////////////
00285  * API functions
00286  */
00287 
00296 CSTRING_EXTERN_C char const* cstring_getStatusCodeString(
00297     CSTRING_RC  rc
00298 );
00299 
00309 CSTRING_EXTERN_C size_t cstring_getStatusCodeStringLength(
00310     CSTRING_RC  rc
00311 );
00312 
00321 #if defined(_MSC_VER) && \
00322     _MSC_VER >= 1400
00323 __declspec(deprecated("cstring_error() is deprecated, and may be removed in a future version: instead use cstring_getStatusCodeString()"))
00324 #endif /* compiler */
00325 CSTRING_EXTERN_C char const* cstring_error(
00326     CSTRING_RC  rc
00327 );
00328 
00336 CSTRING_EXTERN_C CSTRING_RC cstring_init(
00337     struct cstring_t*       pcs
00338 );
00339 
00351 CSTRING_EXTERN_C CSTRING_RC cstring_create(
00352     struct cstring_t*   pcs
00353 ,   char const*         s
00354 );
00355 
00369 CSTRING_EXTERN_C CSTRING_RC cstring_createLen(
00370     struct cstring_t*       pcs
00371 ,   char const*             s
00372 ,   size_t                  cch
00373 );
00374 
00388 CSTRING_EXTERN_C CSTRING_RC cstring_createN(
00389     struct cstring_t*       pcs
00390 ,   char                    ch
00391 ,   size_t                  n
00392 );
00393 
00394 
00415 CSTRING_EXTERN_C CSTRING_RC cstring_createEx(
00416     struct cstring_t*       pcs
00417 ,   char const*             s
00418 ,   cstring_flags_t         flags
00419 ,   void*                   arena
00420 ,   size_t                  capacity
00421 );
00422 
00445 CSTRING_EXTERN_C CSTRING_RC cstring_createLenEx(
00446     struct cstring_t*       pcs
00447 ,   char const*             s
00448 ,   size_t                  cch
00449 ,   cstring_flags_t         flags
00450 ,   void*                   arena
00451 ,   size_t                  capacity
00452 );
00453 
00464 CSTRING_EXTERN_C CSTRING_RC cstring_destroy(
00465     struct cstring_t*       pcs
00466 );
00467 
00468 
00487 CSTRING_EXTERN_C CSTRING_RC cstring_yield2(
00488     struct cstring_t*       pcs
00489 ,   char**                  pPayload
00490 ,   void**                  pRaw
00491 );
00492 
00493 #ifdef CSTRING_OBSOLETE
00494 CSTRING_EXTERN_C CSTRING_RC cstring_yield(
00495     struct cstring_t*       pcs
00496 ,   char**                  ppBuff
00497 );
00498 #endif /* CSTRING_OBSOLETE */
00499 
00516 CSTRING_EXTERN_C CSTRING_RC cstring_setCapacity(
00517     struct cstring_t*       pcs
00518 ,   size_t                  capacity
00519 );
00520 
00521 
00538 CSTRING_EXTERN_C CSTRING_RC cstring_assign(
00539     struct cstring_t*       pcs
00540 ,   char const*             s
00541 );
00542 
00561 CSTRING_EXTERN_C CSTRING_RC cstring_assignLen(
00562     struct cstring_t*       pcs
00563 ,   char const*             s
00564 ,   size_t                  cch
00565 );
00566 
00579 CSTRING_EXTERN_C CSTRING_RC cstring_copy(
00580     struct cstring_t*       pcs
00581 ,   struct cstring_t const* pcsSrc
00582 );
00583 
00596 CSTRING_EXTERN_C CSTRING_RC cstring_append(
00597     struct cstring_t*       pcs
00598 ,   char const*             s
00599 );
00600 
00619 CSTRING_EXTERN_C CSTRING_RC cstring_appendLen(
00620     struct cstring_t*       pcs
00621 ,   char const*             s
00622 ,   size_t                  cch
00623 );
00624 
00636 CSTRING_EXTERN_C CSTRING_RC cstring_truncate(
00637     struct cstring_t*       pcs
00638 ,   size_t                  len
00639 );
00640 
00653 CSTRING_EXTERN_C CSTRING_RC cstring_swap(
00654     struct cstring_t*       pcs1
00655 ,   struct cstring_t*       pcs2
00656 );
00657 
00658 
00676 CSTRING_EXTERN_C CSTRING_RC cstring_readline(
00677     FILE*               stm
00678 ,   struct cstring_t*   pcs
00679 ,   size_t*             numRead /* = NULL */
00680 );
00681 
00698 CSTRING_EXTERN_C CSTRING_RC cstring_writeline(
00699     FILE*                   stm
00700 ,   struct cstring_t const* pcs
00701 ,   size_t*                 numWritten /* = NULL */
00702 );
00703 
00704 
00710 CSTRING_EXTERN_C CSTRING_RC cstring_insert(
00711     struct cstring_t*       pcs
00712 ,   int                     index
00713 ,   char const*             s
00714 );
00715 
00721 CSTRING_EXTERN_C CSTRING_RC cstring_insertLen(
00722     struct cstring_t*       pcs
00723 ,   int                     index
00724 ,   char const*             s
00725 ,   size_t                  cch
00726 );
00727 
00742 CSTRING_EXTERN_C CSTRING_RC cstring_replace(
00743     struct cstring_t*       pcs
00744 ,   int                     index
00745 ,   size_t                  len
00746 ,   char const*             s
00747 );
00748 
00764 CSTRING_EXTERN_C CSTRING_RC cstring_replaceLen(
00765     struct cstring_t*       pcs
00766 ,   int                     index
00767 ,   size_t                  len
00768 ,   char const*             s
00769 ,   size_t                  cch
00770 );
00771 
00785 CSTRING_EXTERN_C CSTRING_RC cstring_replaceAll(
00786     struct cstring_t*       pcs
00787 ,   char const*             f
00788 ,   char const*             t
00789 ,   size_t*                 numReplaced /* = NULL */
00790 );
00791 
00792 /* ////////////////////////////////////////////////////////////////////// */
00793 
00794 #ifndef CSTRING_DOCUMENTATION_SKIP_SECTION
00795 typedef int (*cstring_allocFailHandler)(void* pv, size_t cb, cstring_flags_t flags, void* param);
00796 
00797 
00799 CSTRING_EXTERN_C CSTRING_RC cstring_createLenFn(
00800     struct cstring_t*           pcs
00801 ,   char const*                 s
00802 ,   size_t                      cch
00803 ,   cstring_flags_t             flags
00804 ,   void*                       arena
00805 ,   size_t                      capacity
00806 ,   cstring_allocFailHandler    pfnAllocFailHandler
00807 ,   void*                       param
00808 );
00809 
00811 CSTRING_EXTERN_C CSTRING_RC cstring_setCapacityFn(
00812     struct cstring_t*           pcs
00813 ,   size_t                      capacity
00814 ,   cstring_allocFailHandler    pfnAllocFailHandler
00815 ,   void*                       param
00816 );
00817 
00819 CSTRING_EXTERN_C CSTRING_RC cstring_assignFn(
00820     struct cstring_t*           pcs
00821 ,   char const*                 s
00822 ,   cstring_allocFailHandler    pfnAllocFailHandler
00823 ,   void*                       param
00824 );
00826 CSTRING_EXTERN_C CSTRING_RC cstring_assignLenFn(
00827     struct cstring_t*           pcs
00828 ,   char const*                 s
00829 ,   size_t                      cch
00830 ,   cstring_allocFailHandler    pfnAllocFailHandler
00831 ,   void*                       param
00832 );
00833 
00835 CSTRING_EXTERN_C CSTRING_RC cstring_appendFn(
00836     struct cstring_t*           pcs
00837 ,   char const*                 s
00838 ,   cstring_allocFailHandler    pfnAllocFailHandler
00839 ,   void*                       param
00840 );
00841 
00843 CSTRING_EXTERN_C CSTRING_RC cstring_appendLenFn(
00844     struct cstring_t*           pcs
00845 ,   char const*                 s
00846 ,   size_t                      cch
00847 ,   cstring_allocFailHandler    pfnAllocFailHandler
00848 ,   void*                       param
00849 );
00850 #endif /* !CSTRING_DOCUMENTATION_SKIP_SECTION */
00851 
00852 /* /////////////////////////////////////////////////////////////////////////
00853  * Compiler warnings
00854  */
00855 
00856 #if defined(_MSC_VER)
00857 # if _MSC_VER >= 1200
00858 #  pragma warning(push)
00859 # endif /* compiler */
00860 # pragma warning(disable : 4514) /* unreferenced inline function has been removed */
00861 #endif /* compiler */
00862 
00863 /* /////////////////////////////////////////////////////////////////////////
00864  * String Access Shims
00865  */
00866 
00867 #ifdef __cplusplus
00868 
00869 inline char const* c_str_data_a(struct cstring_t const* pcs)
00870 {
00871     return (NULL == pcs || 0u == pcs->len) ? "" : pcs->ptr;
00872 }
00873 inline char const* c_str_data(struct cstring_t const* pcs)
00874 {
00875     return c_str_data_a(pcs);
00876 }
00877 inline size_t c_str_len_a(struct cstring_t const* pcs)
00878 {
00879     return (NULL == pcs) ? 0u : pcs->len;
00880 }
00881 inline size_t c_str_len(struct cstring_t const* pcs)
00882 {
00883     return c_str_len_a(pcs);
00884 }
00885 inline char const* c_str_ptr_a(struct cstring_t const* pcs)
00886 {
00887     return c_str_data_a(pcs);
00888 }
00889 inline char const* c_str_ptr(struct cstring_t const* pcs)
00890 {
00891     return c_str_data_a(pcs);
00892 }
00893 
00894 inline char const* c_str_data_a(struct cstring_t const& cs)
00895 {
00896     return (0u == cs.len) ? "" : cs.ptr;
00897 }
00898 inline char const* c_str_data(struct cstring_t const& cs)
00899 {
00900     return c_str_data_a(cs);
00901 }
00902 inline size_t c_str_len_a(struct cstring_t const& cs)
00903 {
00904     return cs.len;
00905 }
00906 inline size_t c_str_len(struct cstring_t const& cs)
00907 {
00908     return c_str_len_a(cs);
00909 }
00910 inline char const* c_str_ptr_a(struct cstring_t const& cs)
00911 {
00912     return c_str_data_a(cs);
00913 }
00914 inline char const* c_str_ptr(struct cstring_t const& cs)
00915 {
00916     return c_str_data_a(cs);
00917 }
00918 
00919 inline char const *c_str_data_a(CSTRING_RC rc)
00920 {
00921     return cstring_getStatusCodeString(rc);
00922 }
00923 inline char const *c_str_data(CSTRING_RC rc)
00924 {
00925     return cstring_getStatusCodeString(rc);
00926 }
00927 
00928 inline size_t c_str_len_a(CSTRING_RC rc)
00929 {
00930     return cstring_getStatusCodeStringLength(rc);
00931 }
00932 inline size_t c_str_len(CSTRING_RC rc)
00933 {
00934     return cstring_getStatusCodeStringLength(rc);
00935 }
00936 
00937 inline char const *c_str_ptr_a(CSTRING_RC rc)
00938 {
00939     return cstring_getStatusCodeString(rc);
00940 }
00941 inline char const *c_str_ptr(CSTRING_RC rc)
00942 {
00943     return cstring_getStatusCodeString(rc);
00944 }
00945 
00946 # ifndef _STLSOFT_NO_NAMESPACE
00947 namespace stlsoft
00948 {
00949     using ::c_str_data_a;
00950     using ::c_str_len_a;
00951     using ::c_str_ptr_a;
00952     using ::c_str_data;
00953     using ::c_str_len;
00954     using ::c_str_ptr;
00955 
00956 } /* namespace stlsoft */
00957 # endif /* !_STLSOFT_NO_NAMESPACE */
00958 
00959 #endif /* __cplusplus */
00960 
00961 /* /////////////////////////////////////////////////////////////////////////
00962  * Compiler warnings
00963  */
00964 
00965 #if defined(_MSC_VER)
00966 # if _MSC_VER >= 1200
00967 /* #  pragma warning(pop) */
00968 # endif /* compiler */
00969 #endif /* compiler */
00970 
00971 /* /////////////////////////////////////////////////////////////////////////
00972  * Obsolete
00973  */
00974 
00975 #ifdef CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY
00976 # define CSTRING_F_USE_WIN32_GLOBAL_MEMORY      CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY
00977 #endif
00978 
00979 #ifdef CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY
00980 # define CSTRING_F_USE_WIN32_PROCESSHEAP_MEMORY CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY
00981 #endif
00982 
00983 #ifdef CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY
00984 # define CSTRING_F_USE_WIN32_COM_TASK_MEMORY    CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY
00985 #endif
00986 
00987 /* ////////////////////////////////////////////////////////////////////// */
00988 
00989 #endif /* CSTRING_INCL_CSTRING_H_CSTRING */
00990 
00991 /* ////////////////////////////////////////////////////////////////////// */

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