source: trunk/thirds/cgic206/cgictest.c @ 747

Last change on this file since 747 was 348, checked in by neteler, 13 years ago

set correctly svn propset

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

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png