Page 1 of 1

stitchcounter

Posted: Wed Feb 26, 2014 5:18 pm
by GekkoP
I coded this simple tool to help the missus in her knitting and crocheting. I know that rhowaldt has got you all about the manliness of this stuff, so I figured maybe this could be helpful for you too.

Code: Select all

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

/* stitchcounter: useful tool to help knitting and crocheting
 * usage:
 * - first option: specify original gauge
 *                 specify your gauge
 *                 specify how many values you need
 *                 calculate: x = (my_rows * value) / org_rows
 *                 calculate: x = (my_sts * value) / org_sts
 * - second option: specify your gauge: rows and stitches
 *                  specify how many values you need
 *                  specify cm for each value
 *                  calculate the number of stitches/rows needed for the value */

#define CM_PER_SQUARE 10.0
#define MAX_LINELENGTH 4096
#define MAX_ATTEMPTS 10

static float get_number(const char *prompt)
{
    float value;
    char line[MAX_LINELENGTH];
    int count = 0;
    while (fputs(prompt, stdout) != EOF &&
           fgets(line, sizeof(line), stdin) != 0) {
        if (sscanf(line, "%f", &value) == 1)
            return value;
        if (count++ > MAX_ATTEMPTS) {
            printf("I don't understand what you're typing\n");
            exit(1);
        }
	printf("value must be a number (es.: 1, 0.1, 20.3, etc.)\n");
    }

    printf("I got EOF or an error.\n");
    exit(1);
}

static char get_char(const char *prompt)
{
    char value;
    char line[MAX_LINELENGTH];
    int count = 0;
    while (fputs(prompt, stdout) != EOF &&
           fgets(line, sizeof(line), stdin) != 0) {
        if (sscanf(line, "%s", &value) == 1) {
	    if (!isalpha(value)) {
		printf("value must be either [r] or [s]\n");
	    } else {
		return value;
	    }
	}
        if (count++ > MAX_ATTEMPTS) {
            printf("I don't understand what you're typing\n");
            exit(1);
        }
	printf("value must be either [r] or [s]\n");
    }

    printf("I got EOF or an error.\n");
    exit(1);
}

int opt1()
{
    float rows, sts, org_rows, org_sts, org_val;
    int values;

    printf("original gauge\n");
    org_rows = get_number("\tplease enter the number of rows: ");
    org_sts = get_number("\tplease enter the number of stitches: ");

    printf("your gauge\n");
    rows = get_number("\tplease enter the number of your rows: ");
    sts = get_number("\tplease enter the number of your stitches: ");

    values = get_number("how many values do you need? ");

    int i;
    for (i = 1; i <= values; i++) {
	char sel = get_char("is value rows [r] or stitches [s]? ");
	switch(sel) {
	    case 'r':
   	        org_val = get_number("please enter the rows of the original project: ");
	        printf("your final result is: %3.1f\n", (rows * org_val) / org_rows);
	        break;
	    case 's':
		org_val = get_number("please enter the stitches of the original project: ");
	        printf("your final result is: %3.1f\n", (sts * org_val) / org_sts);
		break;
  	    default:
  	        printf("i don't understand your input\n");
	        break;
	}
    }
    return 0;
}

int opt2()
{
    float rows, sts, rows1cm, sts1cm, cm;
    int values;

    printf("your gauge\n");
    rows = get_number("\tplease enter the number of your rows: ");
    sts = get_number("\tplease enter the number of your stitches: ");

    rows1cm = rows / CM_PER_SQUARE;
    sts1cm = sts / CM_PER_SQUARE;

    values = get_number("how many values do you need? ");

    int i;
    for (i = 1; i <= values; i++) {
	char sel = get_char("is value rows [r] or stitches [s]? ");
	switch(sel) {
	    case 'r':
		cm = get_number("please enter the cms of your project: ");
	        printf("your final result is: %3.1f\n", (rows1cm * cm));
	        break;
	    case 's':
		cm = get_number("please enter the cms of your project: ");
	        printf("your final result is: %3.1f\n", (sts1cm * cm));
		break;
  	    default:
  	        printf("i don't understand your input\n");
	        break;
	}
    }
    return 0;
}

main()
{
    int opt;

    printf("\nstitchcounter: useful tool to help knitting and crocheting\n\n");
    printf("options:\n");
    printf("\t1) calculate stitches from original gauge\n");
    printf("\t2) calculate stitches from dimensions\n");
    opt = get_number("pick your option: ");

    switch(opt) {
        case 1:
            opt1();
	    break;
        case 2:
            opt2();
	    break;
        default:
            printf("option must be 1 or 2\n");
	    break;
    }

    return 0;
}

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:00 pm
by wuxmedia
cool, nice coding!

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:01 pm
by machinebacon
yeah, nice coding - just no idea what it is for :D

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:07 pm
by GekkoP
I'm no knitter, but wife's used it to knit a skirt. She usually starts from a pattern somebody else did, but has to make some math to adjust it to her needs. This tool makes it simpler, does nothing more actually. (and I suppose it can be coded better, I'm no C expert)

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:14 pm
by rust collector
WOW!

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:30 pm
by machinebacon
Oh I see, so you can use it to "convert" the sizes or something like this. Nice then :D

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:33 pm
by GekkoP
^ yes. She uses it to calculate the right amount of stiches for different sizes. :)

Re: stitchcounter

Posted: Wed Feb 26, 2014 7:34 pm
by machinebacon
I hope she always will have to calculate towards a smaller size :D

Re: stitchcounter

Posted: Wed Feb 26, 2014 10:21 pm
by paolo
Wow ! Interesting thing.

Re: stitchcounter

Posted: Thu Feb 27, 2014 11:24 am
by rhowaldt
cool shit Gekko! i will check it out and let you know if i understand it, or maybe my GF does :D (i am no expert)

Re: stitchcounter

Posted: Mon May 11, 2015 6:33 pm
by GekkoP
Just for fun, I ported this little tool in Clojure: https://github.com/manuel-uberti/stitchcounter-clj

Re: stitchcounter

Posted: Wed Aug 19, 2015 2:35 pm
by GekkoP
Just for fun, I ported this little tool in Chicken Scheme: https://github.com/manuel-uberti/stitchcounter-scm

(BTW, my favorite implementation so far)

Re: stitchcounter

Posted: Wed Aug 19, 2015 8:52 pm
by simgin
Hmm, it is a lot shorter in Lisp dialects, than in C. o.O Nice work Manuel!

cheers
simon

Re: stitchcounter

Posted: Thu Aug 20, 2015 7:38 am
by GekkoP
^ Thank you.

Yes, Lisp versions are a lot shorter. But Clojure isn't a good choice for CLI tools, because of all the Java/JVM madness. The executable I get from my Clojure version of stichcounter is over 15MB (!). Only good for learning's sake.

That's where Scheme, and Chicken Scheme in particular, comes in. I got the Lisp I love but I can compile it to C and obtain small and light executables (~88Kb). It still feels like magic.

Re: stitchcounter

Posted: Sat Aug 22, 2015 2:53 pm
by GekkoP
MWE in JavaScript: https://github.com/manuel-uberti/stitchcounter-js

Ok, enough. :D