/* buildgrid.c, Copyright (C) 2002, Tim McLarnan This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Do, please, let me hear from you if you use this package, or if you have questions. Tim McLarnan, Dept. of Mathematics, Earlham College, Richmond, IN 47374 USA timm AT cs.earlham.edu */ /* buildgrid.c, Tim McLarnan, June, 2000. This program gets run as a cgi script, buildgrid.cgi, called from form1.php. It takes the data collected from form1.php (the number of shafts and the source and target numbers of treadles) and builds the form containing the grid on which the user codes the original tie-up. It's silly to have written this in C, but I haven't yet taken the time to learn Perl or Python, and it was easier to use C than to learn a new language. Since I had never used cgi before either, holding to a minimum the number of new technologies I was embracing simultaneously seemed prudent. */ #include #include #define INPUT_LENGTH 200 /* Gets the numerical value associated with from the input string. Since this will look like =, we scan for a number starting 1 character after the end of . */ int getNumericalValue(char *instring, char *name) { int value; char *start = strstr(instring, name); start = start + strlen(name) + 1; sscanf(start, "%d", &value); return(value); } int main() { int row, column; char instring[INPUT_LENGTH]; int ShaftCount; /* Number of shafts in the pattern. */ int TargetCount; /* Number of treadles in the original pattern. */ int TreadleCount; /* Number of treadles on user's loom. */ const int MinShaft = 2, MaxShaft = 40, MinTarget = 2, MaxTarget = 100, MinTreadle = 2, MaxTreadle = 100; int winWidth; /* Write the header for the page we're building. */ fgets(instring, INPUT_LENGTH, stdin); printf("Content-type: text/html\n\n"); printf("Original tie-up\n"); printf("\n"); /* Now get the data from the user's form, and make sure it makes sense. */ ShaftCount = getNumericalValue(instring, "ShaftCount"); TargetCount = getNumericalValue(instring, "TargetCount"); TreadleCount = getNumericalValue(instring, "TreadleCount"); if ((ShaftCount < MinShaft) || (ShaftCount > MaxShaft)) { printf("

Sorry. The number of shafts must be between\n"); printf("%d and %d.

\n", MinShaft, MaxShaft); printf("

Please go back to the previous page and try again.

\n"); printf("

If your problems persist, please "); printf(""); printf("send me mail.\n"); printf("\n"); exit(1); } if ((TargetCount < MinTarget) || (TargetCount > MaxTarget)) { printf("

Sorry. The number of treadles on the original tie-up must\n"); printf("be between %d and %d.

\n", MinTarget, MaxTarget); printf("

Please go back to the previous page and try again.

\n"); printf("

If your problems persist, please "); printf("

If your problems persist, please "); printf(""); printf("send me mail.\n"); printf("\n"); exit(1); } if ((TreadleCount < MinTreadle) || (TreadleCount > MaxTreadle)) { printf("

Sorry. The number of treadles on your loom must be between\n"); printf("%d and %d.

\n", MinTreadle, MaxTreadle); printf("

Please go back to the previous page and try again.

\n"); printf("

If your problems persist, please "); printf("

If your problems persist, please "); printf(""); printf("send me mail.\n"); printf("\n"); exit(1); } /* Now build the form with the grid of checkboxes. */ winWidth = ( 25 * TargetCount > 500 ? 25 * TargetCount : 500); printf("\n"); printf("
\n"); printf("\n"); printf("\n"); printf("
"); printf(""); printf("

\n"); printf("

Tim's Rudimentary
Treadle Reducer


\n"); printf("
\n"); printf("
"); printf("\n", ShaftCount); printf("\n", TargetCount); printf("\n"); printf("\n"); printf("\n"); printf("\n", ShaftCount); printf("\n"); printf("\n"); printf("\n", TargetCount); printf("\n"); printf("\n"); printf("\n", TreadleCount); printf("
Number of shafts:%d
Number of treadles in the original tie-up:%d
Number of treadles on your loom:
\n"); printf("

Click the check-boxes below to form the original tie-up.\n"); printf("As usual in these diagrams, horizontal rows represent shafts,\n"); printf("and vertical columns represent treadles.\n"); printf("The diagram begins with no ties. "); printf("Click buttons to designate ties.

\n"); printf("\n"); printf("\n"); for (row = 0; row < ShaftCount; row++) { printf("\n"); for (column = 0; column < TargetCount; column++) { printf("\n", row, column); } printf("\n"); } printf("
\n"); printf("

For many tie-ups, finding a reduction takes a second or two.\n"); printf("On the other hand, for complicated tie-ups, and particularly in\n"); printf("cases where no reduction to fewer treadles exists, the search\n"); printf("can take minutes, hours, or, in theory, years. (This is why\n"); printf("you're using a computer instead of doing it by hand, right?)

\n"); printf("As soon as a reduction is found, we'll quit and tell you.

\n"); printf("

If we don't find a quick solution, what's the longest time\n"); printf("you're willing to wait for an answer while we keep looking?\n

"); printf("

I'm willing to give up after waiting

"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("
10 secs. 30 secs. 1 min. 2 mins.
10 mins. 30 mins. 2 hrs. 1 day.
\n"); printf("\n"); printf("
\n"); printf("

Tim McLarnan,
\n"); printf("
Tremewan Professor of Mathematics\n"); printf("
Earlham College,\n"); printf("
Richmond, IN 47374 USA\n"); printf("

"); printf("Send me mail.

\n"); printf("


\n"); return(0); }