📄 arcrunch.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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <roots.h>
/* ############################################################################# */
/* # Convert rectangular structure to output stream # */
/* # # */
/* # CrunchRectangle crunches the iput rectange of dimension # */
/* # cpl*nline to nline=1 and cpl the total byte count. # */
/* # # */
/* # The routine removes trailing blanks, will insert tabs, and will # */
/* # apppend <CR> <LF>. # */
/* # # */
/* # The routine will set nline=1 and cpl to the total length. # */
/* # # */
/* # We make no attempt to minimize the output storage space; # */
/* # # */
/* ############################################################################# */
long CopyWithTabs(BYTE sout[],BYTE sin[], long incpl,long tab);
BYTE CrunchRectangle(BlockCplNline* rect, /* ASCII block */
long tabdist /* tab distance to use */
)
/* ---------------------------------------------------------------------------- */
{ /* CrunchRectangle START */
/* ---------------------------------------------------------------------------- */
/* local data */
ByteArray out={NULL,0};
long outcpl=0,i;
/* open output block to theoretical limit */
ExpandByte(out,((rect->cpl+2)*rect->nline));
for(i=1; i<=rect->nline; i++) outcpl+=CopyWithTabs(out.array+outcpl,
BlockPointer(*rect,i),rect->cpl,tabdist);
free(rect->block);
rect->block=out.array;
rect->cpl=outcpl;
rect->nline=1;
rect->nAlloc=out.nAlloc;
return 0;
/* ---------------------------------------------------------------------------- */
} /* CrunchRectangle END */
/* ---------------------------------------------------------------------------- */
long CopyWithTabs(BYTE sout[], BYTE sin[],long incpl,long tab)
/* ---------------------------------------------------------------------------- */
{ /* CopyWithTabs START */
/* ---------------------------------------------------------------------------- */
long c,i,j,insert,lastchar;
/* set counters */
insert=-1; /* spot to hold the next tab */
j=0; /* output index */
lastchar=-1; /* index of last non-blank output character */
/* scan all characters */
for(i=0; i<incpl; ++i)
{
if(sin[i]=='\0') sin[i]=' ';
c=sin[i];
/* record position of last non-blank. */
/* insert a tab when we reach an even multiple of "tab". */
/* maintain the "insert" pointer so that we know were to put the tab */
/* when we reach one */
/* establish an insertion place: xxxxxxxx' ' */
if(c==' ' && insert==-1)
{
insert=j;
sout[j++]=(BYTE)c;
continue;
}
/* store non-blank in: xxxxx'c' */
if(c!=' ' && insert==-1)
{
sout[j++]=(BYTE)c;
lastchar=j-1;
continue;
}
/* store a tab ahead of current c */
if(insert>=0 && (i%tab)==0)
{
j=insert;
sout[j++]='\t';
sout[j++]=(BYTE)c;
if(c==' ') insert=j-1; else {insert=-1; lastchar=j-1;}
continue;
}
/* otherwise store current c and set flags as shown */
if(c!=' '){lastchar=j; insert=-1;}
sout[j++]=(BYTE)c;
}
/* ---------------------------------- */
/* done - append trailing data */
/* ---------------------------------- */
j=lastchar+1;
#ifndef UNIX
sout[j++]='\r'; /* leave off for UNIX */
#endif
sout[j++]='\n';
return j;
/* ---------------------------------------------------------------------------- */
} /* CopyWithTabs END */
/* ---------------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -