home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap5 / 5_5 / check_c / check.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  2.2 KB  |  134 lines

  1.  
  2. #include "parsehtm.h"
  3. #include <stdio.h>
  4.  
  5. Dictionary userData;
  6.  
  7. #ifndef strcasecmp
  8.  
  9. int (strcasecmp)(const char *s1,const char *s2)
  10. {
  11.     int returnValue = 1;
  12.     
  13.     if((NULL != s1)&&(NULL != s2))
  14.     {
  15.         char test1,test2;
  16.         
  17.         for(;;++s1,++s2)
  18.         {
  19.             test1 = tolower(*s1);
  20.             test2 = tolower(*s2);
  21.             
  22.             if(test1 != test2)
  23.             {
  24.                 returnValue = 
  25.                     ((test1 < test2) ? -1:+1);
  26.                 break;
  27.             }
  28.             else if(test1 == '\0')
  29.             {
  30.                 returnValue = 0;
  31.                 break;
  32.             }
  33.         }
  34.     }
  35.     else if((NULL == s1)&&(NULL == s2))
  36.     {
  37.         returnValue = 0;
  38.     }
  39.     
  40.     return returnValue;
  41. }
  42.  
  43. #endif
  44.  
  45. void checkboxHandler(String ts,String as,String et,Dictionary td)
  46. {
  47.     String value = 0;
  48.     String data = 0;
  49.     String name = 0;
  50.     
  51.     value = dict_valueForKey(td,"TYPE");
  52.     
  53.     if(value && value->string)
  54.     {
  55.         if(!strcasecmp(value->string,"checkbox"))
  56.         {
  57.             value = dict_valueForKey(td,"VALUE");
  58.             name = dict_valueForKey(td,"NAME");
  59.             if(name && name->string)
  60.             {
  61.                 data = dict_valueForKey(userData,name->string);
  62.             }
  63.             
  64.             if(data && value && !strcmp(value->string,data->string))
  65.             {
  66.                 value = dict_valueForKey(td,"CHECKED");
  67.                 
  68.                 if(value)
  69.                 {
  70.                     string_setStringValue(value,"CHECKED");
  71.                 }
  72.                 else
  73.                 {
  74.                     value = string_alloc(8);
  75.                     string_setStringValue(value,"CHECKED");
  76.                     dict_setValueForKey(td,value->string,value);
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 value = dict_orphanValueForKey(td,"CHECKED");
  82.                 
  83.                 if(value)
  84.                 {
  85.                     string_free(value);
  86.                 }
  87.             }
  88.             
  89.             value = stringForTagDict(td);
  90.             
  91.             if(value)
  92.             {
  93.                 string_setStringValue(ts,value->string);
  94.             
  95.                 string_free(value);
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101. void main(int argc, char *argv[])
  102. {
  103.     String output;
  104.     String value;
  105.     
  106.     userData = dict_alloc();
  107.     
  108.     initializeHtmlParsingLibrary();
  109.     
  110.     dict_setValueForKey(handlerDict,"INPUT",checkboxHandler);
  111.     
  112.     value = string_alloc(8);
  113.     string_setStringValue(value,"Stephen");
  114.     dict_setValueForKey(userData,"name",value);
  115.     
  116.     value = string_alloc(4);
  117.     string_setStringValue(value,"AMEX");
  118.     dict_setValueForKey(userData,"card",value);
  119.     
  120.     output = parseHtml("chk_c.htm");
  121.     
  122.     if(output && output->string)
  123.     {
  124.         printf("Content-type: text/html\n\n");
  125.         fwrite(output->string,sizeof(char),strlen(output->string),stdout);
  126.         printf("\n");
  127.         
  128.         string_free(output);
  129.     }
  130.     
  131.     exit(0);
  132. }
  133.  
  134.