home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / HTTPD / HTTPD_SO.TAR / httpd_1.3 / cgi-src / imagemap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  7.4 KB  |  255 lines

  1. /*
  2. ** mapper 1.2
  3. ** 7/26/93 Kevin Hughes, kevinh@pulua.hcc.hawaii.edu
  4. ** "macmartinized" polygon code copyright 1992 by Eric Haines, erich@eye.com
  5. ** All suggestions, help, etc. gratefully accepted!
  6. **
  7. ** 1.1 : Better formatting, added better polygon code.
  8. ** 1.2 : Changed isname(), added config file specification.
  9. **
  10. ** 11/13/93: Rob McCool, robm@ncsa.uiuc.edu
  11. **
  12. ** Rewrote configuration stuff for NCSA /htbin script
  13. **
  14. ** 12/05/93: Rob McCool, robm@ncsa.uiuc.edu
  15. ** 
  16. ** Made CGI/1.0 compliant.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #ifndef pyr
  22. #include <stdlib.h>
  23. #else
  24. #include <ctype.h>
  25. #endif
  26.  
  27. #define CONF_FILE "/usr/local/etc/httpd/conf/imagemap.conf"
  28.  
  29. #define MAXLINE 500
  30. #define MAXVERTS 100
  31. #define X 0
  32. #define Y 1
  33.  
  34. int isname(char);
  35.  
  36. int main(int argc, char **argv)
  37. {
  38.     char input[MAXLINE], *mapname, def[MAXLINE], conf[MAXLINE];
  39.     double testpoint[2], pointarray[MAXVERTS][2];
  40.     int i, j, k;
  41.     FILE *fp;
  42.     char *t;
  43.     
  44.     if (argc != 2)
  45.         servererr("Wrong number of arguments, client may not support ISMAP.");
  46.     mapname=getenv("PATH_INFO");
  47.  
  48.     if((!mapname) || (!mapname[0]))
  49.         servererr("No map name given. Please read the <A HREF=\"http://hoohoo.ncsa.uiuc.edu/docs/setup/admin/Imagemap.html\">instructions</A>.<P>");
  50.  
  51.  
  52.     mapname++;
  53.     if(!(t = strchr(argv[1],',')))
  54.         servererr("Your client doesn't support image mapping properly.");
  55.     *t++ = '\0';
  56.     testpoint[X] = (double) atoi(argv[1]);
  57.     testpoint[Y] = (double) atoi(t);
  58.     
  59.     if ((fp = fopen(CONF_FILE, "r")) == NULL)
  60.         servererr("Couldn't open configuration file.");
  61.  
  62.     while(!(getline(input,MAXLINE,fp))) {
  63.         char confname[MAXLINE];
  64.         if((input[0] == '#') || (!input[0]))
  65.             continue;
  66.         for(i=0;isname(input[i]) && (input[i] != ':');i++)
  67.             confname[i] = input[i];
  68.         confname[i] = '\0';
  69.         if(!strcmp(confname,mapname))
  70.             goto found;
  71.     }
  72.     if(feof(fp))
  73.         servererr("Map not found in configuration file.");
  74.   found:
  75.     fclose(fp);
  76.     while(isspace(input[i]) || input[i] == ':') ++i;
  77.  
  78.     for(j=0;input[i] && isname(input[i]);++i,++j)
  79.         conf[j] = input[i];
  80.     conf[j] = '\0';
  81.  
  82.     if(!(fp=fopen(conf,"r")))
  83.         servererr("Couldn't open map file.");
  84.     
  85.     while(!(getline(input,MAXLINE,fp))) {
  86.         char type[MAXLINE];
  87.         char url[MAXLINE];
  88.         char num[10];
  89.  
  90.         if((input[0] == '#') || (!input[0]))
  91.             continue;
  92.  
  93.         type[0] = '\0';url[0] = '\0';
  94.  
  95.         for(i=0;isname(input[i]) && (input[i]);i++)
  96.             type[i] = input[i];
  97.         type[i] = '\0';
  98.  
  99.         while(isspace(input[i])) ++i;
  100.         for(j=0;input[i] && isname(input[i]);++i,++j)
  101.             url[j] = input[i];
  102.         url[j] = '\0';
  103.  
  104.         if(!strcmp(type,"default")) {
  105.             strcpy(def,url);
  106.             continue;
  107.         }
  108.  
  109.         k=0;
  110.         while (input[i]) {
  111.             while (isspace(input[i]) || input[i] == ',')
  112.                 i++;
  113.             j = 0;
  114.             while (isdigit(input[i]))
  115.                 num[j++] = input[i++];
  116.             num[j] = '\0';
  117.             if (num[0] != '\0')
  118.                 pointarray[k][X] = (double) atoi(num);
  119.             else
  120.                 break;
  121.             while (isspace(input[i]) || input[i] == ',')
  122.                 i++;
  123.             j = 0;
  124.             while (isdigit(input[i]))
  125.                 num[j++] = input[i++];
  126.             num[j] = '\0';
  127.             if (num[0] != '\0')
  128.                 pointarray[k++][Y] = (double) atoi(num);
  129.             else {
  130.                 fclose(fp);
  131.                 servererr("Missing y value.");
  132.             }
  133.         }
  134.         pointarray[k][X] = -1;
  135.         if(!strcmp(type,"poly"))
  136.             if(pointinpoly(testpoint,pointarray))
  137.                 sendmesg(url);
  138.         if(!strcmp(type,"circle"))
  139.             if(pointincircle(testpoint,pointarray))
  140.                 sendmesg(url);
  141.         if(!strcmp(type,"rect"))
  142.             if(pointinrect(testpoint,pointarray))
  143.                 sendmesg(url);
  144.     }
  145.     if(def[0])
  146.         sendmesg(def);
  147.     servererr("No default specified.");
  148. }
  149.  
  150. sendmesg(char *url)
  151. {
  152.     printf("Location: %s%c%c",url,10,10);
  153.     printf("This document has moved <A HREF=\"%s\">here</A>%c",url,10);
  154.     exit(1);
  155. }
  156.  
  157. int pointinrect(double point[2], double coords[MAXVERTS][2])
  158. {
  159.         return ((point[X] >= coords[0][X] && point[X] <= coords[1][X]) &&
  160.         (point[Y] >= coords[0][Y] && point[Y] <= coords[1][Y]));
  161. }
  162.  
  163. int pointincircle(double point[2], double coords[MAXVERTS][2])
  164. {
  165.         int radius1, radius2;
  166.  
  167.         radius1 = ((coords[0][Y] - coords[1][Y]) * (coords[0][Y] -
  168.         coords[1][Y])) + ((coords[0][X] - coords[1][X]) * (coords[0][X] -
  169.         coords[1][X]));
  170.         radius2 = ((coords[0][Y] - point[Y]) * (coords[0][Y] - point[Y])) +
  171.         ((coords[0][X] - point[X]) * (coords[0][X] - point[X]));
  172.         return (radius2 <= radius1);
  173. }
  174.  
  175. int pointinpoly(double point[2], double pgon[MAXVERTS][2])
  176. {
  177.         int i, numverts, inside_flag, xflag0;
  178.         int crossings;
  179.         double *p, *stop;
  180.         double tx, ty, y;
  181.  
  182.         for (i = 0; pgon[i][X] != -1 && i < MAXVERTS; i++)
  183.                 ;
  184.         numverts = i;
  185.         crossings = 0;
  186.  
  187.         tx = point[X];
  188.         ty = point[Y];
  189.         y = pgon[numverts - 1][Y];
  190.  
  191.         p = (double *) pgon + 1;
  192.         if ((y >= ty) != (*p >= ty)) {
  193.                 if ((xflag0 = (pgon[numverts - 1][X] >= tx)) ==
  194.                 (*(double *) pgon >= tx)) {
  195.                         if (xflag0)
  196.                                 crossings++;
  197.                 }
  198.                 else {
  199.                         crossings += (pgon[numverts - 1][X] - (y - ty) *
  200.                         (*(double *) pgon - pgon[numverts - 1][X]) /
  201.                         (*p - y)) >= tx;
  202.                 }
  203.         }
  204.  
  205.         stop = pgon[numverts];
  206.  
  207.         for (y = *p, p += 2; p < stop; y = *p, p += 2) {
  208.                 if (y >= ty) {
  209.                         while ((p < stop) && (*p >= ty))
  210.                                 p += 2;
  211.                         if (p >= stop)
  212.                                 break;
  213.                         if ((xflag0 = (*(p - 3) >= tx)) == (*(p - 1) >= tx)) {
  214.                                 if (xflag0)
  215.                                         crossings++;
  216.                         }
  217.                         else {
  218.                                 crossings += (*(p - 3) - (*(p - 2) - ty) *
  219.                                 (*(p - 1) - *(p - 3)) / (*p - *(p - 2))) >= tx;
  220.                         }
  221.                 }
  222.                 else {
  223.                         while ((p < stop) && (*p < ty))
  224.                                 p += 2;
  225.                         if (p >= stop)
  226.                                 break;
  227.                         if ((xflag0 = (*(p - 3) >= tx)) == (*(p - 1) >= tx)) {
  228.                                 if (xflag0)
  229.                                         crossings++;
  230.                         }
  231.                         else {
  232.                                 crossings += (*(p - 3) - (*(p - 2) - ty) *
  233.                                 (*(p - 1) - *(p - 3)) / (*p - *(p - 2))) >= tx;
  234.                         }
  235.                 }
  236.         }
  237.         inside_flag = crossings & 0x01;
  238.         return (inside_flag);
  239. }
  240.  
  241. servererr(char *msg)
  242. {
  243.     printf("Content-type: text/html%c%c",10,10);
  244.     printf("<title>Mapping Server Error</title>");
  245.     printf("<h1>Mapping Server Error</h1>");
  246.     printf("This server encountered an error:<p>");
  247.     printf("%s", msg);
  248.     exit(-1);
  249. }
  250.  
  251. int isname(char c)
  252. {
  253.         return (!isspace(c));
  254. }
  255.