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

/*
 * Function adapted from example in The Computer
 * Languages Benchmark Game
 *
 * ASCII switch added by <NAME> and inspired
 * by code from <NAME>
 * (http://docs.parrot.org/parrot/0.9.1/html/examples/pir/mandel.pir.html)
 */
int write_mandelbrot_to_stream(int w, int h, FILE *stream, int bitmap) {
    int bit_num = 0;
    char byte_acc = 0;
    int i, iter = 50;
    double x, y, limit = 2.0;
    double Zr, Zi, Cr, Ci, Tr, Ti;
    const char* ochars = " .:-;!/>)|&IH%*#";

    if(1 == bitmap)
        fprintf(stream, "P4\n%d %d\n", w, h);

    for(y=0;y<h;++y) 
    {
        for(x=0;x<w;++x)
        {
            Zr = Zi = Tr = Ti = 0.0;
            Cr = (2.0*x/w - 1.5); Ci=(2.0*y/h - 1.0);

            for (i=0;i<iter && (Tr+Ti <= limit*limit);++i)
            {
                Zi = 2.0*Zr*Zi + Ci;
                Zr = Tr - Ti + Cr;
                Tr = Zr * Zr;
                Ti = Zi * Zi;
            }

            if(1 == bitmap) {
                byte_acc <<= 1; 
                if(Tr+Ti <= limit*limit) byte_acc |= 0x01;

                ++bit_num; 

                if(bit_num == 8)
                {
                    putc(byte_acc, stream);
                    byte_acc = 0;
                    bit_num = 0;
                }
                else if(x == w-1)
                {
                    byte_acc <<= (8-w%8);
                    putc(byte_acc, stream);
                    byte_acc = 0;
                    bit_num = 0;
                }
            } else {
                if(iter == i) {
                    putc(ochars[0], stream);
                } else {
                    putc(ochars[i & 15], stream);
                }
            }
        }
        if(0 == bitmap)
            putc('\n', stream);
    }
    return 0;
}

/*
 * Function written by ThiefMaster@StackOverflow
 * http://stackoverflow.com/a/5309508/461813
 */
const char *get_filename_ext(const char *filename) {
    const char *dot = strrchr(filename, '.');
    if(!dot || dot == filename) return "";
    return dot + 1;
}

int main (int argc, char **argv)
{
    FILE *stream;
    int w, h = 0;
    char *opentype = "w";
    char *filename = "mandelbrot.txt";
    const char *binary_ext = "pbm";
    int binary = 0;
    int memory_stream = 1;
        char *bp;
        size_t size;

    w = h = atoi(argv[1]);

    if(get_filename_ext(filename) == binary_ext) {
        opentype = "wb";
        binary = 1;
    }

    if(1 == memory_stream) {
        stream = open_memstream(&bp, &size);
    } else {
        stream = fopen(filename, opentype);
    }
    if(stream == NULL)
        exit(-1);

    write_mandelbrot_to_stream(w, h, stream, binary);

    fclose(stream);

    if(1 == memory_stream) {
        // this is unnecessary
        char *ret = malloc(size);
        sprintf(ret, "%s", bp);<commit_msg>Add some more docs for myself<commit_after>#include<stdio.h>
#include<string.h>
#include<stdarg.h>
#include<stdlib.h>

/*
 * Function adapted from example in The Computer
 * Languages Benchmark Game
 *
 * ASCII switch added by <NAME> and inspired
 * by code from <NAME>
 * (http://docs.parrot.org/parrot/0.9.1/html/examples/pir/mandel.pir.html)
 */
int write_mandelbrot_to_stream(int w, int h, FILE *stream, int bitmap) {
    int bit_num = 0;
    char byte_acc = 0;
    int i, iter = 50;
    double x, y, limit = 2.0;
    double Zr, Zi, Cr, Ci, Tr, Ti;
    const char* ochars = " .:-;!/>)|&IH%*#";

    if(1 == bitmap)
        fprintf(stream, "P4\n%d %d\n", w, h);

    for(y=0;y<h;++y) 
    {
        for(x=0;x<w;++x)
        {
            Zr = Zi = Tr = Ti = 0.0;
            Cr = (2.0*x/w - 1.5); Ci=(2.0*y/h - 1.0);

            for (i=0;i<iter && (Tr+Ti <= limit*limit);++i)
            {
                Zi = 2.0*Zr*Zi + Ci;
                Zr = Tr - Ti + Cr;
                Tr = Zr * Zr;
                Ti = Zi * Zi;
            }

            if(1 == bitmap) {
                byte_acc <<= 1; 
                if(Tr+Ti <= limit*limit) byte_acc |= 0x01;

                ++bit_num; 

                if(bit_num == 8)
                {
                    putc(byte_acc, stream);
                    byte_acc = 0;
                    bit_num = 0;
                }
                else if(x == w-1)
                {
                    byte_acc <<= (8-w%8);
                    putc(byte_acc, stream);
                    byte_acc = 0;
                    bit_num = 0;
                }
            } else {
                if(iter == i) {
                    putc(ochars[0], stream);
                } else {
                    putc(ochars[i & 15], stream);
                }
            }
        }
        if(0 == bitmap)
            putc('\n', stream);
    }
    return 0;
}

/*
 * Function written by ThiefMaster@StackOverflow
 * http://stackoverflow.com/a/5309508/461813
 */
const char *get_filename_ext(const char *filename) {
    const char *dot = strrchr(filename, '.');
    if(!dot || dot == filename) return "";
    return dot + 1;
}

int main (int argc, char **argv)
{
    FILE *stream;
    int w, h = 0;
    char *opentype = "w";
    char *filename = "mandelbrot.txt";
    const char *binary_ext = "pbm";
    int binary = 0;
    int memory_stream = 1;


    // mem buffer tracking vars
    char *bp;
    size_t size;

    w = h = atoi(argv[1]);

    // get the extension from the file path
    // to determine type of stream to open (binary)
    if(get_filename_ext(filename) == binary_ext) {
        opentype = "wb";
        binary = 1;
    }

    if(1 == memory_stream) {
        stream = open_memstream(&bp, &size);
    } else {
        stream = fopen(filename, opentype);
    }
    if(stream == NULL)
        exit(-1);

    write_mandelbrot_to_stream(w, h, stream, binary);

    fclose(stream);

    if(1 == memory_stream) {
        // this is unnecessary
        char *ret = malloc(size);
        sprintf(ret, "%s", bp);
{'fraction_non_alphanumeric': 0.14736488754615643, 'fraction_numerical': 0.022155085599194362, 'mean_word_length': 1.436222403924775, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2170244', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2035, 'n_words': 483}