2 blk - generate or modify block values
6 blk(val [, len, chunk]);
11 val non-null string, block, or named block
13 return block or named block
16 With only integer arguments, blk(len, chunk) attempts to
17 allocate a block of memory consisting of len octets (unsigned 8-bit
18 bytes). Allocation is always done in multiples of chunk
19 octets, so the actual allocation size of len rounded up
20 to the next multiple of chunk.
22 The default value for len is 0. The default value for chunk is 256.
24 If the allocation is successful, blk(len, chunk) returns a value B, say,
25 for which the octets in the block may be referenced by B[0], B[1],
26 ... , B[len-1], these all initially having zero value.
28 The octets B[i] for i >= len always have zero value. If B[i] with
29 some i >= len is referenced, size(B) is increased to i + 1. For example:
33 has an effect like that of two operations on a file stream fs:
47 The value of chunk is stored as the "chunksize" for B.
49 The size(B) builtin returns the current len for the block; sizeof(B)
50 returns its maxsize; memsize(B) returns maxsize + overhead for any block
51 value. Also size(B) is analogous to the length of a file stream in that
52 if size(B) < sizeof(B):
56 will append one octet to B and increment size(B).
58 The builtin test(B) returns 1 or 0 according as at least one octet
59 is nonzero or all octets are zero. If B1 and B2 are blocks, they are
60 considered equal (B1 == B2) if they have the same length and the
61 same data, i.e. B1[i] == B2[i] for 0 <= i < len. Chunksizes
62 and maxsizes are ignored.
64 The output for print B occupies two lines, the first line giving
65 the chunksize, number of octets allocated (len rounded up to the
66 next chunk) and len, and the second line up to 30 octets of data.
67 If the datalen is zero, the second line is blank. If the datalen
68 exceeds 30, this indicated by a trailing "...".
70 If a block value B created by B = blk(len, chunk) is assigned to
71 another variable by C = B, a new block of the same structure as B
72 is created to become the value of C, and the octets in B are copied
73 to this new block. A block with possibly different length or
74 chunksize is created by C = blk(B, newlen, newchunk), only the first
75 min(len, newlen) octets being copied from B; later octets are
76 assigned zero value. If omitted, newlen and newchunk default to
77 the current datalen and chunk-size for B. The current datalen,
78 chunksize and number of allocated octets for B may be changed by:
80 B = blk(B, newlen, newchunk).
82 No data is lost if newlen is greater than or equal to the old
85 The memory block allocated by blk(len, chunk) is freed at or before
86 termination of the statement in which this occurred, the memory
87 allocated in B = blk(len, chunk) is freed when B is assigned another
90 With a string str as its first argument, blk(str [, len, chunk])
91 when called for the first time creates a block with str as its
92 name. Here there no restriction on the characters used in str;
93 thus the string may include white space or characters normally used
94 for punctuation or operators. Any subsequent call to blk(str, ...)
95 with the same str will refer to the same named block.
97 A named block is assigned length and chunksize and consequent
98 maximum size in the same way as unnamed blocks. A major difference
99 is that in assignments, a named block is not copied. Thus, if a
100 block A has been created by:
108 will give a second variable B referring to the same block as A.
109 Either A[i] = x or B[i] = x may then be used to assign a value
110 to an octet in the block. Its length or chunksize may be changed by
115 A = blk(A, len, chunk);
117 null(blk(A, len, chunk)).
119 These have the same effect on A; when working interactively, the
120 last two avoid printing of the new value for A.
122 Named blocks are assigned index numbers 0, 1, 2, ..., in the order
123 of their creation. The block with index id is returned by blocks(id).
124 With no argument, blocks() returns the number of current unfreed
127 The memory allocated to a named block is freed by the blkfree()
128 function with argument the named block, its name, or its id number.
129 The block remains in existence but with a null data pointer,
130 its length and size being reduced to zero. A new block of memory
131 may be allocated to it, with possibly new length and chunksize by:
133 blk(val [, len, chunk])
135 where val is either the named block or its name.
137 The printing output for a named block is in three lines, the first
138 line displaying its id number and name, the other two as for an
139 unnamed block, except that "NULL" is printed if the memory has been
142 The identifying numbers and names of the current named blocks are
146 If A and B are named blocks, A == B will be true only if they refer
147 to the same block of memory. Thus, blocks with the same data and
148 datalen will be considered unequal if they have different names.
150 If A is a named block, str(A) returns the name of the block.
152 Values may be assigned to the early octets of a named or unnamed
153 block by use of = { } initialization as for matrices.
161 chunksize = 10, maxsize = 20, datalen = 15
162 00000000000000ff00000000000000
166 chunksize = 10, maxsize = 20, datalen = 18
167 00000000000000ff0000000000000000007f
170 Index out of bounds for block
172 ; print size(B), sizeof(B)
175 ; B = blk(B, 100, 20)
177 chunksize = 20, maxsize = 120, datalen = 100
178 00000000000000ff0000000000000000007f000000000000000000000000...
180 ; C = blk(B, 10} = {1,2,3}
182 chunksize = 20, maxsize = 20, datalen = 10
188 chunksize = 256, maxsize = 256, datalen = 0
195 chunksize = 256, maxsize = 256, datalen = 18
196 00000000000000ff0000000000000000007f
201 chunksize = 256, maxsize = 1024, datalen = 1000
202 00000000000000ff0000000000000000007f000000000000000000000000...
207 chunksize = 16, maxsize = 1008, datalen = 1000
208 00000000000000ff0000000000000000007f000000000000000000000000...
216 BLOCK *blkalloc(int len, int chunk)
217 void blk_free(BLOCK *blk)
218 BLOCK *blkrealloc(BLOCK *blk, int newlen, int newchunk)
219 void blktrunc(BLOCK *blk)
220 BLOCK *blk_copy(BLOCK *blk)
221 int blk_cmp(BLOCK *a, BLOCK *b)
222 void blk_print(BLOCK *blk)
223 void nblock_print(NBLOCK *nblk)
224 NBLOCK *reallocnblock(int id, int len, int chunk)
225 NBLOCK *createnblock(char *name, int len, int chunk)
226 int findnblockid(char * name)
227 int removenblock(int id)
228 int countnblocks(void)
229 void shownblocks(void)
230 NBLOCK *findnblock(int id)
231 BLOCK *copyrealloc(BLOCK *blk, int newlen, int newchunk)
236 ## Copyright (C) 1999-2006 Landon Curt Noll
238 ## Calc is open software; you can redistribute it and/or modify it under
239 ## the terms of the version 2.1 of the GNU Lesser General Public License
240 ## as published by the Free Software Foundation.
242 ## Calc is distributed in the hope that it will be useful, but WITHOUT
243 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
244 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
245 ## Public License for more details.
247 ## A copy of version 2.1 of the GNU Lesser General Public License is
248 ## distributed with calc under the filename COPYING-LGPL. You should have
249 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
250 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
252 ## @(#) $Revision: 30.1 $
253 ## @(#) $Id: blk,v 30.1 2007/03/16 11:10:42 chongo Exp $
254 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/blk,v $
256 ## Under source code control: 1997/04/05 13:07:13
257 ## File existed as early as: 1997
259 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
260 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/