Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/JString.c

Go to the documentation of this file.
00001 #ifndef __J2K__JSTRING_C__
00002 #define __J2K__JSTRING_C__
00003 
00004 /*
00005 http://www.cygnus.com/~manfredh/libgxx/libg++-2.8.1.3.tar.gz
00006 http://www.europe.redhat.com/documentation/HOWTO/C++Programming-HOWTO-15.php3
00007 http://www.mike95.com/c_plusplus/classes/JString/
00008 
00009 This new version of JString 
00010 is an enhanced and more optimized version 
00011 based on libg++ 2.8.1.3 string algorithm
00012 which is also LGPL licensed.
00013 
00014 Copyright (C) 1988 Free Software Foundation - LGPL licensed.
00015 written by Doug Lea (dl@rocky.oswego.edu)
00016 
00017 Refer to license.txt for the complete LGPL license documentation.
00018 */
00019 
00020 #include <j2k/Fred/fast_alloc.c>
00021 
00022  /* Internal String representations */
00023 typedef struct _JString_t   
00024 {
00025   size_t   bufferSize;   /* Allocated Buffer Space                         */
00026   size_t   length;       /* The Actual String Length                       */
00027   short    nbRefs;       /* Reference Counter                              */
00028   char     lock;         /* Lock, if direct access (CString compatibility) */
00029 
00030   char     data[1];      /* The string is at least '\0', so one null byte  */
00031                          /* It must be allocated/expanded via realloc()    */
00032 } JString_t;
00033 
00034 
00035 typedef struct _JSubString_t
00036 {
00037   size_t         pos;    /* Starting position of the string in rep         */
00038   size_t         len;    /* Length of the substring                        */
00039   JString_t*     str;    /* The String I'm a substring of                  */
00040  _JSubString_t*  next;   /* For a link-list of JString parts...            */
00041 
00042 } JSubString_t;
00043 
00044 #define JSTRING_SIZE     ( sizeof( JString_t    ) )
00045 #define JSUBSTRING_SIZE  ( sizeof( JSubString_t ) )
00046 
00047 #define JSTRING_INIT     { 0, 0, 1, 0, { 0 } }
00048 #define JSUBSTRING_INIT  { 0, 0, 0, 0 }
00049 
00050 
00051 /* Primitive operations on JString_t -- nearly all JString functions use those. */
00052 
00053 JString_t* JString_malloc( register size_t  buffer_size )
00054 {
00055   buffer_size += sizeof( JString_t );
00056   return (JString_t*)fast_malloc( buffer_size );
00057 }
00058 
00059 JString_t* JString_calloc( register size_t  buffer_size )
00060 {
00061   buffer_size += sizeof( JString_t );
00062   JString_t* s = (JString_t*)fast_malloc( buffer_size );
00063   memset( s, 0, buffer_size );
00064   s->nbRefs = 1;
00065   return s;
00066 }
00067 
00068 void JString_free( JString_t* this )
00069 {
00070   /* delete if not used and unlock */
00071   if( --this->nbRefs <= 0  &&  this->lock == 0 )
00072   {
00073     free( this );
00074   }
00075 }
00076 
00077 /* GOTCHA: We assume length is passed as: strlen( str ) if unknown */
00078 JString_t* JString_create( const char* str, register size_t length, register size_t  buffer_size )
00079 {
00080   /* Make sure the buffer size is at least the length */
00081   if ( buffer_size < length ) 
00082   {
00083     buffer_size = length;
00084   }
00085 
00086   JString_t* s = JString_calloc( buffer_size );
00087 
00088   if ( str != NULL )
00089   {
00090     memcpy( s->data, str, length );
00091   }
00092 
00093   return s;
00094 }
00095 
00096 JString_t* JString_safe_create( const char* str )
00097 {
00098   return JString_create( str, strlen( str ), 80 ); 
00099 }
00100 
00101 #endif

Generated on Sun Oct 14 18:46:11 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001