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