Harry Rose

Home » C » Noobify

Noobify

This is a quick bit of code I wrote when I couldn't sleep. Its to help people do that annoying alternate caps typing ("Why would you help them?" I hear you ask. Because I'm craaazy!).

I would put an example of its output but its so horrible to look at and so annoying I wont force it upon anybody who might be shocked and offended by it.

This doesn't use any fancy libraries (surprisingly) so you can build it just by using

	gcc noob.c -o noob

/**
 * Noobify - A program that helps you type LiKe ThIs (i.e like a noob :P)
 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 
#define FROM_STDIN 1
#define FROM_FILE 2
#define FROM_CMD 3
 
/**
 * Makes chars in a string altnernately upper case and lower case.
 */
char * noobify(char * string)
{
    char * working = string;
    int isUpper = 1;
    while( * working != '\0')
    {
        if(isUpper)
        {
            *working = toupper(*working);
        }
        else
        {
            *working = tolower(*working);
        }
        isUpper = 1-isUpper;
        working++;
    }
    return string;
}
 
/**
 * Calls noobify on an array of strings starting from offset 
 * and ending after length elements..
 */
void noobifyArray(char ** array, int length, int offset)
{
    int i;
    for(i = offset; i < length; i++)
    {
        char * word = array[i];
        noobify(word);
        printf("%s ",word);
    }
}
 
/**
 * Calls noobify on the contents of the file (already opened) pointed to
 * by file.
 */
void noobifyFileStream(FILE * file)
{
    const int bufferSize = 1024;
    char buffer[bufferSize];
    int printed = 0;
    
    while(!feof(file))
    {
        int amtread = 0;
        amtread = fread(buffer,sizeof(char),bufferSize-1,file);
        buffer[amtread] = '\0';
        noobify(buffer);
        printf("%s%s",(printed)? " ":"", buffer);
        printed = 1;
    }
 
}
 
/**
 * Noobifies the file with the given filename
 */
void noobifyFile(char * filename)
{
    FILE * file = fopen(filename, "r");
    if(!file)
    {
        fprintf(stderr,"Error opening file %s\n",filename);
        exit(1);
    }
    noobifyFileStream(file);
    fclose(file);
}
 
/**
 * Noobifies strings accepted from the standard input.
 */
void noobifyStdin()
{
    noobifyFileStream(stdin);
}
 
/**
 * Check if the file with the given filename exists.
 */
int file_exists(const char * filename)
{
    FILE * file;
    if (file = fopen(filename, "r"))
    {
        fclose(file);
        return 1;
    }
    return 0;
}
 
/**
 * Work out where the strings should be coming from.
 *     If there are no command line arguments then accept from stdin.
 *    If there are at least two command line arguments (the first is
 *        -f and the second the file to open) read from a file.
 *    Otherwise just use any command line arguments as input.
 */
int determineSource(int argc, char ** argv)
{
    if(argc == 1)
    {
        return FROM_STDIN;
    }
    else if(argc > 2 && strcmp("-f",argv[1]) == 0)
    {
        return FROM_FILE;
    }
    else
    {
        return FROM_CMD;
    }
}
 
int main(int argc, char ** argv)
{
    int source = determineSource(argc,argv);
    switch(source)
    {
        case FROM_STDIN:
            noobifyStdin();
        break;
 
        case FROM_FILE:
            noobifyFile(argv[2]);
        break;
 
        default:
            noobifyArray(argv,argc,1);
    }
    return 0;
}