Technical Notes
Overview
#include
#include
#include
#include
#include
#define LF 10
#define CR 13
char buf1[80]; /* source buffer */
char buf2[320]; /* destination buffer */
char crlf[] = "\r\n"; /* insert string */
char sfname[80], ch, dfname[80], ftemp[13];
int inhandle, outhandle; /* file handles */
int bufstat; /* status of buffer copies */
int nbytes;
unsigned int srcsize, destsize = sizeof(buf2)-1, insize = sizeof(crlf)-1;
void *srcbuf = buf1, *destbuf = buf2;
void *inbuf = crlf;
char msg[] = "LF2CRLF Converts single linefeeds into carriage return/linefeed pairs.\r\n\
SYNTAX: LF2CRLF [path]filename.ext\r\n\
NOTES: This utility converts linefeeds(LF) in the source\r\n\
file to carriage return/linefeeds (CR/LF) in the\r\n\
destination file. CR/LF pairs from the source file\r\n\
are not modified. The destination filename will be\r\n\
filename.NEW\r\n";
int main()
{
if (!_arg_count())
{
_put_str(msg);
return;
}
inhandle = _openex_h(_arg_next(sfname, &ch),O_RDONLY);
if (inhandle == -1)
{
_put_str("Unable to open file ");
_put_str(sfname);
return;
}
_split_path(dfname, sfname);
_split_fname(ftemp, sfname);
_str_cat(dfname, ftemp); /* copy path and filename */
_str_cat(dfname, ".new"); /* append extension */
outhandle = _open_h(dfname, O_RDWR+O_CREAT+O_TRUNC);
if (outhandle == -1)
_put_str("Unable to open output file");
nbytes = _read_h(inhandle, buf1, sizeof(buf1)-1);
srcsize = nbytes;
while (nbytes != 0)
{
bufstat = _buf_ccpy(&srcbuf, &srcsize, &destbuf, &destsize, LF);
if (bufstat == 1) /* is source buffer empty? */
{ /* y: feed it */
nbytes = _read_h(inhandle, buf1, sizeof(buf1)-1);
srcbuf = buf1;
srcsize = nbytes;
}
if (bufstat == -1) /* is destination buffer full? */
{ /* y: flush it */
_write_h(outhandle, buf2, sizeof(buf2)-1);
destbuf = buf2;
destsize = sizeof(buf2)-1;
}
if (bufstat == 0) /* has a linefeed been found? */
{ /* y: CR\LF already exist? */
if (*(char*)((char *)destbuf-2) != CR) /* n: don't copy CR */
{ /* y: insert CR in destination buffer */
((char*)destbuf)--;
destsize--;
bufstat = _buf_cpy(&inbuf, &insize, &destbuf, &destsize);
inbuf = crlf;
insize = sizeof(crlf)-1;
if (bufstat == 0) /* did CR\LF fill destination buffer? */
{ /* y: flush it */
_write_h(outhandle, buf2, sizeof(buf2)-1);
destbuf = buf2;
destsize = sizeof(buf2)-1;
_buf_cpy(&inbuf, &insize, &destbuf, &destsize);
}
}
}
}
destsize = sizeof(buf2)-1 - destsize;
_write_h(outhandle, buf2, destsize); /* flush destination buffer */
return;
}
A sample program similar to the above is provided on the distribution diskettes as REPLACE.C and may be compiled and linked using the following Microsoft C and Borland C command lines:
Microsoft C:
cl /c /I\msc\include /I\sa\include replace.c
link replace,replace,,\sa\lib\_sas \msc\lib\slibce
Borland C:
bcc /c /ms /I\bc\include /I\sa\include replace.c
tlink \bc\lib\c0s replace,replace,,\sa\lib\_sas \bc\lib\cs