aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/stxtools/sbuf.x
blob: a5bead5278a929f76a627740aeff070c73bb73ee (plain) (blame)
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
107
108
109
110
include "sbuf.h"

#---------------------------------------------------------------------------
.help sbuf Mar93 source
.ih
NAME
.nf
sb_open   -- Open an sbuf.
sb_cat    -- Add a string to the end of sbuf.
sb_close  -- Close an sbuf.
sb_string -- Get the string to an sbuf.
.fi
.ih
USAGE
.nf
sb = sb_open()
call sb_cat (sb, str)
call sb_close (sb)
str_ptr = sb_string (sb)
.fi
.ih
ARGUMENTS
.ls sb (pointer :input/output)
The string buffer descriptor.
.le
.ls str (char[ARB] :input)
The string to append to the string buffer.
.le
.ls str_ptr (pointer :output)
A pointer to a string array containing the contents of the string buffer.
When done, the user is required to deallocate this memory using the call
"call mfree (str_ptr, TY_CHAR)".
.le
.ih
DISCUSSION
This interface allows one to handle arbitrarily long strings without
having to worry about the memory management.

There may be other utility routines to add; feel free to do so.
.endhelp
#---------------------------------------------------------------------------
pointer procedure sb_open

pointer sb                      # The sbuf pointer

errchk  malloc

begin
        call malloc (sb, SB_SZ_SB, TY_STRUCT)
        call malloc (SB_PTR(sb), SZ_LINE, TY_CHAR)
        SB_LEN(sb) = 0
        SB_MAXLEN(sb) = SZ_LINE
        
        return (sb)
end
#---------------------------------------------------------------------------
# End of sb_open
#---------------------------------------------------------------------------
procedure sb_close (sb)

pointer sb                      # IO: The sbuf descriptor, NULL on exit.

errchk  mfree

begin
	if (sb != NULL) {
	    call mfree (SB_PTR(sb), TY_CHAR)
	    call mfree (sb, TY_STRUCT)
	}
end
#---------------------------------------------------------------------------
# End of sb_close
#---------------------------------------------------------------------------
pointer procedure sb_string (sb)

pointer sb                      # I:  The sbuf descriptor.

pointer str                     # New string pointer.

begin
        call malloc (str, SB_LEN(sb), TY_CHAR)
        call strcpy (SB_BUF(sb,0), Memc[str], SB_LEN(sb))

        return (str)
end
#---------------------------------------------------------------------------
# End of sb_string
#---------------------------------------------------------------------------
procedure sb_cat (sb, str)

pointer sb                      # I: The sbuf descriptor.
char    str[ARB]                # I:  The string to concatenate.

int     i, strlen()             # Length of input string.

errchk  realloc

begin
        i = strlen (str)
        if (i + SB_LEN(sb) >= SB_MAXLEN(sb)) {
            SB_MAXLEN(sb) = SB_MAXLEN(sb) + i + SZ_LINE
            call realloc (SB_PTR(sb), SB_MAXLEN(sb), TY_CHAR)
        }

        call strcpy (str, SB_BUF(sb,SB_LEN(sb)), i)
        SB_LEN(sb) = SB_LEN(sb) + i
end
#---------------------------------------------------------------------------
# End of sb_cat
#---------------------------------------------------------------------------