Initial revision
[LeanCalc.git] / calc / calc / block.h
1 /*
2  * block - fixed, dynamic, fifo and circular memory blocks
3  *
4  * Copyright (C) 1999  Landon Curt Noll and Ernest Bowen
5  *
6  * Primary author:  Landon Curt Noll
7  *
8  * Calc is open software; you can redistribute it and/or modify it under
9  * the terms of the version 2.1 of the GNU Lesser General Public License
10  * as published by the Free Software Foundation.
11  *
12  * Calc is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
15  * Public License for more details.
16  *
17  * A copy of version 2.1 of the GNU Lesser General Public License is
18  * distributed with calc under the filename COPYING-LGPL.  You should have
19  * received a copy with calc; if not, write to Free Software Foundation, Inc.
20  * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * @(#) $Revision$
23  * @(#) $Id$
24  * @(#) $Source$
25  *
26  * Under source code control:   1997/02/21 05:03:39
27  * File existed as early as:    1997
28  *
29  * chongo <was here> /\oo/\     http://www.isthe.com/chongo/
30  * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
31  */
32
33
34 #if !defined(__BLOCK_H__)
35 #define __BLOCK_H__
36
37
38 /*
39  * block - the basic block structure
40  *
41  * A block comes is one of several types.  At the moment, only fixed
42  * types are defined.
43  *
44  ***
45  *
46  * Block functions and operations:
47  *
48  *      x[i]
49  *              (i-1)th octet
50  *
51  *      blk(len [, blkchunk])
52  *              unnamed block
53  *              len > 0
54  *              blkchunk defaults to BLK_CHUNKSIZE
55  *
56  *      blk(name, [len [, blkchunk]])
57  *              named block
58  *              len > 0
59  *              blkchunk defaults to BLK_CHUNKSIZE
60  *
61  *      blkfree(x)
62  *              Reduce storage down to 0 octetes.
63  *
64  *      size(x)
65  *              The length of data stored in the block.
66  *
67  *      sizeof(x) == blk->maxsize
68  *              Allocation size in memory
69  *
70  *      isblk(x)
71  *              returns 0 is x is not a BLOCK, 1 if x is an
72  *              unnamed block, 2 if x is a named BLOCK
73  *
74  *      blkread(x, size, count, fd [, offset])
75  *      blkwrite(x, size, count, fd [, offset])
76  *              returns number of items written
77  *              offset is restricted in value by block type
78  *
79  *      blkset(x, val, length [, offset])
80  *              only the lower octet of val is used
81  *              offset is restricted in value by block type
82  *
83  *      blkchr(x, val, length [, offset])
84  *              only the lower octet of val is used
85  *              offset is restricted in value by block type
86  *
87  *      blkcpy(dest, src, length [, dest_offset [, src_offset]])
88  *              0 <= length <= blksize(x)
89  *              offset's are restricted in value by block type
90  *              dest may not == src
91  *
92  *      blkmove(dest, src, length [, dest_offset [, src_offset]])
93  *              0 <= length <= blksize(x)
94  *              offset's are restricted in value by block type
95  *              overlapping moves are handeled correctly
96  *
97  *      blkccpy(dest, src, stopval, length [, dest_offset [, src_offset]])
98  *              0 <= length <= blksize(x)
99  *              offset's are restricted in value by block type
100  *
101  *      blkcmp(dest, src, length [, dest_offset [, src_offset]])
102  *              0 <= length <= blksize(x)
103  *              offset's are restricted in value by block type
104  *
105  *      blkswap(x, a, b)
106  *              swaps groups of 'a' octets within each 'b' octets
107  *              b == a is a noop
108  *              b = a*k for some integer k >= 1
109  *
110  *      scatter(src, dest1, dest2 [, dest3 ] ...)
111  *              copy sucessive octets from src into dest1, dest2, ...
112  *                  restarting with dest1 after end of list
113  *              stops at end of src
114  *
115  *      gather(dest, src1, src2 [, src3 ] ...)
116  *              copy first octet from src1, src2, ...
117  *              copy next octet from src1, src2, ...
118  *              ...
119  *              copy last octet from src1, src2, ...
120  *              copy 0 when there is no more data from a given source
121  *
122  *      blkseek(x, offset, {"in","out"})
123  *              some seeks may not be allowed by block type
124  *
125  *      config("blkmaxprint", count)
126  *              number of octets of a block to print, 0 means all
127  *
128  *      config("blkverbose", boolean)
129  *              TRUE => print all lines, FALSE => skip dup lines
130  *
131  *      config("blkbase", "base")
132  *              output block base = { "hex", "octal", "char", "binary", "raw" }
133  *                  binary is base 2, raw is just octet values
134  *
135  *      config("blkfmt", "style")
136  *              style of output = {
137  *                  "line",     lines in blkbase with no spaces between octets
138  *                  "string",   as one long line with no spaces between octets
139  *                  "od_style", position, spaces between octets
140  *                  "hd_style"} position, spaces between octets, chars on end
141  */
142 struct block {
143         LEN blkchunk;   /* allocation chunk size */
144         LEN maxsize;    /* octets actually malloced for this block */
145         LEN datalen;    /* octets of data held this block */
146         USB8 *data;     /* pointer to the 1st octet of the allocated data */
147 };
148 typedef struct block BLOCK;
149
150
151 struct nblock {
152         char *name;
153         int subtype;
154         int id;
155         BLOCK *blk;
156 };
157 typedef struct nblock NBLOCK;
158
159
160 /*
161  * block debug
162  */
163 extern int blk_debug;   /* 0 => debug off */
164
165
166 /*
167  * block defaults
168  */
169 #define BLK_CHUNKSIZE 256       /* default allocation chunk size for blocks */
170
171 #define BLK_DEF_MAXPRINT 256    /* default octets to print */
172
173 #define BLK_BASE_HEX 0          /* output octets in a block in hex */
174 #define BLK_BASE_OCT 1          /* output octets in a block in octal */
175 #define BLK_BASE_CHAR 2         /* output octets in a block in characters */
176 #define BLK_BASE_BINARY 3       /* output octets in a block in base 2 chars */
177 #define BLK_BASE_RAW 4          /* output octets in a block in raw binary */
178
179 #define BLK_FMT_HD_STYLE 0      /* output in base with chars on end of line */
180 #define BLK_FMT_LINE 1          /* output is lines of up to 79 chars */
181 #define BLK_FMT_STRING 2        /* output is one long string */
182 #define BLK_FMT_OD_STYLE 3      /* output in base with chars */
183
184
185 /*
186  * block macros
187  */
188 /* length of data stored in a block */
189 #define blklen(blk) ((blk)->datalen)
190
191 /* block footpint in memory */
192 #define blksizeof(blk) ((blk)->maxsize)
193
194 /* block allocation chunk size */
195 #define blkchunk(blk) ((blk)->blkchunk)
196
197
198 /*
199  * OCTET - what the INDEXADDR produces from a blk[offset]
200  */
201 typedef USB8 OCTET;
202
203
204 /*
205  * external functions
206  */
207 extern BLOCK *blkalloc(int, int);
208 extern void blk_free(BLOCK*);
209 extern BLOCK *blkrealloc(BLOCK*, int, int);
210 extern void blktrunc(BLOCK*);
211 extern BLOCK *blk_copy(BLOCK*);
212 extern int blk_cmp(BLOCK*, BLOCK*);
213 extern void blk_print(BLOCK*);
214 extern void nblock_print(NBLOCK *);
215 extern NBLOCK *createnblock(char *, int, int);
216 extern NBLOCK *reallocnblock(int, int, int);
217 extern int removenblock(int);
218 extern int findnblockid(char *);
219 extern NBLOCK *findnblock(int);
220 extern BLOCK *copyrealloc(BLOCK*, int, int);
221 extern int countnblocks(void);
222 extern void shownblocks(void);
223
224
225 #endif /* !__BLOCK_H__ */