📄 dklaybit.c
字号:
/*
Copyright (c) 2003, Dan Kranz and Arnold Rom
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* The names of its contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* The bit string of length bpl is scanned for like valued bit patterns.
laybit() generates nf field definitions. Each output field points to a
sub-string containing concatenated ones.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <roots.h>
static BYTE bitmask[8] =
{ 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
static BYTE leftones[8] =
{ 0xFF, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
void laybit(void *arg_string, long *bpl, long *fields, long *nf)
{
long i=1, cpl, byte, bit;
int LastBitWasOn=0;
BYTE *string=arg_string;
if (
*bpl < 1 ||
8 != CHAR_BIT)
{
RootsSOS("laybit() sos!");
abort();
}
/* Compute cpl from the input bpl */
cpl = *bpl/CHAR_BIT;
if (*bpl%CHAR_BIT)
++cpl;
/* Clean up the bits to the right of bpl in block! */
byte = *bpl;
byte &= 7;
string[cpl-1] &= leftones[byte];
*nf=0;
/* Look at each byte in string */
for (byte=0; byte < cpl; ++byte) {
/* All bits are off */
if (! string[byte]) {
/* Set the field length if necessary */
if (LastBitWasOn) {
*(fields+1) = i - *fields;
/* Next field */
fields += 2;
LastBitWasOn=0;
}
i += CHAR_BIT;
}
/* Some bits are on */
else {
/* Look at the byte's bits from left to right */
for (bit=0; bit < CHAR_BIT; ++bit, ++i) {
/* This bit is on */
if (string[byte] & bitmask[bit]) {
/* Start a new field definition if necessary */
if (!LastBitWasOn) {
*fields = i;
*nf += 1;
LastBitWasOn=1;
}
}
/* This bit is off */
else {
/* Set the field length if necessary */
if (LastBitWasOn) {
*(fields+1) = i - *fields;
/* Next field */
fields += 2;
LastBitWasOn=0;
}
}
}
}
}
/* Close off the last field definition */
if (LastBitWasOn)
*(fields+1) = i - *fields;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -