Difference between revisions of "Talion/BRAdderCProgram"
From Exalted - Unofficial Wiki
(* Made a tool for adding <br>s to the ends of lines for easy formatting.) |
m (link fix) |
||
| Line 33: | Line 33: | ||
#define CR 13 /* Decimal code of Carriage Return char */ | #define CR 13 /* Decimal code of Carriage Return char */ | ||
#define LF 10 /* Decimal code of Line Feed char */ | #define LF 10 /* Decimal code of Line Feed char */ | ||
| − | #define EOF_MARKER 26 /* Decimal code of DOS end-of-file marker */ | + | #define [[EOF_MARKER]] 26 /* Decimal code of DOS end-of-file marker */ |
| − | #define MAX_REC_LEN 1024 /* Maximum size of input buffer */ | + | #define [[MAX_REC_LEN]] 1024 /* Maximum size of input buffer */ |
| − | int ReadAndBRFile(FILE *input, FILE *output); | + | int [[ReadAndBRFile]](FILE *input, FILE *output); |
| − | void FPrintLine(char * | + | void [[FPrintLine]](char *sz[[ReadLine]], long lSize, FILE *output); |
void syntax(void); | void syntax(void); | ||
| Line 45: | Line 45: | ||
{ | { | ||
char *outputName = "output_br.txt"; | char *outputName = "output_br.txt"; | ||
| − | FILE * | + | FILE *input[[FilePtr]]; /* Pointer to input file */ |
| − | FILE * | + | FILE *output[[FilePtr]]; /* Pointer to output file */ |
if (argc < 2) /* All arguments are required */ | if (argc < 2) /* All arguments are required */ | ||
| Line 54: | Line 54: | ||
} | } | ||
| − | + | input[[FilePtr]] = fopen(argv[1], "r"); | |
| − | if ( | + | if (input[[FilePtr]] == NULL ) /* Could not open file */ |
{ | { | ||
printf("Error opening %s: %s (%u)\n", argv[1], strerror(errno), errno); | printf("Error opening %s: %s (%u)\n", argv[1], strerror(errno), errno); | ||
| Line 62: | Line 62: | ||
} | } | ||
| − | + | output[[FilePtr]] = fopen(outputName, "w"); | |
| − | if (! | + | if (!output[[FilePtr]]) { |
printf("There was a problem opening %s for writing\n", outputName); | printf("There was a problem opening %s for writing\n", outputName); | ||
} | } | ||
| − | ReadAndBRFile( | + | [[ReadAndBRFile]](input[[FilePtr]], output[[FilePtr]]); |
| − | fclose( | + | fclose(input[[FilePtr]]); |
| − | fclose( | + | fclose(output[[FilePtr]]); |
} | } | ||
/******************************************************************************/ | /******************************************************************************/ | ||
| − | int ReadAndBRFile(FILE *input, FILE *output) /* Use: Read text file using */ | + | int [[ReadAndBRFile]](FILE *input, FILE *output) /* Use: Read text file using */ |
/* fread() */ | /* fread() */ | ||
/* */ | /* */ | ||
| Line 86: | Line 86: | ||
/******************************************************************************/ | /******************************************************************************/ | ||
{ | { | ||
| − | long | + | long l[[FileLen]]; /* Length of file */ |
| − | long lIndex; /* Index into | + | long lIndex; /* Index into c[[ThisLine]] array */ |
| − | char | + | char c[[ThisLine]][[[MAX_REC_LEN]]]; /* Contents of current line */ |
char *cFile; /* Dynamically allocated buffer (entire file) */ | char *cFile; /* Dynamically allocated buffer (entire file) */ | ||
| − | char * | + | char *c[[ThisPtr]]; /* Pointer to current position in cFile */ |
| − | fseek(input, 0L, SEEK_END); /* Position to end of file */ | + | fseek(input, 0L, [[SEEK_END]]); /* Position to end of file */ |
| − | + | l[[FileLen]] = ftell(input); /* Get file length */ | |
rewind(input); /* Back to start of file */ | rewind(input); /* Back to start of file */ | ||
| − | cFile = calloc( | + | cFile = calloc(l[[FileLen]] + 1, sizeof(char)); |
if (cFile == NULL ) | if (cFile == NULL ) | ||
| Line 107: | Line 107: | ||
} | } | ||
| − | fread(cFile, | + | 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 */ | lIndex = 0L; /* Reset counters and flags */ | ||
| − | while (* | + | while (*c[[ThisPtr]]) /* Read until reaching null char */ |
{ | { | ||
| − | if (* | + | 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(* | + | if(*c[[ThisPtr]]) |
{ | { | ||
| − | if(* | + | if(*c[[ThisPtr]] == CR || *c[[ThisPtr]] == LF) |
{ | { | ||
| − | * | + | *c[[ThisPtr]]++; |
} | } | ||
} | } | ||
| − | FPrintLine( | + | [[FPrintLine]](c[[ThisLine]], lIndex, output); |
lIndex = 0L; /* Reset counters and flags */ | lIndex = 0L; /* Reset counters and flags */ | ||
} | } | ||
else | else | ||
{ | { | ||
| − | + | c[[ThisLine]][lIndex++] = *c[[ThisPtr]]++; /* Add char to output and increment */ | |
} | } | ||
} | } | ||
| Line 145: | Line 145: | ||
/******************************************************************************/ | /******************************************************************************/ | ||
| − | void FPrintLine(char * | + | void [[FPrintLine]](char *sz[[ReadLine]], long lSize, FILE *output) |
/******************************************************************************/ | /******************************************************************************/ | ||
{ | { | ||
char *cLine = calloc(lSize+1, sizeof(char)); | char *cLine = calloc(lSize+1, sizeof(char)); | ||
| − | strncpy(cLine, | + | strncpy(cLine, sz[[ReadLine]], lSize); |
fprintf(output, "%s", cLine); | fprintf(output, "%s", cLine); | ||
} | } | ||
| Line 157: | Line 157: | ||
/******************************************************************************/ | /******************************************************************************/ | ||
{ | { | ||
| − | printf("\nSyntax: auto_br_adder FileName"); | + | printf("\nSyntax: auto_br_adder [[FileName]]"); |
return; | return; | ||
} /* end syntax() */ | } /* end syntax() */ | ||
</pre> | </pre> | ||
Revision as of 09:05, 3 April 2010
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() */