Hi everyone,
Lately I have started to learn C for an application I want to write with a friend. I have some experience programming in python especially web and network enabled applications. I picked "The C Programming Language Second Edition" which I already read a few months ago though I've forgot many aspects of the language by now.
I've just finished the pointers part so to check how much I understood from the book I've written a small program that paints HTML source code and outputs it to HTML.
main.c:
Code:
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include "srcp.h"
#define USAGE "Usage: %s -f file.html [-o output_file]\n"
int main(int argc, char **argv)
{
char c;
char *src;
char *file_name = NULL;
char *outp = NULL;
while ((c = getopt (argc, argv, "f:o:")) != -1) {
switch (c) {
case 'f':
file_name = optarg;
break;
case 'o':
outp = optarg;
break;
case '?':
return 1;
default:
fprintf(stderr, USAGE, argv[0]);
}
}
if (file_name == NULL) {
fprintf(stderr, USAGE, argv[0]);
return 1;
}
src = file_read(file_name);
if (src == NULL) {
fprintf(stderr, "Can't read file %s\n", file_name);
return 1;
}
srcp_push("<html><head>");
srcp_push("<meta http-equiv='content-type' content='text/html; charset=utf-8' />");
srcp_push("<style type='text/css'> body { color: #fff; background: #000; font-size: smaller; }; </style>");
srcp_push("</head><body>");
srcp_html(src);
srcp_push("</body></html>");
if (outp == NULL) {
printf("%s\n", srcp_getoutput());
} else {
if (file_write(outp, srcp_getoutput()) == NULL) {
fprintf(stderr, "Can't write to location\n");
return 1;
} else printf("Done!\n");
}
srcp_clean();
return 0;
}
srcp.h
Code:
#include "file.h"
#define true 1
#define false 0
#define BUFSIZE 1000
char buf[BUFSIZE];
unsigned bp = 0;
char *output = NULL;
unsigned in_tag = false;
char *srcp_html(char *);
char *tostr(char);
void srcp_push(char *);
void srcp_pushbuf(void);
void srcp_color_brac(char *);
char *srcp_getoutput(void);
char *srcp_getoutput(void)
{
return output;
}
void srcp_color_brac(char *brac)
{
srcp_push("<span style='color: red;'>");
srcp_push(brac);
srcp_push("</span>");
in_tag = (strcmp(brac, ">") == 0) ? false:true;
srcp_push(in_tag ? "<span style='color: #9d3;'>":"</span>");
}
char *srcp_html(char *src)
{
while (*src) {
switch (*src) {
case '>':
srcp_color_brac(">");
break;
case '<':
srcp_color_brac("<");
break;
case '&':
srcp_push("&");
break;
case ' ':
srcp_push(" ");
if (in_tag) {
srcp_push("</span>");
srcp_push("<span style='color: green;'>");
}
break;
case '\n':
srcp_push("<br />\n");
break;
case '\t':
srcp_push(" ");
break;
case '=':
in_tag ? srcp_push("<span style='color: #9b3;'>=</span>"):srcp_push("=");
break;
default:
srcp_push(tostr(*src));
}
src++;
}
srcp_pushbuf();
return output;
}
void srcp_push(char *str)
{
int strsize = strlen(str);
if (bp + strsize < BUFSIZE) {
strcat(buf, str);
bp += strsize;
} else {
srcp_pushbuf();
srcp_push(str);
}
}
void srcp_pushbuf(void)
{
if (output == NULL) {
output = (char *) malloc(BUFSIZE);
strcpy(output, buf);
} else {
output = realloc(output, strlen(output) + BUFSIZE);
strcat(output, buf);
}
buf[bp = 0] = 0;
}
char *tostr(char c)
{
static char ch[2];
ch[0] = c;
ch[1] = 0;
return ch;
}
void srcp_clean(void)
{
if (output != NULL) free(output);
}
file.h
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STATUS_OK "1"
char *file_read(const char *name)
{
char *ptr = NULL;
FILE *fp = fopen(name, "r");
if (!fp) return NULL;
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if ((ptr = calloc(size+1, 1)) == NULL)
goto error_exit;
if (fread(ptr, 1, size, fp) != size)
goto error_exit;
fclose(fp);
return ptr;
error_exit:
fclose(fp);
free(ptr);
return NULL;
}
char *file_write(const char *name, char *text)
{
FILE *fp = fopen(name, "w");
if (!fp) return NULL;
int size = strlen(text);
if (fwrite(text, 1, size, fp) != size) {
fclose(fp);
return NULL;
}
fclose(fp);
return STATUS_OK;
}
Any suggestions or corrections will be welcomed.