home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume9 / bitstring next >
Text File  |  1987-04-19  |  9KB  |  368 lines

  1. Subject: "Bitstring" package
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror
  4.  
  5. Submitted by: vixie!paul (Paul Vixie Esq)
  6. Mod.sources: Volume 9, Issue 51
  7. Archive-name: bitstring
  8.  
  9. [  Just the thing to aid your PL/I conversions.  Seriously, the
  10.    "string of bits" abstraction implemented by this package is
  11.    useful.  --r$  ]
  12.  
  13. #! /bin/sh
  14. ##  This is a shell archive.  Remove anything before this line, then unpack
  15. ##  it by saving it into a file and typing "sh file".  To overwrite existing
  16. ##  files, type "sh file -c".  You can also feed this as standard input via
  17. ##  unshar, or by typing "sh <file".  If this archive is complete, you will
  18. ##  see the following message at the end:
  19. #        "End of shell archive."
  20. # Contents:  Makefile bitstring.3 bitstring.h test.c
  21. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  22. if test -f Makefile -a "${1}" != "-c" ; then 
  23.   echo shar: Will not over-write existing file \"Makefile\"
  24. else
  25. echo shar: Extracting \"Makefile\" \(784 characters\)
  26. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  27. X# Makefile for bitstring macros
  28. X# vix 26feb87 [written]
  29. X# vix 25mar87 [added test program]
  30. X#
  31. X
  32. X# INCDIR might be /usr/local/include or /usr/include/local or some
  33. X# variant, but your cc(1) may not be looking there.  If you put it
  34. X# somewhere your cc(1) doesn't usually look for include files, you
  35. X# will have to compile things with -I or this one won't be found.
  36. X
  37. XINCDIR=/usr/include
  38. X
  39. X# MANEXT and MANDIR will vary from system to system, but usually on
  40. X# a BSD you put the man page for foobar in /usr/man/manl/foobar.l.
  41. X# On SysV, start looking in /usr/catman, and after that, you're on
  42. X# your own.
  43. X
  44. XMANEXT=l
  45. XMANDIR=/usr/man/man$(MANEXT)
  46. X
  47. Xall        :    bitstring.3 bitstring.h
  48. X            cp bitstring.3 $(MANDIR)/bitstring.$(MANEXT)
  49. X            cp bitstring.h $(INCDIR)
  50. X
  51. Xtest        :    test.c
  52. X            cc -O -o test test.c
  53. END_OF_Makefile
  54. if test 784 -ne `wc -c <Makefile`; then
  55.     echo shar: \"Makefile\" unpacked with wrong size!
  56. fi
  57. # end of overwriting check
  58. fi
  59. if test -f bitstring.3 -a "${1}" != "-c" ; then 
  60.   echo shar: Will not over-write existing file \"bitstring.3\"
  61. else
  62. echo shar: Extracting \"bitstring.3\" \(2223 characters\)
  63. sed "s/^X//" >bitstring.3 <<'END_OF_bitstring.3'
  64. X.TH BITSTRING 3  "26 March 1987"
  65. X.UC 4
  66. X.SH NAME
  67. Xbit_decl, bit_ref, bit_test, bit_set, bit_clear,
  68. Xbit_setall, bit_clearall \- bit-string manipulation macros for C
  69. X.SH SYNOPSIS
  70. X.nf
  71. X.B #include <bitstring.h>
  72. X.PP
  73. X.B bit_decl(Name, Nbits)
  74. X.PP
  75. X.B bit_ref(Name)
  76. X.PP
  77. X.B bit_test(Name, BitNum)
  78. X.PP
  79. X.B bit_set(Name, BitNum)
  80. X.PP
  81. X.B bit_clear(Name, BitNum)
  82. X.PP
  83. X.B bit_setall(Name, Nbits)
  84. X.PP
  85. X.B bit_clearall(Name, Nbits)
  86. X.PP
  87. X.fi
  88. X.SH DESCRIPTION
  89. XThese functions operate on strings of bits.  These strings are held in
  90. Xinteger arrays, but this detail is transparent in actual use.
  91. X.PP
  92. X.I Bit_decl
  93. Xdeclares a bit string called
  94. X.I Name
  95. Xas a C variable able to contain
  96. X.I Nbits
  97. Xbits.  This is suitable for actually creating the variable.
  98. X.I Bit_ref
  99. Xcreates a reference to a bit-string called
  100. X.IR Name ;
  101. Xthis is suitable for declaring an external variable, or receiving a
  102. Xbit string as a function argument.
  103. X.PP
  104. X.I Bit_test
  105. Xis an expression that examines bit
  106. X.I BitNum
  107. X(numbered from 0) of string
  108. X.IR Name ,
  109. Xevaluating to a non-zero if the bit is set, zero otherwise.
  110. X.PP
  111. X.I Bit_set
  112. Xand
  113. X.I bit_clear
  114. Xrespectively set and clear bit
  115. X.I BitNum
  116. X(numbered from 0, as above) in string
  117. X.IR Name .
  118. X.PP
  119. X.I Bit_setall
  120. Xand
  121. X.I bit_clearall
  122. Xrespectively set and clear all bits from 0 through
  123. X.I Nbits
  124. X(which must the actual length) of string
  125. X.IR Name .
  126. X.SH AUTHOR
  127. X.nf
  128. XPaul A. Vixie, Esq.
  129. Xucbvax!dual!ptsfa!vixie!paul
  130. Xpaul@vixie.UUCP
  131. X.fi
  132. X.SH EXAMPLE
  133. X.nf
  134. X#include <bitstring.h>
  135. X.PP
  136. Xmain()
  137. X{
  138. X    bit_decl(foobits, 300)
  139. X.PP
  140. X    . . .
  141. X    barfunc(foobits);
  142. X    . . .
  143. X}
  144. X.PP
  145. Xbarfunc(bits)
  146. X    bit_ref(bits)
  147. X{
  148. X    if (bit_test(bits, 25)) {
  149. X        bit_clearall(300)
  150. X        bit_set(bits, 26)
  151. X    }
  152. X}
  153. X.PP
  154. X.fi
  155. X(note: semicolons were not omitted accidentally, above: the macros that
  156. Xgenerate declarations or statements have their own semicolons.)
  157. X.SH BUGS
  158. XGiven the level of abstraction, it is possible to store the length of the
  159. Xstring internally, making it possible to do run-time checking on
  160. X.IR bit_test ,
  161. X.IR bit_set ,
  162. Xand
  163. X.IR bit_clear ,
  164. Xand making it unneccessary to pass the string length to
  165. X.I bit_setall
  166. Xand
  167. X.IR bit_clearall .
  168. XThis should be done as a compile-time option, determined by the value
  169. Xof some macro at the point where <bitstring.h> is included.
  170. END_OF_bitstring.3
  171. if test 2223 -ne `wc -c <bitstring.3`; then
  172.     echo shar: \"bitstring.3\" unpacked with wrong size!
  173. fi
  174. # end of overwriting check
  175. fi
  176. if test -f bitstring.h -a "${1}" != "-c" ; then 
  177.   echo shar: Will not over-write existing file \"bitstring.h\"
  178. else
  179. echo shar: Extracting \"bitstring.h\" \(2652 characters\)
  180. sed "s/^X//" >bitstring.h <<'END_OF_bitstring.h'
  181. X/* bitstring.h - bit string manipulation macros
  182. X * vix 26feb87 [written]
  183. X * vix 03mar87 [fixed stupid bug in setall/clearall]
  184. X * vix 25mar87 [last-minute cleanup before mod.sources gets it]
  185. X */
  186. X
  187. X#ifndef    _bitstring_defined
  188. X#define    _bitstring_defined
  189. X
  190. X/*
  191. X * there is something like this in 4.3, but that's licensed source code that
  192. X * I'd rather not depend on, so I'll reinvent the wheel (incompatibly).
  193. X */
  194. X
  195. X/*
  196. X * except for the number of bits per int, and the other constants, this should
  197. X * port painlessly just about anywhere.  please #ifdef any changes with your
  198. X * compiler-induced constants (check the CC man page, it'll be something like
  199. X * 'vax' or 'mc68000' or 'sun' or some such).  also please mail any changes
  200. X * back to me (ucbvax!dual!ptsfa!vixie!paul) for inclusion in future releases.
  201. X */
  202. X
  203. X/*
  204. X * (constants used internally -- these can change from machine to machine)
  205. X */
  206. X            /*
  207. X             * how many bits in the unit returned by sizeof ?
  208. X             */
  209. X#define    _bit_charsize    8
  210. X
  211. X            /*
  212. X             * what type will the bitstring be an array of ?
  213. X             */
  214. X#define    _bit_type    unsigned int
  215. X
  216. X            /*
  217. X             * how many bits in an int ?
  218. X             */
  219. X#define    _bit_intsiz    (sizeof(_bit_type) * _bit_charsize)
  220. X
  221. X            /*
  222. X             * an int of all '0' bits
  223. X             */
  224. X#define    _bit_0s        ((_bit_type)0)
  225. X
  226. X            /*
  227. X             * an int of all '1' bits
  228. X             */
  229. X#define    _bit_1s        ((_bit_type)~0)
  230. X
  231. X/*
  232. X * (macros used internally)
  233. X */
  234. X    /*
  235. X     * how many int's in a string of N bits?
  236. X     */
  237. X#define    _bit_size(N) \
  238. X    ((N / _bit_intsiz) + ((N % _bit_intsiz) ? 1 : 0))
  239. X
  240. X    /*
  241. X     * which int of the string is bit N in?
  242. X     */
  243. X#define    _bit_intn(N) \
  244. X    ((N) / _bit_intsiz)
  245. X
  246. X    /*
  247. X     * mask for bit N in it's int
  248. X     */
  249. X#define    _bit_mask(N) \
  250. X    (1 << ((N) % _bit_intsiz))
  251. X
  252. X/*
  253. X * (macros used externally)
  254. X */
  255. X    /*
  256. X     * declare (create) Name as a string of N bits
  257. X     */
  258. X#define    bit_decl(Name, N) \
  259. X    _bit_type Name[_bit_size(N)];
  260. X
  261. X    /*
  262. X     * declare (reference) Name as a bit string
  263. X     */
  264. X#define    bit_ref(Name) \
  265. X    _bit_type Name[];
  266. X
  267. X    /*
  268. X     * is bit N of string Name set?
  269. X     */
  270. X#define    bit_test(Name, N) \
  271. X    ((Name)[_bit_intn(N)] & _bit_mask(N))
  272. X
  273. X    /*
  274. X     * set bit N of string Name
  275. X     */
  276. X#define    bit_set(Name, N) \
  277. X    { (Name)[_bit_intn(N)] |= _bit_mask(N); }
  278. X
  279. X    /*
  280. X     * clear bit N of string Name
  281. X     */
  282. X#define    bit_clear(Name, N) \
  283. X    { (Name)[_bit_intn(N)] &= ~_bit_mask(N); }
  284. X
  285. X    /*
  286. X     * set bits 0..N in string Name
  287. X     */
  288. X#define    bit_setall(Name, N) \
  289. X    {    register _bit_i; \
  290. X        for (_bit_i = _bit_size(N)-1; _bit_i >= 0; _bit_i--) \
  291. X            Name[_bit_i]=_bit_1s; \
  292. X    }
  293. X
  294. X    /*
  295. X     * clear bits 0..N in string Name
  296. X     */
  297. X#define    bit_clearall(Name, N) \
  298. X    {    register _bit_i; \
  299. X        for (_bit_i = _bit_size(N)-1; _bit_i >= 0; _bit_i--) \
  300. X            Name[_bit_i]=_bit_0s; \
  301. X    }
  302. X
  303. X#endif    _bitstring_defined
  304. END_OF_bitstring.h
  305. if test 2652 -ne `wc -c <bitstring.h`; then
  306.     echo shar: \"bitstring.h\" unpacked with wrong size!
  307. fi
  308. # end of overwriting check
  309. fi
  310. if test -f test.c -a "${1}" != "-c" ; then 
  311.   echo shar: Will not over-write existing file \"test.c\"
  312. else
  313. echo shar: Extracting \"test.c\" \(756 characters\)
  314. sed "s/^X//" >test.c <<'END_OF_test.c'
  315. X/* test.c - a test jig for bitstring
  316. X * vix 25mar87 [all test programs are messy]
  317. X */
  318. X
  319. X#include <stdio.h>
  320. X#include "bitstring.h"
  321. X
  322. X#define SIZE 50
  323. X
  324. Xmain()
  325. X{
  326. X    char t[10];
  327. X    bit_decl(string, SIZE)
  328. X
  329. X    while (test(string))
  330. X        ;
  331. X}
  332. X
  333. Xstatic test(s)
  334. X    bit_ref(s)
  335. X{
  336. X    int i;
  337. X    char t[10], cmd;
  338. X
  339. X    for (i = 0;  i < SIZE;  i++)
  340. X        putchar(bit_test(s, i) ? '1' : '0');
  341. X    putchar('\n');
  342. X
  343. X    printf("set, clear, Setall, Clearall: "); fflush(stdout);
  344. X    gets(t); if (!t[0]) return 0; else cmd=t[0];
  345. X    if (cmd=='s' || cmd=='c')
  346. X        { printf("\t#"); fflush(stdout); gets(t); i=atoi(t); }
  347. X
  348. X    switch (cmd)
  349. X    {
  350. X    case 's':    bit_set(s, i); break;
  351. X    case 'c':    bit_clear(s, i); break;
  352. X    case 'S':    bit_setall(s, SIZE); break;
  353. X    case 'C':    bit_clearall(s, SIZE); break;
  354. X    default:    return 0;
  355. X    }
  356. X    return 1;
  357. X}
  358. END_OF_test.c
  359. if test 756 -ne `wc -c <test.c`; then
  360.     echo shar: \"test.c\" unpacked with wrong size!
  361. fi
  362. # end of overwriting check
  363. fi
  364. echo shar: End of shell archive.
  365. exit 0
  366.  
  367.  
  368.