Talion/BRAdderCProgram
From Exalted - Unofficial Wiki
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 *szReadLine, long lSize, FILE *output);
void syntax(void);
/******************************************************************************/
int main(int argc, char *argv[])
/******************************************************************************/
{
char *outputName = "output_br.txt";
FILE *inputFilePtr; /* Pointer to input file */
FILE *outputFilePtr; /* Pointer to output file */
if (argc < 2) /* All arguments are required */
{
syntax();
return 1;
}
inputFilePtr = fopen(argv[1], "r");
if (inputFilePtr == NULL ) /* Could not open file */
{
printf("Error opening %s: %s (%u)\n", argv[1], strerror(errno), errno);
return 1;
}
outputFilePtr = fopen(outputName, "w");
if (!outputFilePtr) {
printf("There was a problem opening %s for writing\n", outputName);
}
ReadAndBRFile(inputFilePtr, outputFilePtr);
fclose(inputFilePtr);
fclose(outputFilePtr);
}
/******************************************************************************/
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 lFileLen; /* Length of file */
long lIndex; /* Index into cThisLine array */
char cThisLine[MAX_REC_LEN]; /* Contents of current line */
char *cFile; /* Dynamically allocated buffer (entire file) */
char *cThisPtr; /* Pointer to current position in cFile */
fseek(input, 0L, SEEK_END); /* Position to end of file */
lFileLen = ftell(input); /* Get file length */
rewind(input); /* Back to start of file */
cFile = calloc(lFileLen + 1, sizeof(char));
if (cFile == NULL )
{
printf("\nInsufficient memory to read file.\n");
return 0;
}
fread(cFile, lFileLen, 1, input); /* Read the entire file into cFile */
cThisPtr = cFile; /* Point to beginning of array */
lIndex = 0L; /* Reset counters and flags */
while (*cThisPtr) /* Read until reaching null char */
{
if (*cThisPtr == CR || *cThisPtr == LF)
{
cThisLine[lIndex++] = '<';
cThisLine[lIndex++] = 'B';
cThisLine[lIndex++] = 'R';
cThisLine[lIndex++] = '>';
cThisLine[lIndex++] = '\x0D';
cThisLine[lIndex] = '\x0A';
*cThisPtr++;
if(*cThisPtr)
{
if(*cThisPtr == CR || *cThisPtr == LF)
{
*cThisPtr++;
}
}
FPrintLine(cThisLine, lIndex, output);
lIndex = 0L; /* Reset counters and flags */
}
else
{
cThisLine[lIndex++] = *cThisPtr++; /* Add char to output and increment */
}
}
return 1;
}
/******************************************************************************/
void FPrintLine(char *szReadLine, long lSize, FILE *output)
/******************************************************************************/
{
char *cLine = calloc(lSize+1, sizeof(char));
strncpy(cLine, szReadLine, lSize);
fprintf(output, "%s", cLine);
}
/******************************************************************************/
void syntax(void) /* Print correct command line syntax */
/******************************************************************************/
{
printf("\nSyntax: auto_br_adder FileName");
return;
} /* end syntax() */