ftp.nice.ch/pub/next/connectivity/news/NewsBase.3.02.s.tar.gz#/NewsBase302.source/MMEdit/crlf.c

This is crlf.c in view mode; [Download] [Up]

/*$Copyright:
 * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
 * Institute for Supercomputing Research
 * All rights reserved.
 * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
 *
 * You may freely copy, distribute and reuse the code in this program under 
 * following conditions.
 * - to include this notice in the source code, if it is to be distributed 
 *   with source code.
 * - to add the file named "COPYING" within the code, which shall include 
 *   GNU GENERAL PUBLIC LICENSE(*).
 * - to display an acknowledgement in binary code as follows: "This product
 *   includes software developed by Recruit Co.,Ltd., ISR."
 * - to display a notice which shall state that the users may freely copy,
 *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
 *   LICENSE(*)
 * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
 *
 *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
$*/
/*  
 *  filter program for processing the end charactor for each line
 *  This routine is for handling \r\n and \n for the convention of both
 *  network and local disk.
 */

#import <stdio.h>
#import <streams/streams.h>

//#define		TEST

#ifdef TEST

/*  main routine for testing
 *     crlf {-lf, -crlf}
 *
 *	input: standard in
 *	output: standard out
 */
main(int argc, char **argv)
{
    void tolf(NXStream *in, NXStream *out);
    void tocrlf(NXStream *in, NXStream *out);
    NXStream *in, *out;
    int		stdInFileDesc, stdOutFileDesc;
    
    if (argc != 2) {
	fprintf(stderr,"Usage: crlf {-lf, -crlf}\n");
	exit(1);
    }
    
    /* get data from stdin */
    stdInFileDesc = fileno(stdin);
    stdOutFileDesc = fileno(stdout);
    
    in = NXOpenFile(stdInFileDesc, NX_READONLY);
    out = NXOpenFile(stdOutFileDesc, NX_WRITEONLY);
    
    ++argv;	/*skip first arg, because first one is command itself */
    
    if (strcmp("-lf", *argv) == 0) {
	tolf(in, out);
	NXClose(in); NXClose(out);
    } else if (strcmp("-crlf", *argv) == 0) {
	tocrlf(in, out);
	NXClose(in); NXClose(out);
    } else {
	fprintf(stderr, "Usage: invalid option\n");
    }
    
    return (0);
}

#endif /*TEST*/

/*  process the end of each line
 *     \n    -->  \n
 *     \r\n  -->  \n
 *
 *     	NXStream *in:  input stream 
 *	NXStream *out:  output stream 
 *	(in, out must be allocated in caller routine)
 *	return:  none
 */
void tolf(NXStream *in, NXStream *out)
{
    int		ch1, ch2;
    
    if ((ch1 = NXGetc(in)) == EOF) {
	return;
    }

    while ((ch2=NXGetc(in)) != EOF) {
    	if (ch1 == '\r' && ch2 =='\n') {
	    NXPutc(out, '\n');
	    if ((ch1 = NXGetc(in)) == EOF) {
		return;
	    }
	} else {
	    NXPutc(out, ch1);
	    ch1 = ch2;
	}
    }
    NXPutc(out, ch1);
    return;
}

/*  process the end of each line
 *     \n    -->  \r\n
 *     \r\n  -->  \r\n
 *
 *     	NXStream *in:  input stream 
 *	NXStream *out:  output stream 
 *	(in, out must be allocated in caller routine)
 *	return: none
 */
void tocrlf(NXStream *in, NXStream *out)
{
    int		ch1, ch2;
    
    if ((ch1 = NXGetc(in)) == EOF) {
	return;
    } else if (ch1 == '\n') {
	NXPutc(out, '\r');
	NXPutc(out, '\n');
    } else {
	NXPutc(out, ch1);
    }        

    while ((ch2=NXGetc(in)) != EOF) {
    	if (ch1 != '\r' && ch2 == '\n') {
	    NXPutc(out, '\r');
	    NXPutc(out, '\n');
	} else {
	    NXPutc(out, ch2);
	}
	ch1 = ch2;
    }

    return;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.