-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrb_string.c
More file actions
107 lines (86 loc) · 2.67 KB
/
rb_string.c
File metadata and controls
107 lines (86 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -MODULE----------------------------------------------------------------------
RapidBATCH
Copyright (C) 2008, 2009 by Phorward Software Technologies, Jan Max Meyer
http://www.phorward-software.com ++ mail@phorward-software.com
File: rb_string.c
Author: Jan Max Meyer
Usage: Provides functions for rbstring-datatype management and manipulation
----------------------------------------------------------------------------- */
/*
* Includes
*/
/* #define __WITH_TRACE */
#include "rb_global.h"
/*
* Global variables
*/
/*
* Functions
*/
/* -FUNCTION--------------------------------------------------------------------
Function: rb_str_pool_append()
Author: Jan Max Meyer
Usage: Inserts a string into an array (pool) of strings, and (re)-
allocates memory, if required. If the string is already
within the pool, it is not inserted, and a pointer to the
existing string will be returned.
Parameters: char*** pool Pointer to the string pool
char* insert The string to be inserted
pboolean do_copy Copy 'insert' if it is
engaged into the pool
Returns: char* Returns the pointer to the
string. (char*)NULL will
be returned on error case.
If do_copy is FALSE, and
the return value is equal
to insert, than, in dynami-
cally allocated strings,
the pointer must not be
free'd.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
char* rb_str_pool_append( char*** pool, char* insert, pboolean do_copy )
{
char** p;
unsigned int cnt;
PROC( "rb_str_pool_append" );
PARMS( "pool", "%p", pool );
PARMS( "insert", "%s", insert );
PARMS( "do_copy", "%d", do_copy );
if( !( pool && insert ) )
{
MSG( "pool is null!" );
RETURN( (char*)NULL );
}
for( p = *pool, cnt = 0; p && *p; p++, cnt++ )
{
VARS( "*p", "%s", *p );
if( !strcmp( *p, insert ) )
break;
}
if( !( p && *p ) )
{
VARS( "cnt", "%d", cnt );
if( !( cnt % MALLOC_STEP ) )
{
MSG( "reallocation required" );
VARS( "( cnt + MALLOC_STEP + 1 )", "%d",
( cnt + MALLOC_STEP + 1 ) );
if( !( *pool = (char**)prealloc( (char**)*pool,
( cnt + MALLOC_STEP + 1 ) * sizeof( char* ) ) ) )
{
MSG( "Can't reserve storage - out of memory?" );
RETURN( (char*)NULL );
}
p = *pool + cnt;
VARS( "p", "%p", p );
VARS( "*p", "%p", *p );
memset( p, 0, ( MALLOC_STEP + 1 ) * sizeof( char* ) );
}
if( do_copy )
insert = pstrdup( insert );
*p = insert;
}
RETURN( *p );
}