1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include <stdio.h>
#include "cgic.h"
void Name();
void Address();
void Hungry();
void Temperature();
void Frogs();
void Color();
void Flavors();
void NonExButtons();
void RadioButtons();
int cgiMain() {
#if DEBUG
/* Load a saved CGI scenario if we're debugging */
if (cgiReadEnvironment("capcgi.dat") != cgiEnvironmentSuccess) {
perror("Couldn't read environment from capcgi.dat");
return 1;
}
#endif
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");
Name();
Address();
Hungry();
Temperature();
Frogs();
Color();
Flavors();
NonExButtons();
RadioButtons();
fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}
void Name() {
char name[81];
int result = cgiFormStringNoNewlines("name", name, 81);
switch (result) {
case cgiFormSuccess:
fprintf(cgiOut, "Name fetched, result code: cgiFormSuccess<br>\n");
break;
case cgiFormTruncated:
fprintf(cgiOut, "Name fetched, result code: cgiFormTruncated<br>\n");
break;
case cgiFormEmpty:
fprintf(cgiOut, "Name fetched, result code: cgiFormEmpty<br>\n");
break;
case cgiFormNotFound:
fprintf(cgiOut, "Name fetched, result code: cgiFormNotFound<br>\n");
break;
case cgiFormMemory:
fprintf(cgiOut, "Name fetched, result code: cgiFormMemory<br>\n");
break;
default:
fprintf(cgiOut, "Name fetched, unexpected result code: %d\n", result);
break;
}
fprintf(cgiOut, "Name: %s<BR>\n", name);
}
void Address() {
char address[241];
cgiFormString("address", address, 241);
fprintf(cgiOut, "Address: <PRE>\n%s</PRE>\n", address);
}
void Hungry() {
if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) {
fprintf(cgiOut, "I'm Hungry!<BR>\n");
} else {
fprintf(cgiOut, "I'm Not Hungry!<BR>\n");
}
}
void Temperature() {
double temperature;
cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6);
fprintf(cgiOut, "My temperature is %f.<BR>\n", temperature);
}
void Frogs() {
int frogsEaten;
cgiFormInteger("frogs", &frogsEaten, 0);
fprintf(cgiOut, "I have eaten %d frogs.<BR>\n", frogsEaten);
}
char *colors[] = {
"Red",
"Green",
"Blue"
};
void Color() {
int colorChoice;
cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0);
fprintf(cgiOut, "I am: %s<BR>\n", colors[colorChoice]);
}
char *flavors[] = {
"pistachio",
"walnut",
"creme"
};
void Flavors() {
int flavorChoices[3];
int i;
int result;
int invalid;
result = cgiFormSelectMultiple("flavors", flavors, 3,
flavorChoices, &invalid);
if (result == cgiFormNotFound) {
fprintf(cgiOut, "I hate ice cream.<p>\n");
} else {
fprintf(cgiOut, "My favorite ice cream flavors are:\n");
fprintf(cgiOut, "<ul>\n");
for (i=0; (i < 3); i++) {
if (flavorChoices[i]) {
fprintf(cgiOut, "<li>%s\n", flavors[i]);
}
}
fprintf(cgiOut, "</ul>\n");
}
}
char *ages[] = {
"1",
"2",
"3",
"4"
};
void RadioButtons() {
int ageChoice;
char ageText[10];
/* Approach #1: check for one of several valid responses.
Good if there are a short list of possible button values and
you wish to enumerate them. */
cgiFormRadio("age", ages, 4, &ageChoice, 0);
fprintf(cgiOut, "Age of Truck: %s (method #1)<BR>\n",
ages[ageChoice]);
/* Approach #2: just get the string. Good
if the information is not critical or if you wish
to verify it in some other way. Note that if
the information is numeric, cgiFormInteger,
cgiFormDouble, and related functions may be
used instead of cgiFormString. */
cgiFormString("age", ageText, 10);
fprintf(cgiOut, "Age of Truck: %s (method #2)<BR>\n", ageText);
}
char *votes[] = {
"A",
"B",
"C",
"D"
};
void NonExButtons() {
int voteChoices[4];
int i;
int result;
int invalid;
char **responses;
/* Method #1: check for valid votes. This is a good idea,
since votes for nonexistent candidates should probably
be discounted... */
fprintf(cgiOut, "Votes (method 1):<BR>\n");
result = cgiFormCheckboxMultiple("vote", votes, 4,
voteChoices, &invalid);
if (result == cgiFormNotFound) {
fprintf(cgiOut, "I hate them all!<p>\n");
} else {
fprintf(cgiOut, "My preferred candidates are:\n");
fprintf(cgiOut, "<ul>\n");
for (i=0; (i < 4); i++) {
if (voteChoices[i]) {
fprintf(cgiOut, "<li>%s\n", votes[i]);
}
}
fprintf(cgiOut, "</ul>\n");
}
/* Method #2: get all the names voted for and trust them.
This is good if the form will change more often
than the code and invented responses are not a danger
or can be checked in some other way. */
fprintf(cgiOut, "Votes (method 2):<BR>\n");
result = cgiFormStringMultiple("vote", &responses);
if (result == cgiFormNotFound) {
fprintf(cgiOut, "I hate them all!<p>\n");
} else {
int i = 0;
fprintf(cgiOut, "My preferred candidates are:\n");
fprintf(cgiOut, "<ul>\n");
while (responses[i]) {
fprintf(cgiOut, "<li>%s\n", responses[i]);
i++;
}
fprintf(cgiOut, "</ul>\n");
}
/* We must be sure to free the string array or a memory
leak will occur. Simply calling free() would free
the array but not the individual strings. The
function cgiStringArrayFree() does the job completely. */
cgiStringArrayFree(responses);
}
|