squareify.c

/* CS 431 assignment #3
 Matt Arnold 3/14/13 

All functions written by me unless otherwise stated
Languge C,  none of that Stroustrup abomination
here THANK YOU VERY MUCH (;

Edited 5/8/22 Turn this into a cgi script for a blog post
This renders a string in a square ascii art fashsion. 
Also Wow I was an arsehat in college  
  */

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

void mksquares (const char * szInital,  const char * szWorking, int ch, int pos, int reps);
void prettyprint(const char * str);
size_t strlcpy(char *dest, const char *src, size_t size);
void strreplace_r(char * str, char ch, int pos, int re);

int main(int argc, char *argv[])
{
        puts("Status: 200 OK\r");
	puts("Content-Type: text/html\r");
	puts("\r");
	char* query;
	char* duri;
	if ((query = getenv("QUERY_STRING")) == NULL) {
		puts("Not a CGI");
		return -1;
	}
	if ((duri = getenv("DOCUMENT_URI")) == NULL) {
		puts("Still not a cgi");
		return -1;
	}
	if (query[0] == '\0') {
		puts("<html>");
		puts(" <head> <title> Ascii Art Gen </title></head>");
		puts("<body>");
		printf("<form action=\"%s \" method=\"get\"", duri);
		puts("<label for=\"v\" > String </label>");
		puts("<input type=\"text\" id=\"v\" name=\"v\"><br><br>");
		puts("<input type=\"submit\" value=\"Submit\">");
		puts("</form>");
		puts("</body></html>");
		return 0;

	}
	char* vx = strstr(query, "v=");
	if (vx == NULL || strlen(vx) < 3) {
		puts("Form data error\r");
		return 0;
	}
	char* buf;
	asprintf(&buf,"%s", &vx[2]); 
        int n = strlen(buf);
	puts("<html>");
	puts("<head> <title> Render </title></head>");
	puts("<body><pre>");
        mksquares(buf, NULL, (n-1), 0, (n*2)-1);
	puts("</pre></body>");
	puts("</html>");
        return 0;
}

/** 
*  mksquares: Implement a solution to the 
* Assginment 3 recursion problem
* Commentary, and jokes within
* CS431
*/
void mksquares (const char * szInital,  const char * szWorking, int ch, int pos, int reps)
{
        char * szNew;
        int expected;
        // Base case if ch goes negitive
         if (ch < 0) {
           prettyprint(szWorking);   
           return;
        }

        // using memcpy to move the constant reference 
    //  into a working buffer, this saves the result 
        // of the previous envornment because we need it at the
        // before the recursive call is made and for the base case
        
        // initilize szWorking. in case it is NUL
        if (szWorking == NULL) {
                szNew = malloc(strlen(szInital) * 2);
                memset(szNew, '\0', strlen(szInital) * 2);
                expected = strlen(szInital) * 2-1;
        }
        else {
                expected = strlen(szWorking) + 1;
                szNew = malloc(expected+1);
                strlcpy(szNew, szWorking, expected);
                prettyprint(szWorking);
        }
        char current = szInital[ch];
        int where = pos;
        strreplace_r(szNew, current, where, reps);
        int np =  pos+1;
        int nr = reps-1;

        mksquares(szInital, szNew, ch-1, np, nr);
        prettyprint(szNew);
        free(szNew);
}
/**
* prettyprint, makes the output look nice and pretty
*/
void prettyprint(const char * str)
{
        int i;
        for (i=0; i < strlen(str); i++) {
                printf("%c ", str[i]);
        }
        printf("\n");
        return;
}
/**
* Replace element in a string a speifiyed number of times
*/
void strreplace_r(char * str, char ch, int pos, int re)
{
        int i;
        for (i = pos; i < re; i++) {
                str[i] = ch;
        }
}

/**
  * strlcpy - Copy a %NUL terminated string into a sized buffer
  * @dest: Where to copy the string to
  * @src: Where to copy the string from
  * @size: size of destination buffer
  *
  * Compatible with *BSD: the result is always a valid
  * NUL-terminated string that fits in the buffer (unless,
  * of course, the buffer size is zero). It does not pad
  * out the result like strncpy() does.
  * Adapted from linux kernel string libary
  * Because the glibc folks are idiots,
  *      and won't give us better string functions
  * 
  * See /usr/src/linux/lib/string.c at line 130     
  */
size_t strlcpy(char *dest, const char *src, size_t size)
{
         size_t ret = strlen(src);

         if (size) {
                 int len = (ret >= size) ? size - 1 : ret;
                 memcpy(dest, src, len);
                 dest[len] = '\0';
         }
         return ret;
}

Generated by GNU Enscript 1.6.5.90.