Krupski
February 27th, 2010, 11:09 PM
Hi all,
I wrote a small program to change plain text files into HTML format (by, for example, replacing new lines with <br> and spaces with " ", etc...)
There is a piece of the code that has nested IF/ELSE decisions and something in the back of my mind tells me that it's clumsy and that there's a better way to do it.
Any suggestions will be greatly appreciated!
Thanks!
-- Roger
Here's the code: (concern in red)
///////////////////////////////////////////////////////////////////////////
//
// "htmlfilt.c" - PLAIN TEXT to HTML filter
// Copyright 2010 Roger A. Krupski <krupski@acsu.buffalo.edu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////
//
// To use: htmlfilt < stdin > stdout
//
// This program simply reads the source line by line and transforms
// spaces into non-breaking spaces ( ) and also replaces tabs
// with the proper number of non-breaking spaces to preserve any
// formatting the file has. It also replaces special characters
// such as "<" and ">", etc...
//
// To include an optional HTML header and/or footer, generate the
// following files: program_name.header.html and program_name.footer.html
// where program_name is htmlfilt (unless you changed it!).
//
// These two optional files are copied to stdout verbatim, so they must
// contain valid HTML code to work properly.
//
///////////////////////////////////////////////////////////////////////////
//
// A typical htmlfilt-header.html might contain the following text:
//
// <p style=
// font-family:courier new,courier,monospace;
// font-weight:normal;
// background-color:#ffffff;
// text-align:left;
// color:#000000;
// font-size:1.0em;
// >
//
// and of course the footer would contain:
//
// </p>
//
///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#undef BUFSIZ
#define BUFSIZ 2048
// file pointer, destination
int readln(FILE*, char*);
// destination, source, delimiter
int bname(char*, const char*, const char*);
int main (int argc, char *argv[])
{
FILE *infile;
const char *header = "%s-header.html";
const char *footer = "%s-footer.html";
char buffer;
char fname[BUFSIZ];
int len, c, x, curpos;
int tabsiz = 8;
if(argc != 1) {
bname(buffer, argv[0], "\\/");
fprintf(stderr, "usage: %s < stdin > stdout\n", buffer);
return 1;
}
bname(buffer, argv[0], "\\/");
sprintf(fname, header, buffer);
infile = fopen(fname, "r");
if(infile != NULL) {
while(!feof(infile)) {
readln(infile, buffer);
fprintf(stdout, "%s\n", buffer);
}
fclose(infile);
}
while(!feof(stdin))
{
curpos = 0;
len = readln(stdin, buffer);
for(c = 0; c < len; c++)
{
[B] if(buffer[c] == 0x09) {
for(x = 0; x < tabsiz-(curpos % tabsiz); x++)
{
fprintf(stdout, " ");
}
curpos +=x;
} else
if(buffer[c] == ' ') { fprintf(stdout, " "); curpos++; } else
if(buffer[c] == '<') { fprintf(stdout, "<"); curpos++; } else
if(buffer[c] == '>') { fprintf(stdout, ">"); curpos++; } else
if(buffer[c] == '&') { fprintf(stdout, "&"); curpos++; } else
if(buffer[c] == 0x22) { fprintf(stdout, """); curpos++; } else
if(buffer[c] == 0x27) { fprintf(stdout, "'"); curpos++; } else
{ fprintf(stdout, "%c", buffer[c]); curpos++; }
}
fprintf(stdout, "<br>\n");
}
bname(buffer, argv[0], "\\/");
sprintf(fname, footer, buffer);
infile = fopen(fname, "r");
if(infile != NULL) {
while(!feof(infile)) {
readln(infile, buffer);
fprintf(stdout, "%s\n", buffer);
}
fclose(infile);
}
return 0;
}
// file pointer, destination
int readln(FILE *fp, char *str)
{
char buf[BUFSIZ];
int len;
buf[0] = 0;
fgets(buf, BUFSIZ, fp);
len = strlen(buf);
while (len >= 0) {
if (buf[len] <= 0x20) { buf[len] = 0; len--; }
else { break; }
}
len++;
buf[len] = 0;
strncpy(str, buf, BUFSIZ);
return strlen(str);
}
// destination, source, delimiter
int bname(char* str, const char* p0, const char* delim)
{
char tmp[BUFSIZ];
char* p1;
char* p2;
strcpy(tmp, p0);
p2 = strtok(tmp, delim);
while(p2 != NULL) {
p1 = p2;
p2 = strtok(NULL, delim);
}
strncpy(str, p1, BUFSIZ);
return strlen(str);
}
Thank you!!
I wrote a small program to change plain text files into HTML format (by, for example, replacing new lines with <br> and spaces with " ", etc...)
There is a piece of the code that has nested IF/ELSE decisions and something in the back of my mind tells me that it's clumsy and that there's a better way to do it.
Any suggestions will be greatly appreciated!
Thanks!
-- Roger
Here's the code: (concern in red)
///////////////////////////////////////////////////////////////////////////
//
// "htmlfilt.c" - PLAIN TEXT to HTML filter
// Copyright 2010 Roger A. Krupski <krupski@acsu.buffalo.edu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////
//
// To use: htmlfilt < stdin > stdout
//
// This program simply reads the source line by line and transforms
// spaces into non-breaking spaces ( ) and also replaces tabs
// with the proper number of non-breaking spaces to preserve any
// formatting the file has. It also replaces special characters
// such as "<" and ">", etc...
//
// To include an optional HTML header and/or footer, generate the
// following files: program_name.header.html and program_name.footer.html
// where program_name is htmlfilt (unless you changed it!).
//
// These two optional files are copied to stdout verbatim, so they must
// contain valid HTML code to work properly.
//
///////////////////////////////////////////////////////////////////////////
//
// A typical htmlfilt-header.html might contain the following text:
//
// <p style=
// font-family:courier new,courier,monospace;
// font-weight:normal;
// background-color:#ffffff;
// text-align:left;
// color:#000000;
// font-size:1.0em;
// >
//
// and of course the footer would contain:
//
// </p>
//
///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#undef BUFSIZ
#define BUFSIZ 2048
// file pointer, destination
int readln(FILE*, char*);
// destination, source, delimiter
int bname(char*, const char*, const char*);
int main (int argc, char *argv[])
{
FILE *infile;
const char *header = "%s-header.html";
const char *footer = "%s-footer.html";
char buffer;
char fname[BUFSIZ];
int len, c, x, curpos;
int tabsiz = 8;
if(argc != 1) {
bname(buffer, argv[0], "\\/");
fprintf(stderr, "usage: %s < stdin > stdout\n", buffer);
return 1;
}
bname(buffer, argv[0], "\\/");
sprintf(fname, header, buffer);
infile = fopen(fname, "r");
if(infile != NULL) {
while(!feof(infile)) {
readln(infile, buffer);
fprintf(stdout, "%s\n", buffer);
}
fclose(infile);
}
while(!feof(stdin))
{
curpos = 0;
len = readln(stdin, buffer);
for(c = 0; c < len; c++)
{
[B] if(buffer[c] == 0x09) {
for(x = 0; x < tabsiz-(curpos % tabsiz); x++)
{
fprintf(stdout, " ");
}
curpos +=x;
} else
if(buffer[c] == ' ') { fprintf(stdout, " "); curpos++; } else
if(buffer[c] == '<') { fprintf(stdout, "<"); curpos++; } else
if(buffer[c] == '>') { fprintf(stdout, ">"); curpos++; } else
if(buffer[c] == '&') { fprintf(stdout, "&"); curpos++; } else
if(buffer[c] == 0x22) { fprintf(stdout, """); curpos++; } else
if(buffer[c] == 0x27) { fprintf(stdout, "'"); curpos++; } else
{ fprintf(stdout, "%c", buffer[c]); curpos++; }
}
fprintf(stdout, "<br>\n");
}
bname(buffer, argv[0], "\\/");
sprintf(fname, footer, buffer);
infile = fopen(fname, "r");
if(infile != NULL) {
while(!feof(infile)) {
readln(infile, buffer);
fprintf(stdout, "%s\n", buffer);
}
fclose(infile);
}
return 0;
}
// file pointer, destination
int readln(FILE *fp, char *str)
{
char buf[BUFSIZ];
int len;
buf[0] = 0;
fgets(buf, BUFSIZ, fp);
len = strlen(buf);
while (len >= 0) {
if (buf[len] <= 0x20) { buf[len] = 0; len--; }
else { break; }
}
len++;
buf[len] = 0;
strncpy(str, buf, BUFSIZ);
return strlen(str);
}
// destination, source, delimiter
int bname(char* str, const char* p0, const char* delim)
{
char tmp[BUFSIZ];
char* p1;
char* p2;
strcpy(tmp, p0);
p2 = strtok(tmp, delim);
while(p2 != NULL) {
p1 = p2;
p2 = strtok(NULL, delim);
}
strncpy(str, p1, BUFSIZ);
return strlen(str);
}
Thank you!!