📄 csplit.c
字号:
/* TS: eliminate any added trailing spaces */
for (i=strlen(s)-1; i && line[i-1] == ' '; i--) continue;
line[i] = '\n';
line[i+1] = '\0';
lines++;
if (NULL == s)
{
if (feof(Fin)) /* end of file */
{
fclose(Fin);
Fin = NULL;
}
else
{
if (lines) printf("(line %d) ", lines);
printf("Error reading from input file: %s\n", ifn);
return(READ);
}
} /*endif NULL string*/
else /* process line */
{
if (line == (strstr(line , SEP_ID))) /* if separator line */
{
s = line + sep_id_len;
if (s == strstr(s, SEP_BF)) /* if begin file */
{
if (NULL != Fout)
{
printf("(line %d) ", lines);
puts("Error: encountered 2nd \"Begin file\" separator");
puts("before \"End file\" separator\n");
return(PROCESS);
}
s += strlen(SEP_BF);
if (1 != sscanf(s, "%s", outfile))
{
printf("(line %d) ", lines);
puts("Error reading separator line\n");
return(PROCESS);
}
if (NULL != (Fout = fopen(outfile, "rt")))
{
key = 0;
printf("Output file already exists: %s\n", outfile);
do
{
printf("Overwrite? (y/n) ");
key = getchar();
puts("");
i = key;
while (i != '\n')
i = getchar(); /* eat all extra keystrokes */
if (('n' == key) || ('N' == key))
return(OUTFILE);
} while (('y' != key) && ('Y' != key));
if (NULL == freopen(outfile, "wt", Fout))
{
printf("Error opening file for output: %s\n", outfile);
return(OUTFILE);
}
}
else
{
if (NULL == (Fout = fopen(outfile, "wt")))
{
printf("Error opening file for output: %s\n", outfile);
return(OUTFILE);
}
}
crc = updcrc(crc, (unsigned char *)line, strlen(line));
}
else if (s == strstr(s, SEP_EF)) /* if end file */
{
if (NULL == Fout)
{
printf("(line %d) ", lines);
puts("Error: encountered \"End file\" separator");
puts("before \"Begin file\" separator\n");
return(PROCESS);
}
if (fclose(Fout))
{
printf("Error closing output file: %s\n", outfile);
return(OUTFILE);
}
Fout = NULL;
crc = updcrc(crc, (unsigned char *)line, strlen(line));
}
else if (s == strstr(s, SEP_BP)) /* if begin part */
{
if (in_section)
{
printf("(line %d) ", lines);
puts("Error: encountered 2nd \"Begin part\" separator");
puts("before \"End part\" separator\n");
return(PROCESS);
}
s += strlen(SEP_BP);
if (2 != sscanf(s, "%d/%d", &seppart, &sepmax))
{
printf("(line %d) ", lines);
puts("Error reading separator line\n");
return(PROCESS);
}
if (0 == maxpart) maxpart = sepmax;
if (maxpart != sepmax)
{
printf("(line %d) ", lines);
puts("Error reading separator line\n");
return(PROCESS);
}
if (curpart+1 != seppart)
{
printf("(line %d) ", lines);
puts("Error: section(s) missing or out-of-order\n");
return(PROCESS);
}
in_section = TRUE;
curpart++;
}
else if (s == strstr(s, SEP_EP)) /* if end part */
{
if (!in_section)
{
printf("(line %d) ", lines);
puts("Error: encountered 2nd \"End part\" separator");
puts("before \"Begin part\" separator\n");
return(PROCESS);
}
s += strlen(SEP_EP);
s = strstr(s, ": ");
if (1 != sscanf(s+2, "%x", &sepcrc))
{
printf("(line %d) ", lines);
puts("Error reading separator line\n");
return(PROCESS);
}
if (crc != sepcrc)
{
printf("(line %d) ", lines);
puts("CRC error\n");
return(PROCESS);
}
crc = 0;
in_section = FALSE;
}
else
{
printf("(line %d) ", lines);
puts("Error reading separator line\n");
return(PROCESS);
}
}
else /* else process data line */
{
if (in_section) /* save only file data */
{
crc = updcrc(crc, (unsigned char *)line, (i = strlen(line)));
if (pos_wrap) /* if possible line wrap in progress */
{
if (0 == strcmp(line, "\\\n")) /* if wrapped line */
{
strcpy(line, line2);
line[strlen(line)-2] = 0; /* remove wrap EOL */
}
else
{
strcat(line2, line);
strcpy(line, line2);
}
pos_wrap = FALSE;
}
else if ('\\' == line[i-2]) /* if possible wrapped line */
{
strcpy(line2, line);
pos_wrap = TRUE;
}
if ((FALSE == pos_wrap) &&
((NULL == Fout) || (EOF == fputs(line, Fout))))
{
puts("Error writing output\n");
return( NULL == Fout ? PROCESS : WRITE );
}
}
}
} /*endif non-NULL string*/
} /*endwhile*/
/* TS: Test for incompete processing. */
if (in_section) {
printf ("Error on end of input when processing section %d of %d\n",
seppart, sepmax);
return (PROCESS);
}
if (seppart != sepmax) {
printf ("Error on end of input after processing section %d of %d\n",
seppart, sepmax);
return (PROCESS);
}
return(NOERR);
}
/*
* my_fgets() - A custom fgets() function that additionally
* expands tabs and deletes trailing whitespace.
*/
char *my_fgets(char *s, int len, FILE * fp, int tabsiz)
{
static char sbuf[2048] = "", /* big enough for many TAB chars */
*beg = sbuf;
static int wrap = FALSE;
char *e, *w, *p = s, *q;
int ch = 0, cnt = 0, i, spaces;
if (TRUE == wrap) /* if line wrap */
{
strcpy(s, "\\\n");
wrap = FALSE;
return(s);
}
while ((cnt < len-1) && ('\n' != ch))
{ /* get next char from buffer */
if (0 == (ch = *beg++)) /* if buffer empty */
{
beg = fgets(sbuf, 1024, fp); /* grab another string */
if (NULL == beg) /* if end of file */
{
beg = sbuf;
*beg = 0;
if (0 == cnt) return(NULL); /* and buffer empty */
}
else
{
w = e = &sbuf[i = strlen(sbuf)]; /* find 1st trailing ws char */
while ((w > sbuf) && (isspace(*(w-1))))
w--;
if (('\n' == *(e-1)) || /* if terminated w/newline char */
(i < (len-1))) /* or unterminated short line */
*w++ = '\n'; /* terminate with newline char */
*w = 0;
ch = *beg++;
}
} /*endif buffer empty*/
if (ch == '\t') /* if TAB character */
{ /* space to next tab stop */
/* TS: The following code has been changed to pad to the next
** tab stop, rather than insert a fixed number of spaces. */
spaces = tabsiz - ((int)(beg - sbuf) - 1) % tabsiz;
memmove(beg + spaces -1, beg, strlen(beg)+1);
for (q = beg-1; spaces > 0; spaces--)
*q++ = ' ';
ch = ' '; /* change character to space */
}
*p++ = (char) ch; /* update output string */
cnt++;
if ((cnt == len-1) && ('\n' != ch)) /* if need to wrap line */
{
beg -= 2; /* make room for "\\\n" characters */
e = beg;
p -= 2;
q = p;
/* unget characters to 1st previous space */
while ((e > sbuf) && (' ' != *(e-1)))
{
q--;
e--;
}
if (e != sbuf) /* if wrap on space char */
{ /* ( else wrap asis ) */
p = q;
beg = e;
}
*p++ = '\\'; /* terminate current line */
*p++ = '\n';
wrap = TRUE; /* flag wrap line pending */
} /*endif line wrap*/
} /*endwhile*/
*p = 0;
return(s);
}
/*
* CRC-16f.c, from Snippets. Calculate, intelligently, the CRC
* of a dataset incrementally given a buffer full at a time.
* Initialize crc to 0 for XMODEM, -1 for CCITT.
*/
/* P, the CRC polynomial, is used by XMODEM (almost CCITT).
* If you change P, you must change crctab[]'s initial value
* to what is printed by initcrctab().
*/
#define P 0x1021
#define W 16 /* number of bits in CRC: don't change it */
#define B 8 /* number of bits per char: don't change it */
unsigned short crctab[1<<B];
unsigned short updcrc( unsigned short icrc,
unsigned char *icp,
unsigned int icnt )
{
register unsigned short crc = icrc;
register unsigned char *cp = icp;
register unsigned int cnt = icnt;
while( cnt-- )
crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
return( crc );
}
void initcrctab()
{
register b, v, i;
for( b = 0; b <= (1<<B)-1; ++b )
{
for( v = b<<(W-B), i = B; --i >= 0; )
v = v&0x8000 ? (v<<1)^P : v<<1;
crctab[b] = v;
}
}
SLST *addlist(char *fname)
{
SLST *new = NULL;
if (strlen(fname) > FNAME)
printf("Input file argument too long: %s\n", fname);
else if (NULL == (new = (SLST *)malloc(sizeof(SLST))))
puts("Error allocating memory.\n");
else
{
strcpy(new->srcfile, fname);
new->next = NULL;
if (NULL == cur) head = new;
else cur->next = new;
}
cur = new;
return(cur);
}
void freelist(void)
{
while (NULL != head)
{
cur = head->next;
free(head);
head = cur;
}
}
/*
* This function creates a linked list of input source files.
* Wildcard specifications are accommodated when ANSI mode is
* not in effect.
*/
int initlist(int argc, char **argv, int argo)
{
int i;
for (i = argo; i < argc; i++) /* process CL arguments */
{
#if !defined(__STDC__)
int done;
DOSFileData fd;
done = FIND_FIRST(argv[i], 0x20, &fd);
if (done)
{
printf("Error with filespec: %s\n", argv[i]);
return(PROCESS);
}
while (!done)
{
if (NULL == addlist(ff_name(&fd))) /* david nugent */
return(MEMORY);
done = FIND_NEXT(&fd);
}
FIND_END(&fd); /* david nugent */
#else
if (NULL == addlist(argv[i]))
return(MEMORY);
#endif
}
return(NOERR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -