Talion/BRAdderCProgram

From Exalted - Unofficial Wiki

Jump to: navigation, search

Automatically adds the br tag to the end of each line, for easier conversion to wiki format.

/*******************************************************************************
*  Name.......: auto_br_adder.c                                                *
*                                                                              *
*  Description: C program to automatically add <br> before a return.           *
*                                                                              *
*  Author.....: Orginally Scott Brueckner and modified by Martin Harber        *
*                                                                              *
*  Date.......: 02/02/2007                                                     *
*                                                                              *
*  Arguments..: int   argc   = Count of command line arguments                 *
*               char *argv[] = Array of pointers to command line arguments     *
*                                argv[0] = path to executable                  *
*                                argv[1] = name of file to process             *
*                                                                              *
*  Return.....: int: 0 = normal completion                                     *
*                    1 = error occurred                                        *
*                                                                              *
*  Compilers..: Visual C++ 6.0   (Win32)                                       *
*                                                                              *
*  Notes......: This program opens the text file specified in argv[1]. It      *
*               stores the results in a file named output_br.txt               *
*		Some code based on an example read.c by Scott Brueckner	       *
*                                                                              *
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define CR 13            /* Decimal code of Carriage Return char */
#define LF 10            /* Decimal code of Line Feed char */
#define [[EOF_MARKER]] 26    /* Decimal code of DOS end-of-file marker */
#define [[MAX_REC_LEN]] 1024 /* Maximum size of input buffer */

int [[ReadAndBRFile]](FILE *input, FILE *output);
void [[FPrintLine]](char *sz[[ReadLine]], long lSize, FILE *output);
void syntax(void);

/******************************************************************************/
int main(int argc, char *argv[])
/******************************************************************************/
{
	char *outputName = "output_br.txt";
	FILE *input[[FilePtr]];            /* Pointer to input file */
	FILE *output[[FilePtr]];		   /* Pointer to output file */
	
	if (argc < 2)                  /* All arguments are required */
	{
		syntax();
		return 1;
	}

	input[[FilePtr]] = fopen(argv[1], "r");
	
	if (input[[FilePtr]] == NULL )             /* Could not open file */
	{
		printf("Error opening %s: %s (%u)\n", argv[1], strerror(errno), errno);
		return 1;
	}

	output[[FilePtr]] = fopen(outputName, "w");
	if (!output[[FilePtr]]) {
		printf("There was a problem opening %s for writing\n", outputName);
	}


	[[ReadAndBRFile]](input[[FilePtr]], output[[FilePtr]]);

	fclose(input[[FilePtr]]);
	fclose(output[[FilePtr]]);
}

/******************************************************************************/
int [[ReadAndBRFile]](FILE *input, FILE *output) /* Use: Read text file using     */
					     /*	     fread()		      */
				             /*				      */
                                             /* Arguments: FILE *input	      */
                                             /*      Pointer to input file    */
                                             /*				      */
                                             /* Return:    int                */
                                             /*              0 = error        */
                                             /*              1 = success      */
/******************************************************************************/
{
	long  l[[FileLen]];               /* Length of file */
	long  lIndex;                 /* Index into c[[ThisLine]] array */
	char  c[[ThisLine]][[[MAX_REC_LEN]]]; /* Contents of current line */
	char *cFile;                  /* Dynamically allocated buffer (entire file) */
	char *c[[ThisPtr]];               /* Pointer to current position in cFile */



	fseek(input, 0L, [[SEEK_END]]);     /* Position to end of file */
	l[[FileLen]] = ftell(input);        /* Get file length */
	rewind(input);                  /* Back to start of file */


	cFile = calloc(l[[FileLen]] + 1, sizeof(char));

	if (cFile == NULL )
	{
		printf("\nInsufficient memory to read file.\n");
		return 0;
	}

	fread(cFile, l[[FileLen]], 1, input); /* Read the entire file into cFile */

	c[[ThisPtr]]    = cFile;              /* Point to beginning of array */

	lIndex    = 0L;                 /* Reset counters and flags */
	while (*c[[ThisPtr]])                 /* Read until reaching null char */
	{
		
		if (*c[[ThisPtr]] == CR || *c[[ThisPtr]] == LF) 
		{
			c[[ThisLine]][lIndex++] = '<';
			c[[ThisLine]][lIndex++] = 'B';
			c[[ThisLine]][lIndex++] = 'R';
			c[[ThisLine]][lIndex++] = '>';
			c[[ThisLine]][lIndex++] = '\x0D';
			c[[ThisLine]][lIndex] = '\x0A';
			*c[[ThisPtr]]++;
			if(*c[[ThisPtr]])
			{
				if(*c[[ThisPtr]] == CR || *c[[ThisPtr]] == LF)
				{
					*c[[ThisPtr]]++;
				}
			}
			[[FPrintLine]](c[[ThisLine]], lIndex, output);
			lIndex    = 0L;                 /* Reset counters and flags */
		} 
		else 
		{
			c[[ThisLine]][lIndex++] = *c[[ThisPtr]]++; /* Add char to output and increment */
		}
	}
	
	
	return 1;
}

/******************************************************************************/
void [[FPrintLine]](char *sz[[ReadLine]], long lSize, FILE *output)
/******************************************************************************/
{
	char *cLine = calloc(lSize+1, sizeof(char));
	strncpy(cLine, sz[[ReadLine]], lSize);
	fprintf(output, "%s", cLine);
}

/******************************************************************************/
void syntax(void) /* Print correct command line syntax                        */
/******************************************************************************/
{
  printf("\nSyntax: auto_br_adder [[FileName]]");
  return;
} /* end syntax() */
Personal tools